diff --git a/CHANGELOG.md b/CHANGELOG.md index e29422a6..ab56eaa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). - `::canonical_basis_with_dim` -> `::canonical_basis_with_dimension` - `::from_elem` -> `::from_element` - `DiagMut` -> `DiagonalMut` + - `UnitQuaternion::new` becomes `UnitQuaternion::from_scaled_axis` or + `UnitQuaternion::from_axisangle`. The new `::new` method now requires a + not-normalized quaternion. + +Methods names starting with `new_with_` now start with `from_`. This is more +idiomatic in Rust. The `Norm` trait now uses an associated type instead of a type parameter. Other similar trait changes are to be expected in the future, e.g., for the @@ -27,8 +33,8 @@ longer unsafe. Instead, their name end with `_unchecked`. In particular: ### Added - A `Unit` type that wraps normalized values. In particular, - `UnitQuaternion` is now replaced by `Unit>`. - - `.ln()`, `.exp()` and `.powf(..)` for quaternions. + `UnitQuaternion` is now an alias for `Unit>`. + - `.ln()`, `.exp()` and `.powf(..)` for quaternions and unit quaternions. - `::from_parts(...)` to build a quaternion from its scalar and vector parts. - The `Norm` trait now has a `try_normalize()` that returns `None` if the diff --git a/src/structs/isometry.rs b/src/structs/isometry.rs index 9dda562b..7b9ceb8e 100644 --- a/src/structs/isometry.rs +++ b/src/structs/isometry.rs @@ -59,7 +59,7 @@ impl Isometry3 { #[inline] pub fn new_observer_frame(eye: &Point3, target: &Point3, up: &Vector3) -> Isometry3 { let new_rotation_matrix = Rotation3::new_observer_frame(&(*target - *eye), up); - Isometry3::new_with_rotation_matrix(eye.to_vector(), new_rotation_matrix) + Isometry3::from_rotation_matrix(eye.to_vector(), new_rotation_matrix) } /// Builds a right-handed look-at view matrix. @@ -77,7 +77,7 @@ impl Isometry3 { let rotation = Rotation3::look_at_rh(&(*target - *eye), up); let trans = rotation * (-*eye); - Isometry3::new_with_rotation_matrix(trans.to_vector(), rotation) + Isometry3::from_rotation_matrix(trans.to_vector(), rotation) } /// Builds a left-handed look-at view matrix. @@ -95,7 +95,7 @@ impl Isometry3 { let rotation = Rotation3::look_at_lh(&(*target - *eye), up); let trans = rotation * (-*eye); - Isometry3::new_with_rotation_matrix(trans.to_vector(), rotation) + Isometry3::from_rotation_matrix(trans.to_vector(), rotation) } } diff --git a/src/structs/isometry_macros.rs b/src/structs/isometry_macros.rs index de0aca4f..14cfb5fd 100644 --- a/src/structs/isometry_macros.rs +++ b/src/structs/isometry_macros.rs @@ -15,7 +15,7 @@ macro_rules! isometry_impl( /// Creates a new isometry from a rotation matrix and a vector. #[inline] - pub fn new_with_rotation_matrix(translation: $vector, rotation: $rotmatrix) -> $t { + pub fn from_rotation_matrix(translation: $vector, rotation: $rotmatrix) -> $t { $t { rotation: rotation, translation: translation @@ -48,7 +48,7 @@ macro_rules! isometry_impl( impl One for $t { #[inline] fn one() -> $t { - $t::new_with_rotation_matrix(::zero(), ::one()) + $t::from_rotation_matrix(::zero(), ::one()) } } @@ -63,7 +63,7 @@ macro_rules! isometry_impl( #[inline] fn mul(self, right: $t) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self.translation + self.rotation * right.translation, self.rotation * right.rotation) } @@ -88,7 +88,7 @@ macro_rules! isometry_impl( #[inline] fn mul(self, right: $rotmatrix) -> $t { - $t::new_with_rotation_matrix(self.translation, self.rotation * right) + $t::from_rotation_matrix(self.translation, self.rotation * right) } } @@ -97,7 +97,7 @@ macro_rules! isometry_impl( #[inline] fn mul(self, right: $t) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self * right.translation, self * right.rotation) } @@ -164,7 +164,7 @@ macro_rules! isometry_impl( #[inline] fn append_translation(&self, t: &$vector) -> $t { - $t::new_with_rotation_matrix(*t + self.translation, self.rotation) + $t::from_rotation_matrix(*t + self.translation, self.rotation) } #[inline] @@ -174,7 +174,7 @@ macro_rules! isometry_impl( #[inline] fn prepend_translation(&self, t: &$vector) -> $t { - $t::new_with_rotation_matrix(self.translation + self.rotation * *t, self.rotation) + $t::from_rotation_matrix(self.translation + self.rotation * *t, self.rotation) } #[inline] @@ -230,7 +230,7 @@ macro_rules! isometry_impl( fn append_rotation(&self, rotation: &$rotvector) -> $t { let delta = $rotmatrix::new(*rotation); - $t::new_with_rotation_matrix(delta * self.translation, delta * self.rotation) + $t::from_rotation_matrix(delta * self.translation, delta * self.rotation) } #[inline] @@ -244,7 +244,7 @@ macro_rules! isometry_impl( fn prepend_rotation(&self, rotation: &$rotvector) -> $t { let delta = $rotmatrix::new(*rotation); - $t::new_with_rotation_matrix(self.translation, self.rotation * delta) + $t::from_rotation_matrix(self.translation, self.rotation * delta) } #[inline] @@ -435,7 +435,7 @@ macro_rules! isometry_impl( #[cfg(feature="arbitrary")] impl Arbitrary for $t { fn arbitrary(g: &mut G) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( Arbitrary::arbitrary(g), Arbitrary::arbitrary(g) ) diff --git a/src/structs/orthographic.rs b/src/structs/orthographic.rs index 9564fcfb..7e355279 100644 --- a/src/structs/orthographic.rs +++ b/src/structs/orthographic.rs @@ -183,7 +183,7 @@ impl OrthographicMatrix3 { } /// Creates a new orthographic projection matrix from an aspect ratio and the vertical field of view. - pub fn new_with_fov(aspect: N, vfov: N, znear: N, zfar: N) -> OrthographicMatrix3 { + pub fn from_fov(aspect: N, vfov: N, znear: N, zfar: N) -> OrthographicMatrix3 { assert!(znear < zfar, "The far plane must be farther than the near plane."); assert!(!::is_zero(&aspect)); @@ -196,7 +196,7 @@ impl OrthographicMatrix3 { /// Creates a new orthographic matrix from a 4D matrix. #[inline] - pub fn new_with_matrix_unchecked(matrix: Matrix4) -> OrthographicMatrix3 { + pub fn from_matrix_unchecked(matrix: Matrix4) -> OrthographicMatrix3 { OrthographicMatrix3 { matrix: matrix } diff --git a/src/structs/perspective.rs b/src/structs/perspective.rs index 0a03c604..4788e279 100644 --- a/src/structs/perspective.rs +++ b/src/structs/perspective.rs @@ -155,7 +155,7 @@ impl PerspectiveMatrix3 { /// Creates a new perspective projection matrix from a 4D matrix. #[inline] - pub fn new_with_matrix_unchecked(matrix: Matrix4) -> PerspectiveMatrix3 { + pub fn from_matrix_unchecked(matrix: Matrix4) -> PerspectiveMatrix3 { PerspectiveMatrix3 { matrix: matrix } diff --git a/src/structs/quaternion.rs b/src/structs/quaternion.rs index 0752a0ac..2c0128ad 100644 --- a/src/structs/quaternion.rs +++ b/src/structs/quaternion.rs @@ -337,7 +337,7 @@ impl UnitQuaternion { let jk = self.as_ref().j * self.as_ref().k * ::cast(2.0); let wi = self.as_ref().w * self.as_ref().i * ::cast(2.0); - Rotation3::new_with_matrix_unchecked( + Rotation3::from_matrix_unchecked( Matrix3::new( ww + ii - jj - kk, ij - wk, wj + ik, wk + ij, ww - ii + jj - kk, jk - wi, diff --git a/src/structs/rotation.rs b/src/structs/rotation.rs index 4c8f63a8..c8be3a11 100644 --- a/src/structs/rotation.rs +++ b/src/structs/rotation.rs @@ -157,7 +157,7 @@ impl Rotation3 { } /// Builds a rotation matrix from an orthogonal matrix. - pub fn new_with_matrix_unchecked(matrix: Matrix3) -> Rotation3 { + pub fn from_matrix_unchecked(matrix: Matrix3) -> Rotation3 { Rotation3 { submatrix: matrix } @@ -166,12 +166,12 @@ impl Rotation3 { /// Creates a new rotation from Euler angles. /// /// The primitive rotations are applied in order: 1 roll − 2 pitch − 3 yaw. - pub fn new_with_euler_angles(roll: N, pitch: N, yaw: N) -> Rotation3 { + pub fn from_euler_angles(roll: N, pitch: N, yaw: N) -> Rotation3 { let (sr, cr) = roll.sin_cos(); let (sp, cp) = pitch.sin_cos(); let (sy, cy) = yaw.sin_cos(); - Rotation3::new_with_matrix_unchecked( + Rotation3::from_matrix_unchecked( Matrix3::new( cy * cp, cy * sp * sr - sy * cr, cy * sp * cr + sy * sr, sy * cp, sy * sp * sr + cy * cr, sy * sp * cr - cy * sr, @@ -198,7 +198,7 @@ impl Rotation3 { let xaxis = Norm::normalize(&Cross::cross(up, &zaxis)); let yaxis = Norm::normalize(&Cross::cross(&zaxis, &xaxis)); - Rotation3::new_with_matrix_unchecked(Matrix3::new( + Rotation3::from_matrix_unchecked(Matrix3::new( xaxis.x, yaxis.x, zaxis.x, xaxis.y, yaxis.y, zaxis.y, xaxis.z, yaxis.z, zaxis.z)) diff --git a/src/structs/similarity_macros.rs b/src/structs/similarity_macros.rs index 845e3d6e..73e8aaf7 100644 --- a/src/structs/similarity_macros.rs +++ b/src/structs/similarity_macros.rs @@ -29,12 +29,12 @@ macro_rules! similarity_impl( /// /// The scale factor may be negative but not zero. #[inline] - pub fn new_with_rotation_matrix(translation: $vector, rotation: $rotation_matrix, scale: N) -> $t { + pub fn from_rotation_matrix(translation: $vector, rotation: $rotation_matrix, scale: N) -> $t { assert!(!scale.is_zero(), "A similarity transformation scale factor cannot be zero."); $t { scale: scale, - isometry: $isometry::new_with_rotation_matrix(translation, rotation) + isometry: $isometry::from_rotation_matrix(translation, rotation) } } @@ -42,7 +42,7 @@ macro_rules! similarity_impl( /// /// The scale factor may be negative but not zero. #[inline] - pub fn new_with_isometry(isometry: $isometry, scale: N) -> $t { + pub fn from_isometry(isometry: $isometry, scale: N) -> $t { assert!(!scale.is_zero(), "A similarity transformation scale factor cannot be zero."); $t { @@ -80,7 +80,7 @@ macro_rules! similarity_impl( #[inline] pub fn append_scale(&self, s: &N) -> $t { assert!(!s.is_zero(), "Cannot append a zero scale to a similarity transformation."); - $t::new_with_rotation_matrix(self.isometry.translation * *s, self.isometry.rotation, self.scale * *s) + $t::from_rotation_matrix(self.isometry.translation * *s, self.isometry.rotation, self.scale * *s) } /// Prepends in-place a scale to this similarity transformation. @@ -94,7 +94,7 @@ macro_rules! similarity_impl( #[inline] pub fn prepend_scale(&self, s: &N) -> $t { assert!(!s.is_zero(), "A similarity transformation scale must not be zero."); - $t::new_with_isometry(self.isometry, self.scale * *s) + $t::from_isometry(self.isometry, self.scale * *s) } /// Sets the scale of this similarity transformation. @@ -113,7 +113,7 @@ macro_rules! similarity_impl( impl One for $t { #[inline] fn one() -> $t { - $t::new_with_isometry(::one(), ::one()) + $t::from_isometry(::one(), ::one()) } } @@ -164,7 +164,7 @@ macro_rules! similarity_impl( #[inline] fn mul(self, right: $t) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self.isometry.translation + self.isometry.rotation * (right.isometry.translation * self.scale), self.isometry.rotation * right.isometry.rotation, self.scale * right.scale) @@ -191,7 +191,7 @@ macro_rules! similarity_impl( #[inline] fn mul(self, right: $isometry) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self.isometry.translation + self.isometry.rotation * (right.translation * self.scale), self.isometry.rotation * right.rotation, self.scale) @@ -211,7 +211,7 @@ macro_rules! similarity_impl( #[inline] fn mul(self, right: $t) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self.translation + self.rotation * right.isometry.translation, self.rotation * right.isometry.rotation, right.scale) @@ -228,7 +228,7 @@ macro_rules! similarity_impl( #[inline] fn mul(self, right: $rotation_matrix) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self.isometry.translation, self.isometry.rotation * right, self.scale) @@ -247,7 +247,7 @@ macro_rules! similarity_impl( #[inline] fn mul(self, right: $t) -> $t { - $t::new_with_rotation_matrix( + $t::from_rotation_matrix( self * right.isometry.translation, self * right.isometry.rotation, right.scale) @@ -389,7 +389,7 @@ macro_rules! similarity_impl( scale = rng.gen(); } - $t::new_with_isometry(rng.gen(), scale) + $t::from_isometry(rng.gen(), scale) } } @@ -402,7 +402,7 @@ macro_rules! similarity_impl( #[cfg(feature="arbitrary")] impl Arbitrary for $t { fn arbitrary(g: &mut G) -> $t { - $t::new_with_isometry( + $t::from_isometry( Arbitrary::arbitrary(g), Arbitrary::arbitrary(g) ) diff --git a/src/traits/operations.rs b/src/traits/operations.rs index 5934293f..7059e30e 100644 --- a/src/traits/operations.rs +++ b/src/traits/operations.rs @@ -173,7 +173,7 @@ pub trait ApproxEq: Sized { impl ApproxEq for f32 { #[inline] fn approx_epsilon(_: Option) -> f32 { - f32::EPSILON * 10.0 + 1.0e-6 } #[inline] @@ -204,7 +204,7 @@ impl ApproxEq for f32 { impl ApproxEq for f64 { #[inline] fn approx_epsilon(_: Option) -> f64 { - f64::EPSILON * 10.0 + 1.0e-6 } #[inline] diff --git a/tests/quat.rs b/tests/quat.rs index 1b0185eb..868f0092 100644 --- a/tests/quat.rs +++ b/tests/quat.rs @@ -9,7 +9,7 @@ fn test_quaternion_as_matrix() { for _ in 0usize .. 10000 { let axis_angle: Vector3 = random(); - assert!(na::approx_eq(&UnitQuaternion::new(axis_angle).to_rotation_matrix(), &Rotation3::new(axis_angle))) + assert!(na::approx_eq(&UnitQuaternion::from_scaled_axis(axis_angle).to_rotation_matrix(), &Rotation3::new(axis_angle))) } } @@ -21,7 +21,7 @@ fn test_quaternion_mul_vec_or_point_as_matrix() { let point: Point3 = random(); let matrix = Rotation3::new(axis_angle); - let quaternion = UnitQuaternion::new(axis_angle); + let quaternion = UnitQuaternion::from_scaled_axis(axis_angle); assert!(na::approx_eq(&(matrix * vector), &(quaternion * vector))); assert!(na::approx_eq(&(matrix * point), &(quaternion * point))); @@ -39,8 +39,8 @@ fn test_quaternion_div_quaternion() { let r1 = Rotation3::new(axis_angle1); let r2 = na::inverse(&Rotation3::new(axis_angle2)).unwrap(); - let q1 = UnitQuaternion::new(axis_angle1); - let q2 = UnitQuaternion::new(axis_angle2); + let q1 = UnitQuaternion::from_scaled_axis(axis_angle1); + let q2 = UnitQuaternion::from_scaled_axis(axis_angle2); assert!(na::approx_eq(&(q1 / q2).to_rotation_matrix(), &(r1 * r2))) } @@ -51,7 +51,7 @@ fn test_quaternion_to_axis_angle() { for _ in 0usize .. 10000 { let axis_angle: Vector3 = random(); - let q = UnitQuaternion::new(axis_angle); + let q = UnitQuaternion::from_scaled_axis(axis_angle); println!("{:?} {:?}", q.rotation(), axis_angle); assert!(na::approx_eq(&q.rotation(), &axis_angle)) @@ -63,8 +63,8 @@ fn test_quaternion_euler_angles() { for _ in 0usize .. 10000 { let angles: Vector3 = random(); - let q = UnitQuaternion::new_with_euler_angles(angles.x, angles.y, angles.z); - let m = Rotation3::new_with_euler_angles(angles.x, angles.y, angles.z); + let q = UnitQuaternion::from_euler_angles(angles.x, angles.y, angles.z); + let m = Rotation3::from_euler_angles(angles.x, angles.y, angles.z); assert!(na::approx_eq(&q.to_rotation_matrix(), &m)) }