Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make from_matrix_uncheckedes const #950

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/geometry/orthographic.rs
Expand Up @@ -66,6 +66,30 @@ impl<'a, T: RealField + Deserialize<'a>> Deserialize<'a> for Orthographic3<T> {
}
}

impl<T> Orthographic3<T> {
/// 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> {
/// Creates a new orthographic projection matrix.
///
Expand Down Expand Up @@ -121,28 +145,6 @@ impl<T: RealField> Orthographic3<T> {
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.
///
/// # 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 fn from_matrix_unchecked(matrix: Matrix4<T>) -> Self {
Self { matrix }
}

/// Creates a new orthographic projection matrix from an aspect ratio and the vertical field of view.
#[inline]
pub fn from_fov(aspect: T, vfov: T, znear: T, zfar: T) -> Self {
Expand Down
20 changes: 11 additions & 9 deletions src/geometry/perspective.rs
Expand Up @@ -67,6 +67,17 @@ impl<'a, T: RealField + Deserialize<'a>> Deserialize<'a> for Perspective3<T> {
}
}

impl<T> Perspective3<T> {
/// Wraps the given matrix to interpret it as a 3D perspective matrix.
///
/// It is not checked whether or not the given matrix actually represents a perspective
/// projection.
#[inline]
pub const fn from_matrix_unchecked(matrix: Matrix4<T>) -> Self {
Self { matrix }
}
}

impl<T: RealField> Perspective3<T> {
/// Creates a new perspective matrix from the aspect ratio, y field of view, and near/far planes.
pub fn new(aspect: T, fovy: T, znear: T, zfar: T) -> Self {
Expand All @@ -92,15 +103,6 @@ impl<T: RealField> Perspective3<T> {
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 a perspective
/// projection.
#[inline]
pub fn from_matrix_unchecked(matrix: Matrix4<T>) -> Self {
Self { matrix }
}

/// Retrieves the inverse of the underlying homogeneous matrix.
#[inline]
#[must_use]
Expand Down
11 changes: 3 additions & 8 deletions src/geometry/rotation.rs
Expand Up @@ -130,10 +130,10 @@ where
}
}

impl<T: Scalar, const D: usize> Rotation<T, D> {
impl<T, const D: usize> Rotation<T, D> {
/// Creates a new rotation from the given square matrix.
///
/// The matrix squareness is checked but not its orthonormality.
/// The matrix orthonormality is not checked.
///
/// # Example
/// ```
Expand All @@ -154,12 +154,7 @@ impl<T: Scalar, const D: usize> Rotation<T, D> {
/// assert_eq!(*rot.matrix(), mat);
/// ```
#[inline]
pub fn from_matrix_unchecked(matrix: SMatrix<T, D, D>) -> Self {
assert!(
matrix.is_square(),
"Unable to create a rotation from a non-square matrix."
);
Comment on lines -158 to -161
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that squareness is still statically enforced, since we take SMatrix<_, D, D>, which doesn't allow for dynamic sizing.


pub const fn from_matrix_unchecked(matrix: SMatrix<T, D, D>) -> Self {
Self { matrix }
}
}
Expand Down