Move files around and complete the doc.

This commit is contained in:
sebcrozet 2018-09-22 13:18:59 +02:00 committed by Sébastien Crozet
parent 98cf1a8d17
commit 3e445430a4
68 changed files with 1408 additions and 577 deletions

View File

@ -7,3 +7,4 @@
* Function overload: the vec version is suffixed by _vec
* Function overload: the methods taking an epsilon as suffixed by _eps
* L1Norm and L2Norm between two vectors have been renamed: l1_distance, l2_distance
* Matrix columnwise comparisons suffixed by _columns, e.g., `equal` -> `equal_columns`

View File

@ -4,92 +4,173 @@ use na::{MatrixMN, VectorN, Vector1, Vector2, Vector3, Vector4,
Matrix2x4, Matrix3x4, Matrix4x3,
Quaternion};
/// A matrix with components of type `N`. It has `R` rows, and `C` columns.
pub type Mat<N, R, C> = MatrixMN<N, R, C>;
/// A column vector with components of type `N`. It has `D` rows (and one column).
pub type Vec<N, R> = VectorN<N, R>;
/// A quaternion with components of type `N`.
pub type Qua<N> = Quaternion<N>;
/// A 1D vector with boolean components.
pub type BVec1 = Vector1<bool>;
/// A 2D vector with boolean components.
pub type BVec2 = Vector2<bool>;
/// A 3D vector with boolean components.
pub type BVec3 = Vector3<bool>;
/// A 4D vector with boolean components.
pub type BVec4 = Vector4<bool>;
/// A 1D vector with `f64` components.
pub type DVec1 = Vector1<f64>;
/// A 2D vector with `f64` components.
pub type DVec2 = Vector2<f64>;
/// A 3D vector with `f64` components.
pub type DVec3 = Vector3<f64>;
/// A 4D vector with `f64` components.
pub type DVec4 = Vector4<f64>;
/// A 1D vector with `i32` components.
pub type IVec1 = Vector1<i32>;
/// A 2D vector with `i32` components.
pub type IVec2 = Vector2<i32>;
/// A 3D vector with `i32` components.
pub type IVec3 = Vector3<i32>;
/// A 4D vector with `i32` components.
pub type IVec4 = Vector4<i32>;
/// A 1D vector with `u32` components.
pub type UVec1 = Vector1<u32>;
/// A 2D vector with `u32` components.
pub type UVec2 = Vector2<u32>;
/// A 3D vector with `u32` components.
pub type UVec3 = Vector3<u32>;
/// A 4D vector with `u32` components.
pub type UVec4 = Vector4<u32>;
/// A 1D vector with `f32` components.
pub type Vec1 = Vector1<f32>;
/// A 2D vector with `f32` components.
pub type Vec2 = Vector2<f32>;
/// A 3D vector with `f32` components.
pub type Vec3 = Vector3<f32>;
/// A 4D vector with `f32` components.
pub type Vec4 = Vector4<f32>;
/// A 1D vector with `u64` components.
pub type U64Vec1 = Vector1<u64>;
/// A 2D vector with `u64` components.
pub type U64Vec2 = Vector2<u64>;
/// A 3D vector with `u64` components.
pub type U64Vec3 = Vector3<u64>;
/// A 4D vector with `u64` components.
pub type U64Vec4 = Vector4<u64>;
/// A 1D vector with `i64` components.
pub type I64Vec1 = Vector1<i64>;
/// A 2D vector with `i64` components.
pub type I64Vec2 = Vector2<i64>;
/// A 3D vector with `i64` components.
pub type I64Vec3 = Vector3<i64>;
/// A 4D vector with `i64` components.
pub type I64Vec4 = Vector4<i64>;
/// A 1D vector with `u32` components.
pub type U32Vec1 = Vector1<u32>;
/// A 2D vector with `u32` components.
pub type U32Vec2 = Vector2<u32>;
/// A 3D vector with `u32` components.
pub type U32Vec3 = Vector3<u32>;
/// A 4D vector with `u32` components.
pub type U32Vec4 = Vector4<u32>;
/// A 1D vector with `i32` components.
pub type I32Vec1 = Vector1<i32>;
/// A 2D vector with `i32` components.
pub type I32Vec2 = Vector2<i32>;
/// A 3D vector with `i32` components.
pub type I32Vec3 = Vector3<i32>;
/// A 4D vector with `i32` components.
pub type I32Vec4 = Vector4<i32>;
/// A 1D vector with `u16` components.
pub type U16Vec1 = Vector1<u16>;
/// A 2D vector with `u16` components.
pub type U16Vec2 = Vector2<u16>;
/// A 3D vector with `u16` components.
pub type U16Vec3 = Vector3<u16>;
/// A 4D vector with `u16` components.
pub type U16Vec4 = Vector4<u16>;
/// A 1D vector with `i16` components.
pub type I16Vec1 = Vector1<i16>;
/// A 2D vector with `i16` components.
pub type I16Vec2 = Vector2<i16>;
/// A 3D vector with `i16` components.
pub type I16Vec3 = Vector3<i16>;
/// A 4D vector with `i16` components.
pub type I16Vec4 = Vector4<i16>;
/// A 1D vector with `u8` components.
pub type U8Vec1 = Vector1<u8>;
/// A 2D vector with `u8` components.
pub type U8Vec2 = Vector2<u8>;
/// A 3D vector with `u8` components.
pub type U8Vec3 = Vector3<u8>;
/// A 4D vector with `u8` components.
pub type U8Vec4 = Vector4<u8>;
/// A 1D vector with `i8` components.
pub type I8Vec1 = Vector1<i8>;
/// A 2D vector with `i8` components.
pub type I8Vec2 = Vector2<i8>;
/// A 3D vector with `i8` components.
pub type I8Vec3 = Vector3<i8>;
/// A 4D vector with `i8` components.
pub type I8Vec4 = Vector4<i8>;
/// A 2x2 matrix with `f64` components.
pub type DMat2 = Matrix2<f64>;
/// A 2x2 matrix with `f64` components.
pub type DMat2x2 = Matrix2<f64>;
/// A 2x3 matrix with `f64` components.
pub type DMat2x3 = Matrix2x3<f64>;
/// A 2x4 matrix with `f64` components.
pub type DMat2x4 = Matrix2x4<f64>;
/// A 3x3 matrix with `f64` components.
pub type DMat3 = Matrix3<f64>;
/// A 3x2 matrix with `f64` components.
pub type DMat3x2 = Matrix3x2<f64>;
/// A 3x3 matrix with `f64` components.
pub type DMat3x3 = Matrix3<f64>;
/// A 3x4 matrix with `f64` components.
pub type DMat3x4 = Matrix3x4<f64>;
/// A 4x4 matrix with `f64` components.
pub type DMat4 = Matrix4<f64>;
/// A 4x2 matrix with `f64` components.
pub type DMat4x2 = Matrix4x2<f64>;
/// A 4x3 matrix with `f64` components.
pub type DMat4x3 = Matrix4x3<f64>;
/// A 4x4 matrix with `f64` components.
pub type DMat4x4 = Matrix4<f64>;
/// A 2x2 matrix with `f32` components.
pub type Mat2 = Matrix2<f32>;
/// A 2x2 matrix with `f32` components.
pub type Mat2x2 = Matrix2<f32>;
/// A 2x2 matrix with `f32` components.
pub type Mat2x3 = Matrix2x3<f32>;
/// A 2x4 matrix with `f32` components.
pub type Mat2x4 = Matrix2x4<f32>;
/// A 3x3 matrix with `f32` components.
pub type Mat3 = Matrix3<f32>;
/// A 3x2 matrix with `f32` components.
pub type Mat3x2 = Matrix3x2<f32>;
/// A 3x3 matrix with `f32` components.
pub type Mat3x3 = Matrix3<f32>;
/// A 3x4 matrix with `f32` components.
pub type Mat3x4 = Matrix3x4<f32>;
/// A 4x2 matrix with `f32` components.
pub type Mat4x2 = Matrix4x2<f32>;
/// A 4x3 matrix with `f32` components.
pub type Mat4x3 = Matrix4x3<f32>;
/// A 4x4 matrix with `f32` components.
pub type Mat4x4 = Matrix4<f32>;
/// A 4x3 matrix with `f32` components.
pub type Mat4 = Matrix4<f32>;
/// A quaternion with f32 components.
pub type Quat = Quaternion<f32>;
/// A quaternion with f64 components.
pub type DQuat = Quaternion<f64>;

View File

