2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2018-05-23 05:58:14 +08:00
|
|
|
use rand::distributions::{Distribution, Standard};
|
|
|
|
use rand::Rng;
|
2017-05-04 10:02:30 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-09-13 12:55:58 +08:00
|
|
|
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
2017-08-03 01:37:44 +08:00
|
|
|
use std::fmt;
|
2017-05-04 10:02:30 +08:00
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
use alga::general::Real;
|
|
|
|
|
2018-05-19 23:15:15 +08:00
|
|
|
use base::dimension::U3;
|
|
|
|
use base::helper;
|
2018-05-23 05:58:14 +08:00
|
|
|
use base::storage::Storage;
|
|
|
|
use base::{Matrix4, Vector, Vector3};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
use geometry::Point3;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/// A 3D orthographic projection stored as an homogeneous 4x4 matrix.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub struct Orthographic3<N: Real> {
|
2018-02-02 19:26:35 +08:00
|
|
|
matrix: Matrix4<N>,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
impl<N: Real> Copy for Orthographic3<N> {}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
impl<N: Real> Clone for Orthographic3<N> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Orthographic3::from_matrix_unchecked(self.matrix.clone())
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real> fmt::Debug for Orthographic3<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
self.matrix.fmt(f)
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real> PartialEq for Orthographic3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, right: &Self) -> bool {
|
|
|
|
self.matrix == right.matrix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-09-13 12:55:58 +08:00
|
|
|
impl<N: Real + Serialize> Serialize for Orthographic3<N> {
|
2017-08-03 01:37:44 +08:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2018-09-13 12:55:58 +08:00
|
|
|
S: Serializer,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
|
|
|
self.matrix.serialize(serializer)
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-09-13 12:55:58 +08:00
|
|
|
impl<'a, N: Real + Deserialize<'a>> Deserialize<'a> for Orthographic3<N> {
|
2017-08-03 01:37:44 +08:00
|
|
|
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2018-09-13 12:55:58 +08:00
|
|
|
Des: Deserializer<'a>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
|
|
|
let matrix = Matrix4::<N>::deserialize(deserializer)?;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
Ok(Orthographic3::from_matrix_unchecked(matrix))
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: Real> Orthographic3<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Creates a new orthographic projection matrix.
|
|
|
|
#[inline]
|
|
|
|
pub fn new(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
left < right,
|
|
|
|
"The left corner must be farther than the right corner."
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
bottom < top,
|
|
|
|
"The top corner must be higher than the bottom corner."
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
znear < zfar,
|
|
|
|
"The far plane must be farther than the near plane."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
let matrix = Matrix4::<N>::identity();
|
2016-12-05 05:44:42 +08:00
|
|
|
let mut res = Self::from_matrix_unchecked(matrix);
|
|
|
|
|
|
|
|
res.set_left_and_right(left, right);
|
|
|
|
res.set_bottom_and_top(bottom, top);
|
|
|
|
res.set_znear_and_zfar(znear, zfar);
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps the given matrix to interpret it as a 3D orthographic matrix.
|
|
|
|
///
|
|
|
|
/// It is not checked whether or not the given matrix actually represents an orthographic
|
|
|
|
/// projection.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn from_matrix_unchecked(matrix: Matrix4<N>) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
Orthographic3 { matrix: matrix }
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new orthographic projection matrix from an aspect ratio and the vertical field of view.
|
|
|
|
#[inline]
|
|
|
|
pub fn from_fov(aspect: N, vfov: N, znear: N, zfar: N) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
znear < zfar,
|
|
|
|
"The far plane must be farther than the near plane."
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
!relative_eq!(aspect, N::zero()),
|
|
|
|
"The apsect ratio must not be zero."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
let half: N = ::convert(0.5);
|
2018-02-02 19:26:35 +08:00
|
|
|
let width = zfar * (vfov * half).tan();
|
2016-12-05 05:44:42 +08:00
|
|
|
let height = width / aspect;
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
Self::new(
|
|
|
|
-width * half,
|
|
|
|
width * half,
|
|
|
|
-height * half,
|
|
|
|
height * half,
|
|
|
|
znear,
|
|
|
|
zfar,
|
|
|
|
)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Retrieves the inverse of the underlying homogeneous matrix.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn inverse(&self) -> Matrix4<N> {
|
2017-02-13 01:17:09 +08:00
|
|
|
let mut res = self.to_homogeneous();
|
|
|
|
|
|
|
|
let inv_m11 = N::one() / self.matrix[(0, 0)];
|
|
|
|
let inv_m22 = N::one() / self.matrix[(1, 1)];
|
|
|
|
let inv_m33 = N::one() / self.matrix[(2, 2)];
|
|
|
|
|
|
|
|
res[(0, 0)] = inv_m11;
|
|
|
|
res[(1, 1)] = inv_m22;
|
|
|
|
res[(2, 2)] = inv_m33;
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
res[(0, 3)] = -self.matrix[(0, 3)] * inv_m11;
|
|
|
|
res[(1, 3)] = -self.matrix[(1, 3)] * inv_m22;
|
|
|
|
res[(2, 3)] = -self.matrix[(2, 3)] * inv_m33;
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Computes the corresponding homogeneous matrix.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn to_homogeneous(&self) -> Matrix4<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix.clone_owned()
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
/// A reference to the underlying homogeneous transformation matrix.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_matrix(&self) -> &Matrix4<N> {
|
|
|
|
&self.matrix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the underlying homogeneous matrix.
|
|
|
|
#[inline]
|
|
|
|
pub fn unwrap(self) -> Matrix4<N> {
|
|
|
|
self.matrix
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// The smallest x-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn left(&self) -> N {
|
|
|
|
(-N::one() - self.matrix[(0, 3)]) / self.matrix[(0, 0)]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The largest x-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn right(&self) -> N {
|
|
|
|
(N::one() - self.matrix[(0, 3)]) / self.matrix[(0, 0)]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The smallest y-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn bottom(&self) -> N {
|
|
|
|
(-N::one() - self.matrix[(1, 3)]) / self.matrix[(1, 1)]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The largest y-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn top(&self) -> N {
|
|
|
|
(N::one() - self.matrix[(1, 3)]) / self.matrix[(1, 1)]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The near plane offset of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn znear(&self) -> N {
|
|
|
|
(N::one() + self.matrix[(2, 3)]) / self.matrix[(2, 2)]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The far plane offset of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn zfar(&self) -> N {
|
|
|
|
(-N::one() + self.matrix[(2, 3)]) / self.matrix[(2, 2)]
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: when we get specialization, specialize the Mul impl instead.
|
|
|
|
/// Projects a point. Faster than matrix multiplication.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn project_point(&self, p: &Point3<N>) -> Point3<N> {
|
|
|
|
Point3::new(
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(0, 0)] * p[0] + self.matrix[(0, 3)],
|
|
|
|
self.matrix[(1, 1)] * p[1] + self.matrix[(1, 3)],
|
2018-02-02 19:26:35 +08:00
|
|
|
self.matrix[(2, 2)] * p[2] + self.matrix[(2, 3)],
|
2016-12-05 05:44:42 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Un-projects a point. Faster than multiplication by the underlying matrix inverse.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn unproject_point(&self, p: &Point3<N>) -> Point3<N> {
|
|
|
|
Point3::new(
|
2017-02-13 01:17:09 +08:00
|
|
|
(p[0] - self.matrix[(0, 3)]) / self.matrix[(0, 0)],
|
|
|
|
(p[1] - self.matrix[(1, 3)]) / self.matrix[(1, 1)],
|
2018-02-02 19:26:35 +08:00
|
|
|
(p[2] - self.matrix[(2, 3)]) / self.matrix[(2, 2)],
|
2017-02-13 01:17:09 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
// FIXME: when we get specialization, specialize the Mul impl instead.
|
|
|
|
/// Projects a vector. Faster than matrix multiplication.
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn project_vector<SB>(&self, p: &Vector<N, U3, SB>) -> Vector3<N>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
SB: Storage<N, U3>,
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
Vector3::new(
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(0, 0)] * p[0],
|
|
|
|
self.matrix[(1, 1)] * p[1],
|
2018-02-02 19:26:35 +08:00
|
|
|
self.matrix[(2, 2)] * p[2],
|
2016-12-05 05:44:42 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the smallest x-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_left(&mut self, left: N) {
|
|
|
|
let right = self.right();
|
|
|
|
self.set_left_and_right(left, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the largest x-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_right(&mut self, right: N) {
|
|
|
|
let left = self.left();
|
|
|
|
self.set_left_and_right(left, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the smallest y-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_bottom(&mut self, bottom: N) {
|
|
|
|
let top = self.top();
|
|
|
|
self.set_bottom_and_top(bottom, top);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the largest y-coordinate of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_top(&mut self, top: N) {
|
|
|
|
let bottom = self.bottom();
|
|
|
|
self.set_bottom_and_top(bottom, top);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the near plane offset of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_znear(&mut self, znear: N) {
|
|
|
|
let zfar = self.zfar();
|
|
|
|
self.set_znear_and_zfar(znear, zfar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the far plane offset of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_zfar(&mut self, zfar: N) {
|
|
|
|
let znear = self.znear();
|
|
|
|
self.set_znear_and_zfar(znear, zfar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the view cuboid coordinates along the `x` axis.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_left_and_right(&mut self, left: N, right: N) {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
left < right,
|
|
|
|
"The left corner must be farther than the right corner."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(0, 0)] = ::convert::<_, N>(2.0) / (right - left);
|
|
|
|
self.matrix[(0, 3)] = -(right + left) / (right - left);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the view cuboid coordinates along the `y` axis.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_bottom_and_top(&mut self, bottom: N, top: N) {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
bottom < top,
|
|
|
|
"The top corner must be higher than the bottom corner."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(1, 1)] = ::convert::<_, N>(2.0) / (top - bottom);
|
|
|
|
self.matrix[(1, 3)] = -(top + bottom) / (top - bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the near and far plane offsets of the view cuboid.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_znear_and_zfar(&mut self, znear: N, zfar: N) {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
!relative_eq!(zfar - znear, N::zero()),
|
|
|
|
"The near-plane and far-plane must not be superimposed."
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
self.matrix[(2, 2)] = -::convert::<_, N>(2.0) / (zfar - znear);
|
|
|
|
self.matrix[(2, 3)] = -(zfar + znear) / (zfar - znear);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 05:58:14 +08:00
|
|
|
impl<N: Real> Distribution<Orthographic3<N>> for Standard
|
|
|
|
where
|
|
|
|
Standard: Distribution<N>,
|
|
|
|
{
|
|
|
|
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<N> {
|
|
|
|
let left = r.gen();
|
2018-02-02 19:26:35 +08:00
|
|
|
let right = helper::reject_rand(r, |x: &N| *x > left);
|
2018-05-23 05:58:14 +08:00
|
|
|
let bottom = r.gen();
|
2018-02-02 19:26:35 +08:00
|
|
|
let top = helper::reject_rand(r, |x: &N| *x > bottom);
|
2018-05-23 05:58:14 +08:00
|
|
|
let znear = r.gen();
|
2018-02-02 19:26:35 +08:00
|
|
|
let zfar = helper::reject_rand(r, |x: &N| *x > znear);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-05-23 05:58:14 +08:00
|
|
|
Orthographic3::new(left, right, bottom, top, znear, zfar)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2017-08-03 01:37:44 +08:00
|
|
|
impl<N: Real + Arbitrary> Arbitrary for Orthographic3<N>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
Matrix4<N>: Send,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
let left = Arbitrary::arbitrary(g);
|
|
|
|
let right = helper::reject(g, |x: &N| *x > left);
|
2016-12-05 05:44:42 +08:00
|
|
|
let bottom = Arbitrary::arbitrary(g);
|
2018-02-02 19:26:35 +08:00
|
|
|
let top = helper::reject(g, |x: &N| *x > bottom);
|
|
|
|
let znear = Arbitrary::arbitrary(g);
|
|
|
|
let zfar = helper::reject(g, |x: &N| *x > znear);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
Self::new(left, right, bottom, top, znear, zfar)
|
|
|
|
}
|
|
|
|
}
|