cgmath/src/vec.rs

410 lines
9.3 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
*/
2012-12-12 01:29:35 +00:00
pub trait Vector<T>: Index<uint, T> Eq {
/**
* Construct the vector from a single value, copying it to each component
*/
static pure 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
*/
pure 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> {
static pure 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> {
static pure 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> {
static pure 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
*/
2012-11-20 06:57:32 +00:00
static pure 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
*/
2012-11-20 06:57:32 +00:00
static pure fn zero() -> self;
2012-12-24 03:46:25 +00:00
/**
* # Return value
*
* True if the vector is equal to zero
*/
pure 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
*/
pure 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
*/
pure 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
*/
pure 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
*/
pure fn sub_v(&self, other: &self) -> self;
/**
* Component-wise vector multiplication
*/
pure fn mul_v(&self, other: &self) -> self;
/**
* Component-wise vector division
*/
pure 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
*/
pure 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> {
static pure fn unit_x() -> self;
static pure fn unit_y() -> self;
/**
* # Return value
*
* The perp dot product of the vector and `other`
*/
pure fn perp_dot(&self, other: &self) -> T;
}
/**
* A 3-dimensional vector with numeric components
*/
pub trait NumericVector3<T>: NumericVector<T> {
static pure fn unit_x() -> self;
static pure fn unit_y() -> self;
static pure fn unit_z() -> self;
/**
* # Return value
*
* The cross product of the vector and `other`
*/
pure fn cross(&self, other: &self) -> self;
}
/**
* A 4-dimensional vector with numeric components
*/
pub trait NumericVector4<T>: NumericVector<T> {
static pure fn unit_x() -> self;
static pure fn unit_y() -> self;
static pure fn unit_z() -> self;
static pure fn unit_w() -> self;
}
/**
* A mutable vector with numeric components
*/
2012-12-13 13:01:42 +00:00
pub trait MutableNumericVector<T>: MutableVector<&self/T>
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
*/
fn add_self_v(&mut self, other: &self);
/**
* Set the vector to the component-wise vector difference
*/
fn sub_self_v(&mut self, other: &self);
/**
* Set the vector to the component-wise vector product
*/
fn mul_self_v(&mut self, other: &self);
/**
* Set the vector to the component-wise vector quotient
*/
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`
*/
fn cross_self(&mut self, other: &self);
}
2013-01-02 06:59:24 +00:00
pub trait ToHomogeneous<H> {
/**
* Convert to a homogenous coordinate
*/
pure fn to_homogeneous(&self) -> H;
}
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
*/
pure 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
*/
pure 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
*/
pure 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
*/
pure fn distance(&self, other: &self) -> T;
2012-12-03 16:32:40 +00:00
/**
* # Return value
*
2013-01-27 22:22:15 +00:00
* The angle between the vector and `other` in radians
*/
2013-01-27 22:22:15 +00:00
pure 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
*/
pure fn normalize(&self) -> self;
2012-12-03 16:32:40 +00:00
/**
* Set the length of the vector whilst preserving the direction
*/
pure 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
*/
pure 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`
*/
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`
*/
pure fn less_than(&self, other: &self) -> BoolVec;
/**
* Component-wise compare of `self <= other`
*/
pure fn less_than_equal(&self, other: &self) -> BoolVec;
/**
* Component-wise compare of `self > other`
*/
pure fn greater_than(&self, other: &self) -> BoolVec;
/**
* Component-wise compare of `self >= other`
*/
pure 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`
*/
pure fn equal(&self, other: &self) -> BoolVec;
/**
* Component-wise compare of `self != other`
*/
pure 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`
*/
pure fn any(&self) -> bool;
/**
* # Return value
*
* `true` only if all components are `true`
*/
pure fn all(&self) -> bool;
/**
* # Return value
*
* the component-wise logical complement
*/
pure fn not(&self) -> self;
}