@ -1,68 +1,88 @@
use std::mem;
use num::FromPrimitive;
use na::{self, Scalar, Real, DefaultAllocator};
use na::{self, Real, DefaultAllocator};
use aliases::{Vec, Mat};
use traits::{Number, Dimension, Alloc};
/// For each matrix or vector component `x` if `x >= 0`; otherwise, it returns `-x`.
pub fn abs<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>) -> Mat<N, R, C>
where DefaultAllocator: Alloc<N, R, C> {
x.abs()
}
/// For each matrix or vector component returns a value equal to the nearest integer that is greater than or equal to `x`.
pub fn ceil<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| x.ceil())
}
pub fn clamp<N: Number>(x: N, minVal: N, maxVal: N) -> N {
na::clamp(x, minVal, maxVal)
/// Returns `min(max(x, min_val), max_val)`.
pub fn clamp<N: Number>(x: N, min_val: N, max_val: N) -> N {
na::clamp(x, min_val, max_val)
}
pub fn clamp2<N: Number, D: Dimension>(x: &Vec<N, D>,minVal: N, maxVal: N) -> Vec<N, D>
/// Returns `min(max(x, min_val), max_val)` for each component in `x` using the floating-point values `min_val and `max_val`.
pub fn clamp2<N: Number, D: Dimension>(x: &Vec<N, D>, min_val: N, max_val: N) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| na::clamp(x, minVal, maxVal))
x.map(|x| na::clamp(x, min_val, max_val))
}
pub fn clamp3<N: Number, D: Dimension>(x: &Vec<N, D>, minVal: &Vec<N, D>, maxVal: &Vec<N, D>) -> Vec<N, D>
/// Returns `min(max(x, min_val), max_val)` for each component in `x` using the components of `min_val` and `max_val` as bounds.
pub fn clamp3<N: Number, D: Dimension>(x: &Vec<N, D>, min_val: &Vec<N, D>, max_val: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
na::clamp(x.clone(), minVal.clone(), maxVal.clone())
na::clamp(x.clone(), min_val.clone(), max_val.clone())
}
/// Returns a signed integer value representing the encoding of a floating-point value.
///
/// The floating-point value's bit-level representation is preserved.
pub fn float_bits_to_int(v: f32) -> i32 {
unsafe { mem::transmute(v) }
}
pub fn float_bits_to_int_vec<D: Dimension>(v: Vec<f32, D>) -> Vec<i32, D>
/// Returns a signed integer value representing the encoding of each component of `v`.
///
/// The floating point value's bit-level representation is preserved.
pub fn float_bits_to_int_vec<D: Dimension>(v: &Vec<f32, D>) -> Vec<i32, D>
where DefaultAllocator: Alloc<f32, D> {
v.map(|v| float_bits_to_int(v))
}
/// Returns an unsigned integer value representing the encoding of a floating-point value.
///
/// The floating-point value's bit-level representation is preserved.
pub fn float_bits_to_uint(v: f32) -> u32 {
unsafe { mem::transmute(v) }
}
/// Returns an unsigned integer value representing the encoding of each component of `v`.
///
/// The floating point value's bit-level representation is preserved.
pub fn float_bits_to_uint_vec<D: Dimension>(v: &Vec<f32, D>) -> Vec<u32, D>
where DefaultAllocator: Alloc<f32, D> {
v.map(|v| float_bits_to_uint(v))
}
/// Returns componentwise a value equal to the nearest integer that is less then or equal to `x`.
pub fn floor<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| x.floor())
}
// FIXME: should be implemented for Vec/Mat?
pub fn fma<N: Number>(a: N, b: N, c: N) -> N {
// FIXME: use an actual FMA
a * b + c
}
//// FIXME: should be implemented for Vec/Mat?
//pub fn fma<N: Number>(a: N, b: N, c: N) -> N {
// // FIXME: use an actual FMA
// a * b + c
//}
/// Returns the fractional part of `x`.
pub fn fract<N: Real>(x: N) -> N {
x.fract()
}
/// Returns the fractional part of each component of `x`.
pub fn fract2<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| x.fract())
@ -76,11 +96,17 @@ pub fn fract2<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
// (x * (-e).exp2(), e)
//}
/// Returns a floating-point value corresponding to a signed integer encoding of a floating-point value.
///
/// If an inf or NaN is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
pub fn int_bits_to_float(v: i32) -> f32 {
unsafe { mem::transmute(v) }
}
/// For each components of `v`, returns a floating-point value corresponding to a signed integer encoding of a floating-point value.
///
/// If an inf or NaN is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
pub fn int_bits_to_float_vec<D: Dimension>(v: &Vec<i32, D>) -> Vec<f32, D>
where DefaultAllocator: Alloc<f32, D> {
v.map(|v| int_bits_to_float(v))
@ -104,47 +130,63 @@ pub fn int_bits_to_float_vec<D: Dimension>(v: &Vec<i32, D>) -> Vec<f32, D>
// x * (exp).exp2()
//}
/// The maximum between two numbers.
pub fn max<N: Number>(x: N, y: N) -> N {
na::sup(&x, &y)
}
/// The maximum between each component of `x` and `y`.
pub fn max2<N: Number, D: Dimension>(x: &Vec<N, D>, y: N) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| na::sup(&x, &y))
}
/// Componentwise maximum between `x` and `y`.
pub fn max3<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
na::sup(x, y)
}
/// The minimum between two numbers.
pub fn min<N: Number>(x: N, y: N) -> N {
na::inf(&x, &y)
}
/// The minimum between each component of `x` and `y`.
pub fn min2<N: Number, D: Dimension>(x: &Vec<N, D>,y: N) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| na::inf(&x, &y))
}
/// Componentwise minimum between `x` and `y`.
pub fn min3<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
na::inf(x, y)
}
/// Returns `x * (1.0 - a) + y * a`, i.e., the linear blend of x and y using the floating-point value a.
///
/// The value for a is not restricted to the range `[0, 1]`.
pub fn mix<N: Number>(x: N, y: N, a: N) -> N {
x * (N::one() - a) + y * a
}
/// Componentwise modulus.
///
/// Returns `x - y * floor(x / y)` for each component in `x` using the corresponding component of `y`.
pub fn mod_<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x % y)
}
/// Modulus between two values.
pub fn modf<N: Number>(x: N, i: N) -> N {
x % i
}
/// Componentwise rounding.
///
/// Values equal to `0.5` are rounded away from `0.0`.
pub fn round<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| x.round())
@ -156,11 +198,16 @@ pub fn round<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
// unimplemented!()
//}
/// Returns 1 if `x > 0`, 0 if `x == 0`, or -1 if `x < 0`.
pub fn sign<N: Number, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| x.signum())
}
/// Returns 0.0 if `x <= edge0` and `1.0 if x >= edge1` and performs smooth Hermite interpolation between 0 and 1 when `edge0 < x < edge1`.
///
/// This is useful in cases where you would want a threshold function with a smooth transition.
/// This is equivalent to: `let result = clamp((x - edge0) / (edge1 - edge0), 0, 1); return t * t * (3 - 2 * t);` Results are undefined if `edge0 >= edge1`.
pub fn smoothstep<N: Number>(edge0: N, edge1: N, x: N) -> N {
let _3: N = FromPrimitive::from_f64(3.0).unwrap();
let _2: N = FromPrimitive::from_f64(2.0).unwrap();
@ -168,6 +215,7 @@ pub fn smoothstep<N: Number>(edge0: N, edge1: N, x: N) -> N {
t * t * (_3 - t * _2)
}
/// Returns 0.0 if `x < edge`, otherwise it returns 1.0.
pub fn step<N: Number>(edge: N, x: N) -> N {
if edge > x {
N::zero()
@ -175,27 +223,35 @@ pub fn step<N: Number>(edge: N, x: N) -> N {
N::one()
}
}
/// Returns 0.0 if `x[i] < edge`, otherwise it returns 1.0.
pub fn step2<N: Number, D: Dimension>(edge: N, x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| step(edge, x))
}
/// Returns 0.0 if `x[i] < edge[i]`, otherwise it returns 1.0.
pub fn step3<N: Number, D: Dimension>(edge: &Vec<N, D>, x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
edge.zip_map(x, |edge, x| step(edge, x))
}
/// Returns a value equal to the nearest integer to `x` whose absolute value is not larger than the absolute value of `x`.
pub fn trunc<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| x.trunc())
}
/// Returns a floating-point value corresponding to a unsigned integer encoding of a floating-point value.
///
/// If an `inf` or `NaN` is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
pub fn uint_bits_to_float(v: u32) -> f32 {
unsafe { mem::transmute(v) }
}
/// For each component of `v`, returns a floating-point value corresponding to a unsigned integer encoding of a floating-point value.
///
/// If an inf or NaN is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
pub fn uint_bits_to_float_vec<D: Dimension>(v: &Vec<u32, D>) -> Vec<f32, D>
where DefaultAllocator: Alloc<f32, D> {
v.map(|v| uint_bits_to_float(v))

View File

@ -1,23 +1,29 @@
use na::{Scalar, Real, U1, U2, U3, U4};
use aliases::{Vec, Mat, Qua};
/// Creates a new 1D vector.
pub fn vec1<N: Scalar>(x: N) -> Vec<N, U1> {
Vec::<N, U1>::new(x)
}
/// Creates a new 2D vector.
pub fn vec2<N: Scalar>(x: N, y: N) -> Vec<N, U2> {
Vec::<N, U2>::new(x, y)
}
/// Creates a new 3D vector.
pub fn vec3<N: Scalar>(x: N, y: N, z: N) -> Vec<N, U3> {
Vec::<N, U3>::new(x, y, z)
}
/// Creates a new 4D vector.
pub fn vec4<N: Scalar>(x: N, y: N, z: N, w: N) -> Vec<N, U4> {
Vec::<N, U4>::new(x, y, z, w)
}
/// Create a new 2x2 matrix.
pub fn mat2<N: Scalar>(m11: N, m12: N, m21: N, m22: N) -> Mat<N, U2, U2> {
Mat::<N, U2, U2>::new(
m11, m12,
@ -25,6 +31,7 @@ pub fn mat2<N: Scalar>(m11: N, m12: N, m21: N, m22: N) -> Mat<N, U2, U2> {
)
}
/// Create a new 2x2 matrix.
pub fn mat2x2<N: Scalar>(m11: N, m12: N, m21: N, m22: N) -> Mat<N, U2, U2> {
Mat::<N, U2, U2>::new(
m11, m12,
@ -32,6 +39,7 @@ pub fn mat2x2<N: Scalar>(m11: N, m12: N, m21: N, m22: N) -> Mat<N, U2, U2> {
)
}
/// Create a new 2x3 matrix.
pub fn mat2x3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N) -> Mat<N, U2, U3> {
Mat::<N, U2, U3>::new(
m11, m12, m13,
@ -39,6 +47,7 @@ pub fn mat2x3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N) -> Mat<
)
}
/// Create a new 2x4 matrix.
pub fn mat2x4<N: Scalar>(m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N, m24: N) -> Mat<N, U2, U4> {
Mat::<N, U2, U4>::new(
m11, m12, m13, m14,
@ -46,6 +55,7 @@ pub fn mat2x4<N: Scalar>(m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N,
)
}
/// Create a new 3x3 matrix.
pub fn mat3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N, m32: N, m33: N) -> Mat<N, U3, U3> {
Mat::<N, U3, U3>::new(
m11, m12, m13,
@ -54,6 +64,7 @@ pub fn mat3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N, m
)
}
/// Create a new 3x2 matrix.
pub fn mat3x2<N: Scalar>(m11: N, m12: N, m21: N, m22: N, m31: N, m32: N) -> Mat<N, U3, U2> {
Mat::<N, U3, U2>::new(
m11, m12,
@ -62,6 +73,7 @@ pub fn mat3x2<N: Scalar>(m11: N, m12: N, m21: N, m22: N, m31: N, m32: N) -> Mat<
)
}
/// Create a new 3x3 matrix.
pub fn mat3x3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N, m32: N, m33: N) -> Mat<N, U3, U3> {
Mat::<N, U3, U3>::new(
m11, m12, m13,
@ -70,6 +82,7 @@ pub fn mat3x3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N,
)
}
/// Create a new 3x4 matrix.
pub fn mat3x4<N: Scalar>(m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N, m24: N, m31: N, m32: N, m33: N, m34: N) -> Mat<N, U3, U4> {
Mat::<N, U3, U4>::new(
m11, m12, m13, m14,
@ -78,6 +91,7 @@ pub fn mat3x4<N: Scalar>(m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N,
)
}
/// Create a new 4x2 matrix.
pub fn mat4x2<N: Scalar>(m11: N, m12: N, m21: N, m22: N, m31: N, m32: N, m41: N, m42: N) -> Mat<N, U4, U2> {
Mat::<N, U4, U2>::new(
m11, m12,
@ -87,6 +101,7 @@ pub fn mat4x2<N: Scalar>(m11: N, m12: N, m21: N, m22: N, m31: N, m32: N, m41: N,
)
}
/// Create a new 4x3 matrix.
pub fn mat4x3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N, m32: N, m33: N, m41: N, m42: N, m43: N) -> Mat<N, U4, U3> {
Mat::<N, U4, U3>::new(
m11, m12, m13,
@ -96,6 +111,7 @@ pub fn mat4x3<N: Scalar>(m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N,
)
}
/// Create a new 4x4 matrix.
pub fn mat4x4<N: Scalar>(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) -> Mat<N, U4, U4> {
Mat::<N, U4, U4>::new(
m11, m12, m13, m14,
@ -105,6 +121,7 @@ pub fn mat4x4<N: Scalar>(m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N,
)
}
/// Create a new 4x4 matrix.
pub fn mat4<N: Scalar>(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) -> Mat<N, U4, U4> {
Mat::<N, U4, U4>::new(
m11, m12, m13, m14,
@ -114,6 +131,7 @@ pub fn mat4<N: Scalar>(m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N, m
)
}
/// Creates a new quaternion.
pub fn quat<N: Real>(x: N, y: N, z: N, w: N) -> Qua<N> {
Qua::new(x, y, z, w)
}

View File

@ -2,38 +2,44 @@ use na::{Real, DefaultAllocator};
use aliases::Vec;
use traits::{Alloc, Dimension};
/// Componentwise exponential.
pub fn exp<N: Real, D: Dimension>(v: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| x.exp())
}
/// Componentwise base-2 exponential.
pub fn exp2<N: Real, D: Dimension>(v: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| x.exp2())
}
/// Compute the inverse of the square root of each component of `v`.
pub fn inversesqrt<N: Real, D: Dimension>(v: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| N::one() / x.sqrt())
}
/// Componentwise logarithm.
pub fn log<N: Real, D: Dimension>(v: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| x.ln())
}
/// Componentwise base-2 logarithm.
pub fn log2<N: Real, D: Dimension>(v: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| x.log2())
}
/// Componentwise power.
pub fn pow<N: Real, D: Dimension>(base: &Vec<N, D>, exponent: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
base.zip_map(exponent, |b, e| b.powf(e))
}
/// Componentwise square root.
pub fn sqrt<N: Real, D: Dimension>(v: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| x.sqrt())

View File

@ -0,0 +1,172 @@
use na::{Real, U4, Orthographic3, Perspective3};
use aliases::Mat;
//pub fn frustum<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//pub fn frustum_lh<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_lr_no<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_lh_zo<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_no<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_rh<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_rh_zo<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn frustum_zo<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//pub fn infinite_perspective<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn infinite_perspective_lh<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn infinite_perspective_rh<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn infinite_ortho<N: Real>(left: N, right: N, bottom: N, top: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
/// Creates a matrix for an orthographic parallel viewing volume, using the right handedness and OpenGL near and far clip planes definition.
pub fn ortho<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
Orthographic3::new(left, right, bottom, top, znear, zfar).unwrap()
}
//pub fn ortho_lh<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_lh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_lh_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_rh<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_rh_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn ortho_zo<N: Real>(left: N, right: N, bottom: N, top: N, znear: N, zfar: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
/// Creates a matrix for a symetric perspective-view frustum based on the right handedness and OpenGL near and far clip planes definition.
pub fn perspective<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
Perspective3::new(fovy, aspect, near, far).unwrap()
}
//pub fn perspective_fov<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_lh<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_lh_no<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_lh_zo<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_no<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_rh<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_rh_no<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_rh_zo<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_fov_zo<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_lh<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_lh_no<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_lh_zo<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_no<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_rh<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_rh_no<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_rh_zo<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn perspective_zo<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn tweaked_infinite_perspective<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}
//
//pub fn tweaked_infinite_perspective_ep<N: Real>(fovy: N, aspect: N, near: N, ep: N) -> Mat<N, U4, U4> {
// unimplemented!()
//}

View File

@ -0,0 +1,122 @@
use na::{self, Real, U2, U3, U4, Vector3, Vector4, Matrix4};
use aliases::{Mat, Vec};
/// Define a picking region.
///
/// # Parameters
/// * `center`: Specify the center of a picking region in window coordinates.
// * `delta`: Specify the width and height, respectively, of the picking region in window coordinates.
// * `viewport`: Rendering viewport
pub fn pick_matrix<N: Real>(center: &Vec<N, U2>, delta: &Vec<N, U2>, viewport: &Vec<N, U4>) -> Mat<N, U4, U4> {
let shift = Vector3::new(
(viewport.z - (center.x - viewport.x) * na::convert(2.0)) / delta.x,
(viewport.w - (center.y - viewport.y) * na::convert(2.0)) / delta.y,
N::zero()
);
let result = Matrix4::new_translation(&shift);
result.prepend_nonuniform_scaling(&Vector3::new(viewport.z / delta.x, viewport.w / delta.y, N::one()))
}
/// Map the specified object coordinates `(obj.x, obj.y, obj.z)` into window coordinates using OpenGL near and far clip planes definition.
///
/// # Parameters
/// * `obj`: Specify the object coordinates.
/// * `model`: Specifies the current modelview matrix.
/// * `proj`: Specifies the current projection matrix.
/// * `viewport`: Specifies the current viewport.
pub fn project<N: Real>(obj: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
project_no(obj, model, proj, viewport)
}
/// Map the specified object coordinates (obj.x, obj.y, obj.z) into window coordinates.
///
/// The near and far clip planes correspond to z normalized device coordinates of -1 and +1 respectively. (OpenGL clip volume definition)
///
/// # Parameters
/// * `obj`: Specify the object coordinates.
/// * `model`: Specifies the current modelview matrix.
/// * `proj`: Specifies the current projection matrix.
/// * `viewport`: Specifies the current viewport.
pub fn project_no<N: Real>(obj: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let proj = project_zo(obj, model, proj, viewport);
Vector3::new(proj.x, proj.y, proj.z * na::convert(0.5) + na::convert(0.5))
}
/// Map the specified object coordinates (obj.x, obj.y, obj.z) into window coordinates.
///
/// The near and far clip planes correspond to z normalized device coordinates of 0 and +1 respectively. (Direct3D clip volume definition)
///
/// # Parameters
/// * `obj`: Specify the object coordinates.
/// * `model`: Specifies the current modelview matrix.
/// * `proj`: Specifies the current projection matrix.
/// * `viewport`: Specifies the current viewport.
pub fn project_zo<N: Real>(obj: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let normalized = proj * model * Vector4::new(obj.x, obj.y, obj.z, N::one());
let scale = N::one() / normalized.w;
Vector3::new(
viewport.x + (viewport.z * (normalized.x * scale + N::one()) * na::convert(0.5)),
viewport.y + (viewport.w * (normalized.y * scale + N::one()) * na::convert(0.5)),
normalized.z * scale,
)
}
/// Map the specified window coordinates (win.x, win.y, win.z) into object coordinates using OpengGL near and far clip planes definition.
///
/// # Parameters
/// * `obj`: Specify the window coordinates to be mapped.
/// * `model`: Specifies the current modelview matrix.
/// * `proj`: Specifies the current projection matrix.
/// * `viewport`: Specifies the current viewport.
pub fn unproject<N: Real>(win: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
unproject_no(win, model, proj, viewport)
}
/// Map the specified window coordinates (win.x, win.y, win.z) into object coordinates.
///
/// The near and far clip planes correspond to z normalized device coordinates of -1 and +1 respectively. (OpenGL clip volume definition)
///
/// # Parameters
/// * `obj`: Specify the window coordinates to be mapped.
/// * `model`: Specifies the current modelview matrix.
/// * `proj`: Specifies the current projection matrix.
/// * `viewport`: Specifies the current viewport.
pub fn unproject_no<N: Real>(win: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let _2: N = na::convert(2.0);
let transform = (proj * model).try_inverse().unwrap_or(Matrix4::zeros());
let pt = Vector4::new(
_2 * (win.x - viewport.x) / viewport.z - N::one(),
_2 * (win.y - viewport.y) / viewport.w - N::one(),
_2 * win.z - N::one(),
N::one(),
);
let result = transform * pt;
result.fixed_rows::<U3>(0) / result.w
}
/// Map the specified window coordinates (win.x, win.y, win.z) into object coordinates.
///
/// The near and far clip planes correspond to z normalized device coordinates of 0 and +1 respectively. (Direct3D clip volume definition)
///
/// # Parameters
/// * `obj`: Specify the window coordinates to be mapped.
/// * `model`: Specifies the current modelview matrix.
/// * `proj`: Specifies the current projection matrix.
/// * `viewport`: Specifies the current viewport.
pub fn unproject_zo<N: Real>(win: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let _2: N = na::convert(2.0);
let transform = (proj * model).try_inverse().unwrap_or(Matrix4::zeros());
let pt = Vector4::new(
_2 * (win.x - viewport.x) / viewport.z - N::one(),
_2 * (win.y - viewport.y) / viewport.w - N::one(),
win.z,
N::one(),
);
let result = transform * pt;
result.fixed_rows::<U3>(0) / result.w
}

