Rename methods starting with `new_with_` to `from_`.

This commit is contained in:
Sébastien Crozet 2016-08-16 10:48:41 +02:00
parent d45c242a15
commit 9c4bff1f85
10 changed files with 51 additions and 45 deletions

View File

@ -14,6 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- `::canonical_basis_with_dim` -> `::canonical_basis_with_dimension` - `::canonical_basis_with_dim` -> `::canonical_basis_with_dimension`
- `::from_elem` -> `::from_element` - `::from_elem` -> `::from_element`
- `DiagMut` -> `DiagonalMut` - `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. 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 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 ### Added
- A `Unit<T>` type that wraps normalized values. In particular, - A `Unit<T>` type that wraps normalized values. In particular,
`UnitQuaternion<N>` is now replaced by `Unit<Quaternion<N>>`. `UnitQuaternion<N>` is now an alias for `Unit<Quaternion<N>>`.
- `.ln()`, `.exp()` and `.powf(..)` for quaternions. - `.ln()`, `.exp()` and `.powf(..)` for quaternions and unit quaternions.
- `::from_parts(...)` to build a quaternion from its scalar and vector - `::from_parts(...)` to build a quaternion from its scalar and vector
parts. parts.
- The `Norm` trait now has a `try_normalize()` that returns `None` if the - The `Norm` trait now has a `try_normalize()` that returns `None` if the

View File

@ -59,7 +59,7 @@ impl<N: BaseFloat> Isometry3<N> {
#[inline] #[inline]
pub fn new_observer_frame(eye: &Point3<N>, target: &Point3<N>, up: &Vector3<N>) -> Isometry3<N> { pub fn new_observer_frame(eye: &Point3<N>, target: &Point3<N>, up: &Vector3<N>) -> Isometry3<N> {
let new_rotation_matrix = Rotation3::new_observer_frame(&(*target - *eye), up); 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. /// Builds a right-handed look-at view matrix.
@ -77,7 +77,7 @@ impl<N: BaseFloat> Isometry3<N> {
let rotation = Rotation3::look_at_rh(&(*target - *eye), up); let rotation = Rotation3::look_at_rh(&(*target - *eye), up);
let trans = rotation * (-*eye); 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. /// Builds a left-handed look-at view matrix.
@ -95,7 +95,7 @@ impl<N: BaseFloat> Isometry3<N> {
let rotation = Rotation3::look_at_lh(&(*target - *eye), up); let rotation = Rotation3::look_at_lh(&(*target - *eye), up);
let trans = rotation * (-*eye); let trans = rotation * (-*eye);
Isometry3::new_with_rotation_matrix(trans.to_vector(), rotation) Isometry3::from_rotation_matrix(trans.to_vector(), rotation)
} }
} }

View File

@ -15,7 +15,7 @@ macro_rules! isometry_impl(
/// Creates a new isometry from a rotation matrix and a vector. /// Creates a new isometry from a rotation matrix and a vector.
#[inline] #[inline]
pub fn new_with_rotation_matrix(translation: $vector<N>, rotation: $rotmatrix<N>) -> $t<N> { pub fn from_rotation_matrix(translation: $vector<N>, rotation: $rotmatrix<N>) -> $t<N> {
$t { $t {
rotation: rotation, rotation: rotation,
translation: translation translation: translation
@ -48,7 +48,7 @@ macro_rules! isometry_impl(
impl<N: BaseFloat> One for $t<N> { impl<N: BaseFloat> One for $t<N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t::new_with_rotation_matrix(::zero(), ::one()) $t::from_rotation_matrix(::zero(), ::one())
} }
} }
@ -63,7 +63,7 @@ macro_rules! isometry_impl(
#[inline] #[inline]
fn mul(self, right: $t<N>) -> $t<N> { fn mul(self, right: $t<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self.translation + self.rotation * right.translation, self.translation + self.rotation * right.translation,
self.rotation * right.rotation) self.rotation * right.rotation)
} }
@ -88,7 +88,7 @@ macro_rules! isometry_impl(
#[inline] #[inline]
fn mul(self, right: $rotmatrix<N>) -> $t<N> { fn mul(self, right: $rotmatrix<N>) -> $t<N> {
$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] #[inline]
fn mul(self, right: $t<N>) -> $t<N> { fn mul(self, right: $t<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self * right.translation, self * right.translation,
self * right.rotation) self * right.rotation)
} }
@ -164,7 +164,7 @@ macro_rules! isometry_impl(
#[inline] #[inline]
fn append_translation(&self, t: &$vector<N>) -> $t<N> { fn append_translation(&self, t: &$vector<N>) -> $t<N> {
$t::new_with_rotation_matrix(*t + self.translation, self.rotation) $t::from_rotation_matrix(*t + self.translation, self.rotation)
} }
#[inline] #[inline]
@ -174,7 +174,7 @@ macro_rules! isometry_impl(
#[inline] #[inline]
fn prepend_translation(&self, t: &$vector<N>) -> $t<N> { fn prepend_translation(&self, t: &$vector<N>) -> $t<N> {
$t::new_with_rotation_matrix(self.translation + self.rotation * *t, self.rotation) $t::from_rotation_matrix(self.translation + self.rotation * *t, self.rotation)
} }
#[inline] #[inline]
@ -230,7 +230,7 @@ macro_rules! isometry_impl(
fn append_rotation(&self, rotation: &$rotvector<N>) -> $t<N> { fn append_rotation(&self, rotation: &$rotvector<N>) -> $t<N> {
let delta = $rotmatrix::new(*rotation); 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] #[inline]
@ -244,7 +244,7 @@ macro_rules! isometry_impl(
fn prepend_rotation(&self, rotation: &$rotvector<N>) -> $t<N> { fn prepend_rotation(&self, rotation: &$rotvector<N>) -> $t<N> {
let delta = $rotmatrix::new(*rotation); 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] #[inline]
@ -435,7 +435,7 @@ macro_rules! isometry_impl(
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]
impl<N: Arbitrary + BaseFloat> Arbitrary for $t<N> { impl<N: Arbitrary + BaseFloat> Arbitrary for $t<N> {
fn arbitrary<G: Gen>(g: &mut G) -> $t<N> { fn arbitrary<G: Gen>(g: &mut G) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
Arbitrary::arbitrary(g), Arbitrary::arbitrary(g),
Arbitrary::arbitrary(g) Arbitrary::arbitrary(g)
) )

View File

@ -183,7 +183,7 @@ impl<N: BaseFloat> OrthographicMatrix3<N> {
} }
/// Creates a new orthographic projection matrix from an aspect ratio and the vertical field of view. /// 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<N> { pub fn from_fov(aspect: N, vfov: N, znear: N, zfar: N) -> OrthographicMatrix3<N> {
assert!(znear < zfar, "The far plane must be farther than the near plane."); assert!(znear < zfar, "The far plane must be farther than the near plane.");
assert!(!::is_zero(&aspect)); assert!(!::is_zero(&aspect));
@ -196,7 +196,7 @@ impl<N: BaseFloat> OrthographicMatrix3<N> {
/// Creates a new orthographic matrix from a 4D matrix. /// Creates a new orthographic matrix from a 4D matrix.
#[inline] #[inline]
pub fn new_with_matrix_unchecked(matrix: Matrix4<N>) -> OrthographicMatrix3<N> { pub fn from_matrix_unchecked(matrix: Matrix4<N>) -> OrthographicMatrix3<N> {
OrthographicMatrix3 { OrthographicMatrix3 {
matrix: matrix matrix: matrix
} }

View File

@ -155,7 +155,7 @@ impl<N: BaseFloat> PerspectiveMatrix3<N> {
/// Creates a new perspective projection matrix from a 4D matrix. /// Creates a new perspective projection matrix from a 4D matrix.
#[inline] #[inline]
pub fn new_with_matrix_unchecked(matrix: Matrix4<N>) -> PerspectiveMatrix3<N> { pub fn from_matrix_unchecked(matrix: Matrix4<N>) -> PerspectiveMatrix3<N> {
PerspectiveMatrix3 { PerspectiveMatrix3 {
matrix: matrix matrix: matrix
} }

View File

@ -337,7 +337,7 @@ impl<N: BaseFloat> UnitQuaternion<N> {
let jk = self.as_ref().j * self.as_ref().k * ::cast(2.0); 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); let wi = self.as_ref().w * self.as_ref().i * ::cast(2.0);
Rotation3::new_with_matrix_unchecked( Rotation3::from_matrix_unchecked(
Matrix3::new( Matrix3::new(
ww + ii - jj - kk, ij - wk, wj + ik, ww + ii - jj - kk, ij - wk, wj + ik,
wk + ij, ww - ii + jj - kk, jk - wi, wk + ij, ww - ii + jj - kk, jk - wi,

View File

@ -157,7 +157,7 @@ impl<N: BaseFloat> Rotation3<N> {
} }
/// Builds a rotation matrix from an orthogonal matrix. /// Builds a rotation matrix from an orthogonal matrix.
pub fn new_with_matrix_unchecked(matrix: Matrix3<N>) -> Rotation3<N> { pub fn from_matrix_unchecked(matrix: Matrix3<N>) -> Rotation3<N> {
Rotation3 { Rotation3 {
submatrix: matrix submatrix: matrix
} }
@ -166,12 +166,12 @@ impl<N: BaseFloat> Rotation3<N> {
/// Creates a new rotation from Euler angles. /// Creates a new rotation from Euler angles.
/// ///
/// The primitive rotations are applied in order: 1 roll 2 pitch 3 yaw. /// 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<N> { pub fn from_euler_angles(roll: N, pitch: N, yaw: N) -> Rotation3<N> {
let (sr, cr) = roll.sin_cos(); let (sr, cr) = roll.sin_cos();
let (sp, cp) = pitch.sin_cos(); let (sp, cp) = pitch.sin_cos();
let (sy, cy) = yaw.sin_cos(); let (sy, cy) = yaw.sin_cos();
Rotation3::new_with_matrix_unchecked( Rotation3::from_matrix_unchecked(
Matrix3::new( Matrix3::new(
cy * cp, cy * sp * sr - sy * cr, cy * sp * cr + sy * sr, cy * cp, cy * sp * sr - sy * cr, cy * sp * cr + sy * sr,
sy * cp, sy * sp * sr + cy * cr, sy * sp * cr - cy * sr, sy * cp, sy * sp * sr + cy * cr, sy * sp * cr - cy * sr,
@ -198,7 +198,7 @@ impl<N: BaseFloat> Rotation3<N> {
let xaxis = Norm::normalize(&Cross::cross(up, &zaxis)); let xaxis = Norm::normalize(&Cross::cross(up, &zaxis));
let yaxis = Norm::normalize(&Cross::cross(&zaxis, &xaxis)); 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.x, yaxis.x, zaxis.x,
xaxis.y, yaxis.y, zaxis.y, xaxis.y, yaxis.y, zaxis.y,
xaxis.z, yaxis.z, zaxis.z)) xaxis.z, yaxis.z, zaxis.z))

View File

@ -29,12 +29,12 @@ macro_rules! similarity_impl(
/// ///
/// The scale factor may be negative but not zero. /// The scale factor may be negative but not zero.
#[inline] #[inline]
pub fn new_with_rotation_matrix(translation: $vector<N>, rotation: $rotation_matrix<N>, scale: N) -> $t<N> { pub fn from_rotation_matrix(translation: $vector<N>, rotation: $rotation_matrix<N>, scale: N) -> $t<N> {
assert!(!scale.is_zero(), "A similarity transformation scale factor cannot be zero."); assert!(!scale.is_zero(), "A similarity transformation scale factor cannot be zero.");
$t { $t {
scale: scale, 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. /// The scale factor may be negative but not zero.
#[inline] #[inline]
pub fn new_with_isometry(isometry: $isometry<N>, scale: N) -> $t<N> { pub fn from_isometry(isometry: $isometry<N>, scale: N) -> $t<N> {
assert!(!scale.is_zero(), "A similarity transformation scale factor cannot be zero."); assert!(!scale.is_zero(), "A similarity transformation scale factor cannot be zero.");
$t { $t {
@ -80,7 +80,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
pub fn append_scale(&self, s: &N) -> $t<N> { pub fn append_scale(&self, s: &N) -> $t<N> {
assert!(!s.is_zero(), "Cannot append a zero scale to a similarity transformation."); 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. /// Prepends in-place a scale to this similarity transformation.
@ -94,7 +94,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
pub fn prepend_scale(&self, s: &N) -> $t<N> { pub fn prepend_scale(&self, s: &N) -> $t<N> {
assert!(!s.is_zero(), "A similarity transformation scale must not be zero."); 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. /// Sets the scale of this similarity transformation.
@ -113,7 +113,7 @@ macro_rules! similarity_impl(
impl<N: BaseFloat> One for $t<N> { impl<N: BaseFloat> One for $t<N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t::new_with_isometry(::one(), ::one()) $t::from_isometry(::one(), ::one())
} }
} }
@ -164,7 +164,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
fn mul(self, right: $t<N>) -> $t<N> { fn mul(self, right: $t<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self.isometry.translation + self.isometry.rotation * (right.isometry.translation * self.scale), self.isometry.translation + self.isometry.rotation * (right.isometry.translation * self.scale),
self.isometry.rotation * right.isometry.rotation, self.isometry.rotation * right.isometry.rotation,
self.scale * right.scale) self.scale * right.scale)
@ -191,7 +191,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
fn mul(self, right: $isometry<N>) -> $t<N> { fn mul(self, right: $isometry<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self.isometry.translation + self.isometry.rotation * (right.translation * self.scale), self.isometry.translation + self.isometry.rotation * (right.translation * self.scale),
self.isometry.rotation * right.rotation, self.isometry.rotation * right.rotation,
self.scale) self.scale)
@ -211,7 +211,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
fn mul(self, right: $t<N>) -> $t<N> { fn mul(self, right: $t<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self.translation + self.rotation * right.isometry.translation, self.translation + self.rotation * right.isometry.translation,
self.rotation * right.isometry.rotation, self.rotation * right.isometry.rotation,
right.scale) right.scale)
@ -228,7 +228,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
fn mul(self, right: $rotation_matrix<N>) -> $t<N> { fn mul(self, right: $rotation_matrix<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self.isometry.translation, self.isometry.translation,
self.isometry.rotation * right, self.isometry.rotation * right,
self.scale) self.scale)
@ -247,7 +247,7 @@ macro_rules! similarity_impl(
#[inline] #[inline]
fn mul(self, right: $t<N>) -> $t<N> { fn mul(self, right: $t<N>) -> $t<N> {
$t::new_with_rotation_matrix( $t::from_rotation_matrix(
self * right.isometry.translation, self * right.isometry.translation,
self * right.isometry.rotation, self * right.isometry.rotation,
right.scale) right.scale)
@ -389,7 +389,7 @@ macro_rules! similarity_impl(
scale = rng.gen(); 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")] #[cfg(feature="arbitrary")]
impl<N: Arbitrary + BaseFloat> Arbitrary for $t<N> { impl<N: Arbitrary + BaseFloat> Arbitrary for $t<N> {
fn arbitrary<G: Gen>(g: &mut G) -> $t<N> { fn arbitrary<G: Gen>(g: &mut G) -> $t<N> {
$t::new_with_isometry( $t::from_isometry(
Arbitrary::arbitrary(g), Arbitrary::arbitrary(g),
Arbitrary::arbitrary(g) Arbitrary::arbitrary(g)
) )

View File

@ -173,7 +173,7 @@ pub trait ApproxEq<Eps>: Sized {
impl ApproxEq<f32> for f32 { impl ApproxEq<f32> for f32 {
#[inline] #[inline]
fn approx_epsilon(_: Option<f32>) -> f32 { fn approx_epsilon(_: Option<f32>) -> f32 {
f32::EPSILON * 10.0 1.0e-6
} }
#[inline] #[inline]
@ -204,7 +204,7 @@ impl ApproxEq<f32> for f32 {
impl ApproxEq<f64> for f64 { impl ApproxEq<f64> for f64 {
#[inline] #[inline]
fn approx_epsilon(_: Option<f64>) -> f64 { fn approx_epsilon(_: Option<f64>) -> f64 {
f64::EPSILON * 10.0 1.0e-6
} }
#[inline] #[inline]

View File

@ -9,7 +9,7 @@ fn test_quaternion_as_matrix() {
for _ in 0usize .. 10000 { for _ in 0usize .. 10000 {
let axis_angle: Vector3<f64> = random(); let axis_angle: Vector3<f64> = 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<f64> = random(); let point: Point3<f64> = random();
let matrix = Rotation3::new(axis_angle); 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 * vector), &(quaternion * vector)));
assert!(na::approx_eq(&(matrix * point), &(quaternion * point))); assert!(na::approx_eq(&(matrix * point), &(quaternion * point)));
@ -39,8 +39,8 @@ fn test_quaternion_div_quaternion() {
let r1 = Rotation3::new(axis_angle1); let r1 = Rotation3::new(axis_angle1);
let r2 = na::inverse(&Rotation3::new(axis_angle2)).unwrap(); let r2 = na::inverse(&Rotation3::new(axis_angle2)).unwrap();
let q1 = UnitQuaternion::new(axis_angle1); let q1 = UnitQuaternion::from_scaled_axis(axis_angle1);
let q2 = UnitQuaternion::new(axis_angle2); let q2 = UnitQuaternion::from_scaled_axis(axis_angle2);
assert!(na::approx_eq(&(q1 / q2).to_rotation_matrix(), &(r1 * r2))) 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 { for _ in 0usize .. 10000 {
let axis_angle: Vector3<f64> = random(); let axis_angle: Vector3<f64> = random();
let q = UnitQuaternion::new(axis_angle); let q = UnitQuaternion::from_scaled_axis(axis_angle);
println!("{:?} {:?}", q.rotation(), axis_angle); println!("{:?} {:?}", q.rotation(), axis_angle);
assert!(na::approx_eq(&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 { for _ in 0usize .. 10000 {
let angles: Vector3<f64> = random(); let angles: Vector3<f64> = random();
let q = UnitQuaternion::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::new_with_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)) assert!(na::approx_eq(&q.to_rotation_matrix(), &m))
} }