2014-11-16 21:04:15 +08:00
|
|
|
|
use traits::structure::{Cast, Row, Basis, BaseFloat, Zero, One};
|
2013-10-06 22:54:09 +08:00
|
|
|
|
use traits::geometry::{Norm, Cross, CrossMatrix, UniformSphereSample};
|
|
|
|
|
use structs::vec::{Vec1, Vec2, Vec3, Vec4};
|
|
|
|
|
use structs::mat::Mat3;
|
2013-07-08 04:34:18 +08:00
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Copy + Mul<N, N> + Sub<N, N>> Cross<Vec1<N>> for Vec2<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline]
|
2014-12-02 01:50:11 +08:00
|
|
|
|
fn cross(&self, other: &Vec2<N>) -> Vec1<N> {
|
|
|
|
|
Vec1::new(self.x * other.y - self.y * other.x)
|
2013-08-05 16:13:44 +08:00
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 05:01:44 +08:00
|
|
|
|
// FIXME: instead of returning a Vec2, define a Mat2x1 matrix?
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Neg<N> + Copy> CrossMatrix<Vec2<N>> for Vec2<N> {
|
2013-08-26 05:01:44 +08:00
|
|
|
|
#[inline]
|
2014-12-02 01:50:11 +08:00
|
|
|
|
fn cross_matrix(&self) -> Vec2<N> {
|
2014-12-18 06:28:32 +08:00
|
|
|
|
Vec2::new(-self.y, self.x)
|
2013-08-26 05:01:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Copy + Mul<N, N> + Sub<N, N>> Cross<Vec3<N>> for Vec3<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline]
|
2014-12-02 01:50:11 +08:00
|
|
|
|
fn cross(&self, other: &Vec3<N>) -> Vec3<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
|
Vec3::new(
|
2014-12-02 01:50:11 +08:00
|
|
|
|
self.y * other.z - self.z * other.y,
|
|
|
|
|
self.z * other.x - self.x * other.z,
|
|
|
|
|
self.x * other.y - self.y * other.x
|
2013-08-05 15:44:56 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Neg<N> + Zero + Copy> CrossMatrix<Mat3<N>> for Vec3<N> {
|
2013-08-26 05:01:44 +08:00
|
|
|
|
#[inline]
|
2014-12-02 01:50:11 +08:00
|
|
|
|
fn cross_matrix(&self) -> Mat3<N> {
|
2013-08-26 05:01:44 +08:00
|
|
|
|
Mat3::new(
|
2014-12-18 06:28:32 +08:00
|
|
|
|
::zero(), -self.z, self.y,
|
|
|
|
|
self.z, ::zero(), -self.x,
|
|
|
|
|
-self.y, self.x, ::zero()
|
2013-08-26 05:01:44 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-06 22:54:09 +08:00
|
|
|
|
// FIXME: implement this for all other vectors
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Copy> Row<Vec1<N>> for Vec2<N> {
|
2013-09-13 16:26:19 +08:00
|
|
|
|
#[inline]
|
2013-10-06 22:54:09 +08:00
|
|
|
|
fn nrows(&self) -> uint {
|
2013-09-13 16:26:19 +08:00
|
|
|
|
2
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 05:01:44 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn row(&self, i: uint) -> Vec1<N> {
|
|
|
|
|
match i {
|
2014-12-18 06:28:32 +08:00
|
|
|
|
0 => Vec1::new(self.x),
|
|
|
|
|
1 => Vec1::new(self.y),
|
2014-10-30 12:25:47 +08:00
|
|
|
|
_ => panic!(format!("Index out of range: 2d vectors do not have {} rows. ", i))
|
2013-08-26 05:01:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn set_row(&mut self, i: uint, r: Vec1<N>) {
|
|
|
|
|
match i {
|
|
|
|
|
0 => self.x = r.x,
|
|
|
|
|
1 => self.y = r.x,
|
2014-10-30 12:25:47 +08:00
|
|
|
|
_ => panic!(format!("Index out of range: 2d vectors do not have {} rows.", i))
|
2013-08-26 05:01:44 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
|
impl<N: One> Basis for Vec1<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn canonical_basis(f: |Vec1<N>| -> bool) {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
f(Vec1::new(::one()));
|
2013-08-05 16:13:44 +08:00
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline(always)]
|
2014-10-27 06:16:44 +08:00
|
|
|
|
fn orthonormal_subspace_basis(_: &Vec1<N>, _: |Vec1<N>| -> bool) { }
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn canonical_basis_element(i: uint) -> Option<Vec1<N>> {
|
|
|
|
|
if i == 0 {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
Some(Vec1::new(::one()))
|
2014-10-27 06:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Copy + One + Zero + Neg<N>> Basis for Vec2<N> {
|
2013-08-17 16:48:45 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn canonical_basis(f: |Vec2<N>| -> bool) {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
if !f(Vec2::new(::one(), ::zero())) { return };
|
|
|
|
|
f(Vec2::new(::zero(), ::one()));
|
2013-08-05 15:44:56 +08:00
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn orthonormal_subspace_basis(n: &Vec2<N>, f: |Vec2<N>| -> bool) {
|
2014-12-18 06:28:32 +08:00
|
|
|
|
f(Vec2::new(-n.y, n.x));
|
2013-08-05 16:13:44 +08:00
|
|
|
|
}
|
2014-10-27 06:16:44 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn canonical_basis_element(i: uint) -> Option<Vec2<N>> {
|
|
|
|
|
if i == 0 {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
Some(Vec2::new(::one(), ::zero()))
|
2014-10-27 06:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
else if i == 1 {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
Some(Vec2::new(::zero(), ::one()))
|
2014-10-27 06:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 22:47:59 +08:00
|
|
|
|
impl<N: BaseFloat> Basis for Vec3<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn canonical_basis(f: |Vec3<N>| -> bool) {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
if !f(Vec3::new(::one(), ::zero(), ::zero())) { return };
|
|
|
|
|
if !f(Vec3::new(::zero(), ::one(), ::zero())) { return };
|
|
|
|
|
f(Vec3::new(::zero(), ::zero(), ::one()));
|
2013-08-05 15:44:56 +08:00
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn orthonormal_subspace_basis(n: &Vec3<N>, f: |Vec3<N>| -> bool) {
|
2013-08-05 15:44:56 +08:00
|
|
|
|
let a =
|
2014-12-18 06:28:32 +08:00
|
|
|
|
if n.x.abs() > n.y.abs() {
|
|
|
|
|
Norm::normalize_cpy(&Vec3::new(n.z, ::zero(), -n.x))
|
2013-08-05 16:13:44 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2014-12-18 06:28:32 +08:00
|
|
|
|
Norm::normalize_cpy(&Vec3::new(::zero(), -n.z, n.y))
|
2013-08-05 16:13:44 +08:00
|
|
|
|
};
|
2013-06-29 08:34:45 +08:00
|
|
|
|
|
2013-10-17 03:44:33 +08:00
|
|
|
|
if !f(Cross::cross(&a, n)) { return };
|
2013-08-05 15:44:56 +08:00
|
|
|
|
f(a);
|
|
|
|
|
}
|
2014-10-27 06:16:44 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn canonical_basis_element(i: uint) -> Option<Vec3<N>> {
|
|
|
|
|
if i == 0 {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
Some(Vec3::new(::one(), ::zero(), ::zero()))
|
2014-10-27 06:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
else if i == 1 {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
Some(Vec3::new(::zero(), ::one(), ::zero()))
|
2014-10-27 06:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
else if i == 2 {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
Some(Vec3::new(::zero(), ::zero(), ::one()))
|
2014-10-27 06:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-29 08:34:45 +08:00
|
|
|
|
}
|
2013-07-04 22:23:08 +08:00
|
|
|
|
|
|
|
|
|
// FIXME: this bad: this fixes definitly the number of samples…
|
2015-01-03 22:19:52 +08:00
|
|
|
|
static SAMPLES_2_F64: [Vec2<f64>; 21] = [
|
2013-08-05 15:44:56 +08:00
|
|
|
|
Vec2 { x: 1.0, y: 0.0 },
|
|
|
|
|
Vec2 { x: 0.95557281, y: 0.29475517 },
|
|
|
|
|
Vec2 { x: 0.82623877, y: 0.56332006 },
|
|
|
|
|
Vec2 { x: 0.6234898, y: 0.78183148 },
|
|
|
|
|
Vec2 { x: 0.36534102, y: 0.93087375 },
|
|
|
|
|
Vec2 { x: 0.07473009, y: 0.9972038 },
|
|
|
|
|
Vec2 { x: -0.22252093, y: 0.97492791 },
|
|
|
|
|
Vec2 { x: -0.5, y: 0.8660254 },
|
|
|
|
|
Vec2 { x: -0.73305187, y: 0.68017274 },
|
|
|
|
|
Vec2 { x: -0.90096887, y: 0.43388374 },
|
|
|
|
|
Vec2 { x: -0.98883083, y: 0.14904227 },
|
|
|
|
|
Vec2 { x: -0.98883083, y: -0.14904227 },
|
|
|
|
|
Vec2 { x: -0.90096887, y: -0.43388374 },
|
|
|
|
|
Vec2 { x: -0.73305187, y: -0.68017274 },
|
|
|
|
|
Vec2 { x: -0.5, y: -0.8660254 },
|
|
|
|
|
Vec2 { x: -0.22252093, y: -0.97492791 },
|
|
|
|
|
Vec2 { x: 0.07473009, y: -0.9972038 },
|
|
|
|
|
Vec2 { x: 0.36534102, y: -0.93087375 },
|
|
|
|
|
Vec2 { x: 0.6234898, y: -0.78183148 },
|
|
|
|
|
Vec2 { x: 0.82623877, y: -0.56332006 },
|
|
|
|
|
Vec2 { x: 0.95557281, y: -0.29475517 },
|
2013-07-04 22:23:08 +08:00
|
|
|
|
];
|
|
|
|
|
|
2013-07-22 18:32:16 +08:00
|
|
|
|
// Those vectors come from bullet 3d
|
2015-01-03 22:19:52 +08:00
|
|
|
|
static SAMPLES_3_F64: [Vec3<f64>; 42] = [
|
2013-08-05 15:44:56 +08:00
|
|
|
|
Vec3 { x: 0.000000 , y: -0.000000, z: -1.000000 },
|
|
|
|
|
Vec3 { x: 0.723608 , y: -0.525725, z: -0.447219 },
|
|
|
|
|
Vec3 { x: -0.276388, y: -0.850649, z: -0.447219 },
|
|
|
|
|
Vec3 { x: -0.894426, y: -0.000000, z: -0.447216 },
|
|
|
|
|
Vec3 { x: -0.276388, y: 0.850649 , z: -0.447220 },
|
|
|
|
|
Vec3 { x: 0.723608 , y: 0.525725 , z: -0.447219 },
|
|
|
|
|
Vec3 { x: 0.276388 , y: -0.850649, z: 0.447220 },
|
|
|
|
|
Vec3 { x: -0.723608, y: -0.525725, z: 0.447219 },
|
|
|
|
|
Vec3 { x: -0.723608, y: 0.525725 , z: 0.447219 },
|
|
|
|
|
Vec3 { x: 0.276388 , y: 0.850649 , z: 0.447219 },
|
|
|
|
|
Vec3 { x: 0.894426 , y: 0.000000 , z: 0.447216 },
|
|
|
|
|
Vec3 { x: -0.000000, y: 0.000000 , z: 1.000000 },
|
|
|
|
|
Vec3 { x: 0.425323 , y: -0.309011, z: -0.850654 },
|
|
|
|
|
Vec3 { x: -0.162456, y: -0.499995, z: -0.850654 },
|
|
|
|
|
Vec3 { x: 0.262869 , y: -0.809012, z: -0.525738 },
|
|
|
|
|
Vec3 { x: 0.425323 , y: 0.309011 , z: -0.850654 },
|
|
|
|
|
Vec3 { x: 0.850648 , y: -0.000000, z: -0.525736 },
|
|
|
|
|
Vec3 { x: -0.525730, y: -0.000000, z: -0.850652 },
|
|
|
|
|
Vec3 { x: -0.688190, y: -0.499997, z: -0.525736 },
|
|
|
|
|
Vec3 { x: -0.162456, y: 0.499995 , z: -0.850654 },
|
|
|
|
|
Vec3 { x: -0.688190, y: 0.499997 , z: -0.525736 },
|
|
|
|
|
Vec3 { x: 0.262869 , y: 0.809012 , z: -0.525738 },
|
|
|
|
|
Vec3 { x: 0.951058 , y: 0.309013 , z: 0.000000 },
|
|
|
|
|
Vec3 { x: 0.951058 , y: -0.309013, z: 0.000000 },
|
|
|
|
|
Vec3 { x: 0.587786 , y: -0.809017, z: 0.000000 },
|
|
|
|
|
Vec3 { x: 0.000000 , y: -1.000000, z: 0.000000 },
|
|
|
|
|
Vec3 { x: -0.587786, y: -0.809017, z: 0.000000 },
|
|
|
|
|
Vec3 { x: -0.951058, y: -0.309013, z: -0.000000 },
|
|
|
|
|
Vec3 { x: -0.951058, y: 0.309013 , z: -0.000000 },
|
|
|
|
|
Vec3 { x: -0.587786, y: 0.809017 , z: -0.000000 },
|
|
|
|
|
Vec3 { x: -0.000000, y: 1.000000 , z: -0.000000 },
|
|
|
|
|
Vec3 { x: 0.587786 , y: 0.809017 , z: -0.000000 },
|
|
|
|
|
Vec3 { x: 0.688190 , y: -0.499997, z: 0.525736 },
|
|
|
|
|
Vec3 { x: -0.262869, y: -0.809012, z: 0.525738 },
|
|
|
|
|
Vec3 { x: -0.850648, y: 0.000000 , z: 0.525736 },
|
|
|
|
|
Vec3 { x: -0.262869, y: 0.809012 , z: 0.525738 },
|
|
|
|
|
Vec3 { x: 0.688190 , y: 0.499997 , z: 0.525736 },
|
|
|
|
|
Vec3 { x: 0.525730 , y: 0.000000 , z: 0.850652 },
|
|
|
|
|
Vec3 { x: 0.162456 , y: -0.499995, z: 0.850654 },
|
|
|
|
|
Vec3 { x: -0.425323, y: -0.309011, z: 0.850654 },
|
|
|
|
|
Vec3 { x: -0.425323, y: 0.309011 , z: 0.850654 },
|
|
|
|
|
Vec3 { x: 0.162456 , y: 0.499995 , z: 0.850654 }
|
2013-07-22 18:32:16 +08:00
|
|
|
|
];
|
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: One + Copy> UniformSphereSample for Vec1<N> {
|
2013-09-08 22:03:03 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn sample(f: |Vec1<N>| -> ()) {
|
2014-11-16 21:04:15 +08:00
|
|
|
|
f(::one())
|
2013-09-08 22:03:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Cast<f64> + Copy> UniformSphereSample for Vec2<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn sample(f: |Vec2<N>| -> ()) {
|
2014-10-28 23:33:56 +08:00
|
|
|
|
for sample in SAMPLES_2_F64.iter() {
|
2014-12-18 06:28:32 +08:00
|
|
|
|
f(Cast::from(*sample))
|
2013-08-14 15:43:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-04 22:23:08 +08:00
|
|
|
|
}
|
2013-07-06 06:54:42 +08:00
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Cast<f64> + Copy> UniformSphereSample for Vec3<N> {
|
2013-08-08 02:53:51 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn sample(f: |Vec3<N>| -> ()) {
|
2014-10-28 23:33:56 +08:00
|
|
|
|
for sample in SAMPLES_3_F64.iter() {
|
2014-12-18 06:28:32 +08:00
|
|
|
|
f(Cast::from(*sample))
|
2013-08-05 16:13:44 +08:00
|
|
|
|
}
|
2013-08-05 15:44:56 +08:00
|
|
|
|
}
|
2013-07-06 06:54:42 +08:00
|
|
|
|
}
|
2013-10-04 00:26:09 +08:00
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
|
impl<N: Cast<f64> + Copy> UniformSphereSample for Vec4<N> {
|
2013-10-04 00:26:09 +08:00
|
|
|
|
#[inline(always)]
|
2013-11-27 18:16:16 +08:00
|
|
|
|
fn sample(_: |Vec4<N>| -> ()) {
|
2014-10-30 12:25:47 +08:00
|
|
|
|
panic!("UniformSphereSample::<Vec4<N>>::sample : Not yet implemented.")
|
2013-10-10 04:59:44 +08:00
|
|
|
|
// for sample in SAMPLES_3_F32.iter() {
|
|
|
|
|
// f(Cast::from(*sample))
|
2013-10-04 00:26:09 +08:00
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|