cgmath/src/vec.rs

484 lines
11 KiB
Rust
Raw Normal View History

use core::cmp::Eq;
use std::cmp::FuzzyEq;
2013-01-27 22:22:15 +00:00
use numeric::Number;
2012-12-13 13:01:42 +00:00
pub use vec2::{Vec2, vec2, dvec2, bvec2, ivec2, uvec2};
pub use vec3::{Vec3, vec3, dvec3, bvec3, ivec3, uvec3};
pub use vec4::{Vec4, vec4, dvec4, bvec4, ivec4, uvec4};
2012-12-13 13:01:42 +00:00
2012-12-03 16:32:40 +00:00
/**
* The base generic vector trait.
*
* # Type parameters
*
* * `T` - The type of the components. This is intended to support boolean,
* integer, unsigned integer, and floating point types.
2012-12-03 16:32:40 +00:00
*/
pub trait Vector<T>: Index<uint, T> + Eq {
/**
* Construct the vector from a single value, copying it to each component
*/
2013-03-28 10:35:51 +00:00
static fn from_value(value: T) -> Self;
2012-12-12 01:29:35 +00:00
/**
* # Return value
*
* A pointer to the first component of the vector
*/
2013-03-28 10:35:51 +00:00
fn to_ptr(&self) -> *T;
}
pub trait MutableVector<T>: Vector<T> {
/**
* Get a mutable reference to the component at `i`
*/
fn index_mut(&mut self, i: uint) -> &self/mut T;
2012-12-04 07:58:03 +00:00
/**
* Swap two components of the vector in place
*/
fn swap(&mut self, a: uint, b: uint);
}
2012-12-03 16:32:40 +00:00
/**
* A generic 2-dimensional vector
*/
2012-11-21 04:08:36 +00:00
pub trait Vector2<T>: Vector<T> {
2013-03-28 10:35:51 +00:00
static fn new(x: T, y: T) -> Self;
2012-11-21 04:08:36 +00:00
}
2012-12-03 16:32:40 +00:00
/**
* A generic 3-dimensional vector
*/
2012-11-21 04:08:36 +00:00
pub trait Vector3<T>: Vector<T> {
2013-03-28 10:35:51 +00:00
static fn new(x: T, y: T, z: T) -> Self;
2012-11-21 04:08:36 +00:00
}
2012-12-03 16:32:40 +00:00
/**
* A generic 4-dimensional vector
*/
2012-11-21 04:08:36 +00:00
pub trait Vector4<T>: Vector<T> {
2013-03-28 10:35:51 +00:00
static fn new(x: T, y: T, z: T, w: T) -> Self;
2012-11-21 04:08:36 +00:00
}
2012-12-03 16:32:40 +00:00
/**
* A vector with numeric components
*/
pub trait NumericVector<T>: Vector<T> + Neg<Self> {
2012-12-03 16:32:40 +00:00
/**
2012-12-05 02:10:05 +00:00
* The standard basis vector
*
2012-12-05 08:09:53 +00:00
* # Return value
2012-12-05 02:10:05 +00:00
*
* A vector with each component set to one
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
static fn identity() -> Self;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 02:10:05 +00:00
* The null vector
*
2012-12-05 08:09:53 +00:00
* # Return value
2012-12-05 02:10:05 +00:00
*
* A vector with each component set to zero
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
static fn zero() -> Self;
2012-12-24 03:46:25 +00:00
/**
* # Return value
*
* True if the vector is equal to zero
*/
2013-03-28 10:35:51 +00:00
fn is_zero(&self) -> bool;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The scalar multiplication of the vector and `value`
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn mul_t(&self, value: T) -> Self;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The scalar division of the vector and `value`
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn div_t(&self, value: T) -> Self;
2012-12-03 16:32:40 +00:00
/**
* Component-wise vector addition
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn add_v(&self, other: &Self) -> Self;
2012-12-03 16:32:40 +00:00
/**
* Component-wise vector subtraction
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn sub_v(&self, other: &Self) -> Self;
/**
* Component-wise vector multiplication
*/
2013-03-28 10:35:51 +00:00
fn mul_v(&self, other: &Self) -> Self;
/**
* Component-wise vector division
*/
2013-03-28 10:35:51 +00:00
fn div_v(&self, other: &Self) -> Self;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The dot product of the vector and `other`
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn dot(&self, other: &Self) -> T;
}
2013-01-27 22:50:49 +00:00
/**
* A 2-dimensional vector with numeric components
*/
pub trait NumericVector2<T>: NumericVector<T> {
2013-03-28 10:35:51 +00:00
static fn unit_x() -> Self;
static fn unit_y() -> Self;
2013-01-27 22:50:49 +00:00
/**
* # Return value
*
* The perp dot product of the vector and `other`
*/
2013-03-28 10:35:51 +00:00
fn perp_dot(&self, other: &Self) -> T;
2013-01-27 22:50:49 +00:00
}
/**
* A 3-dimensional vector with numeric components
*/
pub trait NumericVector3<T>: NumericVector<T> {
2013-03-28 10:35:51 +00:00
static fn unit_x() -> Self;
static fn unit_y() -> Self;
static fn unit_z() -> Self;
2013-01-27 22:50:49 +00:00
/**
* # Return value
*
* The cross product of the vector and `other`
*/
2013-03-28 10:35:51 +00:00
fn cross(&self, other: &Self) -> Self;
2013-01-27 22:50:49 +00:00
}
/**
* A 4-dimensional vector with numeric components
*/
pub trait NumericVector4<T>: NumericVector<T> {
2013-03-28 10:35:51 +00:00
static fn unit_x() -> Self;
static fn unit_y() -> Self;
static fn unit_z() -> Self;
static fn unit_w() -> Self;
2013-01-27 22:50:49 +00:00
}
/**
* A mutable vector with numeric components
*/
pub trait MutableNumericVector<T>: MutableVector<&self/T> +
2012-12-13 13:01:42 +00:00
NumericVector<T> {
/**
* Negate the vector
*/
fn neg_self(&mut self);
/**
* Multiply the vector by a scalar
*/
fn mul_self_t(&mut self, value: T);
/**
* Divide the vector by a scalar
*/
fn div_self_t(&mut self, value: T);
/**
* Set the vector to the component-wise vector sum
*/
2013-01-29 10:23:22 +00:00
fn add_self_v(&mut self, other: &Self);
/**
* Set the vector to the component-wise vector difference
*/
2013-01-29 10:23:22 +00:00
fn sub_self_v(&mut self, other: &Self);
/**
* Set the vector to the component-wise vector product
*/
2013-01-29 10:23:22 +00:00
fn mul_self_v(&mut self, other: &Self);
/**
* Set the vector to the component-wise vector quotient
*/
2013-01-29 10:23:22 +00:00
fn div_self_v(&mut self, other: &Self);
}
2012-12-04 15:34:41 +00:00
/**
* A mutable 3-dimensional vector with numeric components
*/
pub trait MutableNumericVector3<T>: MutableNumericVector<&self/T> {
/**
* Set to the cross product of the vector and `other`
*/
2013-01-29 10:23:22 +00:00
fn cross_self(&mut self, other: &Self);
2012-12-04 15:34:41 +00:00
}
2013-01-02 06:59:24 +00:00
pub trait ToHomogeneous<H> {
/**
* Convert to a homogenous coordinate
*/
2013-03-28 10:35:51 +00:00
fn to_homogeneous(&self) -> H;
2013-01-02 06:59:24 +00:00
}
2012-12-03 16:32:40 +00:00
/**
2012-12-05 02:10:05 +00:00
* A Euclidean (or Affine) vector
*
* # Type parameters
*
* * `T` - The type of the components. This should be a floating point type.
2012-12-03 16:32:40 +00:00
*/
2012-12-05 02:10:05 +00:00
pub trait EuclideanVector<T>: NumericVector<T> {
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The squared length of the vector. This is useful for comparisons where
* the exact length does not need to be calculated.
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn length2(&self) -> T;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The length of the vector
*
* # Performance notes
*
* For instances where the exact length of the vector does not need to be
* known, for example for quaternion-quaternion length comparisons,
* it is advisable to use the `length2` method instead.
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn length(&self) -> T;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The squared distance between the vector and `other`.
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn distance2(&self, other: &Self) -> T;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The distance between the vector and `other`
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn distance(&self, other: &Self) -> T;
/**
* # Return value
*
2013-01-27 22:22:15 +00:00
* The angle between the vector and `other` in radians
*/
2013-03-28 10:35:51 +00:00
fn angle(&self, other: &Self) -> T;
2012-12-03 16:32:40 +00:00
/**
2012-12-05 08:09:53 +00:00
* # Return value
*
* The normalized vector
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn normalize(&self) -> Self;
/**
* Set the length of the vector whilst preserving the direction
*/
2013-03-28 10:35:51 +00:00
fn normalize_to(&self, length: T) -> Self;
2012-12-03 16:32:40 +00:00
/**
2012-12-03 22:31:26 +00:00
* Linearly intoperlate between the vector and `other`
2012-12-05 08:09:53 +00:00
*
* # Return value
*
* The intoperlated vector
2012-12-03 16:32:40 +00:00
*/
2013-03-28 10:35:51 +00:00
fn lerp(&self, other: &Self, amount: T) -> Self;
}
2012-12-05 02:10:05 +00:00
/**
* A mutable Euclidean (or Affine) vector
*
* # Type parameters
*
* * `T` - The type of the components. This should be a floating point type.
*/
pub trait MutableEuclideanVector<T>: MutableNumericVector<&self/T> +
2012-12-05 02:10:05 +00:00
EuclideanVector<T> {
/**
* Normalize the vector
*/
fn normalize_self(&mut self);
/**
* Set the vector to a specified length whilst preserving the direction
*/
fn normalize_self_to(&mut self, length: T);
/**
* Linearly intoperlate the vector towards `other`
*/
2013-01-29 10:23:22 +00:00
fn lerp_self(&mut self, other: &Self, amount: T);
}
/**
* Component-wise vector comparison methods
2012-12-17 06:09:00 +00:00
*
* The methods contained in this trait correspond to the relational functions
* mentioned in Section 8.7 of the [GLSL 4.30.6 specification]
* (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
*/
pub trait OrdinalVector<T, BoolVec>: Vector<T> {
/**
* Component-wise compare of `self < other`
*/
2013-03-28 10:35:51 +00:00
fn less_than(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self <= other`
*/
2013-03-28 10:35:51 +00:00
fn less_than_equal(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self > other`
*/
2013-03-28 10:35:51 +00:00
fn greater_than(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self >= other`
*/
2013-03-28 10:35:51 +00:00
fn greater_than_equal(&self, other: &Self) -> BoolVec;
}
/**
* Component-wise equality comparison methods
2012-12-17 06:09:00 +00:00
*
* The methods contained in this trait correspond to the relational functions
* mentioned in Section 8.7 of the [GLSL 4.30.6 specification]
* (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
*/
pub trait EquableVector<T, BoolVec>: Vector<T> {
/**
* Component-wise compare of `self == other`
*/
2013-03-28 10:35:51 +00:00
fn equal(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self != other`
*/
2013-03-28 10:35:51 +00:00
fn not_equal(&self, other: &Self) -> BoolVec;
}
/**
* A vector with boolean components
2012-12-17 06:09:00 +00:00
*
* The methods contained in this trait correspond to the relational functions
* mentioned in Section 8.7 of the [GLSL 4.30.6 specification]
* (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
*/
pub trait BooleanVector: Vector<bool> {
/**
* # Return value
*
* `true` if of any component is `true`
*/
2013-03-28 10:35:51 +00:00
fn any(&self) -> bool;
/**
* # Return value
*
* `true` only if all components are `true`
*/
2013-03-28 10:35:51 +00:00
fn all(&self) -> bool;
/**
* # Return value
*
* the component-wise logical complement
*/
2013-03-28 10:35:51 +00:00
fn not(&self) -> Self;
2013-02-09 22:42:06 +00:00
}
pub trait TrigVec<T>: Vector<T> {
2013-03-28 10:35:51 +00:00
fn radians(&self) -> Self;
fn degrees(&self) -> Self;
2013-02-09 22:42:06 +00:00
// Triganometric functions
2013-03-28 10:35:51 +00:00
fn sin(&self) -> Self;
fn cos(&self) -> Self;
fn tan(&self) -> Self;
2013-02-09 22:42:06 +00:00
// Inverse triganometric functions
2013-03-28 10:35:51 +00:00
fn asin(&self) -> Self;
fn acos(&self) -> Self;
fn atan(&self) -> Self;
fn atan2(&self, other: Self) -> Self;
2013-02-09 22:42:06 +00:00
// Hyperbolic triganometric functions
2013-03-28 10:35:51 +00:00
fn sinh(&self) -> Self;
fn cosh(&self) -> Self;
fn tanh(&self) -> Self;
// fn asinh() -> Self;
// fn acosh() -> Self;
// fn atanh() -> Self;
2013-02-09 22:42:06 +00:00
}
pub trait ExpVec<T>: Vector<T> {
// Exponential functions
2013-03-28 10:35:51 +00:00
fn pow_t(&self, n: Self) -> Self;
fn pow_v(&self, n: T) -> Self;
fn exp(&self) -> Self;
fn exp2(&self) -> Self;
fn ln(&self) -> Self;
fn ln2(&self) -> Self;
fn sqrt(&self) -> Self;
fn inv_sqrt(&self) -> Self;
2013-02-09 22:42:06 +00:00
}
pub trait ApproxVec<T>: Vector<T> {
// Whole-number approximation functions
2013-03-28 10:35:51 +00:00
fn floor(&self) -> Self;
fn trunc(&self) -> Self;
fn round(&self) -> Self;
// fn round_even(&self) -> Self;
fn ceil(&self) -> Self;
fn fract(&self) -> Self;
2013-02-09 22:42:06 +00:00
}
pub trait SignedVec<T,BV>: Vector<T> {
2013-03-28 10:35:51 +00:00
fn is_positive(&self) -> BV;
fn is_negative(&self) -> BV;
fn is_nonpositive(&self) -> BV;
fn is_nonnegative(&self) -> BV;
fn abs(&self) -> Self;
fn sign(&self) -> Self;
fn copysign(&self, other: Self) -> Self;
2013-02-09 22:42:06 +00:00
}
pub trait ExtentVec<T>: Vector<T> {
2013-03-28 10:35:51 +00:00
fn min_v(&self, other: &Self) -> Self;
fn max_v(&self, other: &Self) -> Self;
fn clamp_v(&self, mn: &Self, mx: &Self) -> Self;
2013-03-28 10:35:51 +00:00
fn min_t(&self, other: T) -> Self;
fn max_t(&self, other: T) -> Self;
fn clamp_t(&self, mn: T, mx: T) -> Self;
2013-02-09 22:42:06 +00:00
}
pub trait MixVec<T>: Vector<T> {
// Functions for blending numbers together
2013-03-28 10:35:51 +00:00
fn mix(&self, other: Self, value: Self) -> Self;
fn smooth_step(&self, edge0: Self, edge1: Self) -> Self;
fn step(&self, edge: Self) -> Self;
}