Remove free-functions alliasing structures constructors.

Those constructors are not idiomatic. Use e.g. `Vec3::new(0, 0, 0)` instead.
This commit is contained in:
Sébastien Crozet 2013-10-14 11:22:38 +02:00
parent ccbc8b4429
commit bb5654d220
7 changed files with 101 additions and 147 deletions

View File

@ -10,9 +10,8 @@ An on-line version of this documentation is available [here](http://crozet.re/na
## Using **nalgebra**
All the functionalities of **nalgebra** are grouped in one place: the `na` module.
This module re-exports everything and includes free functions for all traits methods.
Free functions are useful if you prefer doing something like `na::dot(v1, v2)` instead of
`v1.dot(v2)`.
This module re-exports everything and includes free functions for all traits methods doing
out-of-place modifications.
* You can import the whole prelude, including free functions, using:
@ -31,8 +30,23 @@ use nalgebra::traits::*;
```.rust
use nalgebra::structs::*;
```
Of course, you can still import `nalgebra::na` alone, and get anything you want using the prefix
`na`.
The preffered way to use **nalgebra** is to import types and traits explicitly, and call
free-functions using the `na::` prefix:
```.rust
extern mod nalgebra;
use nalgebra::na::{Vec3, Rot3, Rotation};
use nalgebra::na;
fn main() {
let a = Vec3::new(1.0f64, 1.0, 1.0);
let mut b = Rot3::new(na::zero());
b.append_rotation(&a);
assert!(na::rotation(&b).approx_eq(&a));
}
```
## Features
**nalgebra** is meant to be a general-purpose linear algebra library (but is very far from that…),

View File

@ -36,12 +36,12 @@ free-functions using the `na::` prefix:
```.rust
extern mod nalgebra;
use nalgebra::na::{Rot3, Rotation};
use nalgebra::na::{Vec3, Rot3, Rotation};
use nalgebra::na;
fn main() {
let a = na::vec3(1.0f64, 1.0, 1.0);
let mut b: Rot3<f64> = na::one();
let a = Vec3::new(1.0f64, 1.0, 1.0);
let mut b = Rot3::new(na::zero());
b.append_rotation(&a);

131
src/na.rs
View File

@ -77,85 +77,6 @@ pub fn one<T: One>() -> T {
One::one()
}
/// Creates a new 1d vector.
///
/// This is the same as `Vec1::new(x)`.
#[inline(always)]
pub fn vec1<N>(x: N) -> Vec1<N> {
Vec1::new(x)
}
/// Creates a new 2d vector.
///
/// This is the same as `Vec2::new(x, y)`.
#[inline(always)]
pub fn vec2<N>(x: N, y: N) -> Vec2<N> {
Vec2::new(x, y)
}
/// Creates a new 3d vector.
///
/// This is the same as `Vec3::new(x, y, z)`.
#[inline(always)]
pub fn vec3<N>(x: N, y: N, z: N) -> Vec3<N> {
Vec3::new(x, y, z)
}
/// Creates a new 4d vector.
///
/// This is the same as `Vec4::new(x, y, z, w)`.
#[inline(always)]
pub fn vec4<N>(x: N, y: N, z: N, w: N) -> Vec4<N> {
Vec4::new(x, y, z, w)
}
/// Creates a new 1d matrix.
///
/// This is the same as `Mat1::new(...)`.
#[inline(always)]
pub fn mat1<N>(m11: N) -> Mat1<N> {
Mat1::new(m11)
}
/// Creates a new 2d matrix.
///
/// This is the same as `Mat2::new(...)`.
#[inline(always)]
pub fn mat2<N>(m11: N, m12: N,
m21: N, m22: N) -> Mat2<N> {
Mat2::new(
m11, m12,
m21, m22)
}
/// Creates a new 3d matrix.
///
/// This is the same as `Mat3::new(...)`.
#[inline(always)]
pub fn mat3<N>(m11: N, m12: N, m13: N,
m21: N, m22: N, m23: N,
m31: N, m32: N, m33: N) -> Mat3<N> {
Mat3::new(
m11, m12, m13,
m21, m22, m23,
m31, m32, m33)
}
/// Creates a new 4d matrix.
///
/// This is the same as `Mat4::new(...)`.
#[inline(always)]
pub fn mat4<N>(m11: N, m12: N, m13: N, m14: N,
m21: N, m22: N, m23: N, m24: N,
m31: N, m32: N, m33: N, m34: N,
m41: N, m42: N, m43: N, m44: N) -> Mat4<N> {
Mat4::new(
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44)
}
//
//
// Geometry
@ -170,11 +91,11 @@ pub fn mat4<N>(m11: N, m12: N, m13: N, m14: N,
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::types::{Vec3, Affmat};
/// use nalgebra::types::{Vec3, Iso3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = Affmat::new_translation3d(1.0, 1.0, 1.0);
/// let t = Iso3::new(Vec3::new(1.0, 1.0, 1.0), na::zero());
/// let trans = na::translation(t);
///
/// assert!(trans == Vec3::new(1.0, 1.0, 1.0));
@ -189,11 +110,11 @@ pub fn translation<V, M: Translation<V>>(m: &M) -> V {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::types::{Vec3, Affmat};
/// use nalgebra::types::{Vec3, Iso3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = Affmat::new_translation3d(1.0, 1.0, 1.0);
/// let t = Iso3::new(Vec3::new(1.0, 1.0, 1.0), na::zero());
/// let itrans = na::inv_translation(t);
///
/// assert!(itrans == Vec3::new(-1.0, -1.0, -1.0));
@ -218,15 +139,16 @@ pub fn append_translation<V, M: Translation<V>>(m: &M, v: &V) -> M {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::na::{Vec3, Iso3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::translation3d(1.0, 1.0, 1.0);
/// let v = na::vec3(2.0, 2.0, 2.0);
/// let t = Iso3::new(Vec3::new(1.0, 1.0, 1.0), na::zero());
/// let v = Vec3::new(2.0, 2.0, 2.0);
///
/// let tv = na::translate(&t, &v);
///
/// assert!(tv == na::vec3(3.0, 3.0, 3.0))
/// assert!(tv == Vec3::new(3.0, 3.0, 3.0))
/// }
/// ```
#[inline(always)]
@ -238,15 +160,16 @@ pub fn translate<V, M: Translate<V>>(m: &M, v: &V) -> V {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::na::{Vec3, Iso3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::translation3d(1.0, 1.0, 1.0);
/// let v = na::vec3(2.0, 2.0, 2.0);
/// let t = Iso3::new(Vec3::new(1.0, 1.0, 1.0), na::zero());
/// let v = Vec3::new(2.0, 2.0, 2.0);
///
/// let tv = na::translate(&t, &v);
///
/// assert!(tv == na::vec3(1.0, 1.0, 1.0))
/// assert!(tv == Vec3::new(1.0, 1.0, 1.0))
/// }
#[inline(always)]
pub fn inv_translate<V, M: Translate<V>>(m: &M, v: &V) -> V {
@ -261,12 +184,13 @@ pub fn inv_translate<V, M: Translate<V>>(m: &M, v: &V) -> V {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::na::{Vec3, Rot3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::rot3(1.0, 1.0, 1.0);
/// let t = Rot3::new(Vec3::new(1.0, 1.0, 1.0));
///
/// assert!(na::rotation(t) == na::vec3(1.0, 1.0, 1.0));
/// assert!(na::rotation(t) == Vec3::new(1.0, 1.0, 1.0));
/// }
/// ```
#[inline(always)]
@ -279,12 +203,13 @@ pub fn rotation<V, M: Rotation<V>>(m: &M) -> V {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::na::{Vec3, Rot3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::rot3(1.0, 1.0, 1.0);
/// let t = Rot3::new(Vec3::new(1.0, 1.0, 1.0));
///
/// assert!(na::inv_rotation(t) == na::vec3(-1.0, -1.0, -1.0));
/// assert!(na::inv_rotation(t) == Vec3::new(-1.0, -1.0, -1.0));
/// }
/// ```
#[inline(always)]
@ -296,14 +221,15 @@ pub fn inv_rotation<V, M: Rotation<V>>(m: &M) -> V {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::na::{Vec3, Rot3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::rot3(0.0, 0.0, 0.0);
/// let v = na::vec3(1.0, 1.0, 1.0);
/// let t = Rot3::new(Vec3::new(0.0, 0.0, 0.0));
/// let v = Vec3::new(1.0, 1.0, 1.0);
/// let rt = na::append_rotation(&t, &v);
///
/// assert!(na::rotation(&rt) == na::vec3(1.0, 1.0, 1.0))
/// assert!(na::rotation(&rt) == Vec3::new(1.0, 1.0, 1.0))
/// }
/// ```
#[inline(always)]
@ -319,15 +245,16 @@ pub fn append_rotation<V, M: Rotation<V>>(m: &M, v: &V) -> M {
///
/// ```rust
/// extern mod nalgebra;
/// use nalgebra::na::{Rot3, Vec3};
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::rot3(1.0, 0.0, 0.0);
/// let v = na::vec3(0.0, 0.0, na::pi() / 2.0);
/// let t = Rot3::new(Vec3::new(1.0, 0.0, 0.0));
/// let v = Vec3::new(0.0, 0.0, na::pi() / 2.0);
///
/// let tv = na::rotate(&t, &v);
///
/// assert!(tv == na::vec3(0.0, 1.0, 0.0))
/// assert!(tv == Vec3::new(0.0, 1.0, 0.0))
/// }
/// ```
#[inline(always)]
@ -343,12 +270,12 @@ pub fn rotate<V, M: Rotate<V>>(m: &M, v: &V) -> V {
/// use nalgebra::na;
///
/// pub main() {
/// let t = na::rot3(na::vec3(1.0, 0.0, 0.0));
/// let v = na::vec3(0.0, 0.0, na::pi() / 2.0);
/// let t = Rot3::new(Vec3::new(1.0, 0.0, 0.0));
/// let v = Vec3::new(0.0, 0.0, na::pi() / 2.0);
///
/// let tv = na::rotate(&t, &v);
///
/// assert!(tv == na::vec3(0.0, -1.0, 0.0))
/// assert!(tv == Vec3::new(0.0, -1.0, 0.0))
/// }
/// ```
#[inline(always)]

View File

@ -4,13 +4,13 @@
use std::num::{Zero, One};
use std::rand::{Rand, Rng};
use structs::mat::{Mat3, Mat4, Mat5};
use structs::mat::{Mat3, Mat4};
use traits::structure::{Cast, Dim, Col};
use traits::operations::{Inv};
use traits::geometry::{RotationMatrix, Rotation, Rotate, AbsoluteRotate, Transform, Transformation,
Translate, Translation, ToHomogeneous};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec2MulRhs, Vec3MulRhs, Vec4MulRhs};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec2MulRhs, Vec3MulRhs};
use structs::rot::{Rot2, Rot3, Rot4};
mod metal;
@ -82,7 +82,7 @@ impl<N: Clone + Num + Algebraic> Iso3<N> {
}
}
iso_impl!(Iso2, Rot2, Vec2)
iso_impl!(Iso2, Rot2, Vec2, Vec1)
double_dispatch_binop_decl_trait!(Iso2, Iso2MulRhs)
mul_redispatch_impl!(Iso2, Iso2MulRhs)
rotation_matrix_impl!(Iso2, Rot2, Vec2, Vec1)
@ -103,7 +103,7 @@ iso_mul_iso_impl!(Iso2, Iso2MulRhs)
iso_mul_vec_impl!(Iso2, Vec2, Iso2MulRhs)
vec_mul_iso_impl!(Iso2, Vec2, Vec2MulRhs)
iso_impl!(Iso3, Rot3, Vec3)
iso_impl!(Iso3, Rot3, Vec3, Vec3)
double_dispatch_binop_decl_trait!(Iso3, Iso3MulRhs)
mul_redispatch_impl!(Iso3, Iso3MulRhs)
rotation_matrix_impl!(Iso3, Rot3, Vec3, Vec3)
@ -124,7 +124,8 @@ iso_mul_iso_impl!(Iso3, Iso3MulRhs)
iso_mul_vec_impl!(Iso3, Vec3, Iso3MulRhs)
vec_mul_iso_impl!(Iso3, Vec3, Vec3MulRhs)
iso_impl!(Iso4, Rot4, Vec4)
/*
iso_impl!(Iso4, Rot4, Vec4, Vec4)
double_dispatch_binop_decl_trait!(Iso4, Iso4MulRhs)
mul_redispatch_impl!(Iso4, Iso4MulRhs)
// rotation_matrix_impl!(Iso4, Rot4, Vec4, Vec4)
@ -144,3 +145,4 @@ translate_impl!(Iso4, Vec4)
iso_mul_iso_impl!(Iso4, Iso4MulRhs)
iso_mul_vec_impl!(Iso4, Vec4, Iso4MulRhs)
vec_mul_iso_impl!(Iso4, Vec4, Vec4MulRhs)
*/

View File

@ -1,11 +1,20 @@
#[macro_escape];
macro_rules! iso_impl(
($t: ident, $submat: ident, $subvec: ident) => (
impl<N> $t<N> {
($t: ident, $submat: ident, $subvec: ident, $subrotvec: ident) => (
impl<N: Clone + Trigonometric + Algebraic + Num> $t<N> {
/// Creates a new isometry from a rotation matrix and a vector.
#[inline]
pub fn new(translation: $subvec<N>, rotation: $submat<N>) -> $t<N> {
pub fn new(translation: $subvec<N>, rotation: $subrotvec<N>) -> $t<N> {
$t {
rotation: $submat::new(rotation),
translation: translation
}
}
/// Creates a new isometry from a rotation matrix and a vector.
#[inline]
pub fn new_with_rotmat(translation: $subvec<N>, rotation: $submat<N>) -> $t<N> {
$t {
rotation: rotation,
translation: translation
@ -41,10 +50,10 @@ macro_rules! dim_impl(
macro_rules! one_impl(
($t: ident) => (
impl<N: One + Zero + Clone> One for $t<N> {
impl<N: Trigonometric + Algebraic + Num + Clone> One for $t<N> {
#[inline]
fn one() -> $t<N> {
$t::new(Zero::zero(), One::one())
$t::new_with_rotmat(Zero::zero(), One::one())
}
}
)
@ -52,10 +61,12 @@ macro_rules! one_impl(
macro_rules! iso_mul_iso_impl(
($t: ident, $tmul: ident) => (
impl<N: Num + Clone> $tmul<N, $t<N>> for $t<N> {
impl<N: Num + Trigonometric + Algebraic + Clone> $tmul<N, $t<N>> for $t<N> {
#[inline]
fn binop(left: &$t<N>, right: &$t<N>) -> $t<N> {
$t::new(left.translation + left.rotation * right.translation, left.rotation * right.rotation)
$t::new_with_rotmat(
left.translation + left.rotation * right.translation,
left.rotation * right.rotation)
}
}
)
@ -85,7 +96,7 @@ macro_rules! vec_mul_iso_impl(
macro_rules! translation_impl(
($t: ident, $tv: ident) => (
impl<N: Neg<N> + Add<N, N> + Num + Clone> Translation<$tv<N>> for $t<N> {
impl<N: Trigonometric + Num + Algebraic + Clone> Translation<$tv<N>> for $t<N> {
#[inline]
fn translation(&self) -> $tv<N> {
self.translation.clone()
@ -103,7 +114,7 @@ macro_rules! translation_impl(
#[inline]
fn append_translation_cpy(iso: &$t<N>, t: &$tv<N>) -> $t<N> {
$t::new(*t + iso.translation, iso.rotation.clone())
$t::new_with_rotmat(*t + iso.translation, iso.rotation.clone())
}
#[inline]
@ -113,7 +124,7 @@ macro_rules! translation_impl(
#[inline]
fn prepend_translation_cpy(iso: &$t<N>, t: &$tv<N>) -> $t<N> {
$t::new(iso.translation + iso.rotation * *t, iso.rotation.clone())
$t::new_with_rotmat(iso.translation + iso.rotation * *t, iso.rotation.clone())
}
#[inline]
@ -165,7 +176,7 @@ macro_rules! rotation_impl(
fn append_rotation_cpy(t: &$t<N>, rot: &$tav<N>) -> $t<N> {
let delta = $trot::new(rot.clone());
$t::new(delta * t.translation, delta * t.rotation)
$t::new_with_rotmat(delta * t.translation, delta * t.rotation)
}
#[inline]
@ -179,7 +190,7 @@ macro_rules! rotation_impl(
fn prepend_rotation_cpy(t: &$t<N>, rot: &$tav<N>) -> $t<N> {
let delta = $trot::new(rot.clone());
$t::new(t.translation.clone(), t.rotation * delta)
$t::new_with_rotmat(t.translation.clone(), t.rotation * delta)
}
#[inline]
@ -209,7 +220,7 @@ macro_rules! rotate_impl(
macro_rules! transformation_impl(
($t: ident) => (
impl<N: Num + Clone> Transformation<$t<N>> for $t<N> {
impl<N: Num + Trigonometric + Algebraic + Clone> Transformation<$t<N>> for $t<N> {
fn transformation(&self) -> $t<N> {
self.clone()
}

View File

@ -1,7 +1,7 @@
use std::num::{Real, abs};
use std::rand::random;
use std::cmp::ApproxEq;
use na::{DMat, DVec};
use na::{Vec1, DMat, DVec};
use na::Indexable; // FIXME: get rid of that
use na;
@ -89,7 +89,7 @@ fn test_inv_mat6() {
fn test_rotation2() {
do 10000.times {
let randmat: na::Rot2<f64> = na::one();
let ang = na::vec1(abs::<f64>(random()) % Real::pi());
let ang = Vec1::new(abs::<f64>(random()) % Real::pi());
assert!(na::rotation(&na::append_rotation(&randmat, &ang)).approx_eq(&ang));
}

View File

@ -1,7 +1,7 @@
use std::rand::{random};
use std::cmp::ApproxEq;
use na::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
use na::{Iterable, IterableMut}; // FIXME: get rid of that
use na::{Mat3, Iterable, IterableMut}; // FIXME: get rid of that
use na;
macro_rules! test_iterator_impl(
@ -288,35 +288,35 @@ fn test_iterator_vec6() {
#[test]
fn test_ord_vec3() {
// equality
assert!(na::vec3(0.5, 0.5, 0.5) == na::vec3(0.5, 0.5, 0.5));
assert!(!(na::vec3(1.5, 0.5, 0.5) == na::vec3(0.5, 0.5, 0.5)));
assert!(na::vec3(1.5, 0.5, 0.5) != na::vec3(0.5, 0.5, 0.5));
assert!(Vec3::new(0.5, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5));
assert!(!(Vec3::new(1.5, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5)));
assert!(Vec3::new(1.5, 0.5, 0.5) != Vec3::new(0.5, 0.5, 0.5));
// comparable
assert!(na::vec3(0.5, 0.3, 0.3) < na::vec3(1.0, 2.0, 1.0));
assert!(na::vec3(0.5, 0.3, 0.3) <= na::vec3(1.0, 2.0, 1.0));
assert!(na::vec3(2.0, 4.0, 2.0) > na::vec3(1.0, 2.0, 1.0));
assert!(na::vec3(2.0, 4.0, 2.0) >= na::vec3(1.0, 2.0, 1.0));
assert!(Vec3::new(0.5, 0.3, 0.3) < Vec3::new(1.0, 2.0, 1.0));
assert!(Vec3::new(0.5, 0.3, 0.3) <= Vec3::new(1.0, 2.0, 1.0));
assert!(Vec3::new(2.0, 4.0, 2.0) > Vec3::new(1.0, 2.0, 1.0));
assert!(Vec3::new(2.0, 4.0, 2.0) >= Vec3::new(1.0, 2.0, 1.0));
// not comparable
assert!(!(na::vec3(0.0, 3.0, 0.0) < na::vec3(1.0, 2.0, 1.0)));
assert!(!(na::vec3(0.0, 3.0, 0.0) > na::vec3(1.0, 2.0, 1.0)));
assert!(!(na::vec3(0.0, 3.0, 0.0) <= na::vec3(1.0, 2.0, 1.0)));
assert!(!(na::vec3(0.0, 3.0, 0.0) >= na::vec3(1.0, 2.0, 1.0)));
assert!(!(Vec3::new(0.0, 3.0, 0.0) < Vec3::new(1.0, 2.0, 1.0)));
assert!(!(Vec3::new(0.0, 3.0, 0.0) > Vec3::new(1.0, 2.0, 1.0)));
assert!(!(Vec3::new(0.0, 3.0, 0.0) <= Vec3::new(1.0, 2.0, 1.0)));
assert!(!(Vec3::new(0.0, 3.0, 0.0) >= Vec3::new(1.0, 2.0, 1.0)));
}
#[test]
fn test_min_max_vec3() {
assert_eq!(na::vec3(1, 2, 3).max(&na::vec3(3, 2, 1)), na::vec3(3, 2, 3));
assert_eq!(na::vec3(1, 2, 3).min(&na::vec3(3, 2, 1)), na::vec3(1, 2, 1));
assert_eq!(na::vec3(0, 2, 4).clamp(&na::vec3(1, 1, 1), &na::vec3(3, 3, 3)), na::vec3(1, 2, 3));
assert_eq!(Vec3::new(1, 2, 3).max(&Vec3::new(3, 2, 1)), Vec3::new(3, 2, 3));
assert_eq!(Vec3::new(1, 2, 3).min(&Vec3::new(3, 2, 1)), Vec3::new(1, 2, 1));
assert_eq!(Vec3::new(0, 2, 4).clamp(&Vec3::new(1, 1, 1), &Vec3::new(3, 3, 3)), Vec3::new(1, 2, 3));
}
#[test]
fn test_outer_vec3() {
assert_eq!(
na::outer(&na::vec3(1, 2, 3), &na::vec3(4, 5, 6)),
na::mat3(
na::outer(&Vec3::new(1, 2, 3), &Vec3::new(4, 5, 6)),
Mat3::new(
4, 5, 6,
8, 10, 12,
12, 15, 18));