2018-02-02 19:26:35 +08:00
|
|
|
#[cfg(feature = "arbitrary")]
|
2016-12-05 05:44:42 +08:00
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2021-03-02 19:25:12 +08:00
|
|
|
#[cfg(feature = "rand-no-std")]
|
|
|
|
use rand::{
|
|
|
|
distributions::{Distribution, Standard},
|
|
|
|
Rng,
|
|
|
|
};
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2018-10-13 16:25:34 +08:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2017-08-03 01:37:44 +08:00
|
|
|
use std::fmt;
|
2017-05-04 10:02:30 +08:00
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::dimension::U3;
|
|
|
|
use crate::base::storage::Storage;
|
|
|
|
use crate::base::{Matrix4, Vector, Vector3};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::geometry::{Point3, Projective3};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2020-02-23 23:30:11 +08:00
|
|
|
/// A 3D orthographic projection stored as a homogeneous 4x4 matrix.
|
2021-08-03 00:41:46 +08:00
|
|
|
#[repr(C)]
|
2022-04-30 16:32:10 +08:00
|
|
|
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "rkyv-serialize-no-std",
|
2022-07-14 11:02:17 +08:00
|
|
|
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
|
|
|
|
archive(as = "Orthographic3<T::Archived>", bound(archive = "
|
|
|
|
T: rkyv::Archive,
|
|
|
|
Matrix4<T>: rkyv::Archive<Archived = Matrix4<T::Archived>>
|
|
|
|
"))
|
2022-04-30 16:32:10 +08:00
|
|
|
)]
|
2022-03-17 01:07:29 +08:00
|
|
|
#[cfg_attr(feature = "cuda", derive(cust_core::DeviceCopy))]
|
2021-11-23 19:03:11 +08:00
|
|
|
#[derive(Copy, Clone)]
|
2021-07-06 08:04:10 +08:00
|
|
|
pub struct Orthographic3<T> {
|
2021-04-11 17:00:38 +08:00
|
|
|
matrix: Matrix4<T>,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:41:46 +08:00
|
|
|
impl<T: RealField> fmt::Debug for Orthographic3<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
2017-08-03 01:37:44 +08:00
|
|
|
self.matrix.fmt(f)
|
2017-05-04 10:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:41:46 +08:00
|
|
|
impl<T: RealField> PartialEq for Orthographic3<T> {
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, right: &Self) -> bool {
|
|
|
|
self.matrix == right.matrix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 09:36:28 +08:00
|
|
|
#[cfg(feature = "bytemuck")]
|
2021-07-10 04:23:08 +08:00
|
|
|
unsafe impl<T> bytemuck::Zeroable for Orthographic3<T>
|
|
|
|
where
|
|
|
|
T: RealField + bytemuck::Zeroable,
|
|
|
|
Matrix4<T>: bytemuck::Zeroable,
|
|
|
|
{
|
|
|
|
}
|
2021-06-29 09:36:28 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "bytemuck")]
|
2021-07-10 04:23:08 +08:00
|
|
|
unsafe impl<T> bytemuck::Pod for Orthographic3<T>
|
|
|
|
where
|
|
|
|
T: RealField + bytemuck::Pod,
|
|
|
|
Matrix4<T>: bytemuck::Pod,
|
|
|
|
{
|
|
|
|
}
|
2021-06-29 09:36:28 +08:00
|
|
|
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2021-08-03 00:41:46 +08:00
|
|
|
impl<T: RealField + Serialize> Serialize for Orthographic3<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2018-02-02 19:26:35 +08:00
|
|
|
self.matrix.serialize(serializer)
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2021-08-03 00:41:46 +08:00
|
|
|
impl<'a, T: RealField + Deserialize<'a>> Deserialize<'a> for Orthographic3<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
Des: Deserializer<'a>,
|
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
let matrix = Matrix4::<T>::deserialize(deserializer)?;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-02-17 05:29:41 +08:00
|
|
|
Ok(Self::from_matrix_unchecked(matrix))
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:52:57 +08:00
|
|
|
impl<T> Orthographic3<T> {
|
2021-08-03 00:41:46 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
|
|
|
|
/// let mat = Matrix4::new(
|
|
|
|
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
|
|
|
|
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
|
|
|
|
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
|
|
|
|
/// 0.0, 0.0, 0.0, 1.0
|
|
|
|
/// );
|
|
|
|
/// let proj = Orthographic3::from_matrix_unchecked(mat);
|
|
|
|
/// assert_eq!(proj, Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub const fn from_matrix_unchecked(matrix: Matrix4<T>) -> Self {
|
|
|
|
Self { matrix }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: RealField> Orthographic3<T> {
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Creates a new orthographic projection matrix.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// This follows the OpenGL convention, so this will flip the `z` axis.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// // Check this projection actually transforms the view cuboid into the double-unit cube.
|
2021-07-24 10:22:59 +08:00
|
|
|
/// // See https://www.nalgebra.org/docs/user_guide/projections#orthographic-projection for more details.
|
2018-11-10 19:34:17 +08:00
|
|
|
/// let p1 = Point3::new(1.0, 2.0, -0.1);
|
|
|
|
/// let p2 = Point3::new(1.0, 2.0, -1000.0);
|
|
|
|
/// let p3 = Point3::new(1.0, 20.0, -0.1);
|
|
|
|
/// let p4 = Point3::new(1.0, 20.0, -1000.0);
|
|
|
|
/// let p5 = Point3::new(10.0, 2.0, -0.1);
|
|
|
|
/// let p6 = Point3::new(10.0, 2.0, -1000.0);
|
|
|
|
/// let p7 = Point3::new(10.0, 20.0, -0.1);
|
|
|
|
/// let p8 = Point3::new(10.0, 20.0, -1000.0);
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p1), Point3::new(-1.0, -1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p2), Point3::new(-1.0, -1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p3), Point3::new(-1.0, 1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p4), Point3::new(-1.0, 1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p5), Point3::new( 1.0, -1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p6), Point3::new( 1.0, -1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p7), Point3::new( 1.0, 1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p8), Point3::new( 1.0, 1.0, 1.0));
|
|
|
|
///
|
|
|
|
/// // This also works with flipped axis. In other words, we allow that
|
|
|
|
/// // `left > right`, `bottom > top`, and/or `znear > zfar`.
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p1), Point3::new( 1.0, 1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p2), Point3::new( 1.0, 1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p3), Point3::new( 1.0, -1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p4), Point3::new( 1.0, -1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p5), Point3::new(-1.0, 1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p6), Point3::new(-1.0, 1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p7), Point3::new(-1.0, -1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p8), Point3::new(-1.0, -1.0, -1.0));
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-08-03 00:41:46 +08:00
|
|
|
pub fn new(left: T, right: T, bottom: T, top: T, znear: T, zfar: T) -> Self {
|
|
|
|
let matrix = Matrix4::<T>::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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new orthographic projection matrix from an aspect ratio and the vertical field of view.
|
|
|
|
#[inline]
|
2021-08-03 00:41:46 +08:00
|
|
|
pub fn from_fov(aspect: T, vfov: T, znear: T, zfar: T) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
2018-11-10 19:34:17 +08:00
|
|
|
znear != zfar,
|
|
|
|
"The far plane must not be equal to the near plane."
|
2018-02-02 19:26:35 +08:00
|
|
|
);
|
|
|
|
assert!(
|
2021-04-11 17:00:38 +08:00
|
|
|
!relative_eq!(aspect, T::zero()),
|
2020-10-11 16:41:25 +08:00
|
|
|
"The aspect ratio must not be zero."
|
2018-02-02 19:26:35 +08:00
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let half: T = crate::convert(0.5);
|
2021-08-16 00:12:17 +08:00
|
|
|
let width = zfar.clone() * (vfov * half.clone()).tan();
|
2021-08-04 23:34:25 +08:00
|
|
|
let height = width.clone() / aspect;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
Self::new(
|
2021-08-04 23:34:25 +08:00
|
|
|
-width.clone() * half.clone(),
|
|
|
|
width * half.clone(),
|
|
|
|
-height.clone() * half.clone(),
|
2018-02-02 19:26:35 +08:00
|
|
|
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.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// let inv = proj.inverse();
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(inv * proj.as_matrix(), Matrix4::identity());
|
|
|
|
/// assert_relative_eq!(proj.as_matrix() * inv, Matrix4::identity());
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// let inv = proj.inverse();
|
|
|
|
/// assert_relative_eq!(inv * proj.as_matrix(), Matrix4::identity());
|
|
|
|
/// assert_relative_eq!(proj.as_matrix() * inv, Matrix4::identity());
|
|
|
|
/// ```
|
2017-02-13 01:17:09 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-08-03 00:41:46 +08:00
|
|
|
pub fn inverse(&self) -> Matrix4<T> {
|
2021-08-04 23:34:25 +08:00
|
|
|
let mut res = self.clone().to_homogeneous();
|
2017-02-13 01:17:09 +08:00
|
|
|
|
2021-08-04 23:34:25 +08:00
|
|
|
let inv_m11 = T::one() / self.matrix[(0, 0)].clone();
|
|
|
|
let inv_m22 = T::one() / self.matrix[(1, 1)].clone();
|
|
|
|
let inv_m33 = T::one() / self.matrix[(2, 2)].clone();
|
2017-02-13 01:17:09 +08:00
|
|
|
|
2021-08-04 23:34:25 +08:00
|
|
|
res[(0, 0)] = inv_m11.clone();
|
|
|
|
res[(1, 1)] = inv_m22.clone();
|
|
|
|
res[(2, 2)] = inv_m33.clone();
|
2017-02-13 01:17:09 +08:00
|
|
|
|
2021-08-04 23:34:25 +08:00
|
|
|
res[(0, 3)] = -self.matrix[(0, 3)].clone() * inv_m11;
|
|
|
|
res[(1, 3)] = -self.matrix[(1, 3)].clone() * inv_m22;
|
|
|
|
res[(2, 3)] = -self.matrix[(2, 3)].clone() * inv_m33;
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Computes the corresponding homogeneous matrix.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// let expected = Matrix4::new(
|
|
|
|
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
|
|
|
|
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
|
|
|
|
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
|
|
|
|
/// 0.0, 0.0, 0.0, 1.0
|
|
|
|
/// );
|
|
|
|
/// assert_eq!(proj.to_homogeneous(), expected);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-07-06 05:51:27 +08:00
|
|
|
pub fn to_homogeneous(self) -> Matrix4<T> {
|
2018-10-13 16:25:34 +08:00
|
|
|
self.matrix
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
/// A reference to the underlying homogeneous transformation matrix.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// let expected = Matrix4::new(
|
|
|
|
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
|
|
|
|
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
|
|
|
|
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
|
|
|
|
/// 0.0, 0.0, 0.0, 1.0
|
|
|
|
/// );
|
|
|
|
/// assert_eq!(*proj.as_matrix(), expected);
|
|
|
|
/// ```
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn as_matrix(&self) -> &Matrix4<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
&self.matrix
|
|
|
|
}
|
|
|
|
|
2018-09-22 22:02:55 +08:00
|
|
|
/// A reference to this transformation seen as a `Projective3`.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_eq!(proj.as_projective().to_homogeneous(), proj.to_homogeneous());
|
|
|
|
/// ```
|
2018-09-22 22:02:55 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn as_projective(&self) -> &Projective3<T> {
|
2021-08-03 00:41:46 +08:00
|
|
|
unsafe { &*(self as *const Orthographic3<T> as *const Projective3<T>) }
|
2018-09-22 22:02:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This transformation seen as a `Projective3`.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_eq!(proj.to_projective().to_homogeneous(), proj.to_homogeneous());
|
|
|
|
/// ```
|
2018-09-22 22:02:55 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-07-06 05:51:27 +08:00
|
|
|
pub fn to_projective(self) -> Projective3<T> {
|
2018-09-22 22:02:55 +08:00
|
|
|
Projective3::from_matrix_unchecked(self.matrix)
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
/// Retrieves the underlying homogeneous matrix.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// let expected = Matrix4::new(
|
|
|
|
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
|
|
|
|
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
|
|
|
|
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
|
|
|
|
/// 0.0, 0.0, 0.0, 1.0
|
|
|
|
/// );
|
2018-12-10 04:32:35 +08:00
|
|
|
/// assert_eq!(proj.into_inner(), expected);
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn into_inner(self) -> Matrix4<T> {
|
2018-12-10 04:32:35 +08:00
|
|
|
self.matrix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the underlying homogeneous matrix.
|
2021-07-28 07:18:29 +08:00
|
|
|
/// Deprecated: Use [`Orthographic3::into_inner`] instead.
|
2020-03-21 19:16:46 +08:00
|
|
|
#[deprecated(note = "use `.into_inner()` instead")]
|
2018-12-10 04:32:35 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unwrap(self) -> Matrix4<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
self.matrix
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// The left offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_relative_eq!(proj.left(), 1.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// assert_relative_eq!(proj.left(), 10.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn left(&self) -> T {
|
2021-08-04 23:34:25 +08:00
|
|
|
(-T::one() - self.matrix[(0, 3)].clone()) / self.matrix[(0, 0)].clone()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// The right offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_relative_eq!(proj.right(), 10.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// assert_relative_eq!(proj.right(), 1.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn right(&self) -> T {
|
2021-08-04 23:34:25 +08:00
|
|
|
(T::one() - self.matrix[(0, 3)].clone()) / self.matrix[(0, 0)].clone()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// The bottom offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_relative_eq!(proj.bottom(), 2.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// assert_relative_eq!(proj.bottom(), 20.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn bottom(&self) -> T {
|
2021-08-04 23:34:25 +08:00
|
|
|
(-T::one() - self.matrix[(1, 3)].clone()) / self.matrix[(1, 1)].clone()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// The top offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_relative_eq!(proj.top(), 20.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// assert_relative_eq!(proj.top(), 2.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn top(&self) -> T {
|
2021-08-04 23:34:25 +08:00
|
|
|
(T::one() - self.matrix[(1, 3)].clone()) / self.matrix[(1, 1)].clone()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The near plane offset of the view cuboid.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_relative_eq!(proj.znear(), 0.1, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// assert_relative_eq!(proj.znear(), 1000.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn znear(&self) -> T {
|
2021-08-04 23:34:25 +08:00
|
|
|
(T::one() + self.matrix[(2, 3)].clone()) / self.matrix[(2, 2)].clone()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The far plane offset of the view cuboid.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// assert_relative_eq!(proj.zfar(), 1000.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
|
|
|
|
/// assert_relative_eq!(proj.zfar(), 0.1, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn zfar(&self) -> T {
|
2021-08-04 23:34:25 +08:00
|
|
|
(-T::one() + self.matrix[(2, 3)].clone()) / self.matrix[(2, 2)].clone()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: when we get specialization, specialize the Mul impl instead.
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Projects a point. Faster than matrix multiplication.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
///
|
|
|
|
/// let p1 = Point3::new(1.0, 2.0, -0.1);
|
|
|
|
/// let p2 = Point3::new(1.0, 2.0, -1000.0);
|
|
|
|
/// let p3 = Point3::new(1.0, 20.0, -0.1);
|
|
|
|
/// let p4 = Point3::new(1.0, 20.0, -1000.0);
|
|
|
|
/// let p5 = Point3::new(10.0, 2.0, -0.1);
|
|
|
|
/// let p6 = Point3::new(10.0, 2.0, -1000.0);
|
|
|
|
/// let p7 = Point3::new(10.0, 20.0, -0.1);
|
|
|
|
/// let p8 = Point3::new(10.0, 20.0, -1000.0);
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p1), Point3::new(-1.0, -1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p2), Point3::new(-1.0, -1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p3), Point3::new(-1.0, 1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p4), Point3::new(-1.0, 1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p5), Point3::new( 1.0, -1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p6), Point3::new( 1.0, -1.0, 1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p7), Point3::new( 1.0, 1.0, -1.0));
|
|
|
|
/// assert_relative_eq!(proj.project_point(&p8), Point3::new( 1.0, 1.0, 1.0));
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn project_point(&self, p: &Point3<T>) -> Point3<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
Point3::new(
|
2021-08-04 23:34:25 +08:00
|
|
|
self.matrix[(0, 0)].clone() * p[0].clone() + self.matrix[(0, 3)].clone(),
|
|
|
|
self.matrix[(1, 1)].clone() * p[1].clone() + self.matrix[(1, 3)].clone(),
|
|
|
|
self.matrix[(2, 2)].clone() * p[2].clone() + self.matrix[(2, 3)].clone(),
|
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.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Orthographic3, Point3};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
///
|
|
|
|
/// let p1 = Point3::new(-1.0, -1.0, -1.0);
|
|
|
|
/// let p2 = Point3::new(-1.0, -1.0, 1.0);
|
|
|
|
/// let p3 = Point3::new(-1.0, 1.0, -1.0);
|
|
|
|
/// let p4 = Point3::new(-1.0, 1.0, 1.0);
|
|
|
|
/// let p5 = Point3::new( 1.0, -1.0, -1.0);
|
|
|
|
/// let p6 = Point3::new( 1.0, -1.0, 1.0);
|
|
|
|
/// let p7 = Point3::new( 1.0, 1.0, -1.0);
|
|
|
|
/// let p8 = Point3::new( 1.0, 1.0, 1.0);
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p1), Point3::new(1.0, 2.0, -0.1), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p2), Point3::new(1.0, 2.0, -1000.0), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p3), Point3::new(1.0, 20.0, -0.1), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p4), Point3::new(1.0, 20.0, -1000.0), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p5), Point3::new(10.0, 2.0, -0.1), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p6), Point3::new(10.0, 2.0, -1000.0), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p7), Point3::new(10.0, 20.0, -0.1), epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.unproject_point(&p8), Point3::new(10.0, 20.0, -1000.0), epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2017-02-13 01:17:09 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unproject_point(&self, p: &Point3<T>) -> Point3<T> {
|
2017-08-03 01:37:44 +08:00
|
|
|
Point3::new(
|
2021-08-04 23:34:25 +08:00
|
|
|
(p[0].clone() - self.matrix[(0, 3)].clone()) / self.matrix[(0, 0)].clone(),
|
|
|
|
(p[1].clone() - self.matrix[(1, 3)].clone()) / self.matrix[(1, 1)].clone(),
|
|
|
|
(p[2].clone() - self.matrix[(2, 3)].clone()) / self.matrix[(2, 2)].clone(),
|
2017-02-13 01:17:09 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: when we get specialization, specialize the Mul impl instead.
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Projects a vector. Faster than matrix multiplication.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
|
|
|
/// Vectors are not affected by the translation part of the projection.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::{Orthographic3, Vector3};
|
|
|
|
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
///
|
|
|
|
/// let v1 = Vector3::x();
|
|
|
|
/// let v2 = Vector3::y();
|
|
|
|
/// let v3 = Vector3::z();
|
|
|
|
///
|
|
|
|
/// assert_relative_eq!(proj.project_vector(&v1), Vector3::x() * 2.0 / 9.0);
|
|
|
|
/// assert_relative_eq!(proj.project_vector(&v2), Vector3::y() * 2.0 / 18.0);
|
|
|
|
/// assert_relative_eq!(proj.project_vector(&v3), Vector3::z() * -2.0 / 999.9);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn project_vector<SB>(&self, p: &Vector<T, U3, SB>) -> Vector3<T>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
SB: Storage<T, U3>,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
Vector3::new(
|
2021-08-04 23:34:25 +08:00
|
|
|
self.matrix[(0, 0)].clone() * p[0].clone(),
|
|
|
|
self.matrix[(1, 1)].clone() * p[1].clone(),
|
|
|
|
self.matrix[(2, 2)].clone() * p[2].clone(),
|
2016-12-05 05:44:42 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// Sets the left offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_left(2.0);
|
|
|
|
/// assert_relative_eq!(proj.left(), 2.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is OK to set a left offset greater than the current right offset.
|
|
|
|
/// proj.set_left(20.0);
|
|
|
|
/// assert_relative_eq!(proj.left(), 20.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_left(&mut self, left: T) {
|
2016-12-05 05:44:42 +08:00
|
|
|
let right = self.right();
|
|
|
|
self.set_left_and_right(left, right);
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// Sets the right offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_right(15.0);
|
|
|
|
/// assert_relative_eq!(proj.right(), 15.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is OK to set a right offset smaller than the current left offset.
|
|
|
|
/// proj.set_right(-3.0);
|
|
|
|
/// assert_relative_eq!(proj.right(), -3.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_right(&mut self, right: T) {
|
2016-12-05 05:44:42 +08:00
|
|
|
let left = self.left();
|
|
|
|
self.set_left_and_right(left, right);
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// Sets the bottom offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_bottom(8.0);
|
|
|
|
/// assert_relative_eq!(proj.bottom(), 8.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is OK to set a bottom offset greater than the current top offset.
|
|
|
|
/// proj.set_bottom(50.0);
|
|
|
|
/// assert_relative_eq!(proj.bottom(), 50.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_bottom(&mut self, bottom: T) {
|
2016-12-05 05:44:42 +08:00
|
|
|
let top = self.top();
|
|
|
|
self.set_bottom_and_top(bottom, top);
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// Sets the top offset of the view cuboid.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_top(15.0);
|
|
|
|
/// assert_relative_eq!(proj.top(), 15.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is OK to set a top offset smaller than the current bottom offset.
|
|
|
|
/// proj.set_top(-3.0);
|
|
|
|
/// assert_relative_eq!(proj.top(), -3.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_top(&mut self, top: T) {
|
2016-12-05 05:44:42 +08:00
|
|
|
let bottom = self.bottom();
|
|
|
|
self.set_bottom_and_top(bottom, top);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the near plane offset of the view cuboid.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_znear(8.0);
|
|
|
|
/// assert_relative_eq!(proj.znear(), 8.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is OK to set a znear greater than the current zfar.
|
|
|
|
/// proj.set_znear(5000.0);
|
|
|
|
/// assert_relative_eq!(proj.znear(), 5000.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_znear(&mut self, znear: T) {
|
2016-12-05 05:44:42 +08:00
|
|
|
let zfar = self.zfar();
|
|
|
|
self.set_znear_and_zfar(znear, zfar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the far plane offset of the view cuboid.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_zfar(15.0);
|
|
|
|
/// assert_relative_eq!(proj.zfar(), 15.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is OK to set a zfar smaller than the current znear.
|
|
|
|
/// proj.set_zfar(-3.0);
|
|
|
|
/// assert_relative_eq!(proj.zfar(), -3.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_zfar(&mut self, zfar: T) {
|
2016-12-05 05:44:42 +08:00
|
|
|
let znear = self.znear();
|
|
|
|
self.set_znear_and_zfar(znear, zfar);
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// Sets the view cuboid offsets along the `x` axis.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_left_and_right(7.0, 70.0);
|
|
|
|
/// assert_relative_eq!(proj.left(), 7.0, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.right(), 70.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is also OK to have `left > right`.
|
|
|
|
/// proj.set_left_and_right(70.0, 7.0);
|
|
|
|
/// assert_relative_eq!(proj.left(), 70.0, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.right(), 7.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_left_and_right(&mut self, left: T, right: T) {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
2018-11-10 19:34:17 +08:00
|
|
|
left != right,
|
|
|
|
"The left corner must not be equal to the right corner."
|
2018-02-02 19:26:35 +08:00
|
|
|
);
|
2021-08-04 23:34:25 +08:00
|
|
|
self.matrix[(0, 0)] = crate::convert::<_, T>(2.0) / (right.clone() - left.clone());
|
|
|
|
self.matrix[(0, 3)] = -(right.clone() + left.clone()) / (right - left);
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 19:34:17 +08:00
|
|
|
/// Sets the view cuboid offsets along the `y` axis.
|
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_bottom_and_top(7.0, 70.0);
|
|
|
|
/// assert_relative_eq!(proj.bottom(), 7.0, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.top(), 70.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is also OK to have `bottom > top`.
|
|
|
|
/// proj.set_bottom_and_top(70.0, 7.0);
|
|
|
|
/// assert_relative_eq!(proj.bottom(), 70.0, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.top(), 7.0, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_bottom_and_top(&mut self, bottom: T, top: T) {
|
2021-08-04 23:34:25 +08:00
|
|
|
assert_ne!(
|
|
|
|
bottom, top,
|
2018-11-10 19:34:17 +08:00
|
|
|
"The top corner must not be equal to the bottom corner."
|
2018-02-02 19:26:35 +08:00
|
|
|
);
|
2021-08-04 23:34:25 +08:00
|
|
|
self.matrix[(1, 1)] = crate::convert::<_, T>(2.0) / (top.clone() - bottom.clone());
|
|
|
|
self.matrix[(1, 3)] = -(top.clone() + bottom.clone()) / (top - bottom);
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the near and far plane offsets of the view cuboid.
|
2018-11-10 19:34:17 +08:00
|
|
|
///
|
2022-03-23 00:53:46 +08:00
|
|
|
/// # Example
|
2018-11-10 19:34:17 +08:00
|
|
|
/// ```
|
|
|
|
/// # #[macro_use] extern crate approx;
|
|
|
|
/// # use nalgebra::Orthographic3;
|
|
|
|
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
|
|
|
|
/// proj.set_znear_and_zfar(50.0, 5000.0);
|
|
|
|
/// assert_relative_eq!(proj.znear(), 50.0, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.zfar(), 5000.0, epsilon = 1.0e-6);
|
|
|
|
///
|
|
|
|
/// // It is also OK to have `znear > zfar`.
|
|
|
|
/// proj.set_znear_and_zfar(5000.0, 0.5);
|
|
|
|
/// assert_relative_eq!(proj.znear(), 5000.0, epsilon = 1.0e-6);
|
|
|
|
/// assert_relative_eq!(proj.zfar(), 0.5, epsilon = 1.0e-6);
|
|
|
|
/// ```
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn set_znear_and_zfar(&mut self, znear: T, zfar: T) {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
2018-11-10 19:34:17 +08:00
|
|
|
zfar != znear,
|
2018-02-02 19:26:35 +08:00
|
|
|
"The near-plane and far-plane must not be superimposed."
|
|
|
|
);
|
2021-08-04 23:34:25 +08:00
|
|
|
self.matrix[(2, 2)] = -crate::convert::<_, T>(2.0) / (zfar.clone() - znear.clone());
|
|
|
|
self.matrix[(2, 3)] = -(zfar.clone() + znear.clone()) / (zfar - znear);
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:25:12 +08:00
|
|
|
#[cfg(feature = "rand-no-std")]
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: RealField> Distribution<Orthographic3<T>> for Standard
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
Standard: Distribution<T>,
|
2018-05-23 05:58:14 +08:00
|
|
|
{
|
2021-04-10 14:20:30 +08:00
|
|
|
/// Generate an arbitrary random variate for testing purposes.
|
2021-04-11 17:00:38 +08:00
|
|
|
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<T> {
|
2021-03-02 19:25:12 +08:00
|
|
|
use crate::base::helper;
|
2018-05-23 05:58:14 +08:00
|
|
|
let left = r.gen();
|
2021-04-11 17:00:38 +08:00
|
|
|
let right = helper::reject_rand(r, |x: &T| *x > left);
|
2018-05-23 05:58:14 +08:00
|
|
|
let bottom = r.gen();
|
2021-04-11 17:00:38 +08:00
|
|
|
let top = helper::reject_rand(r, |x: &T| *x > bottom);
|
2018-05-23 05:58:14 +08:00
|
|
|
let znear = r.gen();
|
2021-04-11 17:00:38 +08:00
|
|
|
let zfar = helper::reject_rand(r, |x: &T| *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")]
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: RealField + Arbitrary> Arbitrary for Orthographic3<T>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
Matrix4<T>: Send,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2021-03-01 00:52:14 +08:00
|
|
|
fn arbitrary(g: &mut Gen) -> Self {
|
2021-03-02 19:25:12 +08:00
|
|
|
use crate::base::helper;
|
2018-02-02 19:26:35 +08:00
|
|
|
let left = Arbitrary::arbitrary(g);
|
2021-04-11 17:00:38 +08:00
|
|
|
let right = helper::reject(g, |x: &T| *x > left);
|
2016-12-05 05:44:42 +08:00
|
|
|
let bottom = Arbitrary::arbitrary(g);
|
2021-04-11 17:00:38 +08:00
|
|
|
let top = helper::reject(g, |x: &T| *x > bottom);
|
2018-02-02 19:26:35 +08:00
|
|
|
let znear = Arbitrary::arbitrary(g);
|
2021-04-11 17:00:38 +08:00
|
|
|
let zfar = helper::reject(g, |x: &T| *x > znear);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
Self::new(left, right, bottom, top, znear, zfar)
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 16:25:34 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: RealField> From<Orthographic3<T>> for Matrix4<T> {
|
2018-10-13 16:25:34 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
fn from(orth: Orthographic3<T>) -> Self {
|
2018-12-10 04:32:35 +08:00
|
|
|
orth.into_inner()
|
2018-10-13 16:25:34 +08:00
|
|
|
}
|
|
|
|
}
|