nalgebra/geometry/
perspective.rs

1#[cfg(feature = "arbitrary")]
2use quickcheck::{Arbitrary, Gen};
3#[cfg(feature = "rand-no-std")]
4use rand::{
5    distributions::{Distribution, Standard},
6    Rng,
7};
8
9#[cfg(feature = "serde-serialize-no-std")]
10use serde::{Deserialize, Deserializer, Serialize, Serializer};
11use std::fmt;
12
13use simba::scalar::RealField;
14
15use crate::base::dimension::U3;
16use crate::base::storage::Storage;
17use crate::base::{Matrix4, Vector, Vector3};
18
19use crate::geometry::{Point3, Projective3};
20
21/// A 3D perspective projection stored as a homogeneous 4x4 matrix.
22#[repr(C)]
23#[cfg_attr(
24    all(not(target_os = "cuda"), feature = "cuda"),
25    derive(cust::DeviceCopy)
26)]
27#[derive(Copy, Clone)]
28pub struct Perspective3<T> {
29    matrix: Matrix4<T>,
30}
31
32impl<T: RealField> fmt::Debug for Perspective3<T> {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
34        self.matrix.fmt(f)
35    }
36}
37
38impl<T: RealField> PartialEq for Perspective3<T> {
39    #[inline]
40    fn eq(&self, right: &Self) -> bool {
41        self.matrix == right.matrix
42    }
43}
44
45#[cfg(feature = "bytemuck")]
46unsafe impl<T> bytemuck::Zeroable for Perspective3<T>
47where
48    T: RealField + bytemuck::Zeroable,
49    Matrix4<T>: bytemuck::Zeroable,
50{
51}
52
53#[cfg(feature = "bytemuck")]
54unsafe impl<T> bytemuck::Pod for Perspective3<T>
55where
56    T: RealField + bytemuck::Pod,
57    Matrix4<T>: bytemuck::Pod,
58{
59}
60
61#[cfg(feature = "serde-serialize-no-std")]
62impl<T: RealField + Serialize> Serialize for Perspective3<T> {
63    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64    where
65        S: Serializer,
66    {
67        self.matrix.serialize(serializer)
68    }
69}
70
71#[cfg(feature = "serde-serialize-no-std")]
72impl<'a, T: RealField + Deserialize<'a>> Deserialize<'a> for Perspective3<T> {
73    fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
74    where
75        Des: Deserializer<'a>,
76    {
77        let matrix = Matrix4::<T>::deserialize(deserializer)?;
78
79        Ok(Self::from_matrix_unchecked(matrix))
80    }
81}
82
83impl<T> Perspective3<T> {
84    /// Wraps the given matrix to interpret it as a 3D perspective matrix.
85    ///
86    /// It is not checked whether or not the given matrix actually represents a perspective
87    /// projection.
88    #[inline]
89    pub const fn from_matrix_unchecked(matrix: Matrix4<T>) -> Self {
90        Self { matrix }
91    }
92}
93
94impl<T: RealField> Perspective3<T> {
95    /// Creates a new perspective matrix from the aspect ratio, y field of view, and near/far planes.
96    pub fn new(aspect: T, fovy: T, znear: T, zfar: T) -> Self {
97        assert!(
98            relative_ne!(zfar, znear),
99            "The near-plane and far-plane must not be superimposed."
100        );
101        assert!(
102            !relative_eq!(aspect, T::zero()),
103            "The aspect ratio must not be zero."
104        );
105
106        let matrix = Matrix4::identity();
107        let mut res = Self::from_matrix_unchecked(matrix);
108
109        res.set_fovy(fovy);
110        res.set_aspect(aspect);
111        res.set_znear_and_zfar(znear, zfar);
112
113        res.matrix[(3, 3)] = T::zero();
114        res.matrix[(3, 2)] = -T::one();
115
116        res
117    }
118
119    /// Retrieves the inverse of the underlying homogeneous matrix.
120    #[inline]
121    #[must_use]
122    pub fn inverse(&self) -> Matrix4<T> {
123        let mut res = self.clone().to_homogeneous();
124
125        res[(0, 0)] = T::one() / self.matrix[(0, 0)].clone();
126        res[(1, 1)] = T::one() / self.matrix[(1, 1)].clone();
127        res[(2, 2)] = T::zero();
128
129        let m23 = self.matrix[(2, 3)].clone();
130        let m32 = self.matrix[(3, 2)].clone();
131
132        res[(2, 3)] = T::one() / m32.clone();
133        res[(3, 2)] = T::one() / m23.clone();
134        res[(3, 3)] = -self.matrix[(2, 2)].clone() / (m23 * m32);
135
136        res
137    }
138
139    /// Computes the corresponding homogeneous matrix.
140    #[inline]
141    #[must_use]
142    pub fn to_homogeneous(self) -> Matrix4<T> {
143        self.matrix.clone_owned()
144    }
145
146    /// A reference to the underlying homogeneous transformation matrix.
147    #[inline]
148    #[must_use]
149    pub fn as_matrix(&self) -> &Matrix4<T> {
150        &self.matrix
151    }
152
153    /// A reference to this transformation seen as a `Projective3`.
154    #[inline]
155    #[must_use]
156    pub fn as_projective(&self) -> &Projective3<T> {
157        unsafe { &*(self as *const Perspective3<T> as *const Projective3<T>) }
158    }
159
160    /// This transformation seen as a `Projective3`.
161    #[inline]
162    #[must_use]
163    pub fn to_projective(self) -> Projective3<T> {
164        Projective3::from_matrix_unchecked(self.matrix)
165    }
166
167    /// Retrieves the underlying homogeneous matrix.
168    #[inline]
169    pub fn into_inner(self) -> Matrix4<T> {
170        self.matrix
171    }
172
173    /// Retrieves the underlying homogeneous matrix.
174    /// Deprecated: Use [`Perspective3::into_inner`] instead.
175    #[deprecated(note = "use `.into_inner()` instead")]
176    #[inline]
177    pub fn unwrap(self) -> Matrix4<T> {
178        self.matrix
179    }
180
181    /// Gets the `width / height` aspect ratio of the view frustum.
182    #[inline]
183    #[must_use]
184    pub fn aspect(&self) -> T {
185        self.matrix[(1, 1)].clone() / self.matrix[(0, 0)].clone()
186    }
187
188    /// Gets the y field of view of the view frustum.
189    #[inline]
190    #[must_use]
191    pub fn fovy(&self) -> T {
192        (T::one() / self.matrix[(1, 1)].clone()).atan() * crate::convert(2.0)
193    }
194
195    /// Gets the near plane offset of the view frustum.
196    #[inline]
197    #[must_use]
198    pub fn znear(&self) -> T {
199        let ratio =
200            (-self.matrix[(2, 2)].clone() + T::one()) / (-self.matrix[(2, 2)].clone() - T::one());
201
202        self.matrix[(2, 3)].clone() / (ratio * crate::convert(2.0))
203            - self.matrix[(2, 3)].clone() / crate::convert(2.0)
204    }
205
206    /// Gets the far plane offset of the view frustum.
207    #[inline]
208    #[must_use]
209    pub fn zfar(&self) -> T {
210        let ratio =
211            (-self.matrix[(2, 2)].clone() + T::one()) / (-self.matrix[(2, 2)].clone() - T::one());
212
213        (self.matrix[(2, 3)].clone() - ratio * self.matrix[(2, 3)].clone()) / crate::convert(2.0)
214    }
215
216    // TODO: add a method to retrieve znear and zfar simultaneously?
217
218    // TODO: when we get specialization, specialize the Mul impl instead.
219    /// Projects a point. Faster than matrix multiplication.
220    #[inline]
221    #[must_use]
222    pub fn project_point(&self, p: &Point3<T>) -> Point3<T> {
223        let inverse_denom = -T::one() / p[2].clone();
224        Point3::new(
225            self.matrix[(0, 0)].clone() * p[0].clone() * inverse_denom.clone(),
226            self.matrix[(1, 1)].clone() * p[1].clone() * inverse_denom.clone(),
227            (self.matrix[(2, 2)].clone() * p[2].clone() + self.matrix[(2, 3)].clone())
228                * inverse_denom,
229        )
230    }
231
232    /// Un-projects a point. Faster than multiplication by the matrix inverse.
233    #[inline]
234    #[must_use]
235    pub fn unproject_point(&self, p: &Point3<T>) -> Point3<T> {
236        let inverse_denom =
237            self.matrix[(2, 3)].clone() / (p[2].clone() + self.matrix[(2, 2)].clone());
238
239        Point3::new(
240            p[0].clone() * inverse_denom.clone() / self.matrix[(0, 0)].clone(),
241            p[1].clone() * inverse_denom.clone() / self.matrix[(1, 1)].clone(),
242            -inverse_denom,
243        )
244    }
245
246    // TODO: when we get specialization, specialize the Mul impl instead.
247    /// Projects a vector. Faster than matrix multiplication.
248    #[inline]
249    #[must_use]
250    pub fn project_vector<SB>(&self, p: &Vector<T, U3, SB>) -> Vector3<T>
251    where
252        SB: Storage<T, U3>,
253    {
254        let inverse_denom = -T::one() / p[2].clone();
255        Vector3::new(
256            self.matrix[(0, 0)].clone() * p[0].clone() * inverse_denom.clone(),
257            self.matrix[(1, 1)].clone() * p[1].clone() * inverse_denom,
258            self.matrix[(2, 2)].clone(),
259        )
260    }
261
262    /// Updates this perspective matrix with a new `width / height` aspect ratio of the view
263    /// frustum.
264    #[inline]
265    pub fn set_aspect(&mut self, aspect: T) {
266        assert!(
267            !relative_eq!(aspect, T::zero()),
268            "The aspect ratio must not be zero."
269        );
270        self.matrix[(0, 0)] = self.matrix[(1, 1)].clone() / aspect;
271    }
272
273    /// Updates this perspective with a new y field of view of the view frustum.
274    #[inline]
275    pub fn set_fovy(&mut self, fovy: T) {
276        let old_m22 = self.matrix[(1, 1)].clone();
277        let new_m22 = T::one() / (fovy / crate::convert(2.0)).tan();
278        self.matrix[(1, 1)] = new_m22.clone();
279        self.matrix[(0, 0)] *= new_m22 / old_m22;
280    }
281
282    /// Updates this perspective matrix with a new near plane offset of the view frustum.
283    #[inline]
284    pub fn set_znear(&mut self, znear: T) {
285        let zfar = self.zfar();
286        self.set_znear_and_zfar(znear, zfar);
287    }
288
289    /// Updates this perspective matrix with a new far plane offset of the view frustum.
290    #[inline]
291    pub fn set_zfar(&mut self, zfar: T) {
292        let znear = self.znear();
293        self.set_znear_and_zfar(znear, zfar);
294    }
295
296    /// Updates this perspective matrix with new near and far plane offsets of the view frustum.
297    #[inline]
298    pub fn set_znear_and_zfar(&mut self, znear: T, zfar: T) {
299        self.matrix[(2, 2)] = (zfar.clone() + znear.clone()) / (znear.clone() - zfar.clone());
300        self.matrix[(2, 3)] = zfar.clone() * znear.clone() * crate::convert(2.0) / (znear - zfar);
301    }
302}
303
304#[cfg(feature = "rand-no-std")]
305impl<T: RealField> Distribution<Perspective3<T>> for Standard
306where
307    Standard: Distribution<T>,
308{
309    /// Generate an arbitrary random variate for testing purposes.
310    fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Perspective3<T> {
311        use crate::base::helper;
312        let znear = r.gen();
313        let zfar = helper::reject_rand(r, |x: &T| !(x.clone() - znear.clone()).is_zero());
314        let aspect = helper::reject_rand(r, |x: &T| !x.is_zero());
315
316        Perspective3::new(aspect, r.gen(), znear, zfar)
317    }
318}
319
320#[cfg(feature = "arbitrary")]
321impl<T: RealField + Arbitrary> Arbitrary for Perspective3<T> {
322    fn arbitrary(g: &mut Gen) -> Self {
323        use crate::base::helper;
324        let znear: T = Arbitrary::arbitrary(g);
325        let zfar = helper::reject(g, |x: &T| !(x.clone() - znear.clone()).is_zero());
326        let aspect = helper::reject(g, |x: &T| !x.is_zero());
327
328        Self::new(aspect, Arbitrary::arbitrary(g), znear, zfar)
329    }
330}
331
332impl<T: RealField> From<Perspective3<T>> for Matrix4<T> {
333    #[inline]
334    fn from(pers: Perspective3<T>) -> Self {
335        pers.into_inner()
336    }
337}