Add license/copyright notices to source files

This commit is contained in:
Brendan Zabarauskas 2013-06-01 08:01:01 +10:00
parent e630faf239
commit d3bfcc194e
13 changed files with 270 additions and 114 deletions

View file

@ -1,3 +1,18 @@
# Copyright 2013 The Lmath Developers. For a full listing of the authors,
# refer to the AUTHORS file at the top-level directory of this distribution.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
TARGET = lmath TARGET = lmath
ROOT_DIR = . ROOT_DIR = .
@ -17,7 +32,7 @@ $(TARGET):
@mkdir -p $(BUILD_DIR) @mkdir -p $(BUILD_DIR)
@rustc $(SRC_DIR)/$(SRC_CRATE) --out-dir=$(BUILD_DIR) @rustc $(SRC_DIR)/$(SRC_CRATE) --out-dir=$(BUILD_DIR)
@echo "Success" @echo "Success"
all: $(TARGET) all: $(TARGET)
test: test:

15
pkg.rs
View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[pkg_crate(file = "src/lmath.rs")]; #[pkg_crate(file = "src/lmath.rs")];
#[pkg(id = "lmath", vers = "0.1.0")]; #[pkg(id = "lmath", vers = "0.1.0")];
#[pkg_dep(url = "git://github.com/bjz/numeric-rs")]; #[pkg_dep(url = "git://github.com/bjz/numeric-rs")];

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[link(name = "lmath", #[link(name = "lmath",
vers = "0.1.0", vers = "0.1.0",
uuid = "A2DEEB53-EB35-4B44-B247-9044B57E3BA5", uuid = "A2DEEB53-EB35-4B44-B247-9044B57E3BA5",

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cast::transmute; use std::cast::transmute;
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
use std::num::{Zero, One}; use std::num::{Zero, One};

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub trait NumAssign { pub trait NumAssign {
fn add_assign(&mut self, other: &Self); fn add_assign(&mut self, other: &Self);
fn sub_assign(&mut self, other: &Self); fn sub_assign(&mut self, other: &Self);

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use mat::{Mat4, BaseMat4}; use mat::{Mat4, BaseMat4};
use num::NumAssign; use num::NumAssign;

View file

@ -1,11 +1,17 @@
/** // Copyright 2013 The Lmath Developers. For a full listing of the authors,
* > Every morning in the early part of October 1843, on my coming down to // refer to the AUTHORS file at the top-level directory of this distribution.
* breakfast, your brother William Edward and yourself used to ask me: "Well, //
* Papa, can you multiply triples?" Whereto I was always obliged to reply, // Licensed under the Apache License, Version 2.0 (the "License");
* with a sad shake of the head, "No, I can only add and subtract them." // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* Sir William Hamilton //
*/ // http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cast::transmute; use std::cast::transmute;
use std::cmp::ApproxEq; use std::cmp::ApproxEq;

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use mat::*; use mat::*;
use vec::*; use vec::*;
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
@ -10,53 +25,53 @@ fn test_mat2() {
y: Vec2 { x: 2.0, y: 4.0 } }; y: Vec2 { x: 2.0, y: 4.0 } };
let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 }, let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 },
y: Vec2 { x: 3.0, y: 5.0 } }; y: Vec2 { x: 3.0, y: 5.0 } };
let v1 = vec2::new(1.0, 2.0); let v1 = vec2::new(1.0, 2.0);
let f1 = 0.5; let f1 = 0.5;
assert!(a == mat2::new(1.0, 3.0, assert!(a == mat2::new(1.0, 3.0,
2.0, 4.0)); 2.0, 4.0));
assert!(a == mat2::from_cols(vec2::new(1.0, 3.0), assert!(a == mat2::from_cols(vec2::new(1.0, 3.0),
vec2::new(2.0, 4.0))); vec2::new(2.0, 4.0)));
assert!(mat2::from_value(4.0) == mat2::new(4.0, 0.0, assert!(mat2::from_value(4.0) == mat2::new(4.0, 0.0,
0.0, 4.0)); 0.0, 4.0));
assert!(a[0] == vec2::new(1.0, 3.0)); assert!(a[0] == vec2::new(1.0, 3.0));
assert!(a[1] == vec2::new(2.0, 4.0)); assert!(a[1] == vec2::new(2.0, 4.0));
assert!(a.row(0) == vec2::new(1.0, 2.0)); assert!(a.row(0) == vec2::new(1.0, 2.0));
assert!(a.row(1) == vec2::new(3.0, 4.0)); assert!(a.row(1) == vec2::new(3.0, 4.0));
assert!(a.col(0) == vec2::new(1.0, 3.0)); assert!(a.col(0) == vec2::new(1.0, 3.0));
assert!(a.col(1) == vec2::new(2.0, 4.0)); assert!(a.col(1) == vec2::new(2.0, 4.0));
assert!(mat2::identity() == mat2::new(1.0, 0.0, assert!(mat2::identity() == mat2::new(1.0, 0.0,
0.0, 1.0)); 0.0, 1.0));
assert!(mat2::zero() == mat2::new(0.0, 0.0, assert!(mat2::zero() == mat2::new(0.0, 0.0,
0.0, 0.0)); 0.0, 0.0));
assert!(a.determinant() == -2.0); assert!(a.determinant() == -2.0);
assert!(a.trace() == 5.0); assert!(a.trace() == 5.0);
assert!(a.neg() == mat2::new(-1.0, -3.0, assert!(a.neg() == mat2::new(-1.0, -3.0,
-2.0, -4.0)); -2.0, -4.0));
assert!(-a == a.neg()); assert!(-a == a.neg());
assert!(a.mul_t(f1) == mat2::new(0.5, 1.5, assert!(a.mul_t(f1) == mat2::new(0.5, 1.5,
1.0, 2.0)); 1.0, 2.0));
assert!(a.mul_v(&v1) == vec2::new(5.0, 11.0)); assert!(a.mul_v(&v1) == vec2::new(5.0, 11.0));
assert!(a.add_m(&b) == mat2::new(3.0, 7.0, assert!(a.add_m(&b) == mat2::new(3.0, 7.0,
5.0, 9.0)); 5.0, 9.0));
assert!(a.sub_m(&b) == mat2::new(-1.0, -1.0, assert!(a.sub_m(&b) == mat2::new(-1.0, -1.0,
-1.0, -1.0)); -1.0, -1.0));
assert!(a.mul_m(&b) == mat2::new(10.0, 22.0, assert!(a.mul_m(&b) == mat2::new(10.0, 22.0,
13.0, 29.0)); 13.0, 29.0));
assert!(a.dot(&b) == 40.0); assert!(a.dot(&b) == 40.0);
assert!(a.transpose() == mat2::new(1.0, 2.0, assert!(a.transpose() == mat2::new(1.0, 2.0,
3.0, 4.0)); 3.0, 4.0));
@ -65,21 +80,21 @@ fn test_mat2() {
assert!(mat2::new(0.0, 2.0, assert!(mat2::new(0.0, 2.0,
0.0, 5.0).inverse().is_none()); 0.0, 5.0).inverse().is_none());
let ident = mat2::identity(); let ident = mat2::identity();
assert!(ident.is_identity()); assert!(ident.is_identity());
assert!(ident.is_symmetric()); assert!(ident.is_symmetric());
assert!(ident.is_diagonal()); assert!(ident.is_diagonal());
assert!(!ident.is_rotated()); assert!(!ident.is_rotated());
assert!(ident.is_invertible()); assert!(ident.is_invertible());
assert!(!a.is_identity()); assert!(!a.is_identity());
assert!(!a.is_symmetric()); assert!(!a.is_symmetric());
assert!(!a.is_diagonal()); assert!(!a.is_diagonal());
assert!(a.is_rotated()); assert!(a.is_rotated());
assert!(a.is_invertible()); assert!(a.is_invertible());
let c = mat2::new(2.0, 1.0, let c = mat2::new(2.0, 1.0,
1.0, 2.0); 1.0, 2.0);
assert!(!c.is_identity()); assert!(!c.is_identity());
@ -87,13 +102,13 @@ fn test_mat2() {
assert!(!c.is_diagonal()); assert!(!c.is_diagonal());
assert!(c.is_rotated()); assert!(c.is_rotated());
assert!(c.is_invertible()); assert!(c.is_invertible());
assert!(mat2::from_value(6.0).is_diagonal()); assert!(mat2::from_value(6.0).is_diagonal());
assert!(a.to_mat3() == mat3::new(1.0, 3.0, 0.0, assert!(a.to_mat3() == mat3::new(1.0, 3.0, 0.0,
2.0, 4.0, 0.0, 2.0, 4.0, 0.0,
0.0, 0.0, 1.0)); 0.0, 0.0, 1.0));
assert!(a.to_mat4() == mat4::new(1.0, 3.0, 0.0, 0.0, assert!(a.to_mat4() == mat4::new(1.0, 3.0, 0.0, 0.0,
2.0, 4.0, 0.0, 0.0, 2.0, 4.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
@ -105,49 +120,49 @@ fn test_mat2_mut() {
y: Vec2 { x: 2.0, y: 4.0 } }; y: Vec2 { x: 2.0, y: 4.0 } };
let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 }, let b = Mat2 { x: Vec2 { x: 2.0, y: 4.0 },
y: Vec2 { x: 3.0, y: 5.0 } }; y: Vec2 { x: 3.0, y: 5.0 } };
let f1 = 0.5; let f1 = 0.5;
let mut mut_a: mat2 = a; let mut mut_a: mat2 = a;
mut_a.swap_cols(0, 1); mut_a.swap_cols(0, 1);
assert!(mut_a.col(0) == a.col(1)); assert!(mut_a.col(0) == a.col(1));
assert!(mut_a.col(1) == a.col(0)); assert!(mut_a.col(1) == a.col(0));
mut_a = a; mut_a = a;
mut_a.swap_rows(0, 1); mut_a.swap_rows(0, 1);
assert!(mut_a.row(0) == a.row(1)); assert!(mut_a.row(0) == a.row(1));
assert!(mut_a.row(1) == a.row(0)); assert!(mut_a.row(1) == a.row(0));
mut_a = a; mut_a = a;
mut_a.set(&b); mut_a.set(&b);
assert!(mut_a == b); assert!(mut_a == b);
mut_a = a; mut_a = a;
mut_a.to_identity(); mut_a.to_identity();
assert!(mut_a.is_identity()); assert!(mut_a.is_identity());
mut_a = a; mut_a = a;
mut_a.to_zero(); mut_a.to_zero();
assert!(mut_a == mat2::zero()); assert!(mut_a == mat2::zero());
mut_a = a; mut_a = a;
mut_a.mul_self_t(f1); mut_a.mul_self_t(f1);
assert!(mut_a == a.mul_t(f1)); assert!(mut_a == a.mul_t(f1));
mut_a = a; mut_a = a;
mut_a.add_self_m(&b); mut_a.add_self_m(&b);
assert!(mut_a == a.add_m(&b)); assert!(mut_a == a.add_m(&b));
mut_a = a; mut_a = a;
mut_a.sub_self_m(&b); mut_a.sub_self_m(&b);
assert!(mut_a == a.sub_m(&b)); assert!(mut_a == a.sub_m(&b));
mut_a = a; mut_a = a;
mut_a.invert_self(); mut_a.invert_self();
assert!(mut_a == a.inverse().unwrap()); assert!(mut_a == a.inverse().unwrap());
mut_a = a; mut_a = a;
mut_a.transpose_self(); mut_a.transpose_self();
assert!(mut_a == a.transpose()); assert!(mut_a == a.transpose());
// mut_a = a; // mut_a = a;
@ -169,51 +184,51 @@ fn test_mat3() {
let b = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, let b = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 },
y: Vec3 { x: 3.0, y: 6.0, z: 9.0 }, y: Vec3 { x: 3.0, y: 6.0, z: 9.0 },
z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } }; z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } };
let v1 = vec3::new(1.0, 2.0, 3.0); let v1 = vec3::new(1.0, 2.0, 3.0);
let f1 = 0.5; let f1 = 0.5;
assert!(a == mat3::new(1.0, 4.0, 7.0, assert!(a == mat3::new(1.0, 4.0, 7.0,
2.0, 5.0, 8.0, 2.0, 5.0, 8.0,
3.0, 6.0, 9.0)); 3.0, 6.0, 9.0));
assert!(a == mat3::from_cols(vec3::new(1.0, 4.0, 7.0), assert!(a == mat3::from_cols(vec3::new(1.0, 4.0, 7.0),
vec3::new(2.0, 5.0, 8.0), vec3::new(2.0, 5.0, 8.0),
vec3::new(3.0, 6.0, 9.0))); vec3::new(3.0, 6.0, 9.0)));
assert!(a[0] == vec3::new(1.0, 4.0, 7.0)); assert!(a[0] == vec3::new(1.0, 4.0, 7.0));
assert!(a[1] == vec3::new(2.0, 5.0, 8.0)); assert!(a[1] == vec3::new(2.0, 5.0, 8.0));
assert!(a[2] == vec3::new(3.0, 6.0, 9.0)); assert!(a[2] == vec3::new(3.0, 6.0, 9.0));
assert!(a.row(0) == vec3::new(1.0, 2.0, 3.0)); assert!(a.row(0) == vec3::new(1.0, 2.0, 3.0));
assert!(a.row(1) == vec3::new(4.0, 5.0, 6.0)); assert!(a.row(1) == vec3::new(4.0, 5.0, 6.0));
assert!(a.row(2) == vec3::new(7.0, 8.0, 9.0)); assert!(a.row(2) == vec3::new(7.0, 8.0, 9.0));
assert!(a.col(0) == vec3::new(1.0, 4.0, 7.0)); assert!(a.col(0) == vec3::new(1.0, 4.0, 7.0));
assert!(a.col(1) == vec3::new(2.0, 5.0, 8.0)); assert!(a.col(1) == vec3::new(2.0, 5.0, 8.0));
assert!(a.col(2) == vec3::new(3.0, 6.0, 9.0)); assert!(a.col(2) == vec3::new(3.0, 6.0, 9.0));
assert!(mat3::identity() == mat3::new(1.0, 0.0, 0.0, assert!(mat3::identity() == mat3::new(1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 1.0)); 0.0, 0.0, 1.0));
assert!(mat3::zero() == mat3::new(0.0, 0.0, 0.0, assert!(mat3::zero() == mat3::new(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));
assert!(a.determinant() == 0.0); assert!(a.determinant() == 0.0);
assert!(a.trace() == 15.0); assert!(a.trace() == 15.0);
assert!(a.neg() == mat3::new(-1.0, -4.0, -7.0, assert!(a.neg() == mat3::new(-1.0, -4.0, -7.0,
-2.0, -5.0, -8.0, -2.0, -5.0, -8.0,
-3.0, -6.0, -9.0)); -3.0, -6.0, -9.0));
assert!(-a == a.neg()); assert!(-a == a.neg());
assert!(a.mul_t(f1) == mat3::new(0.5, 2.0, 3.5, assert!(a.mul_t(f1) == mat3::new(0.5, 2.0, 3.5,
1.0, 2.5, 4.0, 1.0, 2.5, 4.0,
1.5, 3.0, 4.5)); 1.5, 3.0, 4.5));
assert!(a.mul_v(&v1) == vec3::new(14.0, 32.0, 50.0)); assert!(a.mul_v(&v1) == vec3::new(14.0, 32.0, 50.0));
assert!(a.add_m(&b) == mat3::new(3.0, 9.0, 15.0, assert!(a.add_m(&b) == mat3::new(3.0, 9.0, 15.0,
5.0, 11.0, 17.0, 5.0, 11.0, 17.0,
7.0, 13.0, 19.0)); 7.0, 13.0, 19.0));
@ -224,36 +239,36 @@ fn test_mat3() {
42.0, 96.0, 150.0, 42.0, 96.0, 150.0,
48.0, 111.0, 174.0)); 48.0, 111.0, 174.0));
assert!(a.dot(&b) == 330.0); assert!(a.dot(&b) == 330.0);
assert!(a.transpose() == mat3::new(1.0, 2.0, 3.0, assert!(a.transpose() == mat3::new(1.0, 2.0, 3.0,
4.0, 5.0, 6.0, 4.0, 5.0, 6.0,
7.0, 8.0, 9.0)); 7.0, 8.0, 9.0));
assert!(a.inverse().is_none()); assert!(a.inverse().is_none());
assert!(mat3::new(2.0, 4.0, 6.0, assert!(mat3::new(2.0, 4.0, 6.0,
0.0, 2.0, 4.0, 0.0, 2.0, 4.0,
0.0, 0.0, 1.0).inverse().unwrap() 0.0, 0.0, 1.0).inverse().unwrap()
== mat3::new(0.5, -1.0, 1.0, == mat3::new(0.5, -1.0, 1.0,
0.0, 0.5, -2.0, 0.0, 0.5, -2.0,
0.0, 0.0, 1.0)); 0.0, 0.0, 1.0));
let ident: Mat3<float> = BaseMat::identity(); let ident: Mat3<float> = BaseMat::identity();
assert!(ident.inverse().unwrap() == ident); assert!(ident.inverse().unwrap() == ident);
assert!(ident.is_identity()); assert!(ident.is_identity());
assert!(ident.is_symmetric()); assert!(ident.is_symmetric());
assert!(ident.is_diagonal()); assert!(ident.is_diagonal());
assert!(!ident.is_rotated()); assert!(!ident.is_rotated());
assert!(ident.is_invertible()); assert!(ident.is_invertible());
assert!(!a.is_identity()); assert!(!a.is_identity());
assert!(!a.is_symmetric()); assert!(!a.is_symmetric());
assert!(!a.is_diagonal()); assert!(!a.is_diagonal());
assert!(a.is_rotated()); assert!(a.is_rotated());
assert!(!a.is_invertible()); assert!(!a.is_invertible());
let c = mat3::new(3.0, 2.0, 1.0, let c = mat3::new(3.0, 2.0, 1.0,
2.0, 3.0, 2.0, 2.0, 3.0, 2.0,
1.0, 2.0, 3.0); 1.0, 2.0, 3.0);
@ -262,14 +277,14 @@ fn test_mat3() {
assert!(!c.is_diagonal()); assert!(!c.is_diagonal());
assert!(c.is_rotated()); assert!(c.is_rotated());
assert!(c.is_invertible()); assert!(c.is_invertible());
assert!(mat3::from_value(6.0).is_diagonal()); assert!(mat3::from_value(6.0).is_diagonal());
assert!(a.to_mat4() == mat4::new(1.0, 4.0, 7.0, 0.0, assert!(a.to_mat4() == mat4::new(1.0, 4.0, 7.0, 0.0,
2.0, 5.0, 8.0, 0.0, 2.0, 5.0, 8.0, 0.0,
3.0, 6.0, 9.0, 0.0, 3.0, 6.0, 9.0, 0.0,
0.0, 0.0, 0.0, 1.0)); 0.0, 0.0, 0.0, 1.0));
// to_Quaternion // to_Quaternion
} }
@ -283,60 +298,60 @@ fn test_mat3_mut() {
let c = Mat3 { x: Vec3 { x: 2.0, y: 4.0, z: 6.0 }, let c = Mat3 { x: Vec3 { x: 2.0, y: 4.0, z: 6.0 },
y: Vec3 { x: 0.0, y: 2.0, z: 4.0 }, y: Vec3 { x: 0.0, y: 2.0, z: 4.0 },
z: Vec3 { x: 0.0, y: 0.0, z: 1.0 } }; z: Vec3 { x: 0.0, y: 0.0, z: 1.0 } };
let f1 = 0.5; let f1 = 0.5;
let mut mut_a: mat3 = a; let mut mut_a: mat3 = a;
let mut mut_c: mat3 = c; let mut mut_c: mat3 = c;
mut_a.swap_cols(0, 2); mut_a.swap_cols(0, 2);
assert!(mut_a.col(0) == a.col(2)); assert!(mut_a.col(0) == a.col(2));
assert!(mut_a.col(2) == a.col(0)); assert!(mut_a.col(2) == a.col(0));
mut_a = a; mut_a = a;
mut_a.swap_cols(1, 2); mut_a.swap_cols(1, 2);
assert!(mut_a.col(1) == a.col(2)); assert!(mut_a.col(1) == a.col(2));
assert!(mut_a.col(2) == a.col(1)); assert!(mut_a.col(2) == a.col(1));
mut_a = a; mut_a = a;
mut_a.swap_rows(0, 2); mut_a.swap_rows(0, 2);
assert!(mut_a.row(0) == a.row(2)); assert!(mut_a.row(0) == a.row(2));
assert!(mut_a.row(2) == a.row(0)); assert!(mut_a.row(2) == a.row(0));
mut_a = a; mut_a = a;
mut_a.swap_rows(1, 2); mut_a.swap_rows(1, 2);
assert!(mut_a.row(1) == a.row(2)); assert!(mut_a.row(1) == a.row(2));
assert!(mut_a.row(2) == a.row(1)); assert!(mut_a.row(2) == a.row(1));
mut_a = a; mut_a = a;
mut_a.set(&b); mut_a.set(&b);
assert!(mut_a == b); assert!(mut_a == b);
mut_a = a; mut_a = a;
mut_a.to_identity(); mut_a.to_identity();
assert!(mut_a.is_identity()); assert!(mut_a.is_identity());
mut_a = a; mut_a = a;
mut_a.to_zero(); mut_a.to_zero();
assert!(mut_a == mat3::zero()); assert!(mut_a == mat3::zero());
mut_a = a; mut_a = a;
mut_a.mul_self_t(f1); mut_a.mul_self_t(f1);
assert!(mut_a == a.mul_t(f1)); assert!(mut_a == a.mul_t(f1));
mut_a = a; mut_a = a;
mut_a.add_self_m(&b); mut_a.add_self_m(&b);
assert!(mut_a == a.add_m(&b)); assert!(mut_a == a.add_m(&b));
mut_a = a; mut_a = a;
mut_a.sub_self_m(&b); mut_a.sub_self_m(&b);
assert!(mut_a == a.sub_m(&b)); assert!(mut_a == a.sub_m(&b));
mut_a = a; mut_a = a;
mut_c.invert_self(); mut_c.invert_self();
assert!(mut_c == c.inverse().unwrap()); assert!(mut_c == c.inverse().unwrap());
// mut_c = c; // mut_c = c;
mut_a.transpose_self(); mut_a.transpose_self();
assert!(mut_a == a.transpose()); assert!(mut_a == a.transpose());
// mut_a = a; // mut_a = a;
@ -366,45 +381,45 @@ fn test_mat4() {
y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 }, y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 },
z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 }, z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 },
w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } }; w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } };
let v1 = vec4::new(1.0, 2.0, 3.0, 4.0); let v1 = vec4::new(1.0, 2.0, 3.0, 4.0);
let f1 = 0.5; let f1 = 0.5;
assert!(a == mat4::new(1.0, 5.0, 9.0, 13.0, assert!(a == mat4::new(1.0, 5.0, 9.0, 13.0,
2.0, 6.0, 10.0, 14.0, 2.0, 6.0, 10.0, 14.0,
3.0, 7.0, 11.0, 15.0, 3.0, 7.0, 11.0, 15.0,
4.0, 8.0, 12.0, 16.0)); 4.0, 8.0, 12.0, 16.0));
assert!(a == mat4::from_cols(vec4::new(1.0, 5.0, 9.0, 13.0), assert!(a == mat4::from_cols(vec4::new(1.0, 5.0, 9.0, 13.0),
vec4::new(2.0, 6.0, 10.0, 14.0), vec4::new(2.0, 6.0, 10.0, 14.0),
vec4::new(3.0, 7.0, 11.0, 15.0), vec4::new(3.0, 7.0, 11.0, 15.0),
vec4::new(4.0, 8.0, 12.0, 16.0))); vec4::new(4.0, 8.0, 12.0, 16.0)));
assert!(mat4::from_value(4.0) == mat4::new(4.0, 0.0, 0.0, 0.0, assert!(mat4::from_value(4.0) == mat4::new(4.0, 0.0, 0.0, 0.0,
0.0, 4.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0,
0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 4.0, 0.0,
0.0, 0.0, 0.0, 4.0)); 0.0, 0.0, 0.0, 4.0));
assert!(a[0] == vec4::new(1.0, 5.0, 9.0, 13.0)); assert!(a[0] == vec4::new(1.0, 5.0, 9.0, 13.0));
assert!(a[1] == vec4::new(2.0, 6.0, 10.0, 14.0)); assert!(a[1] == vec4::new(2.0, 6.0, 10.0, 14.0));
assert!(a[2] == vec4::new(3.0, 7.0, 11.0, 15.0)); assert!(a[2] == vec4::new(3.0, 7.0, 11.0, 15.0));
assert!(a[3] == vec4::new(4.0, 8.0, 12.0, 16.0)); assert!(a[3] == vec4::new(4.0, 8.0, 12.0, 16.0));
assert!(a.row(0) == vec4::new( 1.0, 2.0, 3.0, 4.0)); assert!(a.row(0) == vec4::new( 1.0, 2.0, 3.0, 4.0));
assert!(a.row(1) == vec4::new( 5.0, 6.0, 7.0, 8.0)); assert!(a.row(1) == vec4::new( 5.0, 6.0, 7.0, 8.0));
assert!(a.row(2) == vec4::new( 9.0, 10.0, 11.0, 12.0)); assert!(a.row(2) == vec4::new( 9.0, 10.0, 11.0, 12.0));
assert!(a.row(3) == vec4::new(13.0, 14.0, 15.0, 16.0)); assert!(a.row(3) == vec4::new(13.0, 14.0, 15.0, 16.0));
assert!(a.col(0) == vec4::new(1.0, 5.0, 9.0, 13.0)); assert!(a.col(0) == vec4::new(1.0, 5.0, 9.0, 13.0));
assert!(a.col(1) == vec4::new(2.0, 6.0, 10.0, 14.0)); assert!(a.col(1) == vec4::new(2.0, 6.0, 10.0, 14.0));
assert!(a.col(2) == vec4::new(3.0, 7.0, 11.0, 15.0)); assert!(a.col(2) == vec4::new(3.0, 7.0, 11.0, 15.0));
assert!(a.col(3) == vec4::new(4.0, 8.0, 12.0, 16.0)); assert!(a.col(3) == vec4::new(4.0, 8.0, 12.0, 16.0));
assert!(mat4::identity() == mat4::new(1.0, 0.0, 0.0, 0.0, assert!(mat4::identity() == mat4::new(1.0, 0.0, 0.0, 0.0,
0.0, 1.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, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0)); 0.0, 0.0, 0.0, 1.0));
assert!(mat4::zero() == mat4::new(0.0, 0.0, 0.0, 0.0, assert!(mat4::zero() == mat4::new(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, 0.0, 0.0, 0.0, 0.0,
@ -412,19 +427,19 @@ fn test_mat4() {
assert!(a.determinant() == 0.0); assert!(a.determinant() == 0.0);
assert!(a.trace() == 34.0); assert!(a.trace() == 34.0);
assert!(a.neg() == mat4::new(-1.0, -5.0, -9.0, -13.0, assert!(a.neg() == mat4::new(-1.0, -5.0, -9.0, -13.0,
-2.0, -6.0, -10.0, -14.0, -2.0, -6.0, -10.0, -14.0,
-3.0, -7.0, -11.0, -15.0, -3.0, -7.0, -11.0, -15.0,
-4.0, -8.0, -12.0, -16.0)); -4.0, -8.0, -12.0, -16.0));
assert!(-a == a.neg()); assert!(-a == a.neg());
assert!(a.mul_t(f1) == mat4::new(0.5, 2.5, 4.5, 6.5, assert!(a.mul_t(f1) == mat4::new(0.5, 2.5, 4.5, 6.5,
1.0, 3.0, 5.0, 7.0, 1.0, 3.0, 5.0, 7.0,
1.5, 3.5, 5.5, 7.5, 1.5, 3.5, 5.5, 7.5,
2.0, 4.0, 6.0, 8.0)); 2.0, 4.0, 6.0, 8.0));
assert!(a.mul_v(&v1) == vec4::new(30.0, 70.0, 110.0, 150.0)); assert!(a.mul_v(&v1) == vec4::new(30.0, 70.0, 110.0, 150.0));
assert!(a.add_m(&b) == mat4::new(3.0, 11.0, 19.0, 27.0, assert!(a.add_m(&b) == mat4::new(3.0, 11.0, 19.0, 27.0,
5.0, 13.0, 21.0, 29.0, 5.0, 13.0, 21.0, 29.0,
7.0, 15.0, 23.0, 31.0, 7.0, 15.0, 23.0, 31.0,
@ -438,34 +453,34 @@ fn test_mat4() {
120.0, 280.0, 440.0, 600.0, 120.0, 280.0, 440.0, 600.0,
130.0, 306.0, 482.0, 658.0)); 130.0, 306.0, 482.0, 658.0));
assert!(a.dot(&b) == 1632.0); assert!(a.dot(&b) == 1632.0);
assert!(a.transpose() == mat4::new( 1.0, 2.0, 3.0, 4.0, assert!(a.transpose() == mat4::new( 1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0, 5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0, 9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0)); 13.0, 14.0, 15.0, 16.0));
assert!(c.inverse().unwrap() assert!(c.inverse().unwrap()
.fuzzy_eq(&mat4::new( 5.0, -4.0, 1.0, 0.0, .fuzzy_eq(&mat4::new( 5.0, -4.0, 1.0, 0.0,
-4.0, 8.0, -4.0, 0.0, -4.0, 8.0, -4.0, 0.0,
4.0, -8.0, 4.0, 8.0, 4.0, -8.0, 4.0, 8.0,
-3.0, 4.0, 1.0, -8.0).mul_t(0.125))); -3.0, 4.0, 1.0, -8.0).mul_t(0.125)));
let ident = mat4::identity(); let ident = mat4::identity();
assert!(ident.inverse().unwrap() == ident); assert!(ident.inverse().unwrap() == ident);
assert!(ident.is_identity()); assert!(ident.is_identity());
assert!(ident.is_symmetric()); assert!(ident.is_symmetric());
assert!(ident.is_diagonal()); assert!(ident.is_diagonal());
assert!(!ident.is_rotated()); assert!(!ident.is_rotated());
assert!(ident.is_invertible()); assert!(ident.is_invertible());
assert!(!a.is_identity()); assert!(!a.is_identity());
assert!(!a.is_symmetric()); assert!(!a.is_symmetric());
assert!(!a.is_diagonal()); assert!(!a.is_diagonal());
assert!(a.is_rotated()); assert!(a.is_rotated());
assert!(!a.is_invertible()); assert!(!a.is_invertible());
let c = mat4::new(4.0, 3.0, 2.0, 1.0, let c = mat4::new(4.0, 3.0, 2.0, 1.0,
3.0, 4.0, 3.0, 2.0, 3.0, 4.0, 3.0, 2.0,
2.0, 3.0, 4.0, 3.0, 2.0, 3.0, 4.0, 3.0,
@ -475,7 +490,7 @@ fn test_mat4() {
assert!(!c.is_diagonal()); assert!(!c.is_diagonal());
assert!(c.is_rotated()); assert!(c.is_rotated());
assert!(c.is_invertible()); assert!(c.is_invertible());
assert!(mat4::from_value(6.0).is_diagonal()); assert!(mat4::from_value(6.0).is_diagonal());
} }
@ -492,60 +507,60 @@ fn test_mat4_mut() {
y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 }, y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 },
z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 }, z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 },
w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } }; w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } };
let f1 = 0.5; let f1 = 0.5;
let mut mut_a: mat4 = a; let mut mut_a: mat4 = a;
let mut mut_c: mat4 = c; let mut mut_c: mat4 = c;
mut_a.swap_cols(0, 3); mut_a.swap_cols(0, 3);
assert!(mut_a.col(0) == a.col(3)); assert!(mut_a.col(0) == a.col(3));
assert!(mut_a.col(3) == a.col(0)); assert!(mut_a.col(3) == a.col(0));
mut_a = a; mut_a = a;
mut_a.swap_cols(1, 2); mut_a.swap_cols(1, 2);
assert!(mut_a.col(1) == a.col(2)); assert!(mut_a.col(1) == a.col(2));
assert!(mut_a.col(2) == a.col(1)); assert!(mut_a.col(2) == a.col(1));
mut_a = a; mut_a = a;
mut_a.swap_rows(0, 3); mut_a.swap_rows(0, 3);
assert!(mut_a.row(0) == a.row(3)); assert!(mut_a.row(0) == a.row(3));
assert!(mut_a.row(3) == a.row(0)); assert!(mut_a.row(3) == a.row(0));
mut_a = a; mut_a = a;
mut_a.swap_rows(1, 2); mut_a.swap_rows(1, 2);
assert!(mut_a.row(1) == a.row(2)); assert!(mut_a.row(1) == a.row(2));
assert!(mut_a.row(2) == a.row(1)); assert!(mut_a.row(2) == a.row(1));
mut_a = a; mut_a = a;
mut_a.set(&b); mut_a.set(&b);
assert!(mut_a == b); assert!(mut_a == b);
mut_a = a; mut_a = a;
mut_a.to_identity(); mut_a.to_identity();
assert!(mut_a.is_identity()); assert!(mut_a.is_identity());
mut_a = a; mut_a = a;
mut_a.to_zero(); mut_a.to_zero();
assert!(mut_a == mat4::zero()); assert!(mut_a == mat4::zero());
mut_a = a; mut_a = a;
mut_a.mul_self_t(f1); mut_a.mul_self_t(f1);
assert!(mut_a == a.mul_t(f1)); assert!(mut_a == a.mul_t(f1));
mut_a = a; mut_a = a;
mut_a.add_self_m(&b); mut_a.add_self_m(&b);
assert!(mut_a == a.add_m(&b)); assert!(mut_a == a.add_m(&b));
mut_a = a; mut_a = a;
mut_a.sub_self_m(&b); mut_a.sub_self_m(&b);
assert!(mut_a == a.sub_m(&b)); assert!(mut_a == a.sub_m(&b));
mut_a = a; mut_a = a;
mut_c.invert_self(); mut_c.invert_self();
assert!(mut_c == c.inverse().unwrap()); assert!(mut_c == c.inverse().unwrap());
// mut_c = c; // mut_c = c;
mut_a.transpose_self(); mut_a.transpose_self();
assert!(mut_a == a.transpose()); assert!(mut_a == a.transpose());
// mut_a = a; // mut_a = a;

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use mat::*; use mat::*;
@ -9,13 +24,13 @@ use vec::*;
#[test] #[test]
fn test_quat() { fn test_quat() {
let a = Quat { s: 1.0, v: Vec3 { x: 2.0, y: 3.0, z: 4.0 } }; let a = Quat { s: 1.0, v: Vec3 { x: 2.0, y: 3.0, z: 4.0 } };
assert!(a == quat::from_sv(1.0, vec3::new(2.0, 3.0, 4.0))); assert!(a == quat::from_sv(1.0, vec3::new(2.0, 3.0, 4.0)));
assert!(a == quat::new(1.0, 2.0, 3.0, 4.0)); assert!(a == quat::new(1.0, 2.0, 3.0, 4.0));
assert!(quat::zero() == quat::new(0.0, 0.0, 0.0, 0.0)); assert!(quat::zero() == quat::new(0.0, 0.0, 0.0, 0.0));
assert!(quat::identity() == quat::new(1.0, 0.0, 0.0, 0.0)); assert!(quat::identity() == quat::new(1.0, 0.0, 0.0, 0.0));
assert!(a.s == 1.0); assert!(a.s == 1.0);
assert!(a.v.x == 2.0); assert!(a.v.x == 2.0);
assert!(a.v.y == 3.0); assert!(a.v.y == 3.0);
@ -30,9 +45,9 @@ fn test_quat() {
#[test] #[test]
fn test_quat_2() { fn test_quat_2() {
let v = vec3::new(1f32, 0f32, 0f32); let v = vec3::new(1f32, 0f32, 0f32);
let q = quat::from_angle_axis((-45f32).radians(), &vec3::new(0f32, 0f32, -1f32)); let q = quat::from_angle_axis((-45f32).radians(), &vec3::new(0f32, 0f32, -1f32));
// http://www.wolframalpha.com/input/?i={1,0}+rotate+-45+degrees // http://www.wolframalpha.com/input/?i={1,0}+rotate+-45+degrees
assert!(q.mul_v(&v).fuzzy_eq(&vec3::new(1f32/2f32.sqrt(), 1f32/2f32.sqrt(), 0f32))); assert!(q.mul_v(&v).fuzzy_eq(&vec3::new(1f32/2f32.sqrt(), 1f32/2f32.sqrt(), 0f32)));
assert!(q.mul_v(&v).length() == v.length()); assert!(q.mul_v(&v).length() == v.length());

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cmp::FuzzyEq; use std::cmp::FuzzyEq;
use std::num::Real::{frac_pi_2, frac_pi_3}; use std::num::Real::{frac_pi_2, frac_pi_3};

View file

@ -1,3 +1,18 @@
// Copyright 2013 The Lmath Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::cast::transmute; use std::cast::transmute;
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
use std::num::{Zero, One}; use std::num::{Zero, One};