View File

@ -0,0 +1,76 @@
use na::DefaultAllocator;
use aliases::{Vec, Mat};
use traits::{Alloc, Number, Dimension};
/// Perform a component-wise equal-to comparison of two matrices.
///
/// Return a boolean vector which components value is True if this expression is satisfied per column of the matrices.
pub fn equal_columns<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = x.column(i) == y.column(i)
}
res
}
/// Returns the component-wise comparison of `|x - y| < epsilon`.
///
/// True if this expression is satisfied.
pub fn equal_columns_eps<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: N) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
equal_columns_eps_vec(x, y, &Vec::<_, C>::repeat(epsilon))
}
/// Returns the component-wise comparison of `|x - y| < epsilon`.
///
/// True if this expression is satisfied.
pub fn equal_columns_eps_vec<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: &Vec<N, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = (x.column(i) - y.column(i)).abs() < Vec::<_, R>::repeat(epsilon[i])
}
res
}
/// Perform a component-wise not-equal-to comparison of two matrices.
///
/// Return a boolean vector which components value is True if this expression is satisfied per column of the matrices.
pub fn not_equal_columns<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = x.column(i) != y.column(i)
}
res
}
/// Returns the component-wise comparison of `|x - y| < epsilon`.
///
/// True if this expression is not satisfied.
pub fn not_equal_columns_eps<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: N) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
not_equal_columns_eps_vec(x, y, &Vec::<_, C>::repeat(epsilon))
}
/// Returns the component-wise comparison of `|x - y| >= epsilon`.
///
/// True if this expression is not satisfied.
pub fn not_equal_columns_eps_vec<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: &Vec<N, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = (x.column(i) - y.column(i)).abs() >= Vec::<_, R>::repeat(epsilon[i])
}
res
}

View File

@ -0,0 +1,68 @@
use na::{DefaultAllocator, Real, U3, U4, Unit, Rotation3, Point3};
use traits::{Dimension, Number, Alloc};
use aliases::{Mat, Vec};
/// The identity matrix.
pub fn identity<N: Number, D: Dimension>() -> Mat<N, D, D>
where DefaultAllocator: Alloc<N, D, D> {
Mat::<N, D, D>::identity()
}
/// Build a look at view matrix based on the right handedness.
///
/// # Parameters
/// * `eye` Position of the camera
/// * `center` Position where the camera is looking at
/// * `u` Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`
pub fn look_at<N: Real>(eye: &Vec<N, U3>, center: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
look_at_rh(eye, center, up)
}
/// Build a left handed look at view matrix.
///
/// # Parameters
/// * `eye` Position of the camera
/// * `center` Position where the camera is looking at
/// * `u` Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`
pub fn look_at_lh<N: Real>(eye: &Vec<N, U3>, center: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
Mat::look_at_lh(&Point3::from_coordinates(*eye), &Point3::from_coordinates(*center), up)
}
/// Build a right handed look at view matrix.
///
/// # Parameters
/// * `eye` Position of the camera
/// * `center` Position where the camera is looking at
/// * `u` Normalized up vector, how the camera is oriented. Typically `(0, 1, 0)`
pub fn look_at_rh<N: Real>(eye: &Vec<N, U3>, center: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
Mat::look_at_rh(&Point3::from_coordinates(*eye), &Point3::from_coordinates(*center), up)
}
/// Builds a rotation 4 * 4 matrix created from an axis vector and an angle.
///
/// # Parameters
/// * m Input matrix multiplied by this rotation matrix.
/// * angle Rotation angle expressed in radians.
/// * axis Rotation axis, recommended to be normalized.
pub fn rotate<N: Real>(m: &Mat<N, U4, U4>, angle: N, axis: &Vec<N, U3>) -> Mat<N, U4, U4> {
m * Rotation3::from_axis_angle(&Unit::new_normalize(*axis), angle).to_homogeneous()
}
/// Builds a scale 4 * 4 matrix created from 3 scalars.
///
/// # Parameters
/// * m Input matrix multiplied by this scale matrix.
/// * v Ratio of scaling for each axis.
pub fn scale<N: Number>(m: &Mat<N, U4, U4>, v: &Vec<N, U3>) -> Mat<N, U4, U4> {
m.prepend_nonuniform_scaling(v)
}
/// Builds a translation 4 * 4 matrix created from a vector of 3 components.
///
/// # Parameters
/// * m Input matrix multiplied by this translation matrix.
/// * v Coordinates of a translation vector.
pub fn translate<N: Number>(m: &Mat<N, U4, U4>, v: &Vec<N, U3>) -> Mat<N, U4, U4> {
m.prepend_translation(v)
}

View File

@ -0,0 +1,20 @@
//! (Reexported) Additional features not specified by GLSL specification
pub use self::matrix_clip_space::*;
pub use self::matrix_projection::*;
pub use self::matrix_relationnal::*;
pub use self::matrix_transform::*;
pub use self::scalar_common::*;
pub use self::scalar_constants::*;
pub use self::vector_common::*;
pub use self::vector_relational::*;
mod matrix_clip_space;
mod matrix_projection;
mod matrix_relationnal;
mod matrix_transform;
mod scalar_common;
mod scalar_constants;
mod vector_common;
mod vector_relational;

View File

@ -2,18 +2,22 @@ use na;
use traits::Number;
/// Returns the maximum value among three.
pub fn max3<N: Number>(a: N, b: N, c: N) -> N {
na::sup(&na::sup(&a, &b), &c)
}
/// Returns the maximum value among four.
pub fn max4<N: Number>(a: N, b: N, c: N, d: N) -> N {
na::sup(&na::sup(&a, &b), &na::sup(&c, &d))
}
/// Returns the maximum value among three.
pub fn min3<N: Number>(a: N, b: N, c: N) -> N {
na::inf(&na::inf(&a, &b), &c)
}
/// Returns the maximum value among four.
pub fn min4<N: Number>(a: N, b: N, c: N, d: N) -> N {
na::inf(&na::inf(&a, &b), &na::inf(&c, &d))
}

View File

@ -1,10 +1,12 @@
use approx::AbsDiffEq;
use na::Real;
/// Default epsilon value used for apporximate comparison.
pub fn epsilon<N: AbsDiffEq<Epsilon = N>>() -> N {
N::default_epsilon()
}
/// The value of PI.
pub fn pi<N: Real>() -> N {
N::pi()
}

View File

@ -3,41 +3,49 @@ use na::{self, DefaultAllocator};
use traits::{Alloc, Number, Dimension};
use aliases::Vec;
/// Componentwise maximum between a vector and a scalar.
pub fn max<N: Number, D: Dimension>(a: &Vec<N, D>, b: N) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
a.map(|a| na::sup(&a, &b))
}
/// Componentwise maximum between two vectors.
pub fn max2<N: Number, D: Dimension>(a: &Vec<N, D>, b: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
na::sup(a, b)
}
/// Componentwise maximum between three vectors.
pub fn max3<N: Number, D: Dimension>(a: &Vec<N, D>, b: &Vec<N, D>, c: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
max2(&max2(a, b), c)
}
/// Componentwise maximum between four vectors.
pub fn max4<N: Number, D: Dimension>(a: &Vec<N, D>, b: &Vec<N, D>, c: &Vec<N, D>, d: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
max2(&max2(a, b), &max2(c, d))
}
pub fn min<N: Number, D: Dimension>(x: &Vec<N, D>,y: N) -> Vec<N, D>
/// Componentwise maximum between a vector and a scalar.
pub fn min<N: Number, D: Dimension>(x: &Vec<N, D>, y: N) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|x| na::inf(&x, &y))
}
/// Componentwise maximum between two vectors.
pub fn min2<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
na::inf(x, y)
}
/// Componentwise maximum between three vectors.
pub fn min3<N: Number, D: Dimension>(a: &Vec<N, D>, b: &Vec<N, D>, c: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
min2(&min2(a, b), c)
}
/// Componentwise maximum between four vectors.
pub fn min4<N: Number, D: Dimension>(a: &Vec<N, D>, b: &Vec<N, D>, c: &Vec<N, D>, d: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
min2(&min2(a, b), &min2(c, d))

View File

@ -3,21 +3,25 @@ use na::{DefaultAllocator};
use traits::{Alloc, Number, Dimension};
use aliases::Vec;
/// Component-wise approximate equality of two vectors, using a scalar epsilon.
pub fn equal_eps<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| abs_diff_eq!(x, y))
x.zip_map(y, |x, y| abs_diff_eq!(x, y, epsilon = epsilon))
}
/// Component-wise approximate equality of two vectors, using a per-component epsilon.
pub fn equal_eps_vec<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_zip_map(y, epsilon, |x, y, eps| abs_diff_eq!(x, y, epsilon = eps))
}
/// Component-wise approximate non-equality of two vectors, using a scalar epsilon.
pub fn not_equal_eps<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| abs_diff_ne!(x, y))
x.zip_map(y, |x, y| abs_diff_ne!(x, y, epsilon = epsilon))
}
/// Component-wise approximate non-equality of two vectors, using a per-component epsilon.
pub fn not_equal_eps_vec<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_zip_map(y, epsilon, |x, y, eps| abs_diff_ne!(x, y, epsilon = eps))

View File

@ -1,170 +0,0 @@
use na::{Real, U4};
use aliases::Mat;
pub fn frustum<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_lh<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_lr_no<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_lh_zo<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_no<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_rh<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_rh_zo<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn frustum_zo<N: Real>(left: N, right: N, bottom: N, top: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn infinite_perspective<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn infinite_perspective_lh<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn infinite_perspective_rh<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn infinite_ortho<N: Real>(left: N, right: N, bottom: N, top: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_lh<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_lh_no<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_lh_zo<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_no<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_rh<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_rh_no<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_rh_zo<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn ortho_zo<N: Real>(left: N, right: N, bottom: N, top: N, zNear: N, zFar: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_lh<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_lh_no<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_lh_zo<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_no<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_rh<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_rh_no<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_rh_zo<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_fov_zo<N: Real>(fov: N, width: N, height: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_lh<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_lh_no<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_lh_zo<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_no<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_rh<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_rh_no<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_rh_zo<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn perspective_zo<N: Real>(fovy: N, aspect: N, near: N, far: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn tweaked_infinite_perspective<N: Real>(fovy: N, aspect: N, near: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn tweaked_infinite_perspective_ep<N: Real>(fovy: N, aspect: N, near: N, ep: N) -> Mat<N, U4, U4> {
unimplemented!()
}

View File

@ -1,67 +0,0 @@
use na::{self, Real, U2, U3, U4, Vector3, Vector4, Matrix4};
use aliases::{Mat, Vec};
pub fn pick_matrix<N: Real>(center: &Vec<N, U2>, delta: &Vec<N, U2>, viewport: &Vec<N, U4>) -> Mat<N, U4, U4> {
let shift = Vector3::new(
(viewport.z - (center.x - viewport.x) * na::convert(2.0)) / delta.x,
(viewport.w - (center.y - viewport.y) * na::convert(2.0)) / delta.y,
N::zero()
);
let result = Matrix4::new_translation(&shift);
result.prepend_nonuniform_scaling(&Vector3::new(viewport.z / delta.x, viewport.w / delta.y, N::one()))
}
pub fn project<N: Real>(obj: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
project_no(obj, model, proj, viewport)
}
pub fn project_no<N: Real>(obj: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let proj = project_zo(obj, model, proj, viewport);
Vector3::new(proj.x, proj.y, proj.z * na::convert(0.5) + na::convert(0.5))
}
pub fn project_zo<N: Real>(obj: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let mut normalized = proj * model * Vector4::new(obj.x, obj.y, obj.z, N::one());
let scale = N::one() / normalized.w;
Vector3::new(
viewport.x + (viewport.z * (normalized.x * scale + N::one()) * na::convert(0.5)),
viewport.y + (viewport.w * (normalized.y * scale + N::one()) * na::convert(0.5)),
normalized.z * scale,
)
}
pub fn unproject<N: Real>(win: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
unproject_no(win, model, proj, viewport)
}
pub fn unproject_no<N: Real>(win: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let _2: N = na::convert(2.0);
let transform = (proj * model).try_inverse().unwrap_or(Matrix4::zeros());
let pt = Vector4::new(
_2 * (win.x - viewport.x) / viewport.z - N::one(),
_2 * (win.y - viewport.y) / viewport.w - N::one(),
_2 * win.z - N::one(),
N::one(),
);
let result = transform * pt;
result.fixed_rows::<U3>(0) / result.w
}
pub fn unproject_zo<N: Real>(win: &Vec<N, U3>, model: &Mat<N, U4, U4>, proj: &Mat<N, U4, U4>, viewport: Vec<N, U4>) -> Vec<N, U3> {
let _2: N = na::convert(2.0);
let transform = (proj * model).try_inverse().unwrap_or(Matrix4::zeros());
let pt = Vector4::new(
_2 * (win.x - viewport.x) / viewport.z - N::one(),
_2 * (win.y - viewport.y) / viewport.w - N::one(),
win.z,
N::one(),
);
let result = transform * pt;
result.fixed_rows::<U3>(0) / result.w
}

View File

@ -1,59 +0,0 @@
use na::DefaultAllocator;
use aliases::{Vec, Mat};
use traits::{Alloc, Number, Dimension};
pub fn equal<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = x.column(i) == y.column(i)
}
res
}
pub fn equal_eps<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: N) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
equal_eps_vec(x, y, &Vec::<_, C>::repeat(epsilon))
}
pub fn equal_eps_vec<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: &Vec<N, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = (x.column(i) - y.column(i)).abs() < Vec::<_, R>::repeat(epsilon[i])
}
res
}
pub fn not_equal<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = x.column(i) != y.column(i)
}
res
}
pub fn not_equal_eps<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: N) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
not_equal_eps_vec(x, y, &Vec::<_, C>::repeat(epsilon))
}
pub fn not_equal_eps_vec<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>, epsilon: &Vec<N, C>) -> Vec<bool, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = Vec::<_, C>::repeat(false);
for i in 0..C::dim() {
res[i] = (x.column(i) - y.column(i)).abs() >= Vec::<_, R>::repeat(epsilon[i])
}
res
}

