Fix circular imports and subsequent build errors

This commit is contained in:
Brendan Zabarauskas 2012-09-08 00:17:26 +10:00
parent 0e8dab2cd0
commit f5b46e2459
11 changed files with 172 additions and 161 deletions

View file

@ -3,8 +3,8 @@
all:
rustc src/om3d.rc --lib --out-dir=lib
test: all
rustc --test -L lib test/*-test.rs -o test/build/test.elf
./test/build/test.elf
rustc --test -L lib test/test_om3d.rc -o test/build/test_om3d.elf
./test/build/test_om3d.elf
clean:
rm -R -f ./lib/*
rm -R -f ./test/*.elf
rm -R -f ./test/build/*

View file

@ -2,27 +2,9 @@
Here's some linear algebra I've been working on. I've translated it over from my unpublished D library that I was using to teach myself 3D mathematics.
For some reason my makefile isn't working - it give me the following error:
rustc src/om3d.rc --lib --out-dir=lib
error: failed to resolve imports
src/quat.rs:5:7: 5:14 error: unresolved import
src/quat.rs:5 import mat::*;
^~~~~~~
src/mat.rs:5:7: 5:15 error: unresolved import
src/mat.rs:5 import quat::*;
^~~~~~~~
src/projection.rs:2:7: 2:14 error: unresolved import
src/projection.rs:2 import mat::*;
^~~~~~~
error: aborting due to 4 previous errors
make: *** [all] Error 101
Any assistance would be most appreciated!
## Todo:
- Unittests: I have full unittest coverage on my D project so the algorithms should be correct. I just haven't implemented them in om3D-rs yet because of the unfortunate compilation issue aove. :(
- Unittests: I have full unittest coverage on my D project so the algorithms should be correct, but this is definately top on my list.
- Vector functions: abs, lerp, min, max
- Matrix Inversion: ugh
- Matrix rotation

View file

@ -2,7 +2,7 @@ import std::cmp::FuzzyEq;
import cmp::Ord;
import num::Num;
// import to_str::ToStr;
import quat::*;
import quat::quat;
import vec::*;
// TODO: Unittests! I've probably made lots of mistakes...
@ -87,14 +87,14 @@ pure fn mat2_v(col0:vec2, col1:vec2) -> mat2 {
#[inline(always)]
pure fn mat2_zero() -> mat2 {
mat2 (0.0, 0.0,
0.0, 0.0)
mat2(0f, 0f,
0f, 0f)
}
#[inline(always)]
pure fn mat2_identity() -> mat2 {
mat2 (1.0, 0.0,
0.0, 1.0)
mat2(1f, 0f,
0f, 1f)
}
//
@ -204,8 +204,8 @@ impl mat2: Matrix<float, vec2> {
#[inline(always)]
pure fn is_diagonal() -> bool {
self[0][1].fuzzy_eq(&0.0) &&
self[1][0].fuzzy_eq(&0.0)
self[0][1].fuzzy_eq(&0f) &&
self[1][0].fuzzy_eq(&0f)
}
#[inline(always)]
@ -246,16 +246,16 @@ pure fn mat3_v(col0:vec3, col1:vec3, col2:vec3) -> mat3 {
#[inline(always)]
pure fn mat3_zero() -> mat3 {
mat3 (0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0)
mat3 (0f, 0f, 0f,
0f, 0f, 0f,
0f, 0f, 0f)
}
#[inline(always)]
pure fn mat3_identity() -> mat3 {
mat3 (1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0)
mat3 (1f, 0f, 0f,
0f, 1f, 0f,
0f, 0f, 1f)
}
//
@ -385,14 +385,14 @@ impl mat3: Matrix<float, vec3> {
#[inline(always)]
pure fn is_diagonal() -> bool {
self[0][1].fuzzy_eq(&0.0) &&
self[0][2].fuzzy_eq(&0.0) &&
self[0][1].fuzzy_eq(&0f) &&
self[0][2].fuzzy_eq(&0f) &&
self[1][0].fuzzy_eq(&0.0) &&
self[1][2].fuzzy_eq(&0.0) &&
self[1][0].fuzzy_eq(&0f) &&
self[1][2].fuzzy_eq(&0f) &&
self[2][0].fuzzy_eq(&0.0) &&
self[2][1].fuzzy_eq(&0.0)
self[2][0].fuzzy_eq(&0f) &&
self[2][1].fuzzy_eq(&0f)
}
#[inline(always)]
@ -403,50 +403,51 @@ impl mat3: Matrix<float, vec3> {
impl mat3: Matrix3<float, vec3> {
#[inline(always)]
pure fn scale(&&vec:V) -> mat3 {
self.mul_m(mat3(vec.x(), 0, 0,
0, vec.y(), 0,
0, 0, vec.z()))
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], 0,
self[1][0], self[1][1], self[1][2], 0,
self[2][0], self[2][1], self[2][2], 0,
0, 0, 0, 1)
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 {
// Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's
// paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
let w:float, x:float, y:float, z:float, s:float;
let mut s:float;
let w:float, x:float, y:float, z:float;
let trace:float = self[0][0] + self[1][1] + self[2][2];
if trace >= 0 {
s = sqrt(trace + 1);
if trace >= 0f {
s = sqrt(trace + 1f);
w = 0.5 * s;
s = 0.5 / s;
x = self[1][2] - self[2][1] * s;
y = self[2][0] - self[0][2] * s;
z = self[0][1] - self[1][0] * s;
} else if (self[0][0] > self[1][1]) && (self[0][0] > self[2][2]) {
s = sqrt(1 + self[0][0] - self[1][1] - self[2][2]);
s = sqrt(1f + self[0][0] - self[1][1] - self[2][2]);
w = 0.5 * s;
s = 0.5 / s;
x = self[0][1] - self[1][0] * s;
y = self[2][0] - self[0][2] * s;
z = self[1][2] - self[2][1] * s;
} else if self[1][1] > self[2][2] {
s = sqrt(1 + self[1][1] - self[0][0] - self[2][2]);
s = sqrt(1f + self[1][1] - self[0][0] - self[2][2]);
w = 0.5 * s;
s = 0.5 / s;
x = self[0][1] - self[1][0] * s;
y = self[1][2] - self[2][1] * s;
z = self[2][0] - self[0][2] * s;
} else {
s = sqrt(1 + self[2][2] - self[0][0] - self[1][1]);
s = sqrt(1f + self[2][2] - self[0][0] - self[1][1]);
w = 0.5 * s;
s = 0.5 / s;
x = self[2][0] - self[0][2] * s;
@ -491,18 +492,18 @@ pure fn mat4_v(col0:vec4, col1:vec4, col2:vec4, col3:vec4) -> mat4 {
#[inline(always)]
pure fn mat4_zero() -> mat4 {
mat4 (0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0)
mat4 (0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f,
0f, 0f, 0f, 0f)
}
#[inline(always)]
pure fn mat4_identity() -> mat4 {
mat4 (1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0)
mat4 (1f, 0f, 0f, 0f,
0f, 1f, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f)
}
//
@ -655,21 +656,21 @@ impl mat4: Matrix<float, vec4> {
#[inline(always)]
pure fn is_diagonal() -> bool {
self[0][1].fuzzy_eq(&0.0) &&
self[0][2].fuzzy_eq(&0.0) &&
self[0][3].fuzzy_eq(&0.0) &&
self[0][1].fuzzy_eq(&0f) &&
self[0][2].fuzzy_eq(&0f) &&
self[0][3].fuzzy_eq(&0f) &&
self[1][0].fuzzy_eq(&0.0) &&
self[1][2].fuzzy_eq(&0.0) &&
self[1][3].fuzzy_eq(&0.0) &&
self[1][0].fuzzy_eq(&0f) &&
self[1][2].fuzzy_eq(&0f) &&
self[1][3].fuzzy_eq(&0f) &&
self[2][0].fuzzy_eq(&0.0) &&
self[2][1].fuzzy_eq(&0.0) &&
self[2][3].fuzzy_eq(&0.0) &&
self[2][0].fuzzy_eq(&0f) &&
self[2][1].fuzzy_eq(&0f) &&
self[2][3].fuzzy_eq(&0f) &&
self[3][0].fuzzy_eq(&0.0) &&
self[3][1].fuzzy_eq(&0.0) &&
self[3][2].fuzzy_eq(&0.0)
self[3][0].fuzzy_eq(&0f) &&
self[3][1].fuzzy_eq(&0f) &&
self[3][2].fuzzy_eq(&0f)
}
#[inline(always)]
@ -681,20 +682,20 @@ impl mat4: Matrix<float, vec4> {
impl mat4: Matrix4<float, vec4> {
#[inline(always)]
pure fn scale(&&vec:vec3) -> mat4 {
self.mul_m(mat3(vec.x(), 0, 0, 0,
0, vec.y(), 0, 0,
0, 0, vec.z(), 0,
0, 0, 0, 1))
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 {
mat4(self[0],
self[1],
self[2],
vec4(self[3][0] + vec.x(),
self[3][1] + vec.y(),
self[3][2] + vec.z(),
self[3][3]))
mat4_v(self[0],
self[1],
self[2],
vec4(self[3][0] + vec.x(),
self[3][1] + vec.y(),
self[3][2] + vec.z(),
self[3][3]))
}
}

View file

@ -1,5 +1,6 @@
import float::consts::pi;
import mat::*;
import float::tan;
import mat::mat4;
//
// Create a perspective projection matrix
@ -7,8 +8,8 @@ import mat::*;
// fov is in degrees
// http://www.opengl.org/wiki/GluPerspective_code
//
pure fn perspective(fovy:float, aspectRatio:float, near:float, fa:float) -> mat4 {
let ymax = near * tan(fovy * PI / 360.0);
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,22 +22,22 @@ pure fn perspective(fovy:float, aspectRatio:float, near:float, fa:float) -> mat4
// TODO: double check algorithm
//
pure fn frustum(left:float, right:float, bottom:float, top:float, near:float, far:float) -> mat4 {
let m00:float = (2*near)/(right-left);
let m01:float = 0.0;
let m02:float = 0.0;
let m03:float = 0.0;
let m10:float = 0.0;
let m11:float = (2*near)/(top-bottom);
let m12:float = 0.0;
let m13:float = 0.0;
let m20:float = (right+left)/(right-left);
let m21:float = (top+bottom)/(top-bottom);
let m22:float = -(far+near)/(far-near);
let m23:float = -1.0;
let m30:float = 0.0;
let m31:float = 0.0;
let m32:float = -(2*far*near)/(far-near);
let m33:float = 0.0;
let m00:float = (2f * near) / (right - left);
let m01:float = 0f;
let m02:float = 0f;
let m03:float = 0f;
let m10:float = 0f;
let m11:float = (2f * near)/(top - bottom);
let m12:float = 0f;
let m13:float = 0f;
let m20:float = (right + left) / (right - left);
let m21:float = (top + bottom) / (top - bottom);
let m22:float = -(far + near) / (far - near);
let m23:float = -1f;
let m30:float = 0f;
let m31:float = 0f;
let m32:float = -(2f * far * near) / (far - near);
let m33:float = 0f;
return mat4(m00, m01, m02, m03,
m10, m11, m12, m13,

View file

@ -1,9 +1,10 @@
import std::cmp::FuzzyEq;
import cmp::Ord;
import float::sqrt;
import num::Num;
import to_str::ToStr;
import mat::*;
import vec::*;
import mat::{mat3, mat4};
import vec::vec3;
// TODO: Unittests! I've probably made lots of mistakes...
@ -14,10 +15,10 @@ trait Quaternion<T:Num Ord FuzzyEq> {
pure fn dim() -> uint;
pure fn index(&&index:uint) -> T;
fn w() -> T;
fn x() -> T;
fn y() -> T;
fn z() -> T;
pure fn w() -> T;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
pure fn neg() -> self;
@ -53,6 +54,12 @@ trait Quaternion<T:Num Ord FuzzyEq> {
//
struct quat { data:[float * 4] }
//
// Constants
//
#[inline(always)] pure fn quat_zero() -> quat { quat(0f, 0f, 0f, 0f) }
#[inline(always)] pure fn quat_identity() -> quat { quat(1f, 0f, 0f, 0f) }
//
// Quat Constructor
//
@ -64,7 +71,7 @@ pure fn quat(w:float, x:float, y:float, z:float) -> quat {
//
// Quaternion Implementation
//
impl quat<float> {
impl quat: Quaternion<float> {
#[inline(always)]
pure fn dim() -> uint { 4 }
@ -78,10 +85,10 @@ impl quat<float> {
quat(-self[0], -self[1], -self[2], -self[3])
}
#[inline(always)] fn w() -> float { self.data[0] }
#[inline(always)] fn x() -> float { self.data[1] }
#[inline(always)] fn y() -> float { self.data[2] }
#[inline(always)] fn z() -> float { self.data[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(always)]
pure fn mul_f(&&value:float) -> quat {
@ -146,12 +153,12 @@ impl quat<float> {
#[inline(always)]
pure fn conjugate() -> quat {
quat(self.w(), -self.x(), -self.y(), -self.z());
quat(self.w(), -self.x(), -self.y(), -self.z())
}
#[inline(always)]
pure fn inverse() -> quat {
self.conjugate().mul_f((1f / self.magnitude2()))
}
#[inline(always)]
@ -164,7 +171,7 @@ impl quat<float> {
#[inline(always)]
pure fn magnitude() -> float {
sqrt(self.magnitude2)
sqrt(self.magnitude2())
}
#[inline(always)]
@ -185,9 +192,9 @@ impl quat<float> {
let wz2 = z2 * self.w();
let wx2 = x2 * self.w();
return mat3(1 - yy2 - zz2, xy2 - wz2, xz2 + wy2,
xy2 + wz2, 1 - xx2 - zz2, yz2 - wx2,
xz2 - wy2, yz2 + wx2, 1 - xx2 - yy2);
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)]

View file

@ -45,17 +45,17 @@ trait Vector<T:Num Ord FuzzyEq> {
//
trait Vector2<T:Num Ord FuzzyEq> {
// This is where I wish rust had properties ;)
fn x() -> T;
fn y() -> T;
pure fn x() -> T;
pure fn y() -> T;
}
//
// 3-Dimensional Vector
//
trait Vector3<T:Num Ord FuzzyEq> {
fn x() -> T;
fn y() -> T;
fn z() -> T;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
fn cross(&&other:self) -> self;
}
@ -64,10 +64,10 @@ trait Vector3<T:Num Ord FuzzyEq> {
// 4-Dimensional Vector
//
trait Vector4<T:Num Ord FuzzyEq> {
fn x() -> T;
fn y() -> T;
fn z() -> T;
fn w() -> T;
pure fn x() -> T;
pure fn y() -> T;
pure fn z() -> T;
pure fn w() -> T;
}
@ -91,17 +91,17 @@ pure fn vec2(x:float, y:float) -> vec2 {
//
// Constants
//
#[inline(always)] pure fn vec2_zero() -> vec2 { vec2 (0.0, 0.0) }
#[inline(always)] pure fn vec2_unit_x() -> vec2 { vec2 (1.0, 0.0) }
#[inline(always)] pure fn vec2_unit_y() -> vec2 { vec2 (0.0, 1.0) }
#[inline(always)] pure fn vec2_identity() -> vec2 { vec2 (1.0, 1.0) }
#[inline(always)] pure fn vec2_zero() -> vec2 { vec2 (0f, 0f) }
#[inline(always)] pure fn vec2_unit_x() -> vec2 { vec2 (1f, 0f) }
#[inline(always)] pure fn vec2_unit_y() -> vec2 { vec2 (0f, 1f) }
#[inline(always)] pure fn vec2_identity() -> vec2 { vec2 (1f, 1f) }
//
// Vector2 Implementation
//
impl vec2: Vector2<float> {
#[inline(always)] fn x() -> float { self.data[0] }
#[inline(always)] fn y() -> float { self.data[1] }
#[inline(always)] pure fn x() -> float { self.data[0] }
#[inline(always)] pure fn y() -> float { self.data[1] }
}
//
@ -193,7 +193,7 @@ impl vec2: Vector<float> {
#[inline(always)]
pure fn normalize() -> vec2 {
let n = 1.0 / self.magnitude();
let n = 1f / self.magnitude();
return self.mul_f(n);
}
}
@ -220,11 +220,11 @@ struct vec3 { data:[float * 3] }
//
// Constants
//
#[inline(always)] pure fn vec3_zero() -> vec3 { vec3(0.0, 0.0, 0.0) }
#[inline(always)] pure fn vec3_unit_x() -> vec3 { vec3(1.0, 0.0, 0.0) }
#[inline(always)] pure fn vec3_unit_y() -> vec3 { vec3(0.0, 1.0, 0.0) }
#[inline(always)] pure fn vec3_unit_z() -> vec3 { vec3(0.0, 0.0, 1.0) }
#[inline(always)] pure fn vec3_identity() -> vec3 { vec3(1.0, 1.0, 1.0) }
#[inline(always)] pure fn vec3_zero() -> vec3 { vec3(0f, 0f, 0f) }
#[inline(always)] pure fn vec3_unit_x() -> vec3 { vec3(1f, 0f, 0f) }
#[inline(always)] pure fn vec3_unit_y() -> vec3 { vec3(0f, 1f, 0f) }
#[inline(always)] pure fn vec3_unit_z() -> vec3 { vec3(0f, 0f, 1f) }
#[inline(always)] pure fn vec3_identity() -> vec3 { vec3(1f, 1f, 1f) }
//
// Vec3 Constructor
@ -239,9 +239,9 @@ pure fn vec3(x:float, y:float, z:float) -> vec3 {
// Vector3 Implementation
//
impl vec3: Vector3<float> {
#[inline(always)] fn x() -> float { self.data[0] }
#[inline(always)] fn y() -> float { self.data[1] }
#[inline(always)] fn z() -> float { self.data[2] }
#[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)]
fn cross(&&other:vec3) -> vec3 {
@ -350,7 +350,7 @@ impl vec3: Vector<float> {
#[inline(always)]
pure fn normalize() -> vec3 {
let n = 1.0 / self.magnitude();
let n = 1f / self.magnitude();
return self.mul_f(n);
}
}
@ -377,12 +377,12 @@ struct vec4 { data:[float * 4] }
//
// Constants
//
#[inline(always)] pure fn vec4_zero() -> vec4 { vec4(0.0, 0.0, 0.0, 0.0) }
#[inline(always)] pure fn vec4_unit_x() -> vec4 { vec4(1.0, 0.0, 0.0, 0.0) }
#[inline(always)] pure fn vec4_unit_y() -> vec4 { vec4(0.0, 1.0, 0.0, 0.0) }
#[inline(always)] pure fn vec4_unit_z() -> vec4 { vec4(0.0, 0.0, 1.0, 0.0) }
#[inline(always)] pure fn vec4_unit_w() -> vec4 { vec4(0.0, 0.0, 0.0, 1.0) }
#[inline(always)] pure fn vec4_identity() -> vec4 { vec4(1.0, 1.0, 1.0, 1.0) }
#[inline(always)] pure fn vec4_zero() -> vec4 { vec4(0f, 0f, 0f, 0f) }
#[inline(always)] pure fn vec4_unit_x() -> vec4 { vec4(1f, 0f, 0f, 0f) }
#[inline(always)] pure fn vec4_unit_y() -> vec4 { vec4(0f, 1f, 0f, 0f) }
#[inline(always)] pure fn vec4_unit_z() -> vec4 { vec4(0f, 0f, 1f, 0f) }
#[inline(always)] pure fn vec4_unit_w() -> vec4 { vec4(0f, 0f, 0f, 1f) }
#[inline(always)] pure fn vec4_identity() -> vec4 { vec4(1f, 1f, 1f, 1f) }
//
// Vec4 Constructor
@ -396,10 +396,10 @@ pure fn vec4(x:float, y:float, z:float, w:float) -> vec4 {
// Vector4 Implementation
//
impl vec4: Vector4<float> {
#[inline(always)] fn x() -> float { self.data[0] }
#[inline(always)] fn y() -> float { self.data[1] }
#[inline(always)] fn z() -> float { self.data[2] }
#[inline(always)] fn w() -> float { self.data[3] }
#[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] }
}
//
@ -511,7 +511,7 @@ impl vec4: Vector<float> {
#[inline(always)]
pure fn normalize() -> vec4 {
let n = 1.0 / self.magnitude();
let n = 1f / self.magnitude();
return self.mul_f(n);
}
}

14
test/test_om3d.rc Normal file
View file

@ -0,0 +1,14 @@
#[link(name = "om3d-test",
vers = "0.1",
author = "Brendan Zabarauskas")];
#[comment = "Unittests for om3d"];
#[crate_type = "unittests"];
use std;
use om3d;
mod test_mat;
mod test_projection;
mod test_quat;
mod test_vec;

6
test/test_projection.rs Normal file
View file

@ -0,0 +1,6 @@
// TODO
#[test]
fn test_projection() {
}

View file

@ -1,16 +1,16 @@
// TODO
#[test]
fn test_mat2() {
fn test_vec2() {
}
#[test]
fn test_mat3() {
fn test_vec3() {
}
#[test]
fn test_mat4() {
fn test_vec4() {
}