#[cfg(feature="arbitrary")] use quickcheck::{Arbitrary, Gen}; use rand::{Rand, Rng}; #[cfg(feature = "serde-serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; use alga::general::Real; use core::{Scalar, SquareMatrix, OwnedSquareMatrix, ColumnVector, OwnedColumnVector, MatrixArray}; use core::dimension::{U1, U3, U4}; use core::storage::{OwnedStorage, Storage, StorageMut}; use core::allocator::OwnedAllocator; use core::helper; use geometry::{PointBase, OwnedPoint}; /// A 3D perspective projection stored as an homogeneous 4x4 matrix. #[derive(Debug, Clone, Copy)] // FIXME: Hash pub struct PerspectiveBase> { matrix: SquareMatrix } #[cfg(feature = "serde-serialize")] impl Serialize for PerspectiveBase where N: Scalar, S: Storage, SquareMatrix: Serialize, { fn serialize(&self, serializer: T) -> Result where T: Serializer { self.matrix.serialize(serializer) } } #[cfg(feature = "serde-serialize")] impl<'de, N, S> Deserialize<'de> for PerspectiveBase where N: Scalar, S: Storage, SquareMatrix: Deserialize<'de>, { fn deserialize(deserializer: T) -> Result where T: Deserializer<'de> { SquareMatrix::deserialize(deserializer).map(|x| PerspectiveBase { matrix: x }) } } /// A 3D perspective projection stored as a static homogeneous 4x4 matrix. pub type Perspective3 = PerspectiveBase>; impl Eq for PerspectiveBase where N: Scalar + Eq, S: Storage { } impl PartialEq for PerspectiveBase where N: Scalar, S: Storage { #[inline] fn eq(&self, right: &Self) -> bool { self.matrix == right.matrix } } impl PerspectiveBase where N: Real, S: OwnedStorage, S::Alloc: OwnedAllocator { /// Creates a new perspective matrix from the aspect ratio, y field of view, and near/far planes. pub fn new(aspect: N, fovy: N, znear: N, zfar: N) -> Self { assert!(!relative_eq!(zfar - znear, N::zero()), "The near-plane and far-plane must not be superimposed."); assert!(!relative_eq!(aspect, N::zero()), "The apsect ratio must not be zero."); let matrix = SquareMatrix::::identity(); let mut res = PerspectiveBase::from_matrix_unchecked(matrix); res.set_fovy(fovy); res.set_aspect(aspect); res.set_znear_and_zfar(znear, zfar); res.matrix[(3, 3)] = N::zero(); res.matrix[(3, 2)] = -N::one(); res } /// Wraps the given matrix to interpret it as a 3D perspective matrix. /// /// It is not checked whether or not the given matrix actually represents an orthographic /// projection. #[inline] pub fn from_matrix_unchecked(matrix: SquareMatrix) -> Self { PerspectiveBase { matrix: matrix } } } impl PerspectiveBase where N: Real, S: Storage { /// A reference to the underlying homogeneous transformation matrix. #[inline] pub fn as_matrix(&self) -> &SquareMatrix { &self.matrix } /// Retrieves the underlying homogeneous matrix. #[inline] pub fn unwrap(self) -> SquareMatrix { self.matrix } /// Retrieves the inverse of the underlying homogeneous matrix. #[inline] pub fn inverse(&self) -> OwnedSquareMatrix { let mut res = self.to_homogeneous(); res[(0, 0)] = N::one() / self.matrix[(0, 0)]; res[(1, 1)] = N::one() / self.matrix[(1, 1)]; res[(2, 2)] = N::zero(); let m23 = self.matrix[(2, 3)]; let m32 = self.matrix[(3, 2)]; res[(2, 3)] = N::one() / m32; res[(3, 2)] = N::one() / m23; res[(3, 3)] = -self.matrix[(2, 2)] / (m23 * m32); res } /// Computes the corresponding homogeneous matrix. #[inline] pub fn to_homogeneous(&self) -> OwnedSquareMatrix { self.matrix.clone_owned() } /// Gets the `width / height` aspect ratio of the view frustrum. #[inline] pub fn aspect(&self) -> N { self.matrix[(1, 1)] / self.matrix[(0, 0)] } /// Gets the y field of view of the view frustrum. #[inline] pub fn fovy(&self) -> N { (N::one() / self.matrix[(1, 1)]).atan() * ::convert(2.0) } /// Gets the near plane offset of the view frustrum. #[inline] pub fn znear(&self) -> N { let ratio = (-self.matrix[(2, 2)] + N::one()) / (-self.matrix[(2, 2)] - N::one()); self.matrix[(2, 3)] / (ratio * ::convert(2.0)) - self.matrix[(2, 3)] / ::convert(2.0) } /// Gets the far plane offset of the view frustrum. #[inline] pub fn zfar(&self) -> N { let ratio = (-self.matrix[(2, 2)] + N::one()) / (-self.matrix[(2, 2)] - N::one()); (self.matrix[(2, 3)] - ratio * self.matrix[(2, 3)]) / ::convert(2.0) } // FIXME: add a method to retrieve znear and zfar simultaneously? // FIXME: when we get specialization, specialize the Mul impl instead. /// Projects a point. Faster than matrix multiplication. #[inline] pub fn project_point(&self, p: &PointBase) -> OwnedPoint where SB: Storage { let inverse_denom = -N::one() / p[2]; OwnedPoint::::new( self.matrix[(0, 0)] * p[0] * inverse_denom, self.matrix[(1, 1)] * p[1] * inverse_denom, (self.matrix[(2, 2)] * p[2] + self.matrix[(2, 3)]) * inverse_denom ) } /// Un-projects a point. Faster than multiplication by the matrix inverse. #[inline] pub fn unproject_point(&self, p: &PointBase) -> OwnedPoint where SB: Storage { let inverse_denom = self.matrix[(2, 3)] / (p[2] + self.matrix[(2, 2)]); OwnedPoint::::new( p[0] * inverse_denom / self.matrix[(0, 0)], p[1] * inverse_denom / self.matrix[(1, 1)], -inverse_denom ) } // FIXME: when we get specialization, specialize the Mul impl instead. /// Projects a vector. Faster than matrix multiplication. #[inline] pub fn project_vector(&self, p: &ColumnVector) -> OwnedColumnVector where SB: Storage { let inverse_denom = -N::one() / p[2]; OwnedColumnVector::::new( self.matrix[(0, 0)] * p[0] * inverse_denom, self.matrix[(1, 1)] * p[1] * inverse_denom, self.matrix[(2, 2)] ) } } impl PerspectiveBase where N: Real, S: StorageMut { /// Updates this perspective matrix with a new `width / height` aspect ratio of the view /// frustrum. #[inline] pub fn set_aspect(&mut self, aspect: N) { assert!(!relative_eq!(aspect, N::zero()), "The aspect ratio must not be zero."); self.matrix[(0, 0)] = self.matrix[(1, 1)] / aspect; } /// Updates this perspective with a new y field of view of the view frustrum. #[inline] pub fn set_fovy(&mut self, fovy: N) { let old_m22 = self.matrix[(1, 1)]; self.matrix[(1, 1)] = N::one() / (fovy / ::convert(2.0)).tan(); self.matrix[(0, 0)] = self.matrix[(0, 0)] * (self.matrix[(1, 1)] / old_m22); } /// Updates this perspective matrix with a new near plane offset of the view frustrum. #[inline] pub fn set_znear(&mut self, znear: N) { let zfar = self.zfar(); self.set_znear_and_zfar(znear, zfar); } /// Updates this perspective matrix with a new far plane offset of the view frustrum. #[inline] pub fn set_zfar(&mut self, zfar: N) { let znear = self.znear(); self.set_znear_and_zfar(znear, zfar); } /// Updates this perspective matrix with new near and far plane offsets of the view frustrum. #[inline] pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) { self.matrix[(2, 2)] = (zfar + znear) / (znear - zfar); self.matrix[(2, 3)] = zfar * znear * ::convert(2.0) / (znear - zfar); } } impl Rand for PerspectiveBase where N: Real + Rand, S: OwnedStorage, S::Alloc: OwnedAllocator { fn rand(r: &mut R) -> Self { let znear = Rand::rand(r); let zfar = helper::reject_rand(r, |&x: &N| !(x - znear).is_zero()); let aspect = helper::reject_rand(r, |&x: &N| !x.is_zero()); Self::new(aspect, Rand::rand(r), znear, zfar) } } #[cfg(feature="arbitrary")] impl Arbitrary for PerspectiveBase where N: Real + Arbitrary, S: OwnedStorage + Send, S::Alloc: OwnedAllocator { fn arbitrary(g: &mut G) -> Self { let znear = Arbitrary::arbitrary(g); let zfar = helper::reject(g, |&x: &N| !(x - znear).is_zero()); let aspect = helper::reject(g, |&x: &N| !x.is_zero()); Self::new(aspect, Arbitrary::arbitrary(g), znear, zfar) } }