Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cmp::{Ordering,PartialEq,PartialOrd};
- use std::ops::{Add,AddAssign,Div,DivAssign,Index,IndexMut,Mul,MulAssign,Neg,Rem,RemAssign,Sub,SubAssign};
- use std::hash::{Hash,Hasher};
- use std::mem;
- use std::f32;
- #[derive(Debug, Default, Copy, Clone)]
- pub struct Vector3
- {
- pub x: f32,
- pub y: f32,
- pub z: f32,
- }
- impl Vector3
- {
- pub fn abs(&self) -> Vector3
- {
- Vector3
- {
- x: self.x.abs(),
- y: self.y.abs(),
- z: self.z.abs()
- }
- }
- pub fn back() -> Vector3
- {
- Vector3 { z: -1f32, ..Default::default() }
- }
- pub fn clamp(&self, min: Vector3, max: Vector3) -> Vector3
- {
- let mut x = self.x;
- x = if x > max.x { max.x } else { x };
- x = if x < min.x { min.x } else { x };
- let mut y = self.y;
- y = if y > max.y { max.y } else { y };
- y = if y < min.y { min.y } else { y };
- let mut z = self.z;
- z = if z > max.z { max.z } else { z };
- z = if z < min.z { min.z } else { z };
- Vector3 { x: x, y: y, z: z }
- }
- pub fn clamp_length(&self, max: f32) -> Vector3
- {
- if self.length_squared() > max * max
- {
- return self.normalized() * max;
- }
- *self
- }
- pub fn cross(&self, v: Vector3) -> Vector3
- {
- Vector3
- {
- x: self.y * v.z - self.z * v.y,
- y: self.z * v.x - self.x * v.z,
- z: self.x * v.y - self.y * v.x
- }
- }
- pub fn distance(&self, v: Vector3) -> f32
- {
- (*self - v).length()
- }
- pub fn distance_squared(&self, v: Vector3) -> f32
- {
- (*self - v).length_squared()
- }
- pub fn dot(&self, v: Vector3) -> f32
- {
- self.x * v.x + self.y * v.y + self.z * v.z
- }
- pub fn down() -> Vector3
- {
- Vector3 { y: -1f32, ..Default::default() }
- }
- pub fn exclude(&self, ex: Vector3) -> Vector3
- {
- ex - self.project(ex)
- }
- pub fn forward() -> Vector3
- {
- Vector3 { z: 1f32, ..Default::default() }
- }
- pub fn left() -> Vector3
- {
- Vector3 { x: -1f32, ..Default::default() }
- }
- pub fn length(&self) -> f32
- {
- self.length_squared().sqrt()
- }
- pub fn length_squared(&self) -> f32
- {
- self.dot(*self)
- }
- pub fn lerp(&self, v: Vector3, amount: f32) -> Vector3
- {
- (*self * (1f32 - amount)) + v * amount
- }
- pub fn max(&self, v: Vector3) -> Vector3
- {
- Vector3
- {
- x: if self.x > v.x { self.x } else { v.x },
- y: if self.y > v.y { self.y } else { v.y },
- z: if self.z > v.z { self.z } else { v.z }
- }
- }
- pub fn min(&self, v: Vector3) -> Vector3
- {
- Vector3
- {
- x: if self.x < v.x { self.x } else { v.x },
- y: if self.y < v.y { self.y } else { v.y },
- z: if self.z < v.z { self.z } else { v.z }
- }
- }
- pub fn move_towards(&self, target: Vector3, max_dist_delta: f32) -> Vector3
- {
- let dist = target - *self;
- let magn = dist.length();
- if magn <= max_dist_delta || magn == 0f32 { return target; }
- *self + dist / magn * max_dist_delta
- }
- pub fn normalized(&self) -> Vector3
- {
- *self / self.length()
- }
- pub fn one() -> Vector3
- {
- Vector3 { x: 1f32, y: 1f32, z: 1f32 }
- }
- pub fn project(&self, n: Vector3) -> Vector3
- {
- let dot = n.length_squared();
- if dot < f32::EPSILON { return Vector3::zero(); }
- n * self.dot(n) / dot
- }
- pub fn project_on_plane(&self, pn: Vector3) -> Vector3
- {
- *self - self.project(pn)
- }
- pub fn reflect(&self, n: Vector3) -> Vector3
- {
- *self - (n * self.dot(n) * 2f32)
- }
- pub fn right() -> Vector3
- {
- Vector3 { x: 1f32, ..Default::default() }
- }
- pub fn sqrt(&self) -> Vector3
- {
- Vector3
- {
- x: self.x.sqrt(),
- y: self.y.sqrt(),
- z: self.z.sqrt()
- }
- }
- pub fn transform(&self, r: Quaternion) -> Vector3
- {
- let rx = r.x + r.x;
- let ry = r.y + r.y;
- let rz = r.z + r.z;
- let rwx = r.w * rx;
- let rwy = r.w * ry;
- let rwz = r.w * rz;
- let rxx = r.x * rx;
- let rxy = r.x * ry;
- let rxz = r.x * rz;
- let ryy = r.y * ry;
- let ryz = r.y * rz;
- let rzz = r.z * rz;
- Vector3
- {
- x: self.x*(1f32 - ryy - rzz) + self.y*(rxy - rwz) + self.z*(rxz + rwy),
- y: self.x*(rxy + rwz) + self.y*(1f32 - rxx - rzz) + self.z*(ryz - rwx),
- z: self.x*(rxz - rwy) + self.y*(ryz + rwx) + self.z*(1f32 - rxx - ryy)
- }
- }
- pub fn up() -> Vector3
- {
- Vector3 { y: 1f32, ..Default::default() }
- }
- pub fn zero() -> Vector3
- {
- Vector3 { ..Default::default() }
- }
- }
- impl Hash for Vector3
- {
- fn hash<H: Hasher>(&self, state: &mut H)
- {
- unsafe
- {
- state.write_u32(mem::transmute::<f32, u32>(self.x));
- state.write_u32(mem::transmute::<f32, u32>(self.y));
- state.write_u32(mem::transmute::<f32, u32>(self.z));
- }
- }
- }
- impl Index<usize> for Vector3
- {
- type Output = f32;
- fn index<'a>(&'a self, i: usize) -> &'a f32
- {
- match i
- {
- 0 => &self.x,
- 1 => &self.y,
- 2 => &self.z,
- _ => panic!("Vector3 index out of bounds: {}", i)
- }
- }
- }
- impl IndexMut<usize> for Vector3
- {
- fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut f32
- {
- match i
- {
- 0 => &mut self.x,
- 1 => &mut self.y,
- 2 => &mut self.z,
- _ => panic!("Vector3 index out of bounds: {}", i)
- }
- }
- }
- impl PartialEq for Vector3
- {
- fn eq(&self, other: &Vector3) -> bool
- {
- (self.x == other.x) && (self.y == other.y) && (self.z == other.z)
- }
- }
- impl PartialOrd for Vector3
- {
- fn partial_cmp(&self, other: &Vector3) -> Option<Ordering>
- {
- if (self.x < other.x) && (self.y < other.y) && (self.z < other.z)
- {
- return Some(Ordering::Less);
- }
- else if (self.x > other.x) && (self.y > other.y) && (self.z > other.z)
- {
- return Some(Ordering::Greater);
- }
- else { return Some(Ordering::Equal) }
- }
- }
- impl Add for Vector3
- {
- type Output = Vector3;
- fn add(self, rhs: Vector3) -> Vector3
- {
- Vector3
- {
- x: self.x + rhs.x,
- y: self.y + rhs.y,
- z: self.z + rhs.z
- }
- }
- }
- impl Add<f32> for Vector3
- {
- type Output = Vector3;
- fn add(self, rhs: f32) -> Vector3
- {
- Vector3
- {
- x: self.x + rhs,
- y: self.y + rhs,
- z: self.z + rhs
- }
- }
- }
- impl AddAssign for Vector3
- {
- fn add_assign(&mut self, rhs: Vector3)
- {
- *self = Vector3
- {
- x: self.x + rhs.x,
- y: self.y + rhs.y,
- z: self.z + rhs.z
- };
- }
- }
- impl AddAssign<f32> for Vector3
- {
- fn add_assign(&mut self, rhs: f32)
- {
- *self = Vector3
- {
- x: self.x + rhs,
- y: self.y + rhs,
- z: self.z + rhs
- };
- }
- }
- impl Div for Vector3
- {
- type Output = Vector3;
- fn div(self, rhs: Vector3) -> Vector3
- {
- Vector3
- {
- x: self.x / rhs.x,
- y: self.y / rhs.y,
- z: self.z / rhs.z
- }
- }
- }
- impl Div<f32> for Vector3
- {
- type Output = Vector3;
- fn div(self, rhs: f32) -> Vector3
- {
- Vector3
- {
- x: self.x / rhs,
- y: self.y / rhs,
- z: self.z / rhs
- }
- }
- }
- impl DivAssign for Vector3
- {
- fn div_assign(&mut self, rhs: Vector3)
- {
- *self = Vector3
- {
- x: self.x / rhs.x,
- y: self.y / rhs.y,
- z: self.z / rhs.z
- };
- }
- }
- impl DivAssign<f32> for Vector3
- {
- fn div_assign(&mut self, rhs: f32)
- {
- *self = Vector3
- {
- x: self.x / rhs,
- y: self.y / rhs,
- z: self.z / rhs
- };
- }
- }
- impl Mul for Vector3
- {
- type Output = Vector3;
- fn mul(self, rhs: Vector3) -> Vector3
- {
- Vector3
- {
- x: self.x * rhs.x,
- y: self.y * rhs.y,
- z: self.z * rhs.z
- }
- }
- }
- impl Mul<f32> for Vector3
- {
- type Output = Vector3;
- fn mul(self, rhs: f32) -> Vector3
- {
- Vector3
- {
- x: self.x * rhs,
- y: self.y * rhs,
- z: self.z * rhs
- }
- }
- }
- impl MulAssign for Vector3
- {
- fn mul_assign(&mut self, rhs: Vector3)
- {
- *self = Vector3
- {
- x: self.x * rhs.x,
- y: self.y * rhs.y,
- z: self.z * rhs.z
- };
- }
- }
- impl MulAssign<f32> for Vector3
- {
- fn mul_assign(&mut self, rhs: f32)
- {
- *self = Vector3
- {
- x: self.x * rhs,
- y: self.y * rhs,
- z: self.z * rhs
- };
- }
- }
- impl Neg for Vector3
- {
- type Output = Vector3;
- fn neg(self) -> Vector3
- {
- Vector3 { x: -self.x, y: -self.y, z: -self.z }
- }
- }
- impl Rem for Vector3
- {
- type Output = Vector3;
- fn rem(self, rhs: Vector3) -> Vector3
- {
- Vector3
- {
- x: self.x % rhs.x,
- y: self.y % rhs.y,
- z: self.z % rhs.z
- }
- }
- }
- impl Rem<f32> for Vector3
- {
- type Output = Vector3;
- fn rem(self, rhs: f32) -> Vector3
- {
- Vector3
- {
- x: self.x % rhs,
- y: self.y % rhs,
- z: self.z % rhs
- }
- }
- }
- impl RemAssign for Vector3
- {
- fn rem_assign(&mut self, rhs: Vector3)
- {
- *self = Vector3
- {
- x: self.x % rhs.x,
- y: self.y % rhs.y,
- z: self.z % rhs.z
- };
- }
- }
- impl RemAssign<f32> for Vector3
- {
- fn rem_assign(&mut self, rhs: f32)
- {
- *self = Vector3
- {
- x: self.x % rhs,
- y: self.y % rhs,
- z: self.z % rhs
- };
- }
- }
- impl Sub for Vector3
- {
- type Output = Vector3;
- fn sub(self, rhs: Vector3) -> Vector3
- {
- Vector3
- {
- x: self.x - rhs.x,
- y: self.y - rhs.y,
- z: self.z - rhs.z
- }
- }
- }
- impl Sub<f32> for Vector3
- {
- type Output = Vector3;
- fn sub(self, rhs: f32) -> Vector3
- {
- Vector3
- {
- x: self.x - rhs,
- y: self.y - rhs,
- z: self.z - rhs
- }
- }
- }
- impl SubAssign for Vector3
- {
- fn sub_assign(&mut self, rhs: Vector3)
- {
- *self = Vector3
- {
- x: self.x - rhs.x,
- y: self.y - rhs.y,
- z: self.z - rhs.z
- };
- }
- }
- impl SubAssign<f32> for Vector3
- {
- fn sub_assign(&mut self, rhs: f32)
- {
- *self = Vector3
- {
- x: self.x - rhs,
- y: self.y - rhs,
- z: self.z - rhs
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement