1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
use std::mem::transmute; use std::ops; use std::default::Default; use std::marker::PhantomData; use std::fmt; use units::{ Unit, Untyped }; use super::vec2::Vector2D; use super::vec4::Vector4D; pub type Vec3 = Vector3D<Untyped>; pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { Vector3D { x: x, y: y, z: z, _unit: PhantomData } } pub struct Vector3D<Unit> { pub x: f32, pub y: f32, pub z: f32, _unit: PhantomData<Unit>, } impl<U> AsRef<[f32; 3]> for Vector3D<U> { fn as_ref(&self) -> &[f32; 3] { unsafe { transmute(self) } } } impl<U> AsRef<(f32, f32, f32)> for Vector3D<U> { fn as_ref(&self) -> &(f32, f32, f32) { unsafe { transmute(self) } } } impl<U> AsMut<[f32; 3]> for Vector3D<U> { fn as_mut(&mut self) -> &mut [f32; 3] { unsafe { transmute(self) } } } impl<U> AsMut<(f32, f32, f32)> for Vector3D<U> { fn as_mut(&mut self) -> &mut (f32, f32, f32) { unsafe { transmute(self) } } } impl<U> AsRef<Vector3D<U>> for [f32; 3] { fn as_ref(&self) -> &Vector3D<U> { unsafe { transmute(self) } } } impl<U> AsRef<Vector3D<U>> for (f32, f32, f32) { fn as_ref(&self) -> &Vector3D<U> { unsafe { transmute(self) } } } impl<U> AsMut<Vector3D<U>> for [f32; 3] { fn as_mut(&mut self) -> &mut Vector3D<U> { unsafe { transmute(self) } } } impl<U> AsMut<Vector3D<U>> for (f32, f32, f32) { fn as_mut(&mut self) -> &mut Vector3D<U> { unsafe { transmute(self) } } } impl<U> Default for Vector3D<U> { fn default() -> Vector3D<U> { Vector3D { x: 0.0, y: 0.0, z: 0.0, _unit: PhantomData } } } #[allow(dead_code)] impl<U> Vector3D<U> { pub fn new(x: f32, y: f32, z: f32) -> Vector3D<U> { Vector3D { x: x, y: y, z: z, _unit: PhantomData } } pub fn from_slice(from: &[f32]) -> Vector3D<U> { assert!(from.len() >= 3); return Vector3D { x: from[0], y: from[1], z: from[2], _unit: PhantomData }; } pub fn as_slice<'l>(&'l self) -> &'l [f32] { unsafe { return transmute((&self.x as *const f32, 3 as usize )); } } pub fn as_mut_slice<'l>(&'l mut self) -> &'l mut [f32] { unsafe { return transmute((&mut self.x as *mut f32, 3 as usize )); } } #[inline] pub fn dot(&self, rhs: &Vector3D<U>) -> f32 { return self.x*rhs.x + self.y*rhs.y + self.z*rhs.z; } #[inline] pub fn cross(&self, rhs: &Vector3D<U>) -> Vector3D<U> { return Vector3D { x: (self.y * rhs.z) - (self.z * rhs.y), y: (self.z * rhs.x) - (self.x * rhs.z), z: (self.x * rhs.y) - (self.y * rhs.x), _unit: PhantomData } } pub fn length(&self) -> f32 { return self.square_length().sqrt(); } pub fn square_length(&self) -> f32 { return self.x * self.x + self.y * self.y + self.z * self.z; } pub fn tuple(&self) -> (f32, f32, f32) { (self.x, self.y, self.z) } pub fn array(&self) -> [f32; 3] { [self.x, self.y, self.z] } pub fn xy(&self) -> Vector2D<U> { Vector2D::new(self.x, self.y) } pub fn xz(&self) -> Vector2D<U> { Vector2D::new(self.x, self.z) } pub fn yz(&self) -> Vector2D<U> { Vector2D::new(self.y, self.z) } pub fn yx(&self) -> Vector2D<U> { Vector2D::new(self.y, self.x) } pub fn xyz(&self) -> Vector3D<U> { Vector3D::new(self.x, self.y, self.z) } pub fn zxy(&self) -> Vector3D<U> { Vector3D::new(self.z, self.x, self.y) } pub fn yzx(&self) -> Vector3D<U> { Vector3D::new(self.y, self.z, self.x) } pub fn xzy(&self) -> Vector3D<U> { Vector3D::new(self.x, self.z, self.y) } pub fn yxz(&self) -> Vector3D<U> { Vector3D::new(self.y, self.x, self.z) } pub fn to_vec4(&self, w: f32) -> Vector4D<U> { Vector4D::new(self.x, self.y, self.z, w) } } #[allow(dead_code)] impl<U> ops::Add<Vector3D<U>> for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn add(self, rhs: Vector3D<U>) -> Vector3D<U> { return Vector3D { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, _unit: PhantomData }; } } #[allow(dead_code)] impl<U> ops::Sub<Vector3D<U>> for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn sub(self, rhs: Vector3D<U>) -> Vector3D<U> { return Vector3D { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z, _unit: PhantomData }; } } #[allow(dead_code)] impl<U> ops::Mul<Vector3D<U>> for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn mul(self, rhs: Vector3D<U>) -> Vector3D<U> { return Vector3D { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z, _unit: PhantomData }; } } impl<U> ops::Mul<f32> for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn mul(self, rhs: f32) -> Vector3D<U> { return Vector3D::new(self.x * rhs, self.y * rhs, self.z * rhs); } } #[allow(dead_code)] impl<U> ops::Div<Vector3D<U>> for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn div(self, rhs: Vector3D<U>) -> Vector3D<U> { return Vector3D::new(self.x / rhs.x, self.y / rhs.y, self.z / rhs.z); } } impl<U> ops::Div<f32> for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn div(self, rhs: f32) -> Vector3D<U> { return Vector3D::new(self.x / rhs, self.y / rhs, self.z / rhs); } } #[allow(dead_code)] impl<U> ops::Neg for Vector3D<U> { type Output = Vector3D<U>; #[inline] fn neg(self) -> Vector3D<U> { return Vector3D { x: -self.x, y: -self.y, z: -self.z, _unit: PhantomData }; } } impl<U> Copy for Vector3D<U> {} impl<U> Clone for Vector3D<U> { fn clone(&self) -> Vector3D<U> { *self } } impl<U: Unit> fmt::Debug for Vector3D<U> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Vector3D<{}>[{} {} {}]", U::name(), self.x, self.y, self.z) } } impl<U> PartialEq for Vector3D<U> { fn eq(&self, other: &Vector3D<U>) -> bool { self.x == other.x && self.y == other.y && self.z == other.z } }