1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::{Frame, Scene};
use nalgebra::{convert, zero, Isometry3, Matrix4, Point2, Point3, RealField, Vector2, Vector3};
#[derive(Debug, Clone)]
pub struct Image<N: RealField> {
pos: Point2<N>,
max: Point2<N>,
upp: N,
frame: Frame<N>,
scene: Scene<N>,
view_iso: Isometry3<N>,
view_mat: Matrix4<N>,
proj_mat: Matrix4<N>,
proj_view_mat: Matrix4<N>,
proj_view_inv: Matrix4<N>,
compute_mat: bool,
compute_inv: bool,
}
impl<N: RealField> Image<N> {
pub fn new(frame: &Frame<N>, scene: &Scene<N>, max: Point2<N>) -> Self {
let mut image = Self {
pos: Point2::origin(),
max,
upp: zero(),
frame: frame.clone(),
scene: scene.clone(),
view_iso: Isometry3::identity(),
view_mat: zero(),
proj_mat: zero(),
proj_view_mat: zero(),
proj_view_inv: zero(),
compute_mat: true,
compute_inv: true,
};
image.compute_view(frame);
image.compute_projection_and_upp(frame.distance(), scene);
image.compute_transformation();
image.compute_inverse_transformation();
image
}
pub fn compute(&mut self, frame: Frame<N>, scene: Scene<N>) -> Option<bool> {
let mut compute = false;
if self.frame != frame {
self.compute_view(&frame);
compute = true;
}
if self.frame.distance() != frame.distance() || self.scene != scene {
self.compute_projection_and_upp(frame.distance(), &scene);
compute = true;
}
self.frame = frame;
self.scene = scene;
compute.then(|| {
if self.compute_mat || self.compute_inv {
self.compute_transformation();
}
if self.compute_inv {
self.compute_inverse_transformation()
} else {
true
}
})
}
pub fn set_compute(&mut self, compute_mat: bool, compute_inv: bool) {
self.compute_mat = compute_mat;
self.compute_inv = compute_inv;
}
pub fn pos(&self) -> &Point2<N> {
&self.pos
}
pub fn set_pos(&mut self, pos: Point2<N>) {
self.pos = pos;
}
pub fn max(&self) -> &Point2<N> {
&self.max
}
pub fn set_max(&mut self, max: Point2<N>) {
if self.max != max {
self.scene.set_fov(N::zero());
}
self.max = max;
}
pub fn upp(&self) -> N {
self.upp
}
pub fn view_isometry(&self) -> &Isometry3<N> {
&self.view_iso
}
pub fn view(&self) -> &Matrix4<N> {
&self.view_mat
}
pub fn compute_view(&mut self, frame: &Frame<N>) {
self.view_iso = frame.view();
self.view_mat = self.view_iso.to_homogeneous();
}
pub fn projection(&self) -> &Matrix4<N> {
&self.proj_mat
}
pub fn compute_projection_and_upp(&mut self, zat: N, scene: &Scene<N>) {
let (mat, upp) = scene.projection_and_upp(zat, &self.max);
self.upp = upp;
self.proj_mat = mat;
}
pub fn transformation(&self) -> &Matrix4<N> {
&self.proj_view_mat
}
pub fn compute_transformation(&mut self) {
self.proj_view_mat = self.proj_mat * self.view_mat
}
pub fn inverse_transformation(&self) -> &Matrix4<N> {
&self.proj_view_inv
}
pub fn compute_inverse_transformation(&mut self) -> bool {
let inv = self.proj_view_mat.try_inverse();
if let Some(mat) = inv {
self.proj_view_inv = mat;
}
inv.is_some()
}
pub fn clamp_pos_wrt_max(pos: &Point2<N>, max: &Point2<N>) -> Point2<N> {
Point2::new(pos.x.clamp(N::zero(), max.x), pos.y.clamp(N::zero(), max.y))
}
pub fn clamp_pos(&self, pos: &Point2<N>) -> Point2<N> {
Self::clamp_pos_wrt_max(pos, &self.max)
}
pub fn transform_pos_and_max_wrt_max(
pos: &Point2<N>,
max: &Point2<N>,
) -> (Point2<N>, Point2<N>) {
let max = max * convert(0.5);
(Point2::new(pos.x - max.x, max.y - pos.y), max)
}
pub fn transform_pos(&self, pos: &Point2<N>) -> Point2<N> {
Self::transform_pos_and_max_wrt_max(pos, &self.max).0
}
pub fn transform_vec(pos: &Vector2<N>) -> Vector2<N> {
Vector2::new(pos.x, -pos.y)
}
pub fn project_pos(&self, pos: &Point2<N>) -> Point3<N> {
self.transform_pos(pos)
.coords
.scale(self.upp)
.push(N::zero())
.into()
}
pub fn project_vec(&self, vec: &Vector2<N>) -> Vector3<N> {
Self::transform_vec(vec).scale(self.upp).push(N::zero())
}
}