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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use traits::structure::BaseFloat;
use structs::{Point3, Vector3, Matrix4};
#[cfg(feature="arbitrary")]
use quickcheck::{Arbitrary, Gen};
#[derive(Eq, PartialEq, RustcEncodable, RustcDecodable, Clone, Debug, Copy)]
pub struct Perspective3<N> {
aspect: N,
fovy: N,
znear: N,
zfar: N
}
#[derive(Eq, PartialEq, RustcEncodable, RustcDecodable, Clone, Debug, Copy)]
pub struct PerspectiveMatrix3<N> {
matrix: Matrix4<N>
}
impl<N: BaseFloat> Perspective3<N> {
pub fn new(aspect: N, fovy: N, znear: N, zfar: N) -> Perspective3<N> {
assert!(!::is_zero(&(zfar - znear)));
assert!(!::is_zero(&aspect));
Perspective3 {
aspect: aspect,
fovy: fovy,
znear: znear,
zfar: zfar
}
}
pub fn to_matrix(&self) -> Matrix4<N> {
self.to_perspective_matrix().matrix
}
pub fn to_perspective_matrix(&self) -> PerspectiveMatrix3<N> {
PerspectiveMatrix3::new(self.aspect, self.fovy, self.znear, self.zfar)
}
}
#[cfg(feature="arbitrary")]
impl<N: Arbitrary + BaseFloat> Arbitrary for Perspective3<N> {
fn arbitrary<G: Gen>(g: &mut G) -> Perspective3<N> {
use structs::orthographic::reject;
let znear = Arbitrary::arbitrary(g);
let zfar = reject(g, |&x: &N| !::is_zero(&(x - znear)));
Perspective3::new(Arbitrary::arbitrary(g), Arbitrary::arbitrary(g), znear, zfar)
}
}
impl<N: BaseFloat + Clone> Perspective3<N> {
#[inline]
pub fn aspect(&self) -> N {
self.aspect.clone()
}
#[inline]
pub fn fovy(&self) -> N {
self.fovy.clone()
}
#[inline]
pub fn znear(&self) -> N {
self.znear.clone()
}
#[inline]
pub fn zfar(&self) -> N {
self.zfar.clone()
}
#[inline]
pub fn set_aspect(&mut self, aspect: N) {
self.aspect = aspect;
}
#[inline]
pub fn set_fovy(&mut self, fovy: N) {
self.fovy = fovy;
}
#[inline]
pub fn set_znear(&mut self, znear: N) {
self.znear = znear;
}
#[inline]
pub fn set_zfar(&mut self, zfar: N) {
self.zfar = zfar;
}
#[inline]
pub fn project_point(&self, p: &Point3<N>) -> Point3<N> {
self.to_perspective_matrix().project_point(p)
}
#[inline]
pub fn project_vector(&self, p: &Vector3<N>) -> Vector3<N> {
self.to_perspective_matrix().project_vector(p)
}
}
impl<N: BaseFloat> PerspectiveMatrix3<N> {
pub fn new(aspect: N, fovy: N, znear: N, zfar: N) -> PerspectiveMatrix3<N> {
assert!(!::is_zero(&(znear - zfar)));
assert!(!::is_zero(&aspect));
let matrix: Matrix4<N> = ::one();
let mut res = PerspectiveMatrix3 { matrix: matrix };
res.set_fovy(fovy);
res.set_aspect(aspect);
res.set_znear_and_zfar(znear, zfar);
res.matrix.m44 = ::zero();
res.matrix.m43 = -::one::<N>();
res
}
#[inline]
pub unsafe fn new_with_matrix(matrix: Matrix4<N>) -> PerspectiveMatrix3<N> {
PerspectiveMatrix3 {
matrix: matrix
}
}
#[inline]
pub fn as_matrix<'a>(&'a self) -> &'a Matrix4<N> {
&self.matrix
}
#[inline]
pub fn aspect(&self) -> N {
self.matrix.m22 / self.matrix.m11
}
#[inline]
pub fn fovy(&self) -> N {
let _1: N = ::one();
let _2 = _1 + _1;
(_1 / self.matrix.m22).atan() * _2
}
#[inline]
pub fn znear(&self) -> N {
let _1: N = ::one();
let _2 = _1 + _1;
let ratio = (-self.matrix.m33 + _1) / (-self.matrix.m33 - _1);
self.matrix.m34 / (_2 * ratio) - self.matrix.m34 / _2
}
#[inline]
pub fn zfar(&self) -> N {
let _1: N = ::one();
let _2 = _1 + _1;
let ratio = (-self.matrix.m33 + _1) / (-self.matrix.m33 - _1);
(self.matrix.m34 - ratio * self.matrix.m34) / _2
}
#[inline]
pub fn set_aspect(&mut self, aspect: N) {
assert!(!::is_zero(&aspect));
self.matrix.m11 = self.matrix.m22 / aspect;
}
#[inline]
pub fn set_fovy(&mut self, fovy: N) {
let _1: N = ::one();
let _2 = _1 + _1;
let old_m22 = self.matrix.m22.clone();
self.matrix.m22 = _1 / (fovy / _2).tan();
self.matrix.m11 = self.matrix.m11 * (self.matrix.m22 / old_m22);
}
#[inline]
pub fn set_znear(&mut self, znear: N) {
let zfar = self.zfar();
self.set_znear_and_zfar(znear, zfar);
}
#[inline]
pub fn set_zfar(&mut self, zfar: N) {
let znear = self.znear();
self.set_znear_and_zfar(znear, zfar);
}
#[inline]
pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) {
let _1: N = ::one();
let _2 = _1 + _1;
self.matrix.m33 = (zfar + znear) / (znear - zfar);
self.matrix.m34 = zfar * znear * _2 / (znear - zfar);
}
#[inline]
pub fn project_point(&self, p: &Point3<N>) -> Point3<N> {
let _1: N = ::one();
let inverse_denom = -_1 / p.z;
Point3::new(
self.matrix.m11 * p.x * inverse_denom,
self.matrix.m22 * p.y * inverse_denom,
(self.matrix.m33 * p.z + self.matrix.m34) * inverse_denom
)
}
#[inline]
pub fn project_vector(&self, p: &Vector3<N>) -> Vector3<N> {
let _1: N = ::one();
let inverse_denom = -_1 / p.z;
Vector3::new(
self.matrix.m11 * p.x * inverse_denom,
self.matrix.m22 * p.y * inverse_denom,
self.matrix.m33
)
}
}
impl<N: BaseFloat + Clone> PerspectiveMatrix3<N> {
#[inline]
pub fn to_matrix<'a>(&'a self) -> Matrix4<N> {
self.matrix.clone()
}
}
#[cfg(feature="arbitrary")]
impl<N: Arbitrary + BaseFloat> Arbitrary for PerspectiveMatrix3<N> {
fn arbitrary<G: Gen>(g: &mut G) -> PerspectiveMatrix3<N> {
let x: Perspective3<N> = Arbitrary::arbitrary(g);
x.to_perspective_matrix()
}
}