View File

@ -1,34 +0,0 @@
use na::{DefaultAllocator, Scalar, Real, U3, U4, Unit, Rotation3, Point3};
use traits::{Dimension, Number, Alloc};
use aliases::{Mat, Vec};
pub fn identity<N: Number, D: Dimension>() -> Mat<N, D, D>
where DefaultAllocator: Alloc<N, D, D> {
Mat::<N, D, D>::identity()
}
/// Same as `look_at_rh`
pub fn look_at<N: Real>(eye: &Vec<N, U3>, center: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
look_at_rh(eye, center, up)
}
pub fn look_at_lh<N: Real>(eye: &Vec<N, U3>, center: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
Mat::look_at_lh(&Point3::from_coordinates(*eye), &Point3::from_coordinates(*center), up)
}
pub fn look_at_rh<N: Real>(eye: &Vec<N, U3>, center: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
Mat::look_at_rh(&Point3::from_coordinates(*eye), &Point3::from_coordinates(*center), up)
}
pub fn rotate<N: Real>(m: &Mat<N, U4, U4>, angle: N, axis: &Vec<N, U3>) -> Mat<N, U4, U4> {
m * Rotation3::from_axis_angle(&Unit::new_normalize(*axis), angle).to_homogeneous()
}
pub fn scale<N: Number>(m: &Mat<N, U4, U4>, v: &Vec<N, U3>) -> Mat<N, U4, U4> {
m.prepend_nonuniform_scaling(v)
}
pub fn translate<N: Number>(m: &Mat<N, U4, U4>, v: &Vec<N, U3>) -> Mat<N, U4, U4> {
m.prepend_translation(v)
}

View File

@ -1,22 +1,26 @@
use na::{self, Scalar, Real, U3, DefaultAllocator};
use na::{Real, U3, DefaultAllocator};
use traits::{Number, Alloc, Dimension};
use aliases::Vec;
/// The cross product of two vectors.
pub fn cross<N: Number, D: Dimension>(x: &Vec<N, U3>, y: &Vec<N, U3>) -> Vec<N, U3> {
x.cross(y)
}
/// The distance between two points.
pub fn distance<N: Real, D: Dimension>(p0: &Vec<N, D>, p1: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
(p1 - p0).norm()
}
/// The dot product of two vectors.
pub fn dot<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.dot(y)
}
/// If `dot(nref, i) < 0.0`, return `n`, otherwise, return `-n`.
pub fn faceforward<N: Number, D: Dimension>(n: &Vec<N, D>, i: &Vec<N, D>, nref: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
if nref.dot(i) < N::zero() {
@ -26,22 +30,32 @@ pub fn faceforward<N: Number, D: Dimension>(n: &Vec<N, D>, i: &Vec<N, D>, nref:
}
}
/// The magnitude of a vector.
pub fn length<N: Real, D: Dimension>(x: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.norm()
}
/// The magnitude of a vector.
pub fn magnitude<N: Real, D: Dimension>(x: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.norm()
}
/// Normalizes a vector.
pub fn normalize<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.normalize()
}
/// For the incident vector `i` and surface orientation `n`, returns the reflection direction : `result = i - 2.0 * dot(n, i) * n`.
pub fn reflect<N: Number, D: Dimension>(i: &Vec<N, D>, n: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
let _2 = N::one() + N::one();
i - n * (n.dot(i) * _2)
}
/// For the incident vector `i` and surface normal `n`, and the ratio of indices of refraction `eta`, return the refraction vector.
pub fn refract<N: Real, D: Dimension>(i: &Vec<N, D>, n: &Vec<N, D>, eta: N) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {

View File

@ -1,111 +1,138 @@
use na::{self, Real};
use traits::Number;
/// The euler constant.
pub fn e<N: Real>() -> N {
N::e()
}
/// The euler constant.
pub fn euler<N: Real>() -> N {
N::e()
}
/// Returns `4 / pi`.
pub fn four_over_pi<N: Real>() -> N {
na::convert::<_, N>(4.0) / N::pi()
}
/// Returns the golden ratio.
pub fn golden_ratio<N: Real>() -> N {
(N::one() + root_five()) / na::convert(2.0)
}
/// Returns `4 / pi`.
pub fn half_pi<N: Real>() -> N {
N::frac_pi_2()
}
/// Returns `4 / pi`.
pub fn ln_ln_two<N: Real>() -> N {
N::ln_2().ln()
}
/// Returns `ln(10)`.
pub fn ln_ten<N: Real>() -> N {
N::ln_10()
}
/// Returns `ln(2)`.
pub fn ln_two<N: Real>() -> N {
N::ln_2()
}
/// Returns `1`.
pub fn one<N: Number>() -> N {
N::one()
}
/// Returns `1 / pi`.
pub fn one_over_pi<N: Real>() -> N {
N::frac_1_pi()
}
/// Returns `1 / sqrt(2)`.
pub fn one_over_root_two<N: Real>() -> N {
N::one() / root_two()
}
/// Returns `1 / (2pi)`.
pub fn one_over_two_pi<N: Real>() -> N {
N::frac_1_pi() * na::convert(0.5)
}
/// Returns `pi / 4`.
pub fn quarter_pi<N: Real>() -> N {
N::frac_pi_4()
}
/// Returns `sqrt(5)`.
pub fn root_five<N: Real>() -> N {
na::convert::<_, N>(5.0).sqrt()
}
/// Returns `sqrt(pi / 2)`.
pub fn root_half_pi<N: Real>() -> N {
(N::pi() / na::convert(2.0)).sqrt()
}
/// Returns `sqrt(ln(4))`.
pub fn root_ln_four<N: Real>() -> N {
na::convert::<_, N>(4.0).sqrt()
}
/// Returns the square root of pi.
pub fn root_pi<N: Real>() -> N {
N::pi().sqrt()
}
/// Returns the square root of 3.
pub fn root_three<N: Real>() -> N {
na::convert::<_, N>(3.0).sqrt()
}
/// Returns the square root of 2.
pub fn root_two<N: Real>() -> N {
// FIXME: there should be a ::sqrt_2() on the Real trait.
na::convert::<_, N>(2.0).sqrt()
}
/// Returns the square root of 2pi.
pub fn root_two_pi<N: Real>() -> N {
N::two_pi().sqrt()
}
/// Returns `1 / 3.
pub fn third<N: Real>() -> N {
na::convert(1.0 / 2.0)
}
/// Returns `3 / (2pi)`.
pub fn three_over_two_pi<N: Real>() -> N {
na::convert::<_, N>(3.0) / N::two_pi()
}
/// Returns `2 / pi`.
pub fn two_over_pi<N: Real>() -> N {
N::frac_2_pi()
}
/// Returns `2 / sqrt(pi)`.
pub fn two_over_root_pi<N: Real>() -> N {
N::frac_2_pi()
}
/// Returns `2pi`.
pub fn two_pi<N: Real>() -> N {
N::two_pi()
}
/// Returns `2 / 3`.
pub fn two_thirds<N: Real>() -> N {
na::convert(2.0 / 3.0)
}
/// Returns `0`.
pub fn zero<N: Number>() -> N {
N::zero()
}

View File

@ -0,0 +1,27 @@
use approx::AbsDiffEq;
use na::DefaultAllocator;
use traits::{Alloc, Number, Dimension};
use aliases::Vec;
/// Componentwise approximate equality beween two vectors.
pub fn epsilon_equal<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| abs_diff_eq!(x, y, epsilon = epsilon))
}
/// Componentwise approximate equality beween two scalars.
pub fn epsilon_equal2<N: AbsDiffEq<Epsilon = N>>(x: N, y: N, epsilon: N) -> bool {
abs_diff_eq!(x, y, epsilon = epsilon)
}
/// Componentwise approximate non-equality beween two vectors.
pub fn epsilon_not_equal<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| abs_diff_ne!(x, y, epsilon = epsilon))
}
/// Componentwise approximate non-equality beween two scalars.
pub fn epsilon_not_equal2<N: AbsDiffEq<Epsilon = N>>(x: N, y: N, epsilon: N) -> bool {
abs_diff_ne!(x, y, epsilon = epsilon)
}

View File

@ -3,11 +3,13 @@ use na::{Scalar, DefaultAllocator};
use traits::{Alloc, Dimension};
use aliases::{Vec, Mat};
/// The `index`-th column of the matrix `m`.
pub fn column<N: Scalar, R: Dimension, C: Dimension>(m: &Mat<N, R, C>, index: usize) -> Vec<N, R>
where DefaultAllocator: Alloc<N, R, C> {
m.column(index).into_owned()
}
/// Sets to `x` the `index`-th column of the matrix `m`.
pub fn set_column<N: Scalar, R: Dimension, C: Dimension>(m: &Mat<N, R, C>, index: usize, x: &Vec<N, R>) -> Mat<N, R, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = m.clone();
@ -15,11 +17,13 @@ pub fn set_column<N: Scalar, R: Dimension, C: Dimension>(m: &Mat<N, R, C>, index
res
}
/// The `index`-th row of the matrix `m`.
pub fn row<N: Scalar, R: Dimension, C: Dimension>(m: &Mat<N, R, C>, index: usize) -> Vec<N, C>
where DefaultAllocator: Alloc<N, R, C> {
m.row(index).into_owned().transpose()
}
/// Sets to `x` the `index`-th row of the matrix `m`.
pub fn set_row<N: Scalar, R: Dimension, C: Dimension>(m: &Mat<N, R, C>, index: usize, x: &Vec<N, C>) -> Mat<N, R, C>
where DefaultAllocator: Alloc<N, R, C> {
let mut res = m.clone();

View File

@ -1,14 +1,16 @@
use na::{Real, DefaultAllocator, Transform, TAffine};
use na::{Real, DefaultAllocator};
use traits::{Alloc, Dimension};
use aliases::Mat;
/// Fast matrix inverse for affine matrix.
pub fn affine_inverse<N: Real, D: Dimension>(m: Mat<N, D, D>) -> Mat<N, D, D>
where DefaultAllocator: Alloc<N, D, D> {
// FIXME: this should be optimized.
m.try_inverse().unwrap_or(Mat::<_, D, D>::zeros())
}
/// Compute the transpose of the inverse of a matrix.
pub fn inverse_transpose<N: Real, D: Dimension>(m: Mat<N, D, D>) -> Mat<N, D, D>
where DefaultAllocator: Alloc<N, D, D> {
m.try_inverse().unwrap_or(Mat::<_, D, D>::zeros()).transpose()

View File

@ -0,0 +1,26 @@
//! (Reexported) Recommended features not specified by GLSL specification
//pub use self::bitfield::*;
pub use self::constants::*;
pub use self::epsilon::*;
//pub use self::integer::*;
pub use self::matrix_access::*;
pub use self::matrix_inverse::*;
//pub use self::packing::*;
//pub use self::reciprocal::*;
//pub use self::round::*;
pub use self::type_ptr::*;
//pub use self::ulp::*;
//mod bitfield;
mod constants;
mod epsilon;
//mod integer;
mod matrix_access;
mod matrix_inverse;
//mod packing;
//mod reciprocal;
//mod round;
mod type_ptr;
//mod ulp;

View File

@ -5,140 +5,211 @@ use na::{Scalar, Real, U1, U2, U3, U4, DefaultAllocator,
use traits::{Number, Alloc, Dimension};
use aliases::{Qua, Vec, Mat};
/// Creates a 2x2 matrix from a slice arranged in column-major order.
pub fn make_mat2<N: Scalar>(ptr: &[N]) -> Mat<N, U2, U2> {
Matrix2::from_column_slice(ptr)
}
/// Creates a 2x2 matrix from a slice arranged in column-major order.
pub fn make_mat2x2<N: Scalar>(ptr: &[N]) -> Mat<N, U2, U2> {
Matrix2::from_column_slice(ptr)
}
/// Creates a 2x3 matrix from a slice arranged in column-major order.
pub fn make_mat2x3<N: Scalar>(ptr: &[N]) -> Mat<N, U2, U3> {
Matrix2x3::from_column_slice(ptr)
}
/// Creates a 2x4 matrix from a slice arranged in column-major order.
pub fn make_mat2x4<N: Scalar>(ptr: &[N]) -> Mat<N, U2, U4> {
Matrix2x4::from_column_slice(ptr)
}
/// Creates a 3 matrix from a slice arranged in column-major order.
pub fn make_mat3<N: Scalar>(ptr: &[N]) -> Mat<N, U3, U3> {
Matrix3::from_column_slice(ptr)
}
/// Creates a 3x2 matrix from a slice arranged in column-major order.
pub fn make_mat3x2<N: Scalar>(ptr: &[N]) -> Mat<N, U3, U2> {
Matrix3x2::from_column_slice(ptr)
}
/// Creates a 3x3 matrix from a slice arranged in column-major order.
pub fn make_mat3x3<N: Scalar>(ptr: &[N]) -> Mat<N, U3, U3> {
Matrix3::from_column_slice(ptr)
}
/// Creates a 3x4 matrix from a slice arranged in column-major order.
pub fn make_mat3x4<N: Scalar>(ptr: &[N]) -> Mat<N, U3, U4> {
Matrix3x4::from_column_slice(ptr)
}
/// Creates a 4x4 matrix from a slice arranged in column-major order.
pub fn make_mat4<N: Scalar>(ptr: &[N]) -> Mat<N, U4, U4> {
Matrix4::from_column_slice(ptr)
}
/// Creates a 4x2 matrix from a slice arranged in column-major order.
pub fn make_mat4x2<N: Scalar>(ptr: &[N]) -> Mat<N, U4, U2> {
Matrix4x2::from_column_slice(ptr)
}
/// Creates a 4x3 matrix from a slice arranged in column-major order.
pub fn make_mat4x3<N: Scalar>(ptr: &[N]) -> Mat<N, U4, U3> {
Matrix4x3::from_column_slice(ptr)
}
/// Creates a 4x4 matrix from a slice arranged in column-major order.
pub fn make_mat4x4<N: Scalar>(ptr: &[N]) -> Mat<N, U4, U4> {
Matrix4::from_column_slice(ptr)
}
/// Creates a quaternion from a slice arranged as `[x, y, z, w]`.
pub fn make_quat<N: Real>(ptr: &[N]) -> Qua<N> {
Quaternion::from_vector(Vector4::from_column_slice(ptr))
}
/// Creates a 1D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec1<N: Scalar>(v: &Vec<N, U1>) -> Vec<N, U1> {
*v
}
/// Creates a 1D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec1_2<N: Scalar>(v: &Vec<N, U2>) -> Vec<N, U1> {
Vector1::new(v.x)
}
/// Creates a 1D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec1_3<N: Scalar>(v: &Vec<N, U3>) -> Vec<N, U1> {
Vector1::new(v.x)
}
/// Creates a 1D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec1_4<N: Scalar>(v: &Vec<N, U4>) -> Vec<N, U1> {
Vector1::new(v.x)
}
/// Creates a 2D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec2_1<N: Number>(v: &Vec<N, U1>) -> Vec<N, U2> {
Vector2::new(v.x, N::zero())
}
/// Creates a 2D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec2_2<N: Scalar>(v: &Vec<N, U2>) -> Vec<N, U2> {
*v
}
/// Creates a 2D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec2_3<N: Scalar>(v: &Vec<N, U3>) -> Vec<N, U2> {
Vector2::new(v.x, v.y)
}
/// Creates a 2D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec2_4<N: Scalar>(v: &Vec<N, U4>) -> Vec<N, U2> {
Vector2::new(v.x, v.y)
}
/// Creates a 2D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec2<N: Scalar>(ptr: &[N]) -> Vec<N, U2> {
Vector2::from_column_slice(ptr)
}
/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec3_1<N: Number>(v: &Vec<N, U1>) -> Vec<N, U3> {
Vector3::new(v.x, N::zero(), N::zero())
}
/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec3_2<N: Number>(v: &Vec<N, U2>) -> Vec<N, U3> {
Vector3::new(v.x, v.y, N::zero())
}
/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec3_3<N: Scalar>(v: &Vec<N, U3>) -> Vec<N, U3> {
*v
}
/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec3_4<N: Scalar>(v: &Vec<N, U4>) -> Vec<N, U3> {
Vector3::new(v.x, v.y, v.z)
}
/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec3<N: Scalar>(ptr: &[N]) -> Vec<N, U3> {
Vector3::from_column_slice(ptr)
}
/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec4_1<N: Number>(v: &Vec<N, U1>) -> Vec<N, U4> {
Vector4::new(v.x, N::zero(), N::zero(), N::zero())
}
/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec4_2<N: Number>(v: &Vec<N, U2>) -> Vec<N, U4> {
Vector4::new(v.x, v.y, N::zero(), N::zero())
}
/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec4_3<N: Number>(v: &Vec<N, U3>) -> Vec<N, U4> {
Vector4::new(v.x, v.y, v.z, N::zero())
}
/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec4_4<N: Scalar>(v: &Vec<N, U4>) -> Vec<N, U4> {
*v
}
/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn make_vec4<N: Scalar>(ptr: &[N]) -> Vec<N, U4> {
Vector4::from_column_slice(ptr)
}
/// Converts a matrix or vector to a slice arranged in column-major order.
pub fn value_ptr<N: Scalar, R: Dimension, C: Dimension>(x: &Mat<N, R, C>) -> &[N]
where DefaultAllocator: Alloc<N, R, C> {
x.as_slice()
}
/// Converts a matrix or vector to a mutable slice arranged in column-major order.
pub fn value_ptr_mut<N: Scalar, R: Dimension, C: Dimension>(x: &mut Mat<N, R, C>) -> &mut [N]
where DefaultAllocator: Alloc<N, R, C> {
x.as_mut_slice()

View File

@ -1,23 +0,0 @@
use approx::AbsDiffEq;
use na::DefaultAllocator;
use traits::{Alloc, Number, Dimension};
use aliases::Vec;
pub fn epsilonEqual<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| abs_diff_eq!(x, y, epsilon = epsilon))
}
pub fn epsilonEqual2<N: AbsDiffEq<Epsilon = N>>(x: N, y: N, epsilon: N) -> bool {
abs_diff_eq!(x, y, epsilon = epsilon)
}
pub fn epsilonNotEqual<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| abs_diff_ne!(x, y, epsilon = epsilon))
}
pub fn epsilonNotEqual2<N: AbsDiffEq<Epsilon = N>>(x: N, y: N, epsilon: N) -> bool {
abs_diff_ne!(x, y, epsilon = epsilon)
}

