Mercurial > touhou
annotate src/th06/interpolator.rs @ 715:2b2376811f46
examples: Update luminance.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 23 Sep 2019 00:22:08 +0200 |
parents | 6d4802abe134 |
children | 5016c09e5d7c |
rev | line source |
---|---|
642
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 //! Animation runner. |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 #[derive(Debug, Clone, Copy, PartialEq)] |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 pub(crate) enum Formula { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 Linear, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 Power2, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 InvertPower2, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 impl Formula { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 fn apply(&self, x: f32) -> f32 { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 match self { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 Formula::Linear => x, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 Formula::Power2 => x * x, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 Formula::InvertPower2 => 2. * x - x * x, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 macro_rules! generate_interpolator { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 ($name:ident, $n:tt) => { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 #[derive(Debug, Clone)] |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 pub(crate) struct $name<T> { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 start_values: [T; $n], |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 end_values: [T; $n], |
709
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
26 start_frame: u32, |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
27 end_frame: u32, |
642
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 formula: Formula, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 impl<T> $name<T> |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 where f32: From<T>, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 T: From<f32>, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 T: std::ops::Sub<Output = T>, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 T: std::ops::Add<Output = T>, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 T: Copy, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 T: Default, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 { |
709
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
39 pub fn new(start_values: [T; $n], start_frame: u32, end_values: [T; $n], end_frame: u32, formula: Formula) -> $name<T> { |
642
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 $name { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 start_values, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 end_values, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 start_frame, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 end_frame, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 formula, |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 |
709
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
49 pub fn set_start(&mut self, frame: u32, values: [T; $n]) { |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
50 self.start_values = values; |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
51 self.start_frame = frame; |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
52 } |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
53 |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
54 pub fn set_end(&mut self, frame: u32, values: [T; $n]) { |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
55 self.end_values = values; |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
56 self.end_frame = frame; |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
57 } |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
58 |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
59 pub fn set_end_values(&mut self, values: [T; $n]) { |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
60 self.end_values = values; |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
61 } |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
62 |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
63 pub fn set_end_frame(&mut self, frame: u32) { |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
64 self.end_frame = frame; |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
65 } |
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
66 |
642
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 // XXX: Make it return [T; $n] instead, we don’t want to only do f32 here. |
709
6d4802abe134
Make interpolators use u32 instead of u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
679
diff
changeset
|
68 pub fn values(&self, frame: u32) -> [f32; $n] { |
642
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 if frame + 1 >= self.end_frame { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 // XXX: skip the last interpolation step. |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 // This bug is replicated from the original game. |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 //self.start_frame = self.end_frame; |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 //self.end_values |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 let mut values: [f32; $n] = [Default::default(); $n]; |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 for (i, value) in self.end_values.iter().enumerate() { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 values[i] = f32::from(*value); |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 values |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 } else { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 let mut coeff = (frame - self.start_frame) as f32 / (self.end_frame - self.start_frame) as f32; |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 coeff = self.formula.apply(coeff); |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 let mut values: [f32; $n] = [Default::default(); $n]; |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 for (i, (start, end)) in self.start_values.iter().zip(&self.end_values).enumerate() { |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 values[i] = f32::from(*start + T::from(coeff * f32::from(*end - *start))); |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 values |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 }; |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 } |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 generate_interpolator!(Interpolator1, 1); |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 generate_interpolator!(Interpolator2, 2); |
9e40bd5cc26d
Move interpolators in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 generate_interpolator!(Interpolator3, 3); |
679
6020e33d4fc4
Implement a .std renderer, and its associated VM.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
642
diff
changeset
|
96 generate_interpolator!(Interpolator4, 4); |