Bring code into line with recent updates to Rust
This commit is contained in:
parent
0a60f09042
commit
3df7d557bd
11 changed files with 663 additions and 661 deletions
6
Makefile
6
Makefile
|
@ -13,6 +13,7 @@ TEST_CRATE = $(TEST).rc
|
|||
|
||||
$(TARGET):
|
||||
@echo "Building $(TARGET)"
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
@rustc $(SRC_DIR)/$(SRC_CRATE) --lib -g --out-dir=$(BUILD_DIR)
|
||||
@echo "Success! \o/"
|
||||
|
||||
|
@ -21,10 +22,11 @@ all: $(TARGET)
|
|||
test: all
|
||||
@echo "..."
|
||||
@echo "Building $(TEST)"
|
||||
@mkdir -p $(TEST_BUILD_DIR)
|
||||
@rustc --test -L lib $(TEST_DIR)/$(TEST_CRATE) -g --out-dir=$(TEST_BUILD_DIR)
|
||||
@echo "Success! \o/"
|
||||
@$(TEST_BUILD_DIR)/$(TEST)
|
||||
|
||||
clean:
|
||||
rm -R -f $(BUILD_DIR)/*
|
||||
rm -R -f $(TEST_BUILD_DIR)/*
|
||||
rm -R -f $(BUILD_DIR)
|
||||
rm -R -f $(TEST_BUILD_DIR)
|
366
src/mat.rs
366
src/mat.rs
|
@ -1,10 +1,10 @@
|
|||
import std::cmp::FuzzyEq;
|
||||
import cmp::Ord;
|
||||
import num::Num;
|
||||
// import to_str::ToStr;
|
||||
import math::Sqrt;
|
||||
import quat::quat;
|
||||
import vec::*;
|
||||
use std::cmp::FuzzyEq;
|
||||
use cmp::Ord;
|
||||
use num::Num;
|
||||
// use to_str::ToStr;
|
||||
use math::Sqrt;
|
||||
use quat::Quat;
|
||||
use vec::*;
|
||||
|
||||
//
|
||||
// NxN Matrix
|
||||
|
@ -44,15 +44,15 @@ trait Matrix<T, V> {
|
|||
//
|
||||
trait Matrix3<V3> {
|
||||
pure fn scale(&&vec:V3) -> self;
|
||||
pure fn to_mat4() -> mat4;
|
||||
pure fn to_quat() -> quat;
|
||||
pure fn to_Mat4() -> Mat4;
|
||||
pure fn to_Quat() -> Quat;
|
||||
}
|
||||
|
||||
//
|
||||
// 4x4 Matrix
|
||||
//
|
||||
trait Matrix4<V3, V4> {
|
||||
pure fn scale(&&vec:V3) -> self; // I don't like the use of `vec3` here
|
||||
pure fn scale(&&vec:V3) -> self; // I don't like the use of `Vec3` here
|
||||
pure fn translate(&&vec:V3) -> self;
|
||||
}
|
||||
|
||||
|
@ -64,92 +64,92 @@ trait Matrix4<V3, V4> {
|
|||
//
|
||||
// Mat2: A 2x2, column major matrix
|
||||
//
|
||||
struct mat2 { data:[vec2 * 2] }
|
||||
struct Mat2 { data:[Vec2 * 2] }
|
||||
|
||||
const mat2_zero :mat2 = mat2 { data: [ vec2_zero,
|
||||
const mat2_zero :Mat2 = Mat2 { data: [ vec2_zero,
|
||||
vec2_zero ] };
|
||||
const mat2_identity :mat2 = mat2 { data: [ vec2_unit_x,
|
||||
const mat2_identity :Mat2 = Mat2 { data: [ vec2_unit_x,
|
||||
vec2_unit_y ] };
|
||||
|
||||
//
|
||||
// Mat2 Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn mat2(m00:float, m01:float,
|
||||
m10:float, m11:float) -> mat2 {
|
||||
mat2 { data: [ vec2(m00, m01),
|
||||
vec2(m10, m11) ] }
|
||||
#[inline]
|
||||
pure fn Mat2(m00:float, m01:float,
|
||||
m10:float, m11:float) -> Mat2 {
|
||||
Mat2 { data: [ Vec2(m00, m01),
|
||||
Vec2(m10, m11) ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Construct Mat2 from column vectors
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn mat2_v(col0:vec2, col1:vec2) -> mat2 {
|
||||
mat2 { data: [ col0, col1 ] }
|
||||
#[inline]
|
||||
pure fn mat2_v(col0:Vec2, col1:Vec2) -> Mat2 {
|
||||
Mat2 { data: [ col0, col1 ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Matrix2x2 Implementation
|
||||
//
|
||||
impl mat2: Matrix<float, vec2> {
|
||||
#[inline(always)]
|
||||
impl Mat2: Matrix<float, Vec2> {
|
||||
#[inline]
|
||||
pure fn rows() -> uint { 2 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn cols() -> uint { 2 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_col_major() -> bool { true }
|
||||
|
||||
#[inline(always)]
|
||||
pure fn index(&&i: uint) -> vec2 {
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> Vec2 {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn row(&&i:uint) -> vec2 {
|
||||
vec2(self[0][i],
|
||||
#[inline]
|
||||
pure fn row(&&i:uint) -> Vec2 {
|
||||
Vec2(self[0][i],
|
||||
self[1][i])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn col(&&i:uint) -> vec2 {
|
||||
#[inline]
|
||||
pure fn col(&&i:uint) -> Vec2 {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> mat2 {
|
||||
#[inline]
|
||||
pure fn neg() -> Mat2 {
|
||||
mat2_v(-self[0], -self[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> mat2 {
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Mat2 {
|
||||
mat2_v(self[0].mul_f(value),
|
||||
self[1].mul_f(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_v(&&other:vec2) -> vec2 {
|
||||
vec2(self[0][0]*other[0] + self[1][0]*other[1],
|
||||
#[inline]
|
||||
pure fn mul_v(&&other:Vec2) -> Vec2 {
|
||||
Vec2(self[0][0]*other[0] + self[1][0]*other[1],
|
||||
self[0][1]*other[0] + self[1][1]*other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_m(&&other:mat2) -> mat2 {
|
||||
#[inline]
|
||||
pure fn add_m(&&other:Mat2) -> Mat2 {
|
||||
mat2_v(self[0].add_v(other[0]),
|
||||
self[1].add_v(other[1]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_m(&&other:mat2) -> mat2 {
|
||||
#[inline]
|
||||
pure fn sub_m(&&other:Mat2) -> Mat2 {
|
||||
mat2_v(self[0].sub_v(other[0]),
|
||||
self[1].sub_v(other[1]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_m(&&other:mat2) -> mat2 {
|
||||
mat2(self[0][0]*other[0][0] + self[1][0]*other[0][1],
|
||||
#[inline]
|
||||
pure fn mul_m(&&other:Mat2) -> Mat2 {
|
||||
Mat2(self[0][0]*other[0][0] + self[1][0]*other[0][1],
|
||||
self[0][1]*other[0][0] + self[1][1]*other[0][1],
|
||||
|
||||
self[0][0]*other[1][0] + self[1][0]*other[1][1],
|
||||
|
@ -157,50 +157,50 @@ impl mat2: Matrix<float, vec2> {
|
|||
}
|
||||
|
||||
// TODO - inversion is harrrd D:
|
||||
// #[inline(always)]
|
||||
// pure fn invert(&&other:mat2) -> mat2 {}
|
||||
// #[inline]
|
||||
// pure fn invert(&&other:Mat2) -> Mat2 {}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn transpose() -> mat2 {
|
||||
mat2(self[0][0], self[1][0],
|
||||
#[inline]
|
||||
pure fn transpose() -> Mat2 {
|
||||
Mat2(self[0][0], self[1][0],
|
||||
self[0][1], self[1][1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:mat2) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Mat2) -> bool {
|
||||
self[0].exact_eq(other[0]) &&
|
||||
self[1].exact_eq(other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other:mat2) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other:Mat2) -> bool {
|
||||
self[0].fuzzy_eq(other[0]) &&
|
||||
self[1].fuzzy_eq(other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn eq(&&other:mat2) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Mat2) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_identity() -> bool {
|
||||
self.fuzzy_eq(mat2_identity)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_symmetric() -> bool {
|
||||
self[0][1].fuzzy_eq(&self[1][0]) &&
|
||||
self[1][0].fuzzy_eq(&self[0][1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_diagonal() -> bool {
|
||||
self[0][1].fuzzy_eq(&0f) &&
|
||||
self[1][0].fuzzy_eq(&0f)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_rotated() -> bool {
|
||||
!self.fuzzy_eq(mat2_identity)
|
||||
}
|
||||
|
@ -214,101 +214,101 @@ impl mat2: Matrix<float, vec2> {
|
|||
//
|
||||
// Mat3: A 3x3, column major matrix
|
||||
//
|
||||
struct mat3 { data:[vec3 * 3] }
|
||||
struct Mat3 { data:[Vec3 * 3] }
|
||||
|
||||
const mat3_zero :mat3 = mat3 { data: [ vec3_zero,
|
||||
const mat3_zero :Mat3 = Mat3 { data: [ vec3_zero,
|
||||
vec3_zero,
|
||||
vec3_zero ] };
|
||||
const mat3_identity :mat3 = mat3 { data: [ vec3_unit_x,
|
||||
const mat3_identity :Mat3 = Mat3 { data: [ vec3_unit_x,
|
||||
vec3_unit_y,
|
||||
vec3_unit_z ] };
|
||||
|
||||
//
|
||||
// Mat3 Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn mat3(m00:float, m01:float, m02:float,
|
||||
#[inline]
|
||||
pure fn Mat3(m00:float, m01:float, m02:float,
|
||||
m10:float, m11:float, m12:float,
|
||||
m20:float, m21:float, m22:float) -> mat3 {
|
||||
mat3 { data: [ vec3(m00, m01, m02),
|
||||
vec3(m10, m11, m12),
|
||||
vec3(m20, m21, m22) ] }
|
||||
m20:float, m21:float, m22:float) -> Mat3 {
|
||||
Mat3 { data: [ Vec3(m00, m01, m02),
|
||||
Vec3(m10, m11, m12),
|
||||
Vec3(m20, m21, m22) ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Construct Mat3 from column vectors
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn mat3_v(col0:vec3, col1:vec3, col2:vec3) -> mat3 {
|
||||
mat3 { data: [ col0, col1, col2 ] }
|
||||
#[inline]
|
||||
pure fn mat3_v(col0:Vec3, col1:Vec3, col2:Vec3) -> Mat3 {
|
||||
Mat3 { data: [ col0, col1, col2 ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Matrix3x3 Implementation
|
||||
//
|
||||
impl mat3: Matrix<float, vec3> {
|
||||
#[inline(always)]
|
||||
impl Mat3: Matrix<float, Vec3> {
|
||||
#[inline]
|
||||
pure fn rows() -> uint { 3 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn cols() -> uint { 3 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_col_major() -> bool { true }
|
||||
|
||||
#[inline(always)]
|
||||
pure fn index(&&i: uint) -> vec3 {
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> Vec3 {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn row(&&i:uint) -> vec3 {
|
||||
vec3(self[0][i],
|
||||
#[inline]
|
||||
pure fn row(&&i:uint) -> Vec3 {
|
||||
Vec3(self[0][i],
|
||||
self[1][i],
|
||||
self[2][i])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn col(&&i:uint) -> vec3 {
|
||||
#[inline]
|
||||
pure fn col(&&i:uint) -> Vec3 {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> mat3 {
|
||||
#[inline]
|
||||
pure fn neg() -> Mat3 {
|
||||
mat3_v(-self[0], -self[1], -self[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> mat3 {
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Mat3 {
|
||||
mat3_v(self[0].mul_f(value),
|
||||
self[1].mul_f(value),
|
||||
self[2].mul_f(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_v(&&other:vec3) -> vec3 {
|
||||
vec3(self[0][0]*other[0] + self[1][0]*other[1] + self[2][0]*other[2],
|
||||
#[inline]
|
||||
pure fn mul_v(&&other:Vec3) -> Vec3 {
|
||||
Vec3(self[0][0]*other[0] + self[1][0]*other[1] + self[2][0]*other[2],
|
||||
self[0][1]*other[0] + self[1][1]*other[1] + self[2][1]*other[2],
|
||||
self[0][2]*other[0] + self[1][2]*other[1] + self[2][2]*other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_m(&&other:mat3) -> mat3 {
|
||||
#[inline]
|
||||
pure fn add_m(&&other:Mat3) -> Mat3 {
|
||||
mat3_v(self[0].add_v(other[0]),
|
||||
self[1].add_v(other[1]),
|
||||
self[2].add_v(other[2]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_m(&&other:mat3) -> mat3 {
|
||||
#[inline]
|
||||
pure fn sub_m(&&other:Mat3) -> Mat3 {
|
||||
mat3_v(self[0].sub_v(other[0]),
|
||||
self[1].sub_v(other[1]),
|
||||
self[2].sub_v(other[2]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_m(&&other:mat3) -> mat3 {
|
||||
mat3(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2],
|
||||
#[inline]
|
||||
pure fn mul_m(&&other:Mat3) -> Mat3 {
|
||||
Mat3(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2],
|
||||
self[0][1]*other[0][0] + self[1][1]*other[0][1] + self[2][1]*other[0][2],
|
||||
self[0][2]*other[0][0] + self[1][2]*other[0][1] + self[2][2]*other[0][2],
|
||||
|
||||
|
@ -322,41 +322,41 @@ impl mat3: Matrix<float, vec3> {
|
|||
}
|
||||
|
||||
// TODO - inversion is harrrd D:
|
||||
// #[inline(always)]
|
||||
// pure fn invert(&&other:mat3) -> mat3 {}
|
||||
// #[inline]
|
||||
// pure fn invert(&&other:Mat3) -> Mat3 {}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn transpose() -> mat3 {
|
||||
mat3(self[0][0], self[1][0], self[2][0],
|
||||
#[inline]
|
||||
pure fn transpose() -> Mat3 {
|
||||
Mat3(self[0][0], self[1][0], self[2][0],
|
||||
self[0][1], self[1][1], self[2][1],
|
||||
self[0][2], self[1][2], self[2][2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:mat3) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Mat3) -> bool {
|
||||
self[0].exact_eq(other[0]) &&
|
||||
self[1].exact_eq(other[1]) &&
|
||||
self[2].exact_eq(other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other:mat3) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other:Mat3) -> bool {
|
||||
self[0].fuzzy_eq(other[0]) &&
|
||||
self[1].fuzzy_eq(other[1]) &&
|
||||
self[2].fuzzy_eq(other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn eq(&&other:mat3) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Mat3) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_identity() -> bool {
|
||||
self.fuzzy_eq(mat3_identity)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_symmetric() -> bool {
|
||||
self[0][1].fuzzy_eq(&self[1][0]) &&
|
||||
self[0][2].fuzzy_eq(&self[2][0]) &&
|
||||
|
@ -368,7 +368,7 @@ impl mat3: Matrix<float, vec3> {
|
|||
self[2][1].fuzzy_eq(&self[1][2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_diagonal() -> bool {
|
||||
self[0][1].fuzzy_eq(&0f) &&
|
||||
self[0][2].fuzzy_eq(&0f) &&
|
||||
|
@ -380,31 +380,31 @@ impl mat3: Matrix<float, vec3> {
|
|||
self[2][1].fuzzy_eq(&0f)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_rotated() -> bool {
|
||||
!self.fuzzy_eq(mat3_identity)
|
||||
}
|
||||
}
|
||||
|
||||
impl mat3: Matrix3<vec3> {
|
||||
#[inline(always)]
|
||||
pure fn scale(&&vec:vec3) -> mat3 {
|
||||
self.mul_m(mat3(vec.x(), 0f, 0f,
|
||||
impl Mat3: Matrix3<Vec3> {
|
||||
#[inline]
|
||||
pure fn scale(&&vec:Vec3) -> Mat3 {
|
||||
self.mul_m(Mat3(vec.x(), 0f, 0f,
|
||||
0f, vec.y(), 0f,
|
||||
0f, 0f, vec.z()))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn to_mat4() -> mat4 {
|
||||
mat4(self[0][0], self[0][1], self[0][2], 0f,
|
||||
#[inline]
|
||||
pure fn to_Mat4() -> Mat4 {
|
||||
Mat4(self[0][0], self[0][1], self[0][2], 0f,
|
||||
self[1][0], self[1][1], self[1][2], 0f,
|
||||
self[2][0], self[2][1], self[2][2], 0f,
|
||||
0f, 0f, 0f, 1f)
|
||||
}
|
||||
|
||||
pure fn to_quat() -> quat {
|
||||
pure fn to_Quat() -> Quat {
|
||||
// Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's
|
||||
// paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
|
||||
// paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf
|
||||
|
||||
let mut s:float;
|
||||
let w:float, x:float, y:float, z:float;
|
||||
|
@ -439,7 +439,7 @@ impl mat3: Matrix3<vec3> {
|
|||
y = self[1][2] - self[2][1] * s;
|
||||
z = self[0][1] - self[1][0] * s;
|
||||
}
|
||||
return quat(w, x, y, z);
|
||||
return Quat(w, x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -451,13 +451,13 @@ impl mat3: Matrix3<vec3> {
|
|||
//
|
||||
// Mat4: A 4x4, column major matrix
|
||||
//
|
||||
struct mat4 { data:[vec4 * 4] }
|
||||
struct Mat4 { data:[Vec4 * 4] }
|
||||
|
||||
const mat4_zero :mat4 = mat4 { data: [ vec4_zero,
|
||||
const mat4_zero :Mat4 = Mat4 { data: [ vec4_zero,
|
||||
vec4_zero,
|
||||
vec4_zero,
|
||||
vec4_zero ] };
|
||||
const mat4_identity :mat4 = mat4 { data: [ vec4_unit_x,
|
||||
const mat4_identity :Mat4 = Mat4 { data: [ vec4_unit_x,
|
||||
vec4_unit_y,
|
||||
vec4_unit_z,
|
||||
vec4_unit_w ] };
|
||||
|
@ -465,96 +465,96 @@ const mat4_identity :mat4 = mat4 { data: [ vec4_unit_x,
|
|||
//
|
||||
// Mat4 Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn mat4(m00:float, m01:float, m02:float, m03:float,
|
||||
#[inline]
|
||||
pure fn Mat4(m00:float, m01:float, m02:float, m03:float,
|
||||
m10:float, m11:float, m12:float, m13:float,
|
||||
m20:float, m21:float, m22:float, m23:float,
|
||||
m30:float, m31:float, m32:float, m33:float) -> mat4 {
|
||||
mat4 { data: [ vec4(m00, m01, m02, m03),
|
||||
vec4(m10, m11, m12, m13),
|
||||
vec4(m20, m21, m22, m23),
|
||||
vec4(m30, m31, m32, m33) ] }
|
||||
m30:float, m31:float, m32:float, m33:float) -> Mat4 {
|
||||
Mat4 { data: [ Vec4(m00, m01, m02, m03),
|
||||
Vec4(m10, m11, m12, m13),
|
||||
Vec4(m20, m21, m22, m23),
|
||||
Vec4(m30, m31, m32, m33) ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Construct Mat4 from column vectors
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn mat4_v(col0:vec4, col1:vec4, col2:vec4, col3:vec4) -> mat4 {
|
||||
mat4 { data: [ col0, col1, col2, col3 ] }
|
||||
#[inline]
|
||||
pure fn mat4_v(col0:Vec4, col1:Vec4, col2:Vec4, col3:Vec4) -> Mat4 {
|
||||
Mat4 { data: [ col0, col1, col2, col3 ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Matrix4x4 Implementation
|
||||
//
|
||||
impl mat4: Matrix<float, vec4> {
|
||||
#[inline(always)]
|
||||
impl Mat4: Matrix<float, Vec4> {
|
||||
#[inline]
|
||||
pure fn rows() -> uint { 4 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn cols() -> uint { 4 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_col_major() -> bool { true }
|
||||
|
||||
#[inline(always)]
|
||||
pure fn index(&&i: uint) -> vec4 {
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> Vec4 {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn row(&&i:uint) -> vec4 {
|
||||
vec4(self[0][i],
|
||||
#[inline]
|
||||
pure fn row(&&i:uint) -> Vec4 {
|
||||
Vec4(self[0][i],
|
||||
self[1][i],
|
||||
self[2][i],
|
||||
self[3][i])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn col(&&i:uint) -> vec4 {
|
||||
#[inline]
|
||||
pure fn col(&&i:uint) -> Vec4 {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> mat4 {
|
||||
#[inline]
|
||||
pure fn neg() -> Mat4 {
|
||||
mat4_v(-self[0], -self[1], -self[2], -self[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> mat4 {
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Mat4 {
|
||||
mat4_v(self[0].mul_f(value),
|
||||
self[1].mul_f(value),
|
||||
self[2].mul_f(value),
|
||||
self[3].mul_f(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_v(&&other:vec4) -> vec4 {
|
||||
vec4(self[0][0]*other[0] + self[1][0]*other[1] + self[2][0]*other[2] + self[3][0]*other[3],
|
||||
#[inline]
|
||||
pure fn mul_v(&&other:Vec4) -> Vec4 {
|
||||
Vec4(self[0][0]*other[0] + self[1][0]*other[1] + self[2][0]*other[2] + self[3][0]*other[3],
|
||||
self[0][1]*other[0] + self[1][1]*other[1] + self[2][1]*other[2] + self[3][1]*other[3],
|
||||
self[0][2]*other[0] + self[1][2]*other[1] + self[2][2]*other[2] + self[3][2]*other[3],
|
||||
self[0][3]*other[0] + self[1][3]*other[1] + self[2][3]*other[2] + self[3][3]*other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_m(&&other:mat4) -> mat4 {
|
||||
#[inline]
|
||||
pure fn add_m(&&other:Mat4) -> Mat4 {
|
||||
mat4_v(self[0].add_v(other[0]),
|
||||
self[1].add_v(other[1]),
|
||||
self[2].add_v(other[2]),
|
||||
self[3].add_v(other[3]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_m(&&other:mat4) -> mat4 {
|
||||
#[inline]
|
||||
pure fn sub_m(&&other:Mat4) -> Mat4 {
|
||||
mat4_v(self[0].sub_v(other[0]),
|
||||
self[1].sub_v(other[1]),
|
||||
self[2].sub_v(other[2]),
|
||||
self[3].sub_v(other[3]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_m(&&other:mat4) -> mat4 {
|
||||
mat4(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2] + self[3][0]*other[0][3],
|
||||
#[inline]
|
||||
pure fn mul_m(&&other:Mat4) -> Mat4 {
|
||||
Mat4(self[0][0]*other[0][0] + self[1][0]*other[0][1] + self[2][0]*other[0][2] + self[3][0]*other[0][3],
|
||||
self[0][1]*other[0][0] + self[1][1]*other[0][1] + self[2][1]*other[0][2] + self[3][1]*other[0][3],
|
||||
self[0][2]*other[0][0] + self[1][2]*other[0][1] + self[2][2]*other[0][2] + self[3][2]*other[0][3],
|
||||
self[0][3]*other[0][0] + self[1][3]*other[0][1] + self[2][3]*other[0][2] + self[3][3]*other[0][3],
|
||||
|
@ -576,44 +576,44 @@ impl mat4: Matrix<float, vec4> {
|
|||
}
|
||||
|
||||
// TODO - inversion is harrrd D:
|
||||
// #[inline(always)]
|
||||
// pure fn invert(&&other:mat4) -> mat4 {}
|
||||
// #[inline]
|
||||
// pure fn invert(&&other:Mat4) -> Mat4 {}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn transpose() -> mat4 {
|
||||
mat4(self[0][0], self[1][0], self[2][0], self[3][0],
|
||||
#[inline]
|
||||
pure fn transpose() -> Mat4 {
|
||||
Mat4(self[0][0], self[1][0], self[2][0], self[3][0],
|
||||
self[0][1], self[1][1], self[2][1], self[3][1],
|
||||
self[0][2], self[1][2], self[2][2], self[3][2],
|
||||
self[0][3], self[1][3], self[2][3], self[3][3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:mat4) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Mat4) -> bool {
|
||||
self[0].exact_eq(other[0]) &&
|
||||
self[1].exact_eq(other[1]) &&
|
||||
self[2].exact_eq(other[2]) &&
|
||||
self[3].exact_eq(other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other:mat4) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other:Mat4) -> bool {
|
||||
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(always)]
|
||||
pure fn eq(&&other:mat4) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Mat4) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_identity() -> bool {
|
||||
self.fuzzy_eq(mat4_identity)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_symmetric() -> bool {
|
||||
self[0][1].fuzzy_eq(&self[1][0]) &&
|
||||
self[0][2].fuzzy_eq(&self[2][0]) &&
|
||||
|
@ -632,7 +632,7 @@ impl mat4: Matrix<float, vec4> {
|
|||
self[3][2].fuzzy_eq(&self[2][3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_diagonal() -> bool {
|
||||
self[0][1].fuzzy_eq(&0f) &&
|
||||
self[0][2].fuzzy_eq(&0f) &&
|
||||
|
@ -651,27 +651,27 @@ impl mat4: Matrix<float, vec4> {
|
|||
self[3][2].fuzzy_eq(&0f)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn is_rotated() -> bool {
|
||||
!self.fuzzy_eq(mat4_identity)
|
||||
}
|
||||
}
|
||||
|
||||
impl mat4: Matrix4<vec3, vec4> {
|
||||
#[inline(always)]
|
||||
pure fn scale(&&vec:vec3) -> mat4 {
|
||||
self.mul_m(mat4(vec.x(), 0f, 0f, 0f,
|
||||
impl Mat4: Matrix4<Vec3, Vec4> {
|
||||
#[inline]
|
||||
pure fn scale(&&vec:Vec3) -> Mat4 {
|
||||
self.mul_m(Mat4(vec.x(), 0f, 0f, 0f,
|
||||
0f, vec.y(), 0f, 0f,
|
||||
0f, 0f, vec.z(), 0f,
|
||||
0f, 0f, 0f, 1f))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn translate(&&vec:vec3) -> mat4 {
|
||||
#[inline]
|
||||
pure fn translate(&&vec:Vec3) -> Mat4 {
|
||||
mat4_v(self[0],
|
||||
self[1],
|
||||
self[2],
|
||||
vec4(self[3][0] + vec.x(),
|
||||
Vec4(self[3][0] + vec.x(),
|
||||
self[3][1] + vec.y(),
|
||||
self[3][2] + vec.z(),
|
||||
self[3][3]))
|
||||
|
|
22
src/math.rs
22
src/math.rs
|
@ -1,4 +1,4 @@
|
|||
import cmp::Ord;
|
||||
use cmp::Ord;
|
||||
|
||||
//
|
||||
// Abs
|
||||
|
@ -8,7 +8,7 @@ trait Abs {
|
|||
}
|
||||
|
||||
impl int: Abs {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn abs() -> int {
|
||||
if self >= 0 { self }
|
||||
else {-self }
|
||||
|
@ -16,7 +16,7 @@ impl int: Abs {
|
|||
}
|
||||
|
||||
impl float: Abs {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn abs() -> float {
|
||||
if self >= 0f { self }
|
||||
else {-self }
|
||||
|
@ -24,7 +24,7 @@ impl float: Abs {
|
|||
}
|
||||
|
||||
impl f32: Abs {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn abs() -> f32 {
|
||||
if self >= 0f32 { self }
|
||||
else {-self }
|
||||
|
@ -32,7 +32,7 @@ impl f32: Abs {
|
|||
}
|
||||
|
||||
impl f64: Abs {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn abs() -> f64 {
|
||||
if self >= 0f64 { self }
|
||||
else {-self }
|
||||
|
@ -42,7 +42,7 @@ impl f64: Abs {
|
|||
//
|
||||
// Min
|
||||
//
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn min<T:Copy Ord>(&&a:T, &&b:T) -> T {
|
||||
if a < b { a }
|
||||
else { b }
|
||||
|
@ -51,7 +51,7 @@ pure fn min<T:Copy Ord>(&&a:T, &&b:T) -> T {
|
|||
//
|
||||
// Max
|
||||
//
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn max<T:Copy Ord>(&&a:T, &&b:T) -> T {
|
||||
if a > b { a }
|
||||
else { b }
|
||||
|
@ -65,28 +65,28 @@ trait Sqrt {
|
|||
}
|
||||
|
||||
// impl int: Sqrt {
|
||||
// #[inline(always)]
|
||||
// #[inline]
|
||||
// pure fn sqrt() -> int {
|
||||
// sqrt(self)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl float: Sqrt {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn sqrt() -> float {
|
||||
float::sqrt(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl f32: Sqrt {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn sqrt() -> f32 {
|
||||
f32::sqrt(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl f64: Sqrt {
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn sqrt() -> f64 {
|
||||
f64::sqrt(self)
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#[comment = "Linear algebra library for Rust. Incomplete and probably buggy."];
|
||||
#[crate_type = "lib"];
|
||||
|
||||
use std;
|
||||
extern mod std;
|
||||
|
||||
mod mat;
|
||||
mod math;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import float::consts::pi;
|
||||
import float::tan;
|
||||
import mat::mat4;
|
||||
use float::consts::pi;
|
||||
use float::tan;
|
||||
use mat::Mat4;
|
||||
|
||||
//
|
||||
// Create a perspective projection matrix
|
||||
|
@ -8,7 +8,7 @@ import mat::mat4;
|
|||
// fov is in degrees
|
||||
// http://www.opengl.org/wiki/GluPerspective_code
|
||||
//
|
||||
pure fn perspective(fovy:float, aspectRatio:float, near:float, far:float) -> mat4 {
|
||||
pure fn perspective(fovy:float, aspectRatio:float, near:float, far:float) -> Mat4 {
|
||||
let ymax = near * tan(fovy * pi / 360f);
|
||||
let xmax = ymax * aspectRatio;
|
||||
return frustum(-xmax, xmax, -ymax, ymax, near, far);
|
||||
|
@ -21,7 +21,7 @@ pure fn perspective(fovy:float, aspectRatio:float, near:float, far:float) -> mat
|
|||
//
|
||||
// TODO: double check algorithm
|
||||
//
|
||||
pure fn frustum(left:float, right:float, bottom:float, top:float, near:float, far:float) -> mat4 {
|
||||
pure fn frustum(left:float, right:float, bottom:float, top:float, near:float, far:float) -> Mat4 {
|
||||
let m00 = (2f * near) / (right - left);
|
||||
let m01 = 0f;
|
||||
let m02 = 0f;
|
||||
|
@ -39,7 +39,7 @@ pure fn frustum(left:float, right:float, bottom:float, top:float, near:float, fa
|
|||
let m32 = -(2f * far * near) / (far - near);
|
||||
let m33 = 0f;
|
||||
|
||||
return mat4(m00, m01, m02, m03,
|
||||
return Mat4(m00, m01, m02, m03,
|
||||
m10, m11, m12, m13,
|
||||
m20, m21, m22, m23,
|
||||
m30, m31, m32, m33);
|
||||
|
|
124
src/quat.rs
124
src/quat.rs
|
@ -1,10 +1,10 @@
|
|||
import std::cmp::FuzzyEq;
|
||||
import cmp::Ord;
|
||||
import num::Num;
|
||||
import to_str::ToStr;
|
||||
import math::Sqrt;
|
||||
import mat::{mat3, mat4};
|
||||
import vec::vec3;
|
||||
use std::cmp::FuzzyEq;
|
||||
use cmp::Ord;
|
||||
use num::Num;
|
||||
use to_str::ToStr;
|
||||
use math::Sqrt;
|
||||
use mat::{Mat3, Mat4};
|
||||
use vec::Vec3;
|
||||
|
||||
// TODO: Unittests
|
||||
|
||||
|
@ -25,7 +25,7 @@ trait Quaternion<T> {
|
|||
pure fn mul_f(&&value:T) -> self;
|
||||
pure fn div_f(&&value:T) -> self;
|
||||
|
||||
// pure fn mul_v(&&vec3) -> vec3;
|
||||
// pure fn mul_v(&&Vec3) -> Vec3;
|
||||
|
||||
pure fn add_q(&&other:self) -> self;
|
||||
pure fn sub_q(&&other:self) -> self;
|
||||
|
@ -40,8 +40,8 @@ trait Quaternion<T> {
|
|||
pure fn magnitude2() -> T;
|
||||
pure fn magnitude() -> T;
|
||||
|
||||
pure fn to_mat3() -> mat3;
|
||||
pure fn to_mat4() -> mat4;
|
||||
pure fn to_Mat3() -> Mat3;
|
||||
pure fn to_Mat4() -> Mat4;
|
||||
}
|
||||
|
||||
|
||||
|
@ -52,113 +52,113 @@ trait Quaternion<T> {
|
|||
//
|
||||
// Quat struct definition
|
||||
//
|
||||
struct quat { data:[float * 4] }
|
||||
struct Quat { data:[float * 4] }
|
||||
|
||||
const quat_zero :quat = quat { data: [ 0f, 0f, 0f, 0f ] };
|
||||
const quat_identity :quat = quat { data: [ 1f, 0f, 0f, 0f ] };
|
||||
const quat_zero :Quat = Quat { data: [ 0f, 0f, 0f, 0f ] };
|
||||
const quat_identity :Quat = Quat { data: [ 1f, 0f, 0f, 0f ] };
|
||||
|
||||
//
|
||||
// Quat Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn quat(w:float, x:float, y:float, z:float) -> quat {
|
||||
quat { data: [ w, x, y, z ] }
|
||||
#[inline]
|
||||
pure fn Quat(w:float, x:float, y:float, z:float) -> Quat {
|
||||
Quat { data: [ w, x, y, z ] }
|
||||
}
|
||||
|
||||
//
|
||||
// Quaternion Implementation
|
||||
//
|
||||
impl quat: Quaternion<float> {
|
||||
#[inline(always)]
|
||||
impl Quat: Quaternion<float> {
|
||||
#[inline]
|
||||
pure fn dim() -> uint { 4 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> float {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> quat {
|
||||
quat(-self[0], -self[1], -self[2], -self[3])
|
||||
#[inline]
|
||||
pure fn neg() -> Quat {
|
||||
Quat(-self[0], -self[1], -self[2], -self[3])
|
||||
}
|
||||
|
||||
#[inline(always)] pure fn w() -> float { self.data[0] }
|
||||
#[inline(always)] pure fn x() -> float { self.data[1] }
|
||||
#[inline(always)] pure fn y() -> float { self.data[2] }
|
||||
#[inline(always)] pure fn z() -> float { self.data[3] }
|
||||
#[inline] pure fn w() -> float { self.data[0] }
|
||||
#[inline] pure fn x() -> float { self.data[1] }
|
||||
#[inline] pure fn y() -> float { self.data[2] }
|
||||
#[inline] pure fn z() -> float { self.data[3] }
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> quat {
|
||||
quat(self[0] * value,
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Quat {
|
||||
Quat(self[0] * value,
|
||||
self[1] * value,
|
||||
self[2] * value,
|
||||
self[3] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_f(&&value:float) -> quat {
|
||||
quat(self[0] / value,
|
||||
#[inline]
|
||||
pure fn div_f(&&value:float) -> Quat {
|
||||
Quat(self[0] / value,
|
||||
self[1] / value,
|
||||
self[2] / value,
|
||||
self[3] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_q(&&other:quat) -> quat{
|
||||
quat(self[0] + other[0],
|
||||
#[inline]
|
||||
pure fn add_q(&&other:Quat) -> Quat{
|
||||
Quat(self[0] + other[0],
|
||||
self[1] + other[1],
|
||||
self[2] + other[2],
|
||||
self[3] + other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_q(&&other:quat) -> quat{
|
||||
quat(self[0] - other[0],
|
||||
#[inline]
|
||||
pure fn sub_q(&&other:Quat) -> Quat{
|
||||
Quat(self[0] - other[0],
|
||||
self[1] - other[1],
|
||||
self[2] - other[2],
|
||||
self[3] - other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_q(&&other:quat) -> quat {
|
||||
quat(self.w()*other.w() - self.x()*other.x() - self.y()*other.y() - self.z()*other.z(),
|
||||
#[inline]
|
||||
pure fn mul_q(&&other:Quat) -> Quat {
|
||||
Quat(self.w()*other.w() - self.x()*other.x() - self.y()*other.y() - self.z()*other.z(),
|
||||
self.w()*other.x() + self.x()*other.w() + self.y()*other.z() - self.z()*other.y(),
|
||||
self.w()*other.y() + self.y()*other.w() + self.z()*other.x() - self.x()*other.z(),
|
||||
self.w()*other.z() + self.z()*other.w() + self.x()*other.y() - self.y()*other.x())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:quat) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Quat) -> bool {
|
||||
self[0] == other[0] &&
|
||||
self[1] == other[1] &&
|
||||
self[2] == other[2] &&
|
||||
self[3] == other[3]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other: quat) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other: Quat) -> bool {
|
||||
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(always)]
|
||||
pure fn eq(&&other:quat) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Quat) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn conjugate() -> quat {
|
||||
quat(self.w(), -self.x(), -self.y(), -self.z())
|
||||
#[inline]
|
||||
pure fn conjugate() -> Quat {
|
||||
Quat(self.w(), -self.x(), -self.y(), -self.z())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn inverse() -> quat {
|
||||
#[inline]
|
||||
pure fn inverse() -> Quat {
|
||||
self.conjugate().mul_f((1f / self.magnitude2()))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude2() -> float {
|
||||
self.w() * self.w() +
|
||||
self.x() * self.x() +
|
||||
|
@ -166,13 +166,13 @@ impl quat: Quaternion<float> {
|
|||
self.z() * self.z()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude() -> float {
|
||||
self.magnitude2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn to_mat3() -> mat3 {
|
||||
#[inline]
|
||||
pure fn to_Mat3() -> Mat3 {
|
||||
let x2 = self.x() + self.x();
|
||||
let y2 = self.y() + self.y();
|
||||
let z2 = self.z() + self.z();
|
||||
|
@ -189,22 +189,22 @@ impl quat: Quaternion<float> {
|
|||
let wz2 = z2 * self.w();
|
||||
let wx2 = x2 * self.w();
|
||||
|
||||
return mat3(1f - yy2 - zz2, xy2 - wz2, xz2 + wy2,
|
||||
return Mat3(1f - yy2 - zz2, xy2 - wz2, xz2 + wy2,
|
||||
xy2 + wz2, 1f - xx2 - zz2, yz2 - wx2,
|
||||
xz2 - wy2, yz2 + wx2, 1f - xx2 - yy2);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn to_mat4() -> mat4 {
|
||||
self.to_mat3().to_mat4()
|
||||
#[inline]
|
||||
pure fn to_Mat4() -> Mat4 {
|
||||
self.to_Mat3().to_Mat4()
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Convert To String
|
||||
//
|
||||
impl quat: ToStr {
|
||||
impl Quat: ToStr {
|
||||
fn to_str() -> ~str {
|
||||
fmt!("quat[ %f, %f, %f, %f ]", self.w(), self.x(), self.y(), self.z())
|
||||
fmt!("Quat[ %f, %f, %f, %f ]", self.w(), self.x(), self.y(), self.z())
|
||||
}
|
||||
}
|
438
src/vec.rs
438
src/vec.rs
|
@ -1,8 +1,8 @@
|
|||
import std::cmp::FuzzyEq;
|
||||
import cmp::Ord;
|
||||
import num::Num;
|
||||
import math::{Abs, min, max, Sqrt};
|
||||
import to_str::ToStr;
|
||||
use std::cmp::FuzzyEq;
|
||||
use cmp::Ord;
|
||||
use num::Num;
|
||||
use math::{Abs, min, max, Sqrt};
|
||||
use to_str::ToStr;
|
||||
|
||||
//
|
||||
// N-dimensional Vector
|
||||
|
@ -96,155 +96,155 @@ trait Vector4<T> {
|
|||
//
|
||||
// Vec2
|
||||
//
|
||||
struct vec2 { data:[float * 2] }
|
||||
struct Vec2 { data:[float * 2] }
|
||||
|
||||
const vec2_zero :vec2 = vec2 { data: [ 0f, 0f ] };
|
||||
const vec2_unit_x :vec2 = vec2 { data: [ 1f, 0f ] };
|
||||
const vec2_unit_y :vec2 = vec2 { data: [ 0f, 1f ] };
|
||||
const vec2_identity :vec2 = vec2 { data: [ 1f, 1f ] };
|
||||
const vec2_zero :Vec2 = Vec2 { data: [ 0f, 0f ] };
|
||||
const vec2_unit_x :Vec2 = Vec2 { data: [ 1f, 0f ] };
|
||||
const vec2_unit_y :Vec2 = Vec2 { data: [ 0f, 1f ] };
|
||||
const vec2_identity :Vec2 = Vec2 { data: [ 1f, 1f ] };
|
||||
|
||||
//
|
||||
// Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn vec2(x:float, y:float) -> vec2 {
|
||||
vec2 { data: [ x, y ] }
|
||||
#[inline]
|
||||
pure fn Vec2(x:float, y:float) -> Vec2 {
|
||||
Vec2 { data: [ x, y ] }
|
||||
}
|
||||
|
||||
impl vec2: Vector2<float> {
|
||||
// #[inline(always)]
|
||||
// static pure fn _new(x:float, y:float) -> vec2 {
|
||||
// vec2 { data: [ x, y ] }
|
||||
impl Vec2: Vector2<float> {
|
||||
// #[inline]
|
||||
// static pure fn _new(x:float, y:float) -> Vec2 {
|
||||
// Vec2 { data: [ x, y ] }
|
||||
// }
|
||||
|
||||
#[inline(always)] pure fn x() -> float { self.data[0] }
|
||||
#[inline(always)] pure fn y() -> float { self.data[1] }
|
||||
#[inline] pure fn x() -> float { self.data[0] }
|
||||
#[inline] pure fn y() -> float { self.data[1] }
|
||||
|
||||
// #[inline(always)] static pure fn unit_x() -> vec2 { vec2(1f, 0f) }
|
||||
// #[inline(always)] static pure fn unit_y() -> vec2 { vec2(0f, 1f) }
|
||||
// #[inline(always)] static pure fn unit_z() -> vec2 { vec2(0f, 0f) }
|
||||
// #[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) }
|
||||
}
|
||||
|
||||
impl vec2: Vector<float> {
|
||||
#[inline(always)]
|
||||
impl Vec2: Vector<float> {
|
||||
#[inline]
|
||||
static pure fn dim() -> uint { 2 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> float {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> vec2 {
|
||||
vec2(-self[0], -self[1])
|
||||
#[inline]
|
||||
pure fn neg() -> Vec2 {
|
||||
Vec2(-self[0], -self[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_f(&&value:float) -> vec2 {
|
||||
vec2(self[0] + value,
|
||||
#[inline]
|
||||
pure fn add_f(&&value:float) -> Vec2 {
|
||||
Vec2(self[0] + value,
|
||||
self[1] + value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_f(&&value:float) -> vec2 {
|
||||
vec2(self[0] - value,
|
||||
#[inline]
|
||||
pure fn sub_f(&&value:float) -> Vec2 {
|
||||
Vec2(self[0] - value,
|
||||
self[1] - value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> vec2 {
|
||||
vec2(self[0] * value,
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Vec2 {
|
||||
Vec2(self[0] * value,
|
||||
self[1] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_f(&&value:float) -> vec2 {
|
||||
vec2(self[0] / value,
|
||||
#[inline]
|
||||
pure fn div_f(&&value:float) -> Vec2 {
|
||||
Vec2(self[0] / value,
|
||||
self[1] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_v(&&other: vec2) -> vec2{
|
||||
vec2(self[0] + other[0],
|
||||
#[inline]
|
||||
pure fn add_v(&&other: Vec2) -> Vec2{
|
||||
Vec2(self[0] + other[0],
|
||||
self[1] + other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_v(&&other: vec2) -> vec2{
|
||||
vec2(self[0] - other[0],
|
||||
#[inline]
|
||||
pure fn sub_v(&&other: Vec2) -> Vec2{
|
||||
Vec2(self[0] - other[0],
|
||||
self[1] - other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn dot(&&other: vec2) -> float {
|
||||
#[inline]
|
||||
pure fn dot(&&other: Vec2) -> float {
|
||||
self[0] * other[0] +
|
||||
self[1] * other[1]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:vec2) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Vec2) -> bool {
|
||||
self[0] == other[0] &&
|
||||
self[1] == other[1]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other: vec2) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other: Vec2) -> bool {
|
||||
self[0].fuzzy_eq(&other[0]) &&
|
||||
self[1].fuzzy_eq(&other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn eq(&&other:vec2) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Vec2) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude2() -> float {
|
||||
self[0] * self[0] +
|
||||
self[1] * self[1]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude() -> float {
|
||||
self.magnitude2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn normalize() -> vec2 {
|
||||
#[inline]
|
||||
pure fn normalize() -> Vec2 {
|
||||
let n = 1f / self.magnitude();
|
||||
return self.mul_f(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn lerp(&&other:vec2, &&value:float) -> vec2 {
|
||||
#[inline]
|
||||
pure fn lerp(&&other:Vec2, &&value:float) -> Vec2 {
|
||||
self.add_v((other.sub_v(self)).mul_f(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn abs() -> vec2 {
|
||||
vec2(self[0].abs(),
|
||||
#[inline]
|
||||
pure fn abs() -> Vec2 {
|
||||
Vec2(self[0].abs(),
|
||||
self[1].abs())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn min(&&other:vec2) -> vec2 {
|
||||
vec2(min(self[0], other[0]),
|
||||
#[inline]
|
||||
pure fn min(&&other:Vec2) -> Vec2 {
|
||||
Vec2(min(self[0], other[0]),
|
||||
min(self[1], other[1]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn max(&&other:vec2) -> vec2 {
|
||||
vec2(max(self[0], other[0]),
|
||||
#[inline]
|
||||
pure fn max(&&other:Vec2) -> Vec2 {
|
||||
Vec2(max(self[0], other[0]),
|
||||
max(self[1], other[1]))
|
||||
}
|
||||
|
||||
#[inline(always)] static pure fn zero() -> vec2 { vec2(1f, 1f) }
|
||||
#[inline(always)] static pure fn identity() -> vec2 { vec2(1f, 1f) }
|
||||
#[inline] static pure fn zero() -> Vec2 { Vec2(1f, 1f) }
|
||||
#[inline] static pure fn identity() -> Vec2 { Vec2(1f, 1f) }
|
||||
}
|
||||
|
||||
impl vec2: ToStr {
|
||||
impl Vec2: ToStr {
|
||||
fn to_str() -> ~str {
|
||||
fmt!("vec2[ %f, %f ]", self[0], self[1])
|
||||
fmt!("Vec2[ %f, %f ]", self[0], self[1])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -256,177 +256,177 @@ impl vec2: ToStr {
|
|||
//
|
||||
// Vec3
|
||||
//
|
||||
struct vec3 { data:[float * 3] }
|
||||
struct Vec3 { data:[float * 3] }
|
||||
|
||||
const vec3_zero :vec3 = vec3 { data: [ 0f, 0f, 0f ] };
|
||||
const vec3_unit_x :vec3 = vec3 { data: [ 1f, 0f, 0f ] };
|
||||
const vec3_unit_y :vec3 = vec3 { data: [ 0f, 1f, 0f ] };
|
||||
const vec3_unit_z :vec3 = vec3 { data: [ 0f, 0f, 1f ] };
|
||||
const vec3_identity :vec3 = vec3 { data: [ 1f, 1f, 1f ] };
|
||||
const vec3_zero :Vec3 = Vec3 { data: [ 0f, 0f, 0f ] };
|
||||
const vec3_unit_x :Vec3 = Vec3 { data: [ 1f, 0f, 0f ] };
|
||||
const vec3_unit_y :Vec3 = Vec3 { data: [ 0f, 1f, 0f ] };
|
||||
const vec3_unit_z :Vec3 = Vec3 { data: [ 0f, 0f, 1f ] };
|
||||
const vec3_identity :Vec3 = Vec3 { data: [ 1f, 1f, 1f ] };
|
||||
|
||||
//
|
||||
// Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn vec3(x:float, y:float, z:float) -> vec3 {
|
||||
vec3 { data: [ x, y, z ] }
|
||||
#[inline]
|
||||
pure fn Vec3(x:float, y:float, z:float) -> Vec3 {
|
||||
Vec3 { data: [ x, y, z ] }
|
||||
}
|
||||
|
||||
impl vec3: Vector3<float> {
|
||||
// #[inline(always)]
|
||||
// static pure fn _new(x:float, y:float, z:float) -> vec3 {
|
||||
// vec2 { data: [ x, y, z ] }
|
||||
impl Vec3: Vector3<float> {
|
||||
// #[inline]
|
||||
// static pure fn _new(x:float, y:float, z:float) -> Vec3 {
|
||||
// Vec2 { data: [ x, y, z ] }
|
||||
// }
|
||||
|
||||
#[inline(always)] pure fn x() -> float { self.data[0] }
|
||||
#[inline(always)] pure fn y() -> float { self.data[1] }
|
||||
#[inline(always)] pure fn z() -> float { self.data[2] }
|
||||
#[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(always)]
|
||||
fn cross(&&other:vec3) -> vec3 {
|
||||
vec3((self[1] * other[2]) - (self[2] * other[1]),
|
||||
#[inline]
|
||||
fn cross(&&other:Vec3) -> Vec3 {
|
||||
Vec3((self[1] * other[2]) - (self[2] * other[1]),
|
||||
(self[2] * other[0]) - (self[0] * other[2]),
|
||||
(self[0] * other[1]) - (self[1] * other[0]))
|
||||
}
|
||||
|
||||
// #[inline(always)] static pure fn unit_x() -> vec3 { vec3(1f, 0f, 0f) }
|
||||
// #[inline(always)] static pure fn unit_y() -> vec3 { vec3(0f, 1f, 0f) }
|
||||
// #[inline(always)] static pure fn unit_z() -> vec3 { vec3(0f, 0f, 1f) }
|
||||
// #[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) }
|
||||
}
|
||||
|
||||
impl vec3: Vector<float> {
|
||||
#[inline(always)]
|
||||
impl Vec3: Vector<float> {
|
||||
#[inline]
|
||||
static pure fn dim() -> uint { 3 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> float {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> vec3 {
|
||||
vec3(-self[0], -self[1], -self[2])
|
||||
#[inline]
|
||||
pure fn neg() -> Vec3 {
|
||||
Vec3(-self[0], -self[1], -self[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_f(&&value:float) -> vec3 {
|
||||
vec3(self[0] + value,
|
||||
#[inline]
|
||||
pure fn add_f(&&value:float) -> Vec3 {
|
||||
Vec3(self[0] + value,
|
||||
self[1] + value,
|
||||
self[2] + value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_f(&&value:float) -> vec3 {
|
||||
vec3(self[0] - value,
|
||||
#[inline]
|
||||
pure fn sub_f(&&value:float) -> Vec3 {
|
||||
Vec3(self[0] - value,
|
||||
self[1] - value,
|
||||
self[2] - value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> vec3 {
|
||||
vec3(self[0] * value,
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Vec3 {
|
||||
Vec3(self[0] * value,
|
||||
self[1] * value,
|
||||
self[2] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_f(&&value:float) -> vec3 {
|
||||
vec3(self[0] / value,
|
||||
#[inline]
|
||||
pure fn div_f(&&value:float) -> Vec3 {
|
||||
Vec3(self[0] / value,
|
||||
self[1] / value,
|
||||
self[2] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_v(&&other: vec3) -> vec3{
|
||||
vec3(self[0] + other[0],
|
||||
#[inline]
|
||||
pure fn add_v(&&other: Vec3) -> Vec3{
|
||||
Vec3(self[0] + other[0],
|
||||
self[1] + other[1],
|
||||
self[2] + other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_v(&&other: vec3) -> vec3{
|
||||
vec3(self[0] - other[0],
|
||||
#[inline]
|
||||
pure fn sub_v(&&other: Vec3) -> Vec3{
|
||||
Vec3(self[0] - other[0],
|
||||
self[1] - other[1],
|
||||
self[2] - other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn dot(&&other: vec3) -> float {
|
||||
#[inline]
|
||||
pure fn dot(&&other: Vec3) -> float {
|
||||
self[0] * other[0] +
|
||||
self[1] * other[1] +
|
||||
self[2] * other[2]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:vec3) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Vec3) -> bool {
|
||||
self[0] == other[0] &&
|
||||
self[1] == other[1] &&
|
||||
self[2] == other[2]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other: vec3) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other: Vec3) -> bool {
|
||||
self[0].fuzzy_eq(&other[0]) &&
|
||||
self[1].fuzzy_eq(&other[1]) &&
|
||||
self[2].fuzzy_eq(&other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn eq(&&other:vec3) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Vec3) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude2() -> float {
|
||||
self[0] * self[0] +
|
||||
self[1] * self[1] +
|
||||
self[2] * self[2]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude() -> float {
|
||||
self.magnitude2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn normalize() -> vec3 {
|
||||
#[inline]
|
||||
pure fn normalize() -> Vec3 {
|
||||
let n = 1f / self.magnitude();
|
||||
return self.mul_f(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn lerp(&&other:vec3, &&value:float) -> vec3 {
|
||||
#[inline]
|
||||
pure fn lerp(&&other:Vec3, &&value:float) -> Vec3 {
|
||||
self.add_v((other.sub_v(self)).mul_f(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn abs() -> vec3 {
|
||||
vec3(self[0].abs(),
|
||||
#[inline]
|
||||
pure fn abs() -> Vec3 {
|
||||
Vec3(self[0].abs(),
|
||||
self[1].abs(),
|
||||
self[2].abs())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn min(&&other:vec3) -> vec3 {
|
||||
vec3(min(self[0], other[0]),
|
||||
#[inline]
|
||||
pure fn min(&&other:Vec3) -> Vec3 {
|
||||
Vec3(min(self[0], other[0]),
|
||||
min(self[1], other[1]),
|
||||
min(self[2], other[2]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn max(&&other:vec3) -> vec3 {
|
||||
vec3(max(self[0], other[0]),
|
||||
#[inline]
|
||||
pure fn max(&&other:Vec3) -> Vec3 {
|
||||
Vec3(max(self[0], other[0]),
|
||||
max(self[1], other[1]),
|
||||
max(self[2], other[2]))
|
||||
}
|
||||
|
||||
#[inline(always)] static pure fn zero() -> vec3 { vec3(1f, 1f, 1f) }
|
||||
#[inline(always)] static pure fn identity() -> vec3 { vec3(1f, 1f, 1f) }
|
||||
#[inline] static pure fn zero() -> Vec3 { Vec3(1f, 1f, 1f) }
|
||||
#[inline] static pure fn identity() -> Vec3 { Vec3(1f, 1f, 1f) }
|
||||
}
|
||||
|
||||
impl vec3: ToStr {
|
||||
impl Vec3: ToStr {
|
||||
fn to_str() -> ~str {
|
||||
fmt!("vec3[ %f, %f, %f ]", self[0], self[1], self[2])
|
||||
fmt!("Vec3[ %f, %f, %f ]", self[0], self[1], self[2])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -438,132 +438,132 @@ impl vec3: ToStr {
|
|||
//
|
||||
// Vec4
|
||||
//
|
||||
struct vec4 { data:[float * 4] }
|
||||
struct Vec4 { data:[float * 4] }
|
||||
|
||||
const vec4_zero :vec4 = vec4 { data: [ 0f, 0f, 0f, 0f ] };
|
||||
const vec4_unit_x :vec4 = vec4 { data: [ 1f, 0f, 0f, 0f ] };
|
||||
const vec4_unit_y :vec4 = vec4 { data: [ 0f, 1f, 0f, 0f ] };
|
||||
const vec4_unit_z :vec4 = vec4 { data: [ 0f, 0f, 1f, 0f ] };
|
||||
const vec4_unit_w :vec4 = vec4 { data: [ 0f, 0f, 0f, 1f ] };
|
||||
const vec4_identity :vec4 = vec4 { data: [ 1f, 1f, 1f, 1f ] };
|
||||
const vec4_zero :Vec4 = Vec4 { data: [ 0f, 0f, 0f, 0f ] };
|
||||
const vec4_unit_x :Vec4 = Vec4 { data: [ 1f, 0f, 0f, 0f ] };
|
||||
const vec4_unit_y :Vec4 = Vec4 { data: [ 0f, 1f, 0f, 0f ] };
|
||||
const vec4_unit_z :Vec4 = Vec4 { data: [ 0f, 0f, 1f, 0f ] };
|
||||
const vec4_unit_w :Vec4 = Vec4 { data: [ 0f, 0f, 0f, 1f ] };
|
||||
const vec4_identity :Vec4 = Vec4 { data: [ 1f, 1f, 1f, 1f ] };
|
||||
|
||||
//
|
||||
// Constructor
|
||||
//
|
||||
#[inline(always)]
|
||||
pure fn vec4(x:float, y:float, z:float, w:float) -> vec4 {
|
||||
vec4 { data: [ x, y, z, w ] }
|
||||
#[inline]
|
||||
pure fn Vec4(x:float, y:float, z:float, w:float) -> Vec4 {
|
||||
Vec4 { data: [ x, y, z, w ] }
|
||||
}
|
||||
|
||||
impl vec4: Vector4<float> {
|
||||
// #[inline(always)]
|
||||
// static pure fn _new(x:float, y:float, z:float, w:float) -> vec3 {
|
||||
// vec2 { data: [ x, y, z, w ] }
|
||||
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(always)] pure fn x() -> float { self.data[0] }
|
||||
#[inline(always)] pure fn y() -> float { self.data[1] }
|
||||
#[inline(always)] pure fn z() -> float { self.data[2] }
|
||||
#[inline(always)] pure fn w() -> float { self.data[3] }
|
||||
#[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(always)] static pure fn unit_x() -> vec4 { vec4(1f, 0f, 0f, 0f) }
|
||||
// #[inline(always)] static pure fn unit_y() -> vec4 { vec4(0f, 1f, 0f, 0f) }
|
||||
// #[inline(always)] static pure fn unit_z() -> vec4 { vec4(0f, 0f, 1f, 0f) }
|
||||
// #[inline(always)] static pure fn unit_w() -> vec4 { vec4(0f, 0f, 0f, 1f) }
|
||||
// #[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) }
|
||||
}
|
||||
|
||||
impl vec4: Vector<float> {
|
||||
#[inline(always)]
|
||||
impl Vec4: Vector<float> {
|
||||
#[inline]
|
||||
static pure fn dim() -> uint { 4 }
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn index(&&i: uint) -> float {
|
||||
self.data[i]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn neg() -> vec4 {
|
||||
vec4(-self[0], -self[1], -self[2], -self[3])
|
||||
#[inline]
|
||||
pure fn neg() -> Vec4 {
|
||||
Vec4(-self[0], -self[1], -self[2], -self[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_f(&&value:float) -> vec4 {
|
||||
vec4(self[0] + value,
|
||||
#[inline]
|
||||
pure fn add_f(&&value:float) -> Vec4 {
|
||||
Vec4(self[0] + value,
|
||||
self[1] + value,
|
||||
self[2] + value,
|
||||
self[3] + value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_f(&&value:float) -> vec4 {
|
||||
vec4(self[0] - value,
|
||||
#[inline]
|
||||
pure fn sub_f(&&value:float) -> Vec4 {
|
||||
Vec4(self[0] - value,
|
||||
self[1] - value,
|
||||
self[2] - value,
|
||||
self[3] - value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_f(&&value:float) -> vec4 {
|
||||
vec4(self[0] * value,
|
||||
#[inline]
|
||||
pure fn mul_f(&&value:float) -> Vec4 {
|
||||
Vec4(self[0] * value,
|
||||
self[1] * value,
|
||||
self[2] * value,
|
||||
self[3] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_f(&&value:float) -> vec4 {
|
||||
vec4(self[0] / value,
|
||||
#[inline]
|
||||
pure fn div_f(&&value:float) -> Vec4 {
|
||||
Vec4(self[0] / value,
|
||||
self[1] / value,
|
||||
self[2] / value,
|
||||
self[3] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn add_v(&&other: vec4) -> vec4{
|
||||
vec4(self[0] + other[0],
|
||||
#[inline]
|
||||
pure fn add_v(&&other: Vec4) -> Vec4{
|
||||
Vec4(self[0] + other[0],
|
||||
self[1] + other[1],
|
||||
self[2] + other[2],
|
||||
self[3] + other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn sub_v(&&other: vec4) -> vec4{
|
||||
vec4(self[0] - other[0],
|
||||
#[inline]
|
||||
pure fn sub_v(&&other: Vec4) -> Vec4{
|
||||
Vec4(self[0] - other[0],
|
||||
self[1] - other[1],
|
||||
self[2] - other[2],
|
||||
self[3] - other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn dot(&&other:vec4) -> float {
|
||||
#[inline]
|
||||
pure fn dot(&&other:Vec4) -> float {
|
||||
self[0] * other[0] +
|
||||
self[1] * other[1] +
|
||||
self[2] * other[2] +
|
||||
self[3] * other[3]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn exact_eq(&&other:vec4) -> bool {
|
||||
#[inline]
|
||||
pure fn exact_eq(&&other:Vec4) -> bool {
|
||||
self[0] == other[0] &&
|
||||
self[1] == other[1] &&
|
||||
self[2] == other[2] &&
|
||||
self[3] == other[3]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn fuzzy_eq(&&other: vec4) -> bool {
|
||||
#[inline]
|
||||
pure fn fuzzy_eq(&&other: Vec4) -> bool {
|
||||
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(always)]
|
||||
pure fn eq(&&other:vec4) -> bool {
|
||||
#[inline]
|
||||
pure fn eq(&&other:Vec4) -> bool {
|
||||
self.fuzzy_eq(other)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude2() -> float {
|
||||
self[0] * self[0] +
|
||||
self[1] * self[1] +
|
||||
|
@ -571,52 +571,52 @@ impl vec4: Vector<float> {
|
|||
self[3] * self[3]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[inline]
|
||||
pure fn magnitude() -> float {
|
||||
self.magnitude2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn normalize() -> vec4 {
|
||||
#[inline]
|
||||
pure fn normalize() -> Vec4 {
|
||||
let n = 1f / self.magnitude();
|
||||
return self.mul_f(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn lerp(&&other:vec4, &&value:float) -> vec4 {
|
||||
#[inline]
|
||||
pure fn lerp(&&other:Vec4, &&value:float) -> Vec4 {
|
||||
self.add_v((other.sub_v(self)).mul_f(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn abs() -> vec4 {
|
||||
vec4(self[0].abs(),
|
||||
#[inline]
|
||||
pure fn abs() -> Vec4 {
|
||||
Vec4(self[0].abs(),
|
||||
self[1].abs(),
|
||||
self[2].abs(),
|
||||
self[3].abs())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn min(&&other:vec4) -> vec4 {
|
||||
vec4(min(self[0], other[0]),
|
||||
#[inline]
|
||||
pure fn min(&&other:Vec4) -> Vec4 {
|
||||
Vec4(min(self[0], other[0]),
|
||||
min(self[1], other[1]),
|
||||
min(self[2], other[2]),
|
||||
min(self[3], other[3]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn max(&&other:vec4) -> vec4 {
|
||||
vec4(max(self[0], other[0]),
|
||||
#[inline]
|
||||
pure fn max(&&other:Vec4) -> Vec4 {
|
||||
Vec4(max(self[0], other[0]),
|
||||
max(self[1], other[1]),
|
||||
max(self[2], other[2]),
|
||||
max(self[3], other[3]))
|
||||
}
|
||||
|
||||
#[inline(always)] static pure fn zero() -> vec4 { vec4(1f, 1f, 1f, 1f) }
|
||||
#[inline(always)] static pure fn identity() -> vec4 { vec4(1f, 1f, 1f, 1f) }
|
||||
#[inline] static pure fn zero() -> Vec4 { Vec4(1f, 1f, 1f, 1f) }
|
||||
#[inline] static pure fn identity() -> Vec4 { Vec4(1f, 1f, 1f, 1f) }
|
||||
}
|
||||
|
||||
impl vec4: ToStr {
|
||||
impl Vec4: ToStr {
|
||||
fn to_str() -> ~str {
|
||||
fmt!("vec4[ %f, %f, %f, %f ]", self[0], self[1], self[2], self[3])
|
||||
fmt!("Vec4[ %f, %f, %f, %f ]", self[0], self[1], self[2], self[3])
|
||||
}
|
||||
}
|
182
test/test_mat.rs
182
test/test_mat.rs
|
@ -1,48 +1,48 @@
|
|||
import omath::mat::*;
|
||||
import omath::vec::*;
|
||||
use omath::mat::*;
|
||||
use omath::vec::*;
|
||||
|
||||
// TODO
|
||||
|
||||
#[test]
|
||||
fn test_mat2() {
|
||||
let a = mat2 { data: [ vec2 { data: [ 1f, 3f ] },
|
||||
vec2 { data: [ 2f, 4f ] } ] };
|
||||
let b = mat2 { data: [ vec2 { data: [ 2f, 4f ] },
|
||||
vec2 { data: [ 3f, 5f ] } ] };
|
||||
let v1 = vec2(1f, 2f);
|
||||
fn test_Mat2() {
|
||||
let a = Mat2 { data: [ Vec2 { data: [ 1f, 3f ] },
|
||||
Vec2 { data: [ 2f, 4f ] } ] };
|
||||
let b = Mat2 { data: [ Vec2 { data: [ 2f, 4f ] },
|
||||
Vec2 { data: [ 3f, 5f ] } ] };
|
||||
let v1 = Vec2(1f, 2f);
|
||||
let f1 = 0.5f;
|
||||
|
||||
assert a == mat2(1f, 3f,
|
||||
assert a == Mat2(1f, 3f,
|
||||
2f, 4f);
|
||||
|
||||
assert a == mat2_v(vec2(1f, 3f),
|
||||
vec2(2f, 4f));
|
||||
assert a == mat2_v(Vec2(1f, 3f),
|
||||
Vec2(2f, 4f));
|
||||
|
||||
assert a[0] == vec2(1f, 3f);
|
||||
assert a[1] == vec2(2f, 4f);
|
||||
assert a[0] == Vec2(1f, 3f);
|
||||
assert a[1] == Vec2(2f, 4f);
|
||||
|
||||
assert a.row(0) == vec2(1f, 2f);
|
||||
assert a.row(1) == vec2(3f, 4f);
|
||||
assert a.row(0) == Vec2(1f, 2f);
|
||||
assert a.row(1) == Vec2(3f, 4f);
|
||||
|
||||
assert a.col(0) == vec2(1f, 3f);
|
||||
assert a.col(1) == vec2(2f, 4f);
|
||||
assert a.col(0) == Vec2(1f, 3f);
|
||||
assert a.col(1) == Vec2(2f, 4f);
|
||||
|
||||
assert a.neg() == mat2(-1f, -3f,
|
||||
assert a.neg() == Mat2(-1f, -3f,
|
||||
-2f, -4f);
|
||||
assert -a == a.neg();
|
||||
|
||||
assert a.mul_f(f1) == mat2(0.5f, 1.5f,
|
||||
assert a.mul_f(f1) == Mat2(0.5f, 1.5f,
|
||||
1.0f, 2.0f);
|
||||
assert a.mul_v(v1) == vec2(5f, 11f);
|
||||
assert a.mul_v(v1) == Vec2(5f, 11f);
|
||||
|
||||
assert a.add_m(b) == mat2(3f, 7f,
|
||||
assert a.add_m(b) == Mat2(3f, 7f,
|
||||
5f, 9f);
|
||||
assert a.sub_m(b) == mat2(-1f, -1f,
|
||||
assert a.sub_m(b) == Mat2(-1f, -1f,
|
||||
-1f, -1f);
|
||||
assert a.mul_m(b) == mat2(10.0, 22.0,
|
||||
assert a.mul_m(b) == Mat2(10.0, 22.0,
|
||||
13.0, 29.0);
|
||||
|
||||
assert a.transpose() == mat2(1f, 2f,
|
||||
assert a.transpose() == Mat2(1f, 2f,
|
||||
3f, 4f);
|
||||
|
||||
// exact_eq
|
||||
|
@ -59,7 +59,7 @@ fn test_mat2() {
|
|||
assert !a.is_diagonal();
|
||||
assert a.is_rotated();
|
||||
|
||||
let c = mat2(2f, 1f,
|
||||
let c = Mat2(2f, 1f,
|
||||
1f, 2f);
|
||||
assert !c.is_identity();
|
||||
assert c.is_symmetric();
|
||||
|
@ -68,57 +68,57 @@ fn test_mat2() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_mat3() {
|
||||
let a = mat3 { data: [ vec3 { data: [ 1f, 4f, 7f ] },
|
||||
vec3 { data: [ 2f, 5f, 8f ] },
|
||||
vec3 { data: [ 3f, 6f, 9f ] } ] };
|
||||
let b = mat3 { data: [ vec3 { data: [ 2f, 5f, 8f ] },
|
||||
vec3 { data: [ 3f, 6f, 9f ] },
|
||||
vec3 { data: [ 4f, 7f, 10f ] } ] };
|
||||
let v1 = vec3(1f, 2f, 3f);
|
||||
fn test_Mat3() {
|
||||
let a = Mat3 { data: [ Vec3 { data: [ 1f, 4f, 7f ] },
|
||||
Vec3 { data: [ 2f, 5f, 8f ] },
|
||||
Vec3 { data: [ 3f, 6f, 9f ] } ] };
|
||||
let b = Mat3 { data: [ Vec3 { data: [ 2f, 5f, 8f ] },
|
||||
Vec3 { data: [ 3f, 6f, 9f ] },
|
||||
Vec3 { data: [ 4f, 7f, 10f ] } ] };
|
||||
let v1 = Vec3(1f, 2f, 3f);
|
||||
let f1 = 0.5f;
|
||||
|
||||
assert a == mat3(1f, 4f, 7f,
|
||||
assert a == Mat3(1f, 4f, 7f,
|
||||
2f, 5f, 8f,
|
||||
3f, 6f, 9f);
|
||||
|
||||
assert a == mat3_v(vec3(1f, 4f, 7f),
|
||||
vec3(2f, 5f, 8f),
|
||||
vec3(3f, 6f, 9f));
|
||||
assert a == mat3_v(Vec3(1f, 4f, 7f),
|
||||
Vec3(2f, 5f, 8f),
|
||||
Vec3(3f, 6f, 9f));
|
||||
|
||||
assert a[0] == vec3(1f, 4f, 7f);
|
||||
assert a[1] == vec3(2f, 5f, 8f);
|
||||
assert a[2] == vec3(3f, 6f, 9f);
|
||||
assert a[0] == Vec3(1f, 4f, 7f);
|
||||
assert a[1] == Vec3(2f, 5f, 8f);
|
||||
assert a[2] == Vec3(3f, 6f, 9f);
|
||||
|
||||
assert a.row(0) == vec3(1f, 2f, 3f);
|
||||
assert a.row(1) == vec3(4f, 5f, 6f);
|
||||
assert a.row(2) == vec3(7f, 8f, 9f);
|
||||
assert a.row(0) == Vec3(1f, 2f, 3f);
|
||||
assert a.row(1) == Vec3(4f, 5f, 6f);
|
||||
assert a.row(2) == Vec3(7f, 8f, 9f);
|
||||
|
||||
assert a.col(0) == vec3(1f, 4f, 7f);
|
||||
assert a.col(1) == vec3(2f, 5f, 8f);
|
||||
assert a.col(2) == vec3(3f, 6f, 9f);
|
||||
assert a.col(0) == Vec3(1f, 4f, 7f);
|
||||
assert a.col(1) == Vec3(2f, 5f, 8f);
|
||||
assert a.col(2) == Vec3(3f, 6f, 9f);
|
||||
|
||||
assert a.neg() == mat3(-1f, -4f, -7f,
|
||||
assert a.neg() == Mat3(-1f, -4f, -7f,
|
||||
-2f, -5f, -8f,
|
||||
-3f, -6f, -9f);
|
||||
assert -a == a.neg();
|
||||
|
||||
assert a.mul_f(f1) == mat3(0.5f, 2.0f, 3.5f,
|
||||
assert a.mul_f(f1) == Mat3(0.5f, 2.0f, 3.5f,
|
||||
1.0f, 2.5f, 4.0f,
|
||||
1.5f, 3.0f, 4.5f);
|
||||
assert a.mul_v(v1) == vec3(14f, 32f, 50f);
|
||||
assert a.mul_v(v1) == Vec3(14f, 32f, 50f);
|
||||
|
||||
assert a.add_m(b) == mat3(3f, 9f, 15f,
|
||||
assert a.add_m(b) == Mat3(3f, 9f, 15f,
|
||||
5f, 11f, 17f,
|
||||
7f, 13f, 19f);
|
||||
assert a.sub_m(b) == mat3(-1f, -1f, -1f,
|
||||
assert a.sub_m(b) == Mat3(-1f, -1f, -1f,
|
||||
-1f, -1f, -1f,
|
||||
-1f, -1f, -1f);
|
||||
assert a.mul_m(b) == mat3(36f, 81f, 126f,
|
||||
assert a.mul_m(b) == Mat3(36f, 81f, 126f,
|
||||
42f, 96f, 150f,
|
||||
48f, 111f, 174f);
|
||||
|
||||
assert a.transpose() == mat3(1f, 2f, 3f,
|
||||
assert a.transpose() == Mat3(1f, 2f, 3f,
|
||||
4f, 5f, 6f,
|
||||
7f, 8f, 9f);
|
||||
|
||||
|
@ -136,7 +136,7 @@ fn test_mat3() {
|
|||
assert !a.is_diagonal();
|
||||
assert a.is_rotated();
|
||||
|
||||
let c = mat3(3f, 2f, 1f,
|
||||
let c = Mat3(3f, 2f, 1f,
|
||||
2f, 3f, 2f,
|
||||
1f, 2f, 3f);
|
||||
assert !c.is_identity();
|
||||
|
@ -144,78 +144,78 @@ fn test_mat3() {
|
|||
assert !c.is_diagonal();
|
||||
assert c.is_rotated();
|
||||
|
||||
assert a.to_mat4() == mat4(1f, 4f, 7f, 0f,
|
||||
assert a.to_Mat4() == Mat4(1f, 4f, 7f, 0f,
|
||||
2f, 5f, 8f, 0f,
|
||||
3f, 6f, 9f, 0f,
|
||||
0f, 0f, 0f, 1f);
|
||||
|
||||
// to_quaternion
|
||||
// to_Quaternion
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mat4() {
|
||||
let a = mat4 { data: [ vec4 { data: [ 1f, 5f, 9f, 13f ] },
|
||||
vec4 { data: [ 2f, 6f, 10f, 14f ] },
|
||||
vec4 { data: [ 3f, 7f, 11f, 15f ] },
|
||||
vec4 { data: [ 4f, 8f, 12f, 16f ] } ] };
|
||||
let b = mat4 { data: [ vec4 { data: [ 2f, 6f, 10f, 14f ] },
|
||||
vec4 { data: [ 3f, 7f, 11f, 15f ] },
|
||||
vec4 { data: [ 4f, 8f, 12f, 16f ] },
|
||||
vec4 { data: [ 5f, 9f, 13f, 17f ] } ] };
|
||||
let v1 = vec4(1f, 2f, 3f, 4f);
|
||||
fn test_Mat4() {
|
||||
let a = Mat4 { data: [ Vec4 { data: [ 1f, 5f, 9f, 13f ] },
|
||||
Vec4 { data: [ 2f, 6f, 10f, 14f ] },
|
||||
Vec4 { data: [ 3f, 7f, 11f, 15f ] },
|
||||
Vec4 { data: [ 4f, 8f, 12f, 16f ] } ] };
|
||||
let b = Mat4 { data: [ Vec4 { data: [ 2f, 6f, 10f, 14f ] },
|
||||
Vec4 { data: [ 3f, 7f, 11f, 15f ] },
|
||||
Vec4 { data: [ 4f, 8f, 12f, 16f ] },
|
||||
Vec4 { data: [ 5f, 9f, 13f, 17f ] } ] };
|
||||
let v1 = Vec4(1f, 2f, 3f, 4f);
|
||||
let f1 = 0.5f;
|
||||
|
||||
assert a == mat4(1f, 5f, 9f, 13f,
|
||||
assert a == Mat4(1f, 5f, 9f, 13f,
|
||||
2f, 6f, 10f, 14f,
|
||||
3f, 7f, 11f, 15f,
|
||||
4f, 8f, 12f, 16f);
|
||||
|
||||
assert a == mat4_v(vec4(1f, 5f, 9f, 13f),
|
||||
vec4(2f, 6f, 10f, 14f),
|
||||
vec4(3f, 7f, 11f, 15f),
|
||||
vec4(4f, 8f, 12f, 16f));
|
||||
assert a == mat4_v(Vec4(1f, 5f, 9f, 13f),
|
||||
Vec4(2f, 6f, 10f, 14f),
|
||||
Vec4(3f, 7f, 11f, 15f),
|
||||
Vec4(4f, 8f, 12f, 16f));
|
||||
|
||||
assert a[0] == vec4(1f, 5f, 9f, 13f);
|
||||
assert a[1] == vec4(2f, 6f, 10f, 14f);
|
||||
assert a[2] == vec4(3f, 7f, 11f, 15f);
|
||||
assert a[3] == vec4(4f, 8f, 12f, 16f);
|
||||
assert a[0] == Vec4(1f, 5f, 9f, 13f);
|
||||
assert a[1] == Vec4(2f, 6f, 10f, 14f);
|
||||
assert a[2] == Vec4(3f, 7f, 11f, 15f);
|
||||
assert a[3] == Vec4(4f, 8f, 12f, 16f);
|
||||
|
||||
assert a.row(0) == vec4( 1f, 2f, 3f, 4f);
|
||||
assert a.row(1) == vec4( 5f, 6f, 7f, 8f);
|
||||
assert a.row(2) == vec4( 9f, 10f, 11f, 12f);
|
||||
assert a.row(3) == vec4(13f, 14f, 15f, 16f);
|
||||
assert a.row(0) == Vec4( 1f, 2f, 3f, 4f);
|
||||
assert a.row(1) == Vec4( 5f, 6f, 7f, 8f);
|
||||
assert a.row(2) == Vec4( 9f, 10f, 11f, 12f);
|
||||
assert a.row(3) == Vec4(13f, 14f, 15f, 16f);
|
||||
|
||||
assert a.col(0) == vec4(1f, 5f, 9f, 13f);
|
||||
assert a.col(1) == vec4(2f, 6f, 10f, 14f);
|
||||
assert a.col(2) == vec4(3f, 7f, 11f, 15f);
|
||||
assert a.col(3) == vec4(4f, 8f, 12f, 16f);
|
||||
assert a.col(0) == Vec4(1f, 5f, 9f, 13f);
|
||||
assert a.col(1) == Vec4(2f, 6f, 10f, 14f);
|
||||
assert a.col(2) == Vec4(3f, 7f, 11f, 15f);
|
||||
assert a.col(3) == Vec4(4f, 8f, 12f, 16f);
|
||||
|
||||
assert a.neg() == mat4(-1f, -5f, -9f, -13f,
|
||||
assert a.neg() == Mat4(-1f, -5f, -9f, -13f,
|
||||
-2f, -6f, -10f, -14f,
|
||||
-3f, -7f, -11f, -15f,
|
||||
-4f, -8f, -12f, -16f);
|
||||
assert -a == a.neg();
|
||||
|
||||
assert a.mul_f(f1) == mat4(0.5f, 2.5f, 4.5f, 6.5f,
|
||||
assert a.mul_f(f1) == Mat4(0.5f, 2.5f, 4.5f, 6.5f,
|
||||
1.0f, 3.0f, 5.0f, 7.0f,
|
||||
1.5f, 3.5f, 5.5f, 7.5f,
|
||||
2.0f, 4.0f, 6.0f, 8.0f);
|
||||
assert a.mul_v(v1) == vec4(30.0, 70.0, 110.0, 150.0);
|
||||
assert a.mul_v(v1) == Vec4(30.0, 70.0, 110.0, 150.0);
|
||||
|
||||
assert a.add_m(b) == mat4(3f, 11f, 19f, 27f,
|
||||
assert a.add_m(b) == Mat4(3f, 11f, 19f, 27f,
|
||||
5f, 13f, 21f, 29f,
|
||||
7f, 15f, 23f, 31f,
|
||||
9f, 17f, 25f, 33f);
|
||||
assert a.sub_m(b) == mat4(-1f, -1f, -1f, -1f,
|
||||
assert a.sub_m(b) == Mat4(-1f, -1f, -1f, -1f,
|
||||
-1f, -1f, -1f, -1f,
|
||||
-1f, -1f, -1f, -1f,
|
||||
-1f, -1f, -1f, -1f);
|
||||
assert a.mul_m(b) == mat4(100f, 228f, 356f, 484f,
|
||||
assert a.mul_m(b) == Mat4(100f, 228f, 356f, 484f,
|
||||
110f, 254f, 398f, 542f,
|
||||
120f, 280f, 440f, 600f,
|
||||
130f, 306f, 482f, 658f);
|
||||
|
||||
assert a.transpose() == mat4( 1f, 2f, 3f, 4f,
|
||||
assert a.transpose() == Mat4( 1f, 2f, 3f, 4f,
|
||||
5f, 6f, 7f, 8f,
|
||||
9f, 10f, 11f, 12f,
|
||||
13f, 14f, 15f, 16f);
|
||||
|
@ -234,7 +234,7 @@ fn test_mat4() {
|
|||
assert !a.is_diagonal();
|
||||
assert a.is_rotated();
|
||||
|
||||
let c = mat4(4f, 3f, 2f, 1f,
|
||||
let c = Mat4(4f, 3f, 2f, 1f,
|
||||
3f, 4f, 3f, 2f,
|
||||
2f, 3f, 4f, 3f,
|
||||
1f, 2f, 3f, 4f);
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
#[comment = "Unittests for omath"];
|
||||
#[crate_type = "unittests"];
|
||||
|
||||
use std;
|
||||
use omath;
|
||||
extern mod std;
|
||||
extern mod omath;
|
||||
|
||||
mod test_mat;
|
||||
mod test_math;
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
import omath::mat::*;
|
||||
import omath::quat::*;
|
||||
import omath::vec::*;
|
||||
use omath::mat::*;
|
||||
use omath::quat::*;
|
||||
use omath::vec::*;
|
||||
|
||||
// TODO
|
||||
|
||||
#[test]
|
||||
fn test_quat() {
|
||||
let a = quat { data: [ 1f, 2f, 3f, 4f ] };
|
||||
// let b = quat { data: [ 5f, 6f, 7f, 8f ] };
|
||||
fn test_Quat() {
|
||||
let a = Quat { data: [ 1f, 2f, 3f, 4f ] };
|
||||
// let b = Quat { data: [ 5f, 6f, 7f, 8f ] };
|
||||
// let f1 = 1.5f;
|
||||
// let f2 = 0.5f;
|
||||
|
||||
assert a == quat(1f, 2f, 3f, 4f);
|
||||
assert a == Quat(1f, 2f, 3f, 4f);
|
||||
|
||||
assert quat_zero == quat(0f, 0f, 0f, 0f);
|
||||
assert quat_identity == quat(1f, 0f, 0f, 0f);
|
||||
assert quat_zero == Quat(0f, 0f, 0f, 0f);
|
||||
assert quat_identity == Quat(1f, 0f, 0f, 0f);
|
||||
|
||||
assert a[0] == 1f;
|
||||
assert a[1] == 2f;
|
||||
|
|
150
test/test_vec.rs
150
test/test_vec.rs
|
@ -1,39 +1,39 @@
|
|||
import std::cmp::FuzzyEq;
|
||||
import omath::vec::*;
|
||||
use std::cmp::FuzzyEq;
|
||||
use omath::vec::*;
|
||||
|
||||
// TODO
|
||||
|
||||
#[test]
|
||||
fn test_vec2() {
|
||||
// assert vec2::dim == 2;
|
||||
fn test_Vec2() {
|
||||
// assert Vec2::dim == 2;
|
||||
|
||||
let a = vec2 { data: [ 1f, 2f ] };
|
||||
let b = vec2 { data: [ 3f, 4f ] };
|
||||
let a = Vec2 { data: [ 1f, 2f ] };
|
||||
let b = Vec2 { data: [ 3f, 4f ] };
|
||||
let f1 = 1.5f;
|
||||
let f2 = 0.5f;
|
||||
|
||||
assert a == vec2(1f, 2f);
|
||||
assert a == Vec2(1f, 2f);
|
||||
|
||||
assert vec2_zero == vec2(0f, 0f);
|
||||
assert vec2_unit_x == vec2(1f, 0f);
|
||||
assert vec2_unit_y == vec2(0f, 1f);
|
||||
assert vec2_identity == vec2(1f, 1f);
|
||||
assert vec2_zero == Vec2(0f, 0f);
|
||||
assert vec2_unit_x == Vec2(1f, 0f);
|
||||
assert vec2_unit_y == Vec2(0f, 1f);
|
||||
assert vec2_identity == Vec2(1f, 1f);
|
||||
|
||||
assert a[0] == 1f;
|
||||
assert a[1] == 2f;
|
||||
assert a.x() == 1f;
|
||||
assert a.y() == 2f;
|
||||
|
||||
assert -a == vec2(-1f, -2f);
|
||||
assert a.neg() == vec2(-1f, -2f);
|
||||
assert -a == Vec2(-1f, -2f);
|
||||
assert a.neg() == Vec2(-1f, -2f);
|
||||
|
||||
assert a.add_f(f1) == vec2( 2.5f, 3.5f);
|
||||
assert a.sub_f(f1) == vec2(-0.5f, 0.5f);
|
||||
assert a.mul_f(f1) == vec2( 1.5f, 3.0f);
|
||||
assert a.div_f(f2) == vec2( 2.0f, 4.0f);
|
||||
assert a.add_f(f1) == Vec2( 2.5f, 3.5f);
|
||||
assert a.sub_f(f1) == Vec2(-0.5f, 0.5f);
|
||||
assert a.mul_f(f1) == Vec2( 1.5f, 3.0f);
|
||||
assert a.div_f(f2) == Vec2( 2.0f, 4.0f);
|
||||
|
||||
assert a.add_v(b) == vec2( 4f, 6f);
|
||||
assert a.sub_v(b) == vec2(-2f, -2f);
|
||||
assert a.add_v(b) == Vec2( 4f, 6f);
|
||||
assert a.sub_v(b) == Vec2(-2f, -2f);
|
||||
|
||||
// exact_eq
|
||||
// fuzzy_eq
|
||||
|
@ -42,32 +42,32 @@ fn test_vec2() {
|
|||
assert a.magnitude2().fuzzy_eq(&5f);
|
||||
assert a.magnitude().fuzzy_eq(&2.236068f);
|
||||
|
||||
let c = vec2(-2.0f, -1.0f);
|
||||
let d = vec2( 1.0f, 0.0f);
|
||||
let c = Vec2(-2.0f, -1.0f);
|
||||
let d = Vec2( 1.0f, 0.0f);
|
||||
let f3 = 0.75f;
|
||||
|
||||
assert c.lerp(d, f3) == vec2(0.250f, -0.250f);
|
||||
assert c.abs() == vec2( 2.0f, 1.0f);
|
||||
assert c.min(d) == vec2(-2.0f, -1.0f);
|
||||
assert c.max(d) == vec2( 1.0f, 0.0f);
|
||||
assert c.lerp(d, f3) == Vec2(0.250f, -0.250f);
|
||||
assert c.abs() == Vec2( 2.0f, 1.0f);
|
||||
assert c.min(d) == Vec2(-2.0f, -1.0f);
|
||||
assert c.max(d) == Vec2( 1.0f, 0.0f);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec3() {
|
||||
// assert vec3::dim == 3;
|
||||
fn test_Vec3() {
|
||||
// assert Vec3::dim == 3;
|
||||
|
||||
let a = vec3 { data: [ 1f, 2f, 3f ] };
|
||||
let b = vec3 { data: [ 4f, 5f, 6f ] };
|
||||
let a = Vec3 { data: [ 1f, 2f, 3f ] };
|
||||
let b = Vec3 { data: [ 4f, 5f, 6f ] };
|
||||
let f1 = 1.5f;
|
||||
let f2 = 0.5f;
|
||||
|
||||
assert a == vec3(1f, 2f, 3f);
|
||||
assert a == Vec3(1f, 2f, 3f);
|
||||
|
||||
assert vec3_zero == vec3(0f, 0f, 0f);
|
||||
assert vec3_unit_x == vec3(1f, 0f, 0f);
|
||||
assert vec3_unit_y == vec3(0f, 1f, 0f);
|
||||
assert vec3_unit_z == vec3(0f, 0f, 1f);
|
||||
assert vec3_identity == vec3(1f, 1f, 1f);
|
||||
assert vec3_zero == Vec3(0f, 0f, 0f);
|
||||
assert vec3_unit_x == Vec3(1f, 0f, 0f);
|
||||
assert vec3_unit_y == Vec3(0f, 1f, 0f);
|
||||
assert vec3_unit_z == Vec3(0f, 0f, 1f);
|
||||
assert vec3_identity == Vec3(1f, 1f, 1f);
|
||||
|
||||
assert a[0] == 1f;
|
||||
assert a[1] == 2f;
|
||||
|
@ -76,18 +76,18 @@ fn test_vec3() {
|
|||
assert a.y() == 2f;
|
||||
assert a.z() == 3f;
|
||||
|
||||
assert a.cross(b) == vec3(-3f, 6f, -3f);
|
||||
assert a.cross(b) == Vec3(-3f, 6f, -3f);
|
||||
|
||||
assert -a == vec3(-1f, -2f, -3f);
|
||||
assert a.neg() == vec3(-1f, -2f, -3f);
|
||||
assert -a == Vec3(-1f, -2f, -3f);
|
||||
assert a.neg() == Vec3(-1f, -2f, -3f);
|
||||
|
||||
assert a.add_f(f1) == vec3( 2.5f, 3.5f, 4.5f);
|
||||
assert a.sub_f(f1) == vec3(-0.5f, 0.5f, 1.5f);
|
||||
assert a.mul_f(f1) == vec3( 1.5f, 3.0f, 4.5f);
|
||||
assert a.div_f(f2) == vec3( 2.0f, 4.0f, 6.0f);
|
||||
assert a.add_f(f1) == Vec3( 2.5f, 3.5f, 4.5f);
|
||||
assert a.sub_f(f1) == Vec3(-0.5f, 0.5f, 1.5f);
|
||||
assert a.mul_f(f1) == Vec3( 1.5f, 3.0f, 4.5f);
|
||||
assert a.div_f(f2) == Vec3( 2.0f, 4.0f, 6.0f);
|
||||
|
||||
assert a.add_v(b) == vec3( 5f, 7f, 9f);
|
||||
assert a.sub_v(b) == vec3(-3f, -3f, -3f);
|
||||
assert a.add_v(b) == Vec3( 5f, 7f, 9f);
|
||||
assert a.sub_v(b) == Vec3(-3f, -3f, -3f);
|
||||
|
||||
// exact_eq
|
||||
// fuzzy_eq
|
||||
|
@ -96,33 +96,33 @@ fn test_vec3() {
|
|||
assert a.magnitude2().fuzzy_eq(&14f);
|
||||
assert a.magnitude().fuzzy_eq(&3.74165738677f);
|
||||
|
||||
let c = vec3(-2.0f, -1.0f, 1.0f);
|
||||
let d = vec3( 1.0f, 0.0f, 0.5f);
|
||||
let c = Vec3(-2.0f, -1.0f, 1.0f);
|
||||
let d = Vec3( 1.0f, 0.0f, 0.5f);
|
||||
let f3 = 0.75f;
|
||||
|
||||
assert c.lerp(d, f3) == vec3(0.250f, -0.250f, 0.625f);
|
||||
assert c.abs() == vec3( 2.0f, 1.0f, 1.0f);
|
||||
assert c.min(d) == vec3(-2.0f, -1.0f, 0.5f);
|
||||
assert c.max(d) == vec3( 1.0f, 0.0f, 1.0f);
|
||||
assert c.lerp(d, f3) == Vec3(0.250f, -0.250f, 0.625f);
|
||||
assert c.abs() == Vec3( 2.0f, 1.0f, 1.0f);
|
||||
assert c.min(d) == Vec3(-2.0f, -1.0f, 0.5f);
|
||||
assert c.max(d) == Vec3( 1.0f, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec4() {
|
||||
// assert vec4::dim == 4;
|
||||
fn test_Vec4() {
|
||||
// assert Vec4::dim == 4;
|
||||
|
||||
let a = vec4 { data: [ 1f, 2f, 3f, 4f ] };
|
||||
let b = vec4 { data: [ 5f, 6f, 7f, 8f ] };
|
||||
let a = Vec4 { data: [ 1f, 2f, 3f, 4f ] };
|
||||
let b = Vec4 { data: [ 5f, 6f, 7f, 8f ] };
|
||||
let f1 = 1.5f;
|
||||
let f2 = 0.5f;
|
||||
|
||||
assert a == vec4(1f, 2f, 3f, 4f);
|
||||
assert a == Vec4(1f, 2f, 3f, 4f);
|
||||
|
||||
assert vec4_zero == vec4(0f, 0f, 0f, 0f);
|
||||
assert vec4_unit_x == vec4(1f, 0f, 0f, 0f);
|
||||
assert vec4_unit_y == vec4(0f, 1f, 0f, 0f);
|
||||
assert vec4_unit_z == vec4(0f, 0f, 1f, 0f);
|
||||
assert vec4_unit_w == vec4(0f, 0f, 0f, 1f);
|
||||
assert vec4_identity == vec4(1f, 1f, 1f, 1f);
|
||||
assert vec4_zero == Vec4(0f, 0f, 0f, 0f);
|
||||
assert vec4_unit_x == Vec4(1f, 0f, 0f, 0f);
|
||||
assert vec4_unit_y == Vec4(0f, 1f, 0f, 0f);
|
||||
assert vec4_unit_z == Vec4(0f, 0f, 1f, 0f);
|
||||
assert vec4_unit_w == Vec4(0f, 0f, 0f, 1f);
|
||||
assert vec4_identity == Vec4(1f, 1f, 1f, 1f);
|
||||
|
||||
assert a[0] == 1f;
|
||||
assert a[1] == 2f;
|
||||
|
@ -133,16 +133,16 @@ fn test_vec4() {
|
|||
assert a.z() == 3f;
|
||||
assert a.w() == 4f;
|
||||
|
||||
assert -a == vec4(-1f, -2f, -3f, -4f);
|
||||
assert a.neg() == vec4(-1f, -2f, -3f, -4f);
|
||||
assert -a == Vec4(-1f, -2f, -3f, -4f);
|
||||
assert a.neg() == Vec4(-1f, -2f, -3f, -4f);
|
||||
|
||||
assert a.add_f(f1) == vec4( 2.5f, 3.5f, 4.5f, 5.5f);
|
||||
assert a.sub_f(f1) == vec4(-0.5f, 0.5f, 1.5f, 2.5f);
|
||||
assert a.mul_f(f1) == vec4( 1.5f, 3.0f, 4.5f, 6.0f);
|
||||
assert a.div_f(f2) == vec4( 2.0f, 4.0f, 6.0f, 8.0f);
|
||||
assert a.add_f(f1) == Vec4( 2.5f, 3.5f, 4.5f, 5.5f);
|
||||
assert a.sub_f(f1) == Vec4(-0.5f, 0.5f, 1.5f, 2.5f);
|
||||
assert a.mul_f(f1) == Vec4( 1.5f, 3.0f, 4.5f, 6.0f);
|
||||
assert a.div_f(f2) == Vec4( 2.0f, 4.0f, 6.0f, 8.0f);
|
||||
|
||||
assert a.add_v(b) == vec4( 6f, 8f, 10f, 12f);
|
||||
assert a.sub_v(b) == vec4(-4f, -4f, -4f, -4f);
|
||||
assert a.add_v(b) == Vec4( 6f, 8f, 10f, 12f);
|
||||
assert a.sub_v(b) == Vec4(-4f, -4f, -4f, -4f);
|
||||
|
||||
assert a.dot(b) == 70f;
|
||||
|
||||
|
@ -153,12 +153,12 @@ fn test_vec4() {
|
|||
assert a.magnitude2().fuzzy_eq(&30f);
|
||||
assert a.magnitude().fuzzy_eq(&5.477226f);
|
||||
|
||||
let c = vec4(-2.0f, -1.0f, 1.0f, 2.0f);
|
||||
let d = vec4( 1.0f, 0.0f, 0.5f, 1.0f);
|
||||
let c = Vec4(-2.0f, -1.0f, 1.0f, 2.0f);
|
||||
let d = Vec4( 1.0f, 0.0f, 0.5f, 1.0f);
|
||||
let f3 = 0.75f;
|
||||
|
||||
assert c.lerp(d, f3) == vec4(0.250f, -0.250f, 0.625f, 1.250f);
|
||||
assert c.abs() == vec4( 2.0f, 1.0f, 1.0f, 2.0f);
|
||||
assert c.min(d) == vec4(-2.0f, -1.0f, 0.5f, 1.0f);
|
||||
assert c.max(d) == vec4( 1.0f, 0.0f, 1.0f, 2.0f);
|
||||
assert c.lerp(d, f3) == Vec4(0.250f, -0.250f, 0.625f, 1.250f);
|
||||
assert c.abs() == Vec4( 2.0f, 1.0f, 1.0f, 2.0f);
|
||||
assert c.min(d) == Vec4(-2.0f, -1.0f, 0.5f, 1.0f);
|
||||
assert c.max(d) == Vec4( 1.0f, 0.0f, 1.0f, 2.0f);
|
||||
}
|
Loading…
Reference in a new issue