cgmath/src/vec.rs

622 lines
14 KiB
Rust
Raw Normal View History

use std::cmp::FuzzyEq;
use cmp::Ord;
use num::Num;
use math::{Abs, min, max, Sqrt};
use to_str::ToStr;
2012-09-07 10:48:47 +00:00
//
// N-dimensional Vector
//
2012-09-29 08:41:48 +00:00
pub trait Vector<T> {
2012-09-08 05:54:31 +00:00
static pure fn dim() -> uint;
2012-09-07 10:48:47 +00:00
pure fn index(&&index:uint) -> T;
pure fn neg() -> self;
pure fn add_f(&&value:T) -> self;
pure fn sub_f(&&value:T) -> self;
pure fn mul_f(&&value:T) -> self;
pure fn div_f(&&value:T) -> self;
pure fn add_v(&&other: self) -> self;
pure fn sub_v(&&other: self) -> self;
2012-09-07 15:18:18 +00:00
pure fn dot(&&other: self) -> T;
2012-09-07 10:48:47 +00:00
pure fn exact_eq(&&other:self) -> bool;
pure fn fuzzy_eq(&&other:self) -> bool;
pure fn eq(&&other:self) -> bool;
pure fn magnitude2() -> T;
pure fn magnitude() -> T;
pure fn normalize() -> self;
2012-09-07 18:45:07 +00:00
pure fn lerp(&&other:self, &&value:T) -> self;
pure fn abs() -> self;
2012-09-08 05:54:31 +00:00
pure fn min(&&other:self) -> self;
pure fn max(&&other:self) -> self;
static pure fn zero() -> self;
static pure fn identity() -> self;
2012-09-07 10:48:47 +00:00
}
2012-09-07 18:45:07 +00:00
2012-09-08 05:54:31 +00:00
2012-09-29 08:41:48 +00:00
pub trait Vector2<T> {
// static pure fn _new(x:float, y:float) -> self;
2012-09-07 10:48:47 +00:00
// This is where I wish rust had properties ;)
pure fn x() -> T;
pure fn y() -> T;
2012-09-08 05:54:31 +00:00
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub trait Vector3<T> {
// error: duplicate function definition
// static pure fn _new(x:float, y:float, z:float) -> self;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
2012-09-07 10:48:47 +00:00
2012-09-08 05:54:31 +00:00
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
// static pure fn unit_z() -> self;
2012-09-07 10:48:47 +00:00
fn cross(&&other:self) -> self;
}
2012-09-29 08:41:48 +00:00
pub trait Vector4<T> {
// error: duplicate function definition
// static pure fn _new(x:float, y:float, z:float, w:float) -> self;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
pure fn w() -> T;
2012-09-08 05:54:31 +00:00
// static pure fn unit_x() -> self;
// static pure fn unit_y() -> self;
// static pure fn unit_z() -> self;
// static pure fn unit_w() -> self;
2012-09-07 10:48:47 +00:00
}
//
2012-09-07 18:49:52 +00:00
// Vec2
2012-09-07 10:48:47 +00:00
//
2012-09-29 08:41:48 +00:00
pub struct Vec2 { data:[float * 2] }
2012-09-07 10:48:47 +00:00
2012-09-29 08:41:48 +00:00
pub const vec2_zero :Vec2 = Vec2 { data: [ 0f, 0f ] };
pub const vec2_unit_x :Vec2 = Vec2 { data: [ 1f, 0f ] };
pub const vec2_unit_y :Vec2 = Vec2 { data: [ 0f, 1f ] };
pub const vec2_identity :Vec2 = Vec2 { data: [ 1f, 1f ] };
2012-09-08 05:54:31 +00:00
2012-09-07 10:48:47 +00:00
//
2012-09-07 18:49:52 +00:00
// Constructor
2012-09-07 10:48:47 +00:00
//
#[inline]
2012-09-29 08:41:48 +00:00
pub pure fn Vec2(x:float, y:float) -> Vec2 {
Vec2 { data: [ x, y ] }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec2: Vector2<float> {
// #[inline]
// static pure fn _new(x:float, y:float) -> Vec2 {
// Vec2 { data: [ x, y ] }
// }
#[inline] pure fn x() -> float { self.data[0] }
#[inline] pure fn y() -> float { self.data[1] }
// #[inline] static pure fn unit_x() -> Vec2 { Vec2(1f, 0f) }
// #[inline] static pure fn unit_y() -> Vec2 { Vec2(0f, 1f) }
// #[inline] static pure fn unit_z() -> Vec2 { Vec2(0f, 0f) }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec2: Vector<float> {
#[inline]
2012-09-08 05:54:31 +00:00
static pure fn dim() -> uint { 2 }
2012-09-07 10:48:47 +00:00
#[inline]
2012-09-07 10:48:47 +00:00
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline]
pure fn neg() -> Vec2 {
Vec2(-self[0], -self[1])
2012-09-07 10:48:47 +00:00
}
#[inline]
pure fn add_f(&&value:float) -> Vec2 {
Vec2(self[0] + value,
2012-09-07 10:48:47 +00:00
self[1] + value)
}
#[inline]
pure fn sub_f(&&value:float) -> Vec2 {
Vec2(self[0] - value,
2012-09-07 10:48:47 +00:00
self[1] - value)
}
#[inline]
pure fn mul_f(&&value:float) -> Vec2 {
Vec2(self[0] * value,
2012-09-07 10:48:47 +00:00
self[1] * value)
}
#[inline]
pure fn div_f(&&value:float) -> Vec2 {
Vec2(self[0] / value,
2012-09-07 10:48:47 +00:00
self[1] / value)
}
#[inline]
pure fn add_v(&&other: Vec2) -> Vec2{
Vec2(self[0] + other[0],
2012-09-07 10:48:47 +00:00
self[1] + other[1])
}
#[inline]
pure fn sub_v(&&other: Vec2) -> Vec2{
Vec2(self[0] - other[0],
2012-09-07 10:48:47 +00:00
self[1] - other[1])
}
#[inline]
pure fn dot(&&other: Vec2) -> float {
2012-09-07 15:18:18 +00:00
self[0] * other[0] +
self[1] * other[1]
}
#[inline]
pure fn exact_eq(&&other:Vec2) -> bool {
2012-09-07 10:48:47 +00:00
self[0] == other[0] &&
self[1] == other[1]
}
#[inline]
pure fn fuzzy_eq(&&other: Vec2) -> bool {
2012-09-07 10:48:47 +00:00
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1])
}
#[inline]
pure fn eq(&&other:Vec2) -> bool {
2012-09-07 10:48:47 +00:00
self.fuzzy_eq(other)
}
#[inline]
2012-09-07 10:48:47 +00:00
pure fn magnitude2() -> float {
self[0] * self[0] +
self[1] * self[1]
}
#[inline]
2012-09-07 10:48:47 +00:00
pure fn magnitude() -> float {
2012-09-10 02:55:15 +00:00
self.magnitude2().sqrt()
2012-09-07 10:48:47 +00:00
}
#[inline]
pure fn normalize() -> Vec2 {
let n = 1f / self.magnitude();
2012-09-07 10:48:47 +00:00
return self.mul_f(n);
}
2012-09-07 18:45:07 +00:00
#[inline]
pure fn lerp(&&other:Vec2, &&value:float) -> Vec2 {
2012-09-07 18:45:07 +00:00
self.add_v((other.sub_v(self)).mul_f(value))
}
#[inline]
pure fn abs() -> Vec2 {
Vec2(self[0].abs(),
2012-09-08 05:54:31 +00:00
self[1].abs())
2012-09-07 18:45:07 +00:00
}
#[inline]
pure fn min(&&other:Vec2) -> Vec2 {
Vec2(min(self[0], other[0]),
2012-09-08 05:54:31 +00:00
min(self[1], other[1]))
}
2012-09-07 18:45:07 +00:00
#[inline]
pure fn max(&&other:Vec2) -> Vec2 {
Vec2(max(self[0], other[0]),
2012-09-08 05:54:31 +00:00
max(self[1], other[1]))
}
#[inline] static pure fn zero() -> Vec2 { Vec2(1f, 1f) }
#[inline] static pure fn identity() -> Vec2 { Vec2(1f, 1f) }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec2: ToStr {
2012-09-07 10:48:47 +00:00
fn to_str() -> ~str {
fmt!("Vec2[ %f, %f ]", self[0], self[1])
2012-09-07 10:48:47 +00:00
}
}
//
2012-09-07 18:49:52 +00:00
// Vec3
2012-09-07 10:48:47 +00:00
//
2012-09-29 08:41:48 +00:00
pub struct Vec3 { data:[float * 3] }
2012-09-07 10:48:47 +00:00
2012-09-29 08:41:48 +00:00
pub const vec3_zero :Vec3 = Vec3 { data: [ 0f, 0f, 0f ] };
pub const vec3_unit_x :Vec3 = Vec3 { data: [ 1f, 0f, 0f ] };
pub const vec3_unit_y :Vec3 = Vec3 { data: [ 0f, 1f, 0f ] };
pub const vec3_unit_z :Vec3 = Vec3 { data: [ 0f, 0f, 1f ] };
pub const vec3_identity :Vec3 = Vec3 { data: [ 1f, 1f, 1f ] };
2012-09-07 10:48:47 +00:00
//
2012-09-07 18:49:52 +00:00
// Constructor
2012-09-07 10:48:47 +00:00
//
#[inline]
2012-09-29 08:41:48 +00:00
pub pure fn Vec3(x:float, y:float, z:float) -> Vec3 {
Vec3 { data: [ x, y, z ] }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec3: Vector3<float> {
// #[inline]
// static pure fn _new(x:float, y:float, z:float) -> Vec3 {
// Vec2 { data: [ x, y, z ] }
// }
#[inline] pure fn x() -> float { self.data[0] }
#[inline] pure fn y() -> float { self.data[1] }
#[inline] pure fn z() -> float { self.data[2] }
2012-09-07 10:48:47 +00:00
#[inline]
fn cross(&&other:Vec3) -> Vec3 {
Vec3((self[1] * other[2]) - (self[2] * other[1]),
2012-09-07 10:48:47 +00:00
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
}
// #[inline] static pure fn unit_x() -> Vec3 { Vec3(1f, 0f, 0f) }
// #[inline] static pure fn unit_y() -> Vec3 { Vec3(0f, 1f, 0f) }
// #[inline] static pure fn unit_z() -> Vec3 { Vec3(0f, 0f, 1f) }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec3: Vector<float> {
#[inline]
2012-09-08 05:54:31 +00:00
static pure fn dim() -> uint { 3 }
2012-09-07 10:48:47 +00:00
#[inline]
2012-09-07 10:48:47 +00:00
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline]
pure fn neg() -> Vec3 {
Vec3(-self[0], -self[1], -self[2])
2012-09-07 10:48:47 +00:00
}
#[inline]
pure fn add_f(&&value:float) -> Vec3 {
Vec3(self[0] + value,
2012-09-07 10:48:47 +00:00
self[1] + value,
self[2] + value)
}
#[inline]
pure fn sub_f(&&value:float) -> Vec3 {
Vec3(self[0] - value,
2012-09-07 10:48:47 +00:00
self[1] - value,
self[2] - value)
}
#[inline]
pure fn mul_f(&&value:float) -> Vec3 {
Vec3(self[0] * value,
2012-09-07 10:48:47 +00:00
self[1] * value,
self[2] * value)
}
#[inline]
pure fn div_f(&&value:float) -> Vec3 {
Vec3(self[0] / value,
2012-09-07 10:48:47 +00:00
self[1] / value,
self[2] / value)
}
#[inline]
pure fn add_v(&&other: Vec3) -> Vec3{
Vec3(self[0] + other[0],
2012-09-07 10:48:47 +00:00
self[1] + other[1],
self[2] + other[2])
}
#[inline]
pure fn sub_v(&&other: Vec3) -> Vec3{
Vec3(self[0] - other[0],
2012-09-07 10:48:47 +00:00
self[1] - other[1],
self[2] - other[2])
}
#[inline]
pure fn dot(&&other: Vec3) -> float {
2012-09-07 15:18:18 +00:00
self[0] * other[0] +
self[1] * other[1] +
self[2] * other[2]
}
#[inline]
pure fn exact_eq(&&other:Vec3) -> bool {
2012-09-07 10:48:47 +00:00
self[0] == other[0] &&
self[1] == other[1] &&
self[2] == other[2]
}
#[inline]
pure fn fuzzy_eq(&&other: Vec3) -> bool {
2012-09-07 10:48:47 +00:00
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2])
}
#[inline]
pure fn eq(&&other:Vec3) -> bool {
2012-09-07 10:48:47 +00:00
self.fuzzy_eq(other)
}
#[inline]
2012-09-07 10:48:47 +00:00
pure fn magnitude2() -> float {
self[0] * self[0] +
self[1] * self[1] +
self[2] * self[2]
}
#[inline]
2012-09-07 10:48:47 +00:00
pure fn magnitude() -> float {
2012-09-10 02:55:15 +00:00
self.magnitude2().sqrt()
2012-09-07 10:48:47 +00:00
}
#[inline]
pure fn normalize() -> Vec3 {
let n = 1f / self.magnitude();
2012-09-07 10:48:47 +00:00
return self.mul_f(n);
}
2012-09-07 18:45:07 +00:00
#[inline]
pure fn lerp(&&other:Vec3, &&value:float) -> Vec3 {
2012-09-07 18:45:07 +00:00
self.add_v((other.sub_v(self)).mul_f(value))
}
#[inline]
pure fn abs() -> Vec3 {
Vec3(self[0].abs(),
2012-09-08 05:54:31 +00:00
self[1].abs(),
self[2].abs())
}
#[inline]
pure fn min(&&other:Vec3) -> Vec3 {
Vec3(min(self[0], other[0]),
2012-09-08 05:54:31 +00:00
min(self[1], other[1]),
min(self[2], other[2]))
}
#[inline]
pure fn max(&&other:Vec3) -> Vec3 {
Vec3(max(self[0], other[0]),
2012-09-08 05:54:31 +00:00
max(self[1], other[1]),
max(self[2], other[2]))
}
#[inline] static pure fn zero() -> Vec3 { Vec3(1f, 1f, 1f) }
#[inline] static pure fn identity() -> Vec3 { Vec3(1f, 1f, 1f) }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec3: ToStr {
2012-09-07 10:48:47 +00:00
fn to_str() -> ~str {
fmt!("Vec3[ %f, %f, %f ]", self[0], self[1], self[2])
2012-09-07 10:48:47 +00:00
}
}
//
2012-09-07 18:49:52 +00:00
// Vec4
2012-09-07 10:48:47 +00:00
//
2012-09-29 08:41:48 +00:00
pub struct Vec4 { data:[float * 4] }
2012-09-07 10:48:47 +00:00
2012-09-29 08:41:48 +00:00
pub const vec4_zero :Vec4 = Vec4 { data: [ 0f, 0f, 0f, 0f ] };
pub const vec4_unit_x :Vec4 = Vec4 { data: [ 1f, 0f, 0f, 0f ] };
pub const vec4_unit_y :Vec4 = Vec4 { data: [ 0f, 1f, 0f, 0f ] };
pub const vec4_unit_z :Vec4 = Vec4 { data: [ 0f, 0f, 1f, 0f ] };
pub const vec4_unit_w :Vec4 = Vec4 { data: [ 0f, 0f, 0f, 1f ] };
pub const vec4_identity :Vec4 = Vec4 { data: [ 1f, 1f, 1f, 1f ] };
2012-09-07 10:48:47 +00:00
//
2012-09-07 18:49:52 +00:00
// Constructor
2012-09-07 10:48:47 +00:00
//
#[inline]
2012-09-29 08:41:48 +00:00
pub pure fn Vec4(x:float, y:float, z:float, w:float) -> Vec4 {
Vec4 { data: [ x, y, z, w ] }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec4: Vector4<float> {
// #[inline]
// static pure fn _new(x:float, y:float, z:float, w:float) -> Vec3 {
// Vec2 { data: [ x, y, z, w ] }
// }
#[inline] pure fn x() -> float { self.data[0] }
#[inline] pure fn y() -> float { self.data[1] }
#[inline] pure fn z() -> float { self.data[2] }
#[inline] pure fn w() -> float { self.data[3] }
// #[inline] static pure fn unit_x() -> Vec4 { Vec4(1f, 0f, 0f, 0f) }
// #[inline] static pure fn unit_y() -> Vec4 { Vec4(0f, 1f, 0f, 0f) }
// #[inline] static pure fn unit_z() -> Vec4 { Vec4(0f, 0f, 1f, 0f) }
// #[inline] static pure fn unit_w() -> Vec4 { Vec4(0f, 0f, 0f, 1f) }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec4: Vector<float> {
#[inline]
2012-09-08 05:54:31 +00:00
static pure fn dim() -> uint { 4 }
2012-09-07 10:48:47 +00:00
#[inline]
2012-09-07 10:48:47 +00:00
pure fn index(&&i: uint) -> float {
self.data[i]
}
#[inline]
pure fn neg() -> Vec4 {
Vec4(-self[0], -self[1], -self[2], -self[3])
2012-09-07 10:48:47 +00:00
}
#[inline]
pure fn add_f(&&value:float) -> Vec4 {
Vec4(self[0] + value,
2012-09-07 10:48:47 +00:00
self[1] + value,
self[2] + value,
self[3] + value)
}
#[inline]
pure fn sub_f(&&value:float) -> Vec4 {
Vec4(self[0] - value,
2012-09-07 10:48:47 +00:00
self[1] - value,
self[2] - value,
self[3] - value)
}
#[inline]
pure fn mul_f(&&value:float) -> Vec4 {
Vec4(self[0] * value,
2012-09-07 10:48:47 +00:00
self[1] * value,
self[2] * value,
self[3] * value)
}
#[inline]
pure fn div_f(&&value:float) -> Vec4 {
Vec4(self[0] / value,
2012-09-07 10:48:47 +00:00
self[1] / value,
self[2] / value,
self[3] / value)
}
#[inline]
pure fn add_v(&&other: Vec4) -> Vec4{
Vec4(self[0] + other[0],
2012-09-07 10:48:47 +00:00
self[1] + other[1],
self[2] + other[2],
self[3] + other[3])
}
#[inline]
pure fn sub_v(&&other: Vec4) -> Vec4{
Vec4(self[0] - other[0],
2012-09-07 10:48:47 +00:00
self[1] - other[1],
self[2] - other[2],
self[3] - other[3])
}
#[inline]
pure fn dot(&&other:Vec4) -> float {
2012-09-07 15:18:18 +00:00
self[0] * other[0] +
self[1] * other[1] +
self[2] * other[2] +
self[3] * other[3]
}
#[inline]
pure fn exact_eq(&&other:Vec4) -> bool {
2012-09-07 10:48:47 +00:00
self[0] == other[0] &&
self[1] == other[1] &&
self[2] == other[2] &&
self[3] == other[3]
}
#[inline]
pure fn fuzzy_eq(&&other: Vec4) -> bool {
2012-09-07 10:48:47 +00:00
self[0].fuzzy_eq(&other[0]) &&
self[1].fuzzy_eq(&other[1]) &&
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
#[inline]
pure fn eq(&&other:Vec4) -> bool {
2012-09-07 10:48:47 +00:00
self.fuzzy_eq(other)
}
#[inline]
2012-09-07 10:48:47 +00:00
pure fn magnitude2() -> float {
self[0] * self[0] +
self[1] * self[1] +
self[2] * self[2] +
2012-09-07 18:45:07 +00:00
self[3] * self[3]
2012-09-07 10:48:47 +00:00
}
#[inline]
2012-09-07 10:48:47 +00:00
pure fn magnitude() -> float {
2012-09-10 02:55:15 +00:00
self.magnitude2().sqrt()
2012-09-07 10:48:47 +00:00
}
#[inline]
pure fn normalize() -> Vec4 {
let n = 1f / self.magnitude();
2012-09-07 10:48:47 +00:00
return self.mul_f(n);
}
2012-09-07 18:45:07 +00:00
#[inline]
pure fn lerp(&&other:Vec4, &&value:float) -> Vec4 {
2012-09-07 18:45:07 +00:00
self.add_v((other.sub_v(self)).mul_f(value))
}
#[inline]
pure fn abs() -> Vec4 {
Vec4(self[0].abs(),
2012-09-08 05:54:31 +00:00
self[1].abs(),
self[2].abs(),
self[3].abs())
}
#[inline]
pure fn min(&&other:Vec4) -> Vec4 {
Vec4(min(self[0], other[0]),
2012-09-08 05:54:31 +00:00
min(self[1], other[1]),
min(self[2], other[2]),
min(self[3], other[3]))
}
#[inline]
pure fn max(&&other:Vec4) -> Vec4 {
Vec4(max(self[0], other[0]),
2012-09-08 05:54:31 +00:00
max(self[1], other[1]),
max(self[2], other[2]),
max(self[3], other[3]))
}
#[inline] static pure fn zero() -> Vec4 { Vec4(1f, 1f, 1f, 1f) }
#[inline] static pure fn identity() -> Vec4 { Vec4(1f, 1f, 1f, 1f) }
2012-09-07 10:48:47 +00:00
}
2012-09-29 08:41:48 +00:00
pub impl Vec4: ToStr {
2012-09-07 10:48:47 +00:00
fn to_str() -> ~str {
fmt!("Vec4[ %f, %f, %f, %f ]", self[0], self[1], self[2], self[3])
2012-09-07 10:48:47 +00:00
}
}