View File

@ -1,24 +1,27 @@
use na::{self, Scalar, Real, DefaultAllocator};
use na::{self, DefaultAllocator};
use traits::{Number, Alloc, Dimension};
use aliases::Mat;
/// The sum of every components of the given matrix or vector.
pub fn comp_add<N: Number, R: Dimension, C: Dimension>(m: &Mat<N, R, C>) -> N
where DefaultAllocator: Alloc<N, R, C> {
m.iter().fold(N::zero(), |x, y| x + *y)
}
/// The maximum of every components of the given matrix or vector.
pub fn comp_max<N: Number, R: Dimension, C: Dimension>(m: &Mat<N, R, C>) -> N
where DefaultAllocator: Alloc<N, R, C> {
m.iter().fold(N::min_value(), |x, y| na::sup(&x, y))
}
/// The minimum of every components of the given matrix or vector.
pub fn comp_min<N: Number, R: Dimension, C: Dimension>(m: &Mat<N, R, C>) -> N
where DefaultAllocator: Alloc<N, R, C> {
m.iter().fold(N::max_value(), |x, y| na::inf(&x, y))
}
/// The product of every components of the given matrix or vector.
pub fn comp_mul<N: Number, R: Dimension, C: Dimension>(m: &Mat<N, R, C>) -> N
where DefaultAllocator: Alloc<N, R, C> {
m.iter().fold(N::one(), |x, y| x * *y)

View File

@ -0,0 +1,163 @@
use na::{Real, U3, U4};
use aliases::{Vec, Mat};
pub fn derivedEulerAngleX<N: Real>(angleX: N, angularVelocityX: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn derivedEulerAngleY<N: Real>(angleY: N, angularVelocityY: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn derivedEulerAngleZ<N: Real>(angleZ: N, angularVelocityZ: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleX<N: Real>(angleX: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleXY<N: Real>(angleX: N, angleY: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleXYX<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleXYZ<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleXZ<N: Real>(angleX: N, angleZ: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleXZX<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleXZY<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleY<N: Real>(angleY: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleYX<N: Real>(angleY: N, angleX: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleYXY<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleYXZ<N: Real>(yaw: N, pitch: N, roll: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleYZ<N: Real>(angleY: N, angleZ: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleYZX<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleYZY<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZ<N: Real>(angleZ: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZX<N: Real>(angle: N, angleX: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZXY<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZXZ<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZY<N: Real>(angleZ: N, angleY: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZYX<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn eulerAngleZYZ<N: Real>(t1: N, t2: N, t3: N) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn extractEulerAngleXYX<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleXYZ<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleXZX<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleXZY<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleYXY<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleYXZ<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleYZX<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleYZY<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleZXY<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleZXZ<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleZYX<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn extractEulerAngleZYZ<N: Real>(M: &Mat<N, U4, U4>) -> (N, N, N) {
unimplemented!()
}
pub fn orientate2<N: Real>(angle: N) -> Mat<N, U3, U3> {
unimplemented!()
}
pub fn orientate3<N: Real>(angles: Vec<N, U3>) -> Mat<N, U3, U3> {
unimplemented!()
}
pub fn orientate4<N: Real>(angles: Vec<N, U3>) -> Mat<N, U4, U4> {
unimplemented!()
}
pub fn yawPitchRoll<N: Real>(yaw: N, pitch: N, roll: N) -> Mat<N, U4, U4> {
unimplemented!()
}

View File

@ -3,6 +3,7 @@ use na::U2;
use traits::Number;
use aliases::Vec;
/// The 2D perpendicular product between two vectors.
pub fn cross<N: Number>(v: &Vec<N, U2>, u: &Vec<N, U2>) -> N {
v.perp(u)
}

View File

@ -3,10 +3,12 @@ use na::U3;
use traits::Number;
use aliases::Vec;
/// Returns `true` if `{a, b, c}` forms a left-handed trihedron.
pub fn left_handed<N: Number>(a: &Vec<N, U3>, b: &Vec<N, U3>, c: &Vec<N, U3>) -> bool {
a.cross(b).dot(c) < N::zero()
}
/// Returns `true` if `{a, b, c}` forms a right-handed trihedron.
pub fn right_handed<N: Number>(a: &Vec<N, U3>, b: &Vec<N, U3>, c: &Vec<N, U3>) -> bool {
a.cross(b).dot(c) > N::zero()
}

View File

@ -1,11 +1,13 @@
use na::{self, Real, U3, U4, Matrix4};
use na::{Real, U3, U4, Matrix4};
use aliases::{Vec, Mat};
/// Builds a 3x3 matrix `m` such that for any `v`: `m * v == cross(x, v)`.
pub fn matrix_cross3<N: Real>(x: &Vec<N, U3>) -> Mat<N, U3, U3> {
x.cross_matrix()
}
/// Builds a 4x4 matrix `m` such that for any `v`: `m * v == cross(x, v)`.
pub fn matrix_cross4<N: Real>(x: &Vec<N, U3>) -> Mat<N, U4, U4> {
let m = x.cross_matrix();

View File

@ -4,39 +4,47 @@ use na::{Matrix2, Matrix2x3, Matrix2x4, Matrix3, Matrix3x2, Matrix3x4, Matrix4,
use traits::Number;
use aliases::{Vec, Mat};
/// Builds a 2x2 diagonal matrix.
pub fn diagonal2x2<N: Number>(v: &Vec<N, U2>) -> Mat<N, U2, U2> {
Matrix2::from_diagonal(v)
}
/// Builds a 2x3 diagonal matrix.
pub fn diagonal2x3<N: Number>(v: &Vec<N, U2>) -> Mat<N, U2, U3> {
Matrix2x3::from_partial_diagonal(v.as_slice())
}
/// Builds a 2x4 diagonal matrix.
pub fn diagonal2x4<N: Number>(v: &Vec<N, U2>) -> Mat<N, U2, U4> {
Matrix2x4::from_partial_diagonal(v.as_slice())
}
/// Builds a 3x2 diagonal matrix.
pub fn diagonal3x2<N: Number>(v: &Vec<N, U3>) -> Mat<N, U3, U2> {
Matrix3x2::from_partial_diagonal(v.as_slice())
}
/// Builds a 3x3 diagonal matrix.
pub fn diagonal3x3<N: Number>(v: &Vec<N, U3>) -> Mat<N, U3, U3> {
Matrix3::from_diagonal(v)
}
/// Builds a 3x4 diagonal matrix.
pub fn diagonal3x4<N: Number>(v: &Vec<N, U3>) -> Mat<N, U3, U4> {
Matrix3x4::from_partial_diagonal(v.as_slice())
}
/// Builds a 4x2 diagonal matrix.
pub fn diagonal4x2<N: Number>(v: &Vec<N, U4>) -> Mat<N, U4, U2> {
Matrix4x2::from_partial_diagonal(v.as_slice())
}
/// Builds a 4x3 diagonal matrix.
pub fn diagonal4x3<N: Number>(v: &Vec<N, U4>) -> Mat<N, U4, U3> {
Matrix4x3::from_partial_diagonal(v.as_slice())
}
/// Builds a 4x4 diagonal matrix.
pub fn diagonal4x4<N: Number>(v: &Vec<N, U4>) -> Mat<N, U4, U4> {
Matrix4::from_diagonal(v)
}

View File

@ -0,0 +1,38 @@
//! (Reexported) Experimental features not specified by GLSL specification.
pub use self::component_wise::*;
//pub use self::euler_angles::*;
pub use self::exterior_product::*;
pub use self::handed_coordinate_space::*;
pub use self::matrix_cross_product::*;
pub use self::matrix_operation::*;
pub use self::norm::*;
pub use self::normal::*;
pub use self::normalize_dot::*;
pub use self::rotate_normalized_axis::*;
pub use self::rotate_vector::*;
pub use self::transform::*;
pub use self::transform2::*;
pub use self::transform2d::*;
pub use self::vector_angle::*;
pub use self::vector_query::*;
mod component_wise;
//mod euler_angles;
mod exterior_product;
mod handed_coordinate_space;
mod matrix_cross_product;
mod matrix_operation;
mod norm;
mod normal;
mod normalize_dot;
mod rotate_normalized_axis;
mod rotate_vector;
mod transform;
mod transform2;
mod transform2d;
mod vector_angle;
mod vector_query;

View File

@ -3,36 +3,48 @@ use na::{Real, DefaultAllocator};
use traits::{Alloc, Dimension};
use aliases::Vec;
/// The squared distance between two points.
pub fn distance2<N: Real, D: Dimension>(p0: &Vec<N, D>, p1: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
(p1 - p0).norm_squared()
}
/// The l1-norm of `x - y`.
pub fn l1_distance<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
l1_norm(&(x - y))
}
/// The l1-norm of `v`.
pub fn l1_norm<N: Real, D: Dimension>(v: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
::comp_add(&v.abs())
}
/// The l2-norm of `x - y`.
pub fn l2_distance<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
l2_norm(&(y - x))
}
/// The l2-norm of `v`.
pub fn l2_norm<N: Real, D: Dimension>(x: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.norm()
}
/// The squared magnitude of `x`.
pub fn length2<N: Real, D: Dimension>(x: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.norm_squared()
}
/// The squared magnitude of `x`.
pub fn magnitude2<N: Real, D: Dimension>(x: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.norm_squared()
}
//pub fn lxNorm<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>, unsigned int Depth) -> N
// where DefaultAllocator: Alloc<N, D> {
// unimplemented!()

View File

@ -0,0 +1,10 @@
use na::{Real, U3};
use aliases::Vec;
/// The normal vector of the given triangle.
///
/// The normal is computed as the normalized vector `cross(p2 - p1, p3 - p1)`.
pub fn triangle_normal<N: Real>(p1: &Vec<N, U3>, p2: &Vec<N, U3>, p3: &Vec<N, U3>) -> Vec<N, U3> {
(p2 - p1).cross(&(p3 - p1)).normalize()
}

View File

@ -3,14 +3,15 @@ use na::{Real, DefaultAllocator};
use traits::{Dimension, Alloc};
use aliases::Vec;
pub fn fastNormalizeDot<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
/// The dot product of the normalized version of `x` and `y`.
pub fn fast_normalize_dot<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
// XXX: improve those.
x.normalize().dot(&y.normalize())
}
pub fn normalizeDot<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
/// The dot product of the normalized version of `x` and `y`.
pub fn normalize_dot<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
// XXX: improve those.
x.normalize().dot(&y.normalize())

View File

@ -0,0 +1,13 @@
use na::{Real, Rotation3, Unit, U3, U4};
use aliases::{Vec, Mat};
/// Builds a rotation 4 * 4 matrix created from a normalized axis and an angle.
///
/// # Parameters
/// * `m` - Input matrix multiplied by this rotation matrix.
/// * `angle` - Rotation angle expressed in radians.
/// * `axis` - Rotation axis, must be normalized.
pub fn rotate_normalized_axis<N: Real>(m: &Mat<N, U4, U4>, angle: N, axis: &Vec<N, U3>) -> Mat<N, U4, U4> {
m * Rotation3::from_axis_angle(&Unit::new_unchecked(*axis), angle).to_homogeneous()
}

View File

@ -2,6 +2,7 @@ use na::{Real, U2, U3, U4, Rotation3, Vector3, Unit, UnitComplex};
use aliases::{Vec, Mat};
/// Build the rotation matrix needed to align `normal` and `up`.
pub fn orientation<N: Real>(normal: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
if let Some(r) = Rotation3::rotation_between(normal, up) {
r.to_homogeneous()
@ -10,42 +11,52 @@ pub fn orientation<N: Real>(normal: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4,
}
}
/// Rotate a two dimensional vector.
pub fn rotate2<N: Real>(v: &Vec<N, U2>, angle: N) -> Vec<N, U2> {
UnitComplex::new(angle) * v
}
/// Rotate a three dimensional vector around an axis.
pub fn rotate<N: Real>(v: &Vec<N, U3>, angle: N, normal: &Vec<N, U3>) -> Vec<N, U3> {
Rotation3::from_axis_angle(&Unit::new_normalize(*normal), angle) * v
}
/// Rotate a thee dimensional vector in homogeneous coordinates around an axis.
pub fn rotate4<N: Real>(v: &Vec<N, U4>, angle: N, normal: &Vec<N, U3>) -> Vec<N, U4> {
Rotation3::from_axis_angle(&Unit::new_normalize(*normal), angle).to_homogeneous() * v
}
pub fn rotateX<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
/// Rotate a three dimensional vector around the `X` axis.
pub fn rotate_x<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
Rotation3::from_axis_angle(&Vector3::x_axis(), angle) * v
}
pub fn rotateX4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
/// Rotate a three dimensional vector in homogeneous coordinates around the `X` axis.
pub fn rotate_x4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
Rotation3::from_axis_angle(&Vector3::x_axis(), angle).to_homogeneous() * v
}
pub fn rotateY<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
/// Rotate a three dimensional vector around the `Y` axis.
pub fn rotate_y<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
Rotation3::from_axis_angle(&Vector3::y_axis(), angle) * v
}
pub fn rotateY4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
/// Rotate a three dimensional vector in homogeneous coordinates around the `Y` axis.
pub fn rotate_y4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
Rotation3::from_axis_angle(&Vector3::y_axis(), angle).to_homogeneous() * v
}
pub fn rotateZ<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
/// Rotate a three dimensional vector around the `Z` axis.
pub fn rotate_z<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
Rotation3::from_axis_angle(&Vector3::z_axis(), angle) * v
}
pub fn rotateZ4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
/// Rotate a three dimensional vector in homogeneous coordinates around the `Z` axis.
pub fn rotate_z4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
Rotation3::from_axis_angle(&Vector3::z_axis(), angle).to_homogeneous() * v
}
/// Computes a spehical linear interpolation between the vectors `x` and `y` assumed to be normalized.
pub fn slerp<N: Real>(x: &Vec<N, U3>, y: &Vec<N, U3>, a: N) -> Vec<N, U3> {
unimplemented!()
Unit::new_unchecked(*x).slerp(&Unit::new_unchecked(*y), a).unwrap()
}

View File

@ -3,15 +3,17 @@ use na::{Real, Unit, Rotation3, Matrix4, U3, U4};
use traits::Number;
use aliases::{Vec, Mat};
/// Builds a rotation 4 * 4 matrix created from an axis of 3 scalars and an angle expressed in radians.
pub fn rotate<N: Real>(angle: N, v: &Vec<N, U3>) -> Mat<N, U4, U4> {
Rotation3::from_axis_angle(&Unit::new_normalize(*v), angle).to_homogeneous()
}
/// Transforms a matrix with a scale 4 * 4 matrix created from a vector of 3 components.
pub fn scale<N: Number>(v: &Vec<N, U3>) -> Mat<N, U4, U4> {
Matrix4::new_nonuniform_scaling(v)
}
/// Transforms a matrix with a translation 4 * 4 matrix created from 3 scalars.
pub fn translate<N: Number>(v: &Vec<N, U3>) -> Mat<N, U4, U4> {
Matrix4::new_translation(v)
}

View File

@ -1,9 +1,10 @@
use na::{self, U2, U3, U4, Matrix3, Matrix4};
use na::{U2, U3, U4, Matrix3, Matrix4};
use traits::Number;
use aliases::{Mat, Vec};
pub fn proj2D<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3, U3> {
/// Build planar projection matrix along normal axis and right-multiply it to `m`.
pub fn proj2<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3, U3> {
let mut res = Matrix3::identity();
{
@ -11,10 +12,11 @@ pub fn proj2D<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3,
part -= normal * normal.transpose();
}
res
m * res
}
pub fn proj3D<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4, U4> {
/// Build planar projection matrix along normal axis, and right-multiply it to `m`.
pub fn proj3<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4, U4> {
let mut res = Matrix4::identity();
{
@ -22,10 +24,11 @@ pub fn proj3D<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4,
part -= normal * normal.transpose();
}
res
m * res
}
pub fn reflect2D<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3, U3> {
/// Builds a reflection matrix and right-multiply it to `m`.
pub fn reflect2<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U3, U3> {
let mut res = Matrix3::identity();
{
@ -33,10 +36,11 @@ pub fn reflect2D<N: Number>(m: &Mat<N, U3, U3>, normal: &Vec<N, U2>) -> Mat<N, U
part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
}
res
m * res
}
pub fn reflect3D<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4, U4> {
/// Builds a reflection matrix, and right-multiply it to `m`.
pub fn reflect3<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U4, U4> {
let mut res = Matrix4::identity();
{
@ -44,10 +48,11 @@ pub fn reflect3D<N: Number>(m: &Mat<N, U4, U4>, normal: &Vec<N, U3>) -> Mat<N, U
part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
}
res
m * res
}
pub fn scaleBias<N: Number>(scale: N, bias: N) -> Mat<N, U4, U4> {
/// Builds a scale-bias matrix.
pub fn scale_bias<N: Number>(scale: N, bias: N) -> Mat<N, U4, U4> {
let _0 = N::zero();
let _1 = N::one();
@ -59,11 +64,13 @@ pub fn scaleBias<N: Number>(scale: N, bias: N) -> Mat<N, U4, U4> {
)
}
pub fn scaleBias2<N: Number>(m: &Mat<N, U4, U4>, scale: N, bias: N) -> Mat<N, U4, U4> {
m * scaleBias(scale, bias)
/// Builds a scale-bias matrix, and right-multiply it to `m`.
pub fn scale_bias2<N: Number>(m: &Mat<N, U4, U4>, scale: N, bias: N) -> Mat<N, U4, U4> {
m * scale_bias(scale, bias)
}
pub fn shearX2D<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
/// Transforms a matrix with a shearing on X axis.
pub fn shear_x2<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
let _0 = N::zero();
let _1 = N::one();
@ -75,7 +82,8 @@ pub fn shearX2D<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
m * shear
}
pub fn shearX3D<N: Number>(m: &Mat<N, U4, U4>, y: N, z: N) -> Mat<N, U4, U4> {
/// Transforms a matrix with a shearing on Y axis.
pub fn shear_x3<N: Number>(m: &Mat<N, U4, U4>, y: N, z: N) -> Mat<N, U4, U4> {
let _0 = N::zero();
let _1 = N::one();
let shear = Matrix4::new(
@ -88,7 +96,8 @@ pub fn shearX3D<N: Number>(m: &Mat<N, U4, U4>, y: N, z: N) -> Mat<N, U4, U4> {
m * shear
}
pub fn shearY2D<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
/// Transforms a matrix with a shearing on Y axis.
pub fn shear_y2<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
let _0 = N::zero();
let _1 = N::one();
@ -100,7 +109,8 @@ pub fn shearY2D<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
m * shear
}
pub fn shearY3D<N: Number>(m: &Mat<N, U4, U4>, x: N, z: N) -> Mat<N, U4, U4> {
/// Transforms a matrix with a shearing on Y axis.
pub fn shear_y3<N: Number>(m: &Mat<N, U4, U4>, x: N, z: N) -> Mat<N, U4, U4> {
let _0 = N::zero();
let _1 = N::one();
let shear = Matrix4::new(
@ -113,7 +123,8 @@ pub fn shearY3D<N: Number>(m: &Mat<N, U4, U4>, x: N, z: N) -> Mat<N, U4, U4> {
m * shear
}
pub fn shearZ3D<N: Number>(m: &Mat<N, U4, U4>, x: N, y: N) -> Mat<N, U4, U4> {
/// Transforms a matrix with a shearing on Z axis.
pub fn shear_z3d<N: Number>(m: &Mat<N, U4, U4>, x: N, y: N) -> Mat<N, U4, U4> {
let _0 = N::zero();
let _1 = N::one();
let shear = Matrix4::new(

View File

@ -3,15 +3,18 @@ use na::{Real, U2, U3, UnitComplex, Matrix3};
use traits::Number;
use aliases::{Mat, Vec};
/// Builds a 2D rotation matrix from an angle and right-multiply it to `m`.
pub fn rotate<N: Real>(m: &Mat<N, U3, U3>, angle: N) -> Mat<N, U3, U3> {
m * UnitComplex::new(angle).to_homogeneous()
}
/// Builds a 2D scaling matrix and right-multiply it to `m`.
pub fn scale<N: Number>(m: &Mat<N, U3, U3>, v: &Vec<N, U2>) -> Mat<N, U3, U3> {
m.prepend_nonuniform_scaling(v)
}
pub fn shearX<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
/// Builds a 2D shearing matrix on the `X` axis and right-multiply it to `m`.
pub fn shear_x<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
let _0 = N::zero();
let _1 = N::one();
@ -23,7 +26,8 @@ pub fn shearX<N: Number>(m: &Mat<N, U3, U3>, y: N) -> Mat<N, U3, U3> {
m * shear
}
pub fn shearY<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
/// Builds a 2D shearing matrix on the `Y` axis and right-multiply it to `m`.
pub fn shear_y<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
let _0 = N::zero();
let _1 = N::one();
@ -35,6 +39,7 @@ pub fn shearY<N: Number>(m: &Mat<N, U3, U3>, x: N) -> Mat<N, U3, U3> {
m * shear
}
/// Builds a translation matrix and right-multiply it to `m`.
pub fn translate<N: Number>(m: &Mat<N, U3, U3>, v: &Vec<N, U2>) -> Mat<N, U3, U3> {
m.prepend_translation(v)
}

View File

@ -0,0 +1,19 @@
use na::{DefaultAllocator, Real};
use traits::{Dimension, Alloc};
use aliases::Vec;
/// The angle between two vectors.
pub fn angle<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.angle(y)
}
//pub fn oriented_angle<N: Real>(x: &Vec<N, U2>, y: &Vec<N, U2>) -> N {
// unimplemented!()
//}
//
//pub fn oriented_angle_ref<N: Real>(x: &Vec<N, U3>, y: &Vec<N, U3>, refv: &Vec<N, U3>) -> N {
// unimplemented!()
//}

View File

@ -3,34 +3,40 @@ use na::{Real, DefaultAllocator, U2, U3};
use traits::{Number, Dimension, Alloc};
use aliases::Vec;
/// Returns `true` if two vectors are collinear (up to an epsilon).
pub fn are_collinear<N: Number>(v0: &Vec<N, U3>, v1: &Vec<N, U3>, epsilon: N) -> bool {
is_null(&v0.cross(v1), epsilon)
}
/// Returns `true` if two 2D vectors are collinear (up to an epsilon).
pub fn are_collinear2<N: Number>(v0: &Vec<N, U2>, v1: &Vec<N, U2>, epsilon: N) -> bool {
abs_diff_eq!(v0.perp(v1), N::zero(), epsilon = epsilon)
}
/// Returns `true` if two vectors are orthogonal (up to an epsilon).
pub fn are_orthogonal<N: Number, D: Dimension>(v0: &Vec<N, D>, v1: &Vec<N, D>, epsilon: N) -> bool
where DefaultAllocator: Alloc<N, D> {
abs_diff_eq!(v0.dot(v1), N::zero(), epsilon = epsilon)
}
pub fn are_orthonormal<N: Number, D: Dimension>(v0: &Vec<N, D>, v1: &Vec<N, D>, epsilon: N) -> bool
where DefaultAllocator: Alloc<N, D> {
unimplemented!()
}
//pub fn are_orthonormal<N: Number, D: Dimension>(v0: &Vec<N, D>, v1: &Vec<N, D>, epsilon: N) -> bool
// where DefaultAllocator: Alloc<N, D> {
// unimplemented!()
//}
/// Returns `true` if all the components of `v` are zero (up to an epsilon).
pub fn is_comp_null<N: Number, D: Dimension>(v: &Vec<N, D>, epsilon: N) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
v.map(|x| abs_diff_eq!(x, N::zero(), epsilon = epsilon))
}
/// Returns `true` if `v` has a magnitude of 1 (up to an epsilon).
pub fn is_normalized<N: Real, D: Dimension>(v: &Vec<N, D>, epsilon: N) -> bool
where DefaultAllocator: Alloc<N, D> {
abs_diff_eq!(v.norm_squared(), N::one(), epsilon = epsilon * epsilon)
}
/// Returns `true` if `v` is zero (up to an epsilon).
pub fn is_null<N: Number, D: Dimension>(v: &Vec<N, D>, epsilon: N) -> bool
where DefaultAllocator: Alloc<N, D> {
abs_diff_eq!(*v, Vec::<N, D>::zeros(), epsilon = epsilon)

View File

@ -1,41 +0,0 @@
pub fn mat< 4, 4, T, defaultp > derivedEulerAngleX (T const &angleX, T const &angularVelocityX)
pub fn mat< 4, 4, T, defaultp > derivedEulerAngleY (T const &angleY, T const &angularVelocityY)
pub fn mat< 4, 4, T, defaultp > derivedEulerAngleZ (T const &angleZ, T const &angularVelocityZ)
pub fn mat< 4, 4, T, defaultp > eulerAngleX (T const &angleX)
pub fn mat< 4, 4, T, defaultp > eulerAngleXY (T const &angleX, T const &angleY)
pub fn mat< 4, 4, T, defaultp > eulerAngleXYX (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleXYZ (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleXZ (T const &angleX, T const &angleZ)
pub fn mat< 4, 4, T, defaultp > eulerAngleXZX (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleXZY (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleY (T const &angleY)
pub fn mat< 4, 4, T, defaultp > eulerAngleYX (T const &angleY, T const &angleX)
pub fn mat< 4, 4, T, defaultp > eulerAngleYXY (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleYXZ (T const &yaw, T const &pitch, T const &roll)
pub fn mat< 4, 4, T, defaultp > eulerAngleYZ (T const &angleY, T const &angleZ)
pub fn mat< 4, 4, T, defaultp > eulerAngleYZX (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleYZY (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleZ (T const &angleZ)
pub fn mat< 4, 4, T, defaultp > eulerAngleZX (T const &angle, T const &angleX)
pub fn mat< 4, 4, T, defaultp > eulerAngleZXY (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleZXZ (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleZY (T const &angleZ, T const &angleY)
pub fn mat< 4, 4, T, defaultp > eulerAngleZYX (T const &t1, T const &t2, T const &t3)
pub fn mat< 4, 4, T, defaultp > eulerAngleZYZ (T const &t1, T const &t2, T const &t3)
pub fn void extractEulerAngleXYX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleXYZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleXZX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleXZY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleYXY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleYXZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleYZX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleYZY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleZXY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleZXZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleZYX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn void extractEulerAngleZYZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3)
pub fn mat< 2, 2, T, defaultp > orientate2 (T const &angle)
pub fn mat< 3, 3, T, defaultp > orientate3 (T const &angle)
pub fn mat< 3, 3, T, Q > orientate3 (vec< 3, T, Q > const &angles)
pub fn mat< 4, 4, T, Q > orientate4 (vec< 3, T, Q > const &angles)
pub fn mat< 4, 4, T, defaultp > yawPitchRoll (T const &yaw, T const &pitch, T const &roll)

View File

@ -1,7 +0,0 @@
use na::{Real, U3};
use aliases::Vec;
pub fn triangleNormal<N: Real>(p1: &Vec<N, U3>, p2: &Vec<N, U3>, p3: &Vec<N, U3>) -> Vec<N, U3> {
(p2 - p1).cross(&(p3 - p1)).normalize()
}

View File

@ -1,16 +0,0 @@
pub fn vec< 3, T, Q > cross (qua< T, Q > const &q, vec< 3, T, Q > const &v)
pub fn vec< 3, T, Q > cross (vec< 3, T, Q > const &v, qua< T, Q > const &q)
pub fn T extractRealComponent (qua< T, Q > const &q)
pub fn qua< T, Q > fastMix (qua< T, Q > const &x, qua< T, Q > const &y, T const &a)
pub fn qua< T, Q > intermediate (qua< T, Q > const &prev, qua< T, Q > const &curr, qua< T, Q > const &next)
pub fn T length2 (qua< T, Q > const &q)
pub fn qua< T, Q > quat_identity ()
pub fn vec< 3, T, Q > rotate (qua< T, Q > const &q, vec< 3, T, Q > const &v)
pub fn vec< 4, T, Q > rotate (qua< T, Q > const &q, vec< 4, T, Q > const &v)
pub fn qua< T, Q > rotation (vec< 3, T, Q > const &orig, vec< 3, T, Q > const &dest)
pub fn qua< T, Q > shortMix (qua< T, Q > const &x, qua< T, Q > const &y, T const &a)
pub fn qua< T, Q > squad (qua< T, Q > const &q1, qua< T, Q > const &q2, qua< T, Q > const &s1, qua< T, Q > const &s2, T const &h)
pub fn mat< 3, 3, T, Q > toMat3 (qua< T, Q > const &x)
pub fn mat< 4, 4, T, Q > toMat4 (qua< T, Q > const &x)
pub fn qua< T, Q > toQuat (mat< 3, 3, T, Q > const &x)
pub fn qua< T, Q > toQuat (mat< 4, 4, T, Q > const &x)

View File

@ -1,11 +0,0 @@
use na::{Real, Rotation3, UnitQuaternion, Unit, U3, U4};
use aliases::{Vec, Mat, Qua};
pub fn rotateNormalizedAxis<N: Real>(m: &Mat<N, U4, U4>, angle: N, axis: &Vec<N, U3>) -> Mat<N, U4, U4> {
m * Rotation3::from_axis_angle(&Unit::new_unchecked(*axis), angle).to_homogeneous()
}
pub fn rotateNormalizedAxis2<N: Real>(q: &Qua<N>, angle: N, axis: &Vec<N, U3>) -> Qua<N> {
q * UnitQuaternion::from_axis_angle(&Unit::new_unchecked(*axis), angle).unwrap()
}

View File

@ -1,18 +0,0 @@
use na::{self, DefaultAllocator, Real, U2, U3};
use traits::{Dimension, Alloc};
use aliases::Vec;
pub fn angle<N: Real, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> N
where DefaultAllocator: Alloc<N, D> {
x.angle(y)
}
pub fn oriented_angle<N: Real>(x: &Vec<N, U2>, y: &Vec<N, U2>) -> N {
unimplemented!()
}
pub fn oriented_angle_ref<N: Real>(x: &Vec<N, U3>, y: &Vec<N, U3>, refv: &Vec<N, U3>) -> N {
unimplemented!()
}

View File

@ -1,3 +1,8 @@
#![allow(dead_code)]
extern crate num_traits as num;
#[macro_use]
extern crate approx;
@ -6,6 +11,7 @@ extern crate nalgebra as na;
pub use aliases::*;
pub use constructors::*;
pub use common::*;
pub use geometric::*;
pub use matrix::*;
pub use traits::*;
@ -13,61 +19,23 @@ pub use trigonometric::*;
pub use vector_relational::*;
pub use exponential::*;
pub use ext_vector_relational::*;
pub use gtx_component_wise::*;
pub use gtx::*;
pub use gtc::*;
pub use ext::*;
mod aliases;
pub mod constructors;
mod constructors;
mod common;
pub mod matrix;
pub mod geometric;
mod matrix;
mod geometric;
mod traits;
pub mod trigonometric;
pub mod vector_relational;
pub mod exponential;
pub mod integer;
pub mod packing;
pub mod ext_matrix_clip_space;
pub mod ext_matrix_projection;
pub mod ext_matrix_relationnal;
pub mod ext_matrix_transform;
pub mod ext_quaternion_common;
pub mod ext_quaternion_geometric;
pub mod ext_quaternion_transform;
pub mod ext_quaternion_trigonometric;
pub mod ext_quaternion_relational;
pub mod ext_scalar_common;
pub mod ext_scalar_constants;
pub mod ext_vector_common;
pub mod ext_vector_relational;
//pub mod gtc_bitfield;
pub mod gtc_constants;
pub mod gtc_epsilon;
//pub mod gtc_integer;
pub mod gtc_matrix_access;
pub mod gtc_matrix_inverse;
//pub mod gtc_packing;
pub mod gtc_quaternion;
//pub mod gtc_reciprocal;
//pub mod gtc_round;
pub mod gtc_type_ptr;
//pub mod gtc_ulp;
mod trigonometric;
mod vector_relational;
mod exponential;
//mod integer;
//mod packing;
pub mod gtx_component_wise;
//pub mod gtx_euler_angles;
pub mod gtx_exterior_product;
pub mod gtx_handed_coordinate_space;
pub mod gtx_matrix_cross_product;
pub mod gtx_matrix_operation;
pub mod gtx_norm;
pub mod gtx_normal;
pub mod gtx_normalize_dot;
//pub mod gtx_quaternion;
pub mod gtx_rotate_normalized_axis;
//pub mod gtx_rotate_vector;
pub mod gtx_transform;
//pub mod gtx_transform2;
//pub mod gtx_transform2d;
//pub mod gtx_vector_angle;
pub mod gtx_vector_query;
pub mod ext;
pub mod gtc;
pub mod gtx;
pub mod quat;

View File

@ -1,30 +1,33 @@
use num::Num;
use na::{Scalar, Real, DefaultAllocator, U1};
use na::{Scalar, Real, DefaultAllocator};
use traits::{Alloc, Dimension, Number};
use aliases::{Mat, Vec};
/// The determinant of the matrix `m`.
pub fn determinant<N: Real, D: Dimension>(m: &Mat<N, D, D>) -> N
where DefaultAllocator: Alloc<N, D, D> {
m.determinant()
}
/// The inverse of the matrix `m`.
pub fn inverse<N: Real, D: Dimension>(m: &Mat<N, D, D>) -> Mat<N, D, D>
where DefaultAllocator: Alloc<N, D, D> {
m.clone().try_inverse().unwrap_or(Mat::<N, D, D>::zeros())
}
/// Componentwise multiplication of two matrices.
pub fn matrix_comp_mult<N: Number, R: Dimension, C: Dimension>(x: &Mat<N, R, C>, y: &Mat<N, R, C>) -> Mat<N, R, C>
where DefaultAllocator: Alloc<N, R, C> {
x.component_mul(y)
}
/// Treats the first parameter `c` as a column vector and the second parameter `r` as a row vector and does a linear algebraic matrix multiply `c * r`.
pub fn outer_product<N: Number, R: Dimension, C: Dimension>(c: &Vec<N, R>, r: &Vec<N, C>) -> Mat<N, R, C>
where DefaultAllocator: Alloc<N, R, C> {
c * r.transpose()
}
/// The transpose of the matrix `m`.
pub fn transpose<N: Scalar, R: Dimension, C: Dimension>(x: &Mat<N, R, C>) -> Mat<N, C, R>
where DefaultAllocator: Alloc<N, R, C> {
x.transpose()

View File

@ -1,72 +1,87 @@
use na::{self, Real, U3, U4, UnitQuaternion, Vector3, Rotation3};
use na::{Real, U3, U4, UnitQuaternion, Vector3, Rotation3};
use aliases::{Qua, Vec, Mat};
/// Euler angles of the quaternion as (pitch, yaw, roll).
/// Euler angles of the quaternion `q` as (pitch, yaw, roll).
pub fn euler_angles<N: Real>(x: &Qua<N>) -> Vec<N, U3> {
let q = UnitQuaternion::new_unchecked(*x);
let a = q.to_euler_angles();
Vector3::new(a.2, a.1, a.0)
}
/// Componentwise `>` comparison between two quaternions.
pub fn greater_than<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
::greater_than(&x.coords, &y.coords)
}
/// Componentwise `>=` comparison between two quaternions.
pub fn greater_than_equal<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
::greater_than_equal(&x.coords, &y.coords)
}
/// Componentwise `<` comparison between two quaternions.
pub fn less_than<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
::less_than(&x.coords, &y.coords)
}
/// Componentwise `<=` comparison between two quaternions.
pub fn less_than_equal<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
::less_than_equal(&x.coords, &y.coords)
}
/// Convert a quaternion to a rotation matrix.
pub fn mat3_cast<N: Real>(x: Qua<N>) -> Mat<N, U3, U3> {
let q = UnitQuaternion::new_unchecked(x);
q.to_rotation_matrix().unwrap()
}
/// Convert a quaternion to a rotation matrix in homogeneous coordinates.
pub fn mat4_cast<N: Real>(x: Qua<N>) -> Mat<N, U4, U4> {
let q = UnitQuaternion::new_unchecked(x);
q.to_homogeneous()
}
/// Convert a rotation matrix to a quaternion.
pub fn quat_cast<N: Real>(x: Mat<N, U3, U3>) -> Qua<N> {
let rot = Rotation3::from_matrix_unchecked(x);
UnitQuaternion::from_rotation_matrix(&rot).unwrap()
}
/// Convert a rotation matrix in homogeneous coordinates to a quaternion.
pub fn quat_cast2<N: Real>(x: Mat<N, U4, U4>) -> Qua<N> {
quat_cast(x.fixed_slice::<U3, U3>(0, 0).into_owned())
}
/// Computes a right-handed look-at quaternion (equivalent to a right-handed look-at matrix).
pub fn quat_look_at<N: Real>(direction: &Vec<N, U3>, up: &Vec<N, U3>) -> Qua<N> {
quat_look_at_rh(direction, up)
}
/// Computes a left-handed look-at quaternion (equivalent to a left-handed look-at matrix).
pub fn quat_look_at_lh<N: Real>(direction: &Vec<N, U3>, up: &Vec<N, U3>) -> Qua<N> {
UnitQuaternion::look_at_lh(direction, up).unwrap()
}
/// Computes a right-handed look-at quaternion (equivalent to a right-handed look-at matrix).
pub fn quat_look_at_rh<N: Real>(direction: &Vec<N, U3>, up: &Vec<N, U3>) -> Qua<N> {
UnitQuaternion::look_at_rh(direction, up).unwrap()
}
/// The "roll" euler angle of the quaternion `x` assumed to be normalized.
pub fn roll<N: Real>(x: &Qua<N>) -> N {
// FIXME: optimize this.
euler_angles(x).z
}
/// The "yaw" euler angle of the quaternion `x` assumed to be normalized.
pub fn yaw<N: Real>(x: &Qua<N>) -> N {
// FIXME: optimize this.
euler_angles(x).y
}
/// The "pitch" euler angle of the quaternion `x` assumed to be normalized.
pub fn pitch<N: Real>(x: &Qua<N>) -> N {
// FIXME: optimize this.
euler_angles(x).x

View File

@ -0,0 +1,91 @@
use na::{Real, Unit, Rotation3, Vector4, UnitQuaternion, U3, U4};
use aliases::{Qua, Vec, Mat};
/// Rotate the vector `v` by the quaternion `q` assumed to be normalized.
pub fn cross<N: Real>(q: &Qua<N>, v: &Vec<N, U3>) -> Vec<N, U3> {
UnitQuaternion::new_unchecked(*q) * v
}
/// Rotate the vector `v` by the inverse of the quaternion `q` assumed to be normalized.
pub fn cross2<N: Real>(v: &Vec<N, U3>, q: &Qua<N>) -> Vec<N, U3> {
UnitQuaternion::new_unchecked(*q).inverse() * v
}
/// The quaternion `w` component.
pub fn extract_real_component<N: Real>(q: &Qua<N>) -> N {
q.w
}
/// Normalized linear interpolation between two quaternions.
pub fn fast_mix<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
Unit::new_unchecked(*x).nlerp(&Unit::new_unchecked(*y), a).unwrap()
}
//pub fn intermediate<N: Real>(prev: &Qua<N>, curr: &Qua<N>, next: &Qua<N>) -> Qua<N> {
// unimplemented!()
//}
/// The squared magnitude of a quaternion `q`.
pub fn length2<N: Real>(q: &Qua<N>) -> N {
q.norm_squared()
}
/// The squared magnitude of a quaternion `q`.
pub fn magnitude2<N: Real>(q: &Qua<N>) -> N {
q.norm_squared()
}
/// The quaternion representing the identity rotation.
pub fn quat_identity<N: Real>() -> Qua<N> {
UnitQuaternion::identity().unwrap()
}
/// Rotates a vector by a quaternion assumed to be normalized.
pub fn rotate<N: Real>(q: &Qua<N>, v: &Vec<N, U3>) -> Vec<N, U3> {
UnitQuaternion::new_unchecked(*q) * v
}
/// Rotates a vector in homogeneous coordinates by a quaternion assumed to be normalized.
pub fn rotate2<N: Real>(q: &Qua<N>, v: &Vec<N, U4>) -> Vec<N, U4> {
// UnitQuaternion::new_unchecked(*q) * v
let rotated = Unit::new_unchecked(*q) * v.fixed_rows::<U3>(0);
Vector4::new(rotated.x, rotated.y, rotated.z, v.w)
}
/// The rotation required to align `orig` to `dest`.
pub fn rotation<N: Real>(orig: &Vec<N, U3>, dest: &Vec<N, U3>) -> Qua<N> {
UnitQuaternion::rotation_between(orig, dest).unwrap_or(UnitQuaternion::identity()).unwrap()
}
/// The spherical linear interpolation between two quaternions.
pub fn short_mix<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
Unit::new_normalize(*x).slerp(&Unit::new_normalize(*y), a).unwrap()
}
//pub fn squad<N: Real>(q1: &Qua<N>, q2: &Qua<N>, s1: &Qua<N>, s2: &Qua<N>, h: N) -> Qua<N> {
// unimplemented!()
//}
/// Converts a quaternion to a rotation matrix.
pub fn to_mat3<N: Real>(x: &Qua<N>) -> Mat<N, U3, U3> {
UnitQuaternion::new_unchecked(*x).to_rotation_matrix().unwrap()
}
/// Converts a quaternion to a rotation matrix in homogenous coordinates.
pub fn to_mat4<N: Real>(x: &Qua<N>) -> Mat<N, U4, U4> {
UnitQuaternion::new_unchecked(*x).to_homogeneous()
}
/// Converts a rotation matrix to a quaternion.
pub fn to_quat<N: Real>(x: &Mat<N, U3, U3>) -> Qua<N> {
let r = Rotation3::from_matrix_unchecked(*x);
UnitQuaternion::from_rotation_matrix(&r).unwrap()
}
/// Converts a rotation matrix in homogeneous coordinates to a quaternion.
pub fn to_quat2<N: Real>(x: &Mat<N, U4, U4>) -> Qua<N> {
let rot = x.fixed_slice::<U3, U3>(0, 0).into_owned();
to_quat(&rot)
}

View File

@ -0,0 +1,13 @@
use na::{Real, Unit, UnitQuaternion, U3};
use aliases::{Vec, Qua};
/// Rotates a quaternion from a vector of 3 components normalized axis and an angle.
///
/// # Parameters
/// * `q` - Source orientation
/// * `angle` - Angle expressed in radians.
/// * `axis` - Normalized axis of the rotation, must be normalized.
pub fn rotate_normalized_axis<N: Real>(q: &Qua<N>, angle: N, axis: &Vec<N, U3>) -> Qua<N> {
q * UnitQuaternion::from_axis_angle(&Unit::new_unchecked(*axis), angle).unwrap()
}

View File

@ -0,0 +1,19 @@
//! Definition and operations on quaternions.
pub use self::gtc_quaternion::*;
pub use self::gtx_quaternion::*;
pub use self::gtx_rotate_normalized_axis::*;
pub use self::quaternion_common::*;
pub use self::quaternion_geometric::*;
pub use self::quaternion_relational::*;
pub use self::quaternion_transform::*;
pub use self::quaternion_trigonometric::*;
mod gtc_quaternion;
mod gtx_quaternion;
mod gtx_rotate_normalized_axis;
mod quaternion_common;
mod quaternion_geometric;
mod quaternion_relational;
mod quaternion_transform;
mod quaternion_trigonometric;

View File

@ -1,11 +1,13 @@
use na::{self, Real, Unit, U4};
use na::{self, Real, Unit};
use aliases::{Vec, Qua};
use aliases::Qua;
/// The conjugate of `q`.
pub fn conjugate<N: Real>(q: &Qua<N>) -> Qua<N> {
q.conjugate()
}
/// The inverse of `q`.
pub fn inverse<N: Real>(q: &Qua<N>) -> Qua<N> {
q.try_inverse().unwrap_or(na::zero())
}
@ -18,14 +20,16 @@ pub fn inverse<N: Real>(q: &Qua<N>) -> Qua<N> {
// x.coords.map(|e| e.is_nan())
//}
/// Interpolate linearly between `x` and `y`.
pub fn lerp<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
x.lerp(y, a)
}
pub fn mix<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
x * (N::one() - a) + y * a
}
//pub fn mix<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
// x * (N::one() - a) + y * a
//}
/// Interpolate spherically between `x` and `y`.
pub fn slerp<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
Unit::new_normalize(*x).slerp(&Unit::new_normalize(*y), a).unwrap()
}

View File

@ -2,15 +2,27 @@ use na::Real;
use aliases::Qua;
/// Multiplies two quaternions.
pub fn cross<N: Real>(q1: &Qua<N>, q2: &Qua<N>) -> Qua<N> {
q1 * q2
}
/// The scalar product of two quaternions.
pub fn dot<N: Real>(x: &Qua<N>, y: &Qua<N>) -> N {
x.dot(y)
}
/// The magnitude of the quaternion `q`.
pub fn length<N: Real>(q: &Qua<N>) -> N {
q.norm()
}
/// The magnitude of the quaternion `q`.
pub fn magnitude<N: Real>(q: &Qua<N>) -> N {
q.norm()
}
/// Normalizes the quaternion `q`.
pub fn normalize<N: Real>(q: &Qua<N>) -> Qua<N> {
q.normalize()
}

View File

@ -3,18 +3,22 @@ use na::{Real, U4};
use aliases::{Qua, Vec};
/// Componentwise equality comparison between two quaternions.
pub fn equal<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
::equal(&x.coords, &y.coords)
}
/// Componentwise approximate equality comparison between two quaternions.
pub fn equal_eps<N: Real>(x: &Qua<N>, y: &Qua<N>, epsilon: N) -> Vec<bool, U4> {
::equal_eps(&x.coords, &y.coords, epsilon)
}
/// Componentwise non-equality comparison between two quaternions.
pub fn not_equal<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
::not_equal(&x.coords, &y.coords)
}
/// Componentwise approximate non-equality comparison between two quaternions.
pub fn not_equal_eps<N: Real>(x: &Qua<N>, y: &Qua<N>, epsilon: N) -> Vec<bool, U4> {
::not_equal_eps(&x.coords, &y.coords, epsilon)
}

View File

@ -2,22 +2,26 @@ use na::{Real, U3, UnitQuaternion, Unit};
use aliases::{Vec, Qua};
/// Computes the quaternion exponential.
pub fn exp<N: Real>(q: &Qua<N>) -> Qua<N> {
q.exp()
}
/// Computes the quaternion logarithm.
pub fn log<N: Real>(q: &Qua<N>) -> Qua<N> {
q.ln()
}
/// Raises the quaternion `q` to the power `y`.
pub fn pow<N: Real>(q: &Qua<N>, y: N) -> Qua<N> {
q.powf(y)
}
/// Builds a quaternion from an axis and an angle, and right-multiply it to the quaternion `q`.
pub fn rotate<N: Real>(q: &Qua<N>, angle: N, axis: &Vec<N, U3>) -> Qua<N> {
q * UnitQuaternion::from_axis_angle(&Unit::new_normalize(*axis), angle).unwrap()
}
pub fn sqrt<N: Real>(q: &Qua<N>) -> Qua<N> {
unimplemented!()
}
//pub fn sqrt<N: Real>(q: &Qua<N>) -> Qua<N> {
// unimplemented!()
//}

View File

@ -2,14 +2,17 @@ use na::{Real, U3, Unit, UnitQuaternion, Vector3};
use aliases::{Vec, Qua};
/// The rotation angle of this quaternion assumed to be normalized.
pub fn angle<N: Real>(x: &Qua<N>) -> N {
UnitQuaternion::from_quaternion(*x).angle()
}
pub fn angleAxis<N: Real>(angle: N, axis: &Vec<N, U3>) -> Qua<N> {
/// Creates a quaternion from an axis and an angle.
pub fn angle_axis<N: Real>(angle: N, axis: &Vec<N, U3>) -> Qua<N> {
UnitQuaternion::from_axis_angle(&Unit::new_normalize(*axis), angle).unwrap()
}
/// The rotation axis of a quaternion assumed to be normalized.
pub fn axis<N: Real>(x: &Qua<N>) -> Vec<N, U3> {
if let Some(a) = UnitQuaternion::from_quaternion(*x).axis() {
a.unwrap()

View File

@ -1,4 +1,3 @@
use std::cmp::{PartialOrd, PartialEq};
use num::{Signed, FromPrimitive, Bounded};
use approx::AbsDiffEq;
@ -6,10 +5,12 @@ use alga::general::{Ring, Lattice};
use na::{Scalar, DimName, DimMin, U1};
use na::allocator::Allocator;
/// A type-level number representing a vector, matrix row, or matrix column, dimension.
pub trait Dimension: DimName + DimMin<Self, Output = Self> {}
impl<D: DimName + DimMin<D, Output = Self>> Dimension for D {}
/// A number that can either be an integer or a float.
pub trait Number: Scalar + Ring + Lattice + AbsDiffEq<Epsilon = Self> + Signed + FromPrimitive + Bounded {
}

View File

@ -3,76 +3,92 @@ use na::{self, Real, DefaultAllocator};
use aliases::Vec;
use traits::{Alloc, Dimension};
/// Componentwise arc-cosinus.
pub fn acos<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|e| e.acos())
}
/// Componentwise hyperbolic arc-cosinus.
pub fn acosh<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|e| e.acosh())
}
/// Componentwise arc-sinus.
pub fn asin<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|e| e.asin())
}
/// Componentwise hyperbolic arc-sinus.
pub fn asinh<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|e| e.asinh())
}
/// Componentwise arc-tangent of `y / x`.
pub fn atan2<N: Real, D: Dimension>(y: &Vec<N, D>, x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
y.zip_map(x, |y, x| y.atan2(x))
}
/// Componentwise arc-tangent.
pub fn atan<N: Real, D: Dimension>(y_over_x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
y_over_x.map(|e| e.atan())
}
/// Componentwise hyperbolic arc-tangent.
pub fn atanh<N: Real, D: Dimension>(x: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
x.map(|e| e.atanh())
}
/// Componentwise cosinus.
pub fn cos<N: Real, D: Dimension>(angle: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
angle.map(|e| e.cos())
}
/// Componentwise hyperbolic cosinus.
pub fn cosh<N: Real, D: Dimension>(angle: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
angle.map(|e| e.cosh())
}
/// Componentwise conversion from radians to degrees.
pub fn degrees<N: Real, D: Dimension>(radians: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
radians.map(|e| e * na::convert(180.0) / N::pi())
}
/// Componentwise conversion fro degrees to radians.
pub fn radians<N: Real, D: Dimension>(degrees: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
degrees.map(|e| e * N::pi() / na::convert(180.0))
}
/// Componentwise sinus.
pub fn sin<N: Real, D: Dimension>(angle: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
angle.map(|e| e.sin())
}
/// Componentwise hyperbolic sinus.
pub fn sinh<N: Real, D: Dimension>(angle: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
angle.map(|e| e.sinh())
}
/// Componentwise tangent.
pub fn tan<N: Real, D: Dimension>(angle: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
angle.map(|e| e.tan())
}
/// Componentwise hyperbolic tangent.
pub fn tanh<N: Real, D: Dimension>(angle: &Vec<N, D>) -> Vec<N, D>
where DefaultAllocator: Alloc<N, D> {
angle.map(|e| e.tanh())

View File

@ -1,49 +1,57 @@
use na::{self, Real, DefaultAllocator};
use na::{DefaultAllocator};
use aliases::Vec;
use traits::{Number, Alloc, Dimension};
/// Checks that all the vector components are `true`.
pub fn all<D: Dimension>(v: &Vec<bool, D>) -> bool
where DefaultAllocator: Alloc<bool, D> {
v.iter().all(|x| *x)
}
/// Checks that at least one of the vector components is `true`.
pub fn any<D: Dimension>(v: &Vec<bool, D>) -> bool
where DefaultAllocator: Alloc<bool, D> {
v.iter().any(|x| *x)
}
/// Componentwise equality comparison.
pub fn equal<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x == y)
}
/// Componentwise `>` comparison.
pub fn greater_than<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x > y)
}
/// Componentwise `>=` comparison.
pub fn greater_than_equal<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x >= y)
}
/// Componentwise `<` comparison.
pub fn less_than<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x < y)
}
/// Componentwise `>=` comparison.
pub fn less_than_equal<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x <= y)
}
/// Componentwise not `!`.
pub fn not<D: Dimension>(v: &Vec<bool, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<bool, D> {
v.map(|x| !x)
}
/// Componentwise not-equality `!=`.
pub fn not_equal<N: Number, D: Dimension>(x: &Vec<N, D>, y: &Vec<N, D>) -> Vec<bool, D>
where DefaultAllocator: Alloc<N, D> {
x.zip_map(y, |x, y| x != y)