Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 9.30 KB | None | 0 0
  1. use std::cmp::{Ordering,PartialEq,PartialOrd};
  2. use std::ops::{Add,AddAssign,Div,DivAssign,Index,IndexMut,Mul,MulAssign,Neg,Rem,RemAssign,Sub,SubAssign};
  3. use std::hash::{Hash,Hasher};
  4. use std::mem;
  5. use std::f32;
  6.  
  7. #[derive(Debug, Default, Copy, Clone)]
  8. pub struct Vector3
  9. {
  10.     pub x: f32,
  11.     pub y: f32,
  12.     pub z: f32,
  13. }
  14.  
  15. impl Vector3
  16. {
  17.     pub fn abs(&self) -> Vector3
  18.     {
  19.         Vector3
  20.         {
  21.             x: self.x.abs(),
  22.             y: self.y.abs(),
  23.             z: self.z.abs()
  24.         }
  25.     }
  26.    
  27.     pub fn back() -> Vector3
  28.     {
  29.         Vector3 { z: -1f32, ..Default::default() }
  30.     }
  31.    
  32.     pub fn clamp(&self, min: Vector3, max: Vector3) -> Vector3
  33.     {
  34.         let mut x = self.x;
  35.         x = if x > max.x { max.x } else { x };
  36.         x = if x < min.x { min.x } else { x };
  37.        
  38.         let mut y = self.y;
  39.         y = if y > max.y { max.y } else { y };
  40.         y = if y < min.y { min.y } else { y };
  41.        
  42.         let mut z = self.z;
  43.         z = if z > max.z { max.z } else { z };
  44.         z = if z < min.z { min.z } else { z };
  45.        
  46.         Vector3 { x: x, y: y, z: z }
  47.     }
  48.    
  49.     pub fn clamp_length(&self, max: f32) -> Vector3
  50.     {
  51.         if self.length_squared() > max * max
  52.         {
  53.             return self.normalized() * max;
  54.         }
  55.         *self
  56.     }
  57.    
  58.     pub fn cross(&self, v: Vector3) -> Vector3
  59.     {
  60.         Vector3
  61.         {
  62.             x: self.y * v.z - self.z * v.y,
  63.             y: self.z * v.x - self.x * v.z,
  64.             z: self.x * v.y - self.y * v.x
  65.         }
  66.     }
  67.    
  68.     pub fn distance(&self, v: Vector3) -> f32
  69.     {
  70.         (*self - v).length()
  71.     }
  72.    
  73.     pub fn distance_squared(&self, v: Vector3) -> f32
  74.     {
  75.         (*self - v).length_squared()
  76.     }
  77.    
  78.     pub fn dot(&self, v: Vector3) -> f32
  79.     {
  80.         self.x * v.x + self.y * v.y + self.z * v.z
  81.     }
  82.    
  83.     pub fn down() -> Vector3
  84.     {
  85.         Vector3 { y: -1f32, ..Default::default() }
  86.     }
  87.    
  88.     pub fn exclude(&self, ex: Vector3) -> Vector3
  89.     {
  90.         ex - self.project(ex)
  91.     }
  92.    
  93.     pub fn forward() -> Vector3
  94.     {
  95.         Vector3 { z: 1f32, ..Default::default() }
  96.     }
  97.    
  98.     pub fn left() -> Vector3
  99.     {
  100.         Vector3 { x: -1f32, ..Default::default() }
  101.     }
  102.    
  103.     pub fn length(&self) -> f32
  104.     {
  105.         self.length_squared().sqrt()
  106.     }
  107.    
  108.     pub fn length_squared(&self) -> f32
  109.     {
  110.         self.dot(*self)
  111.     }
  112.    
  113.     pub fn lerp(&self, v: Vector3, amount: f32) -> Vector3
  114.     {
  115.         (*self * (1f32 - amount)) + v * amount
  116.     }
  117.    
  118.     pub fn max(&self, v: Vector3) -> Vector3
  119.     {
  120.         Vector3
  121.         {
  122.             x: if self.x > v.x { self.x } else { v.x },
  123.             y: if self.y > v.y { self.y } else { v.y },
  124.             z: if self.z > v.z { self.z } else { v.z }
  125.         }
  126.     }
  127.    
  128.     pub fn min(&self, v: Vector3) -> Vector3
  129.     {
  130.         Vector3
  131.         {
  132.             x: if self.x < v.x { self.x } else { v.x },
  133.             y: if self.y < v.y { self.y } else { v.y },
  134.             z: if self.z < v.z { self.z } else { v.z }
  135.         }
  136.     }
  137.    
  138.     pub fn move_towards(&self, target: Vector3, max_dist_delta: f32) -> Vector3
  139.     {
  140.         let dist = target - *self;
  141.         let magn = dist.length();
  142.         if magn <= max_dist_delta || magn == 0f32 { return target; }
  143.         *self + dist / magn * max_dist_delta
  144.     }
  145.    
  146.     pub fn normalized(&self) -> Vector3
  147.     {
  148.         *self / self.length()
  149.     }
  150.    
  151.     pub fn one() -> Vector3
  152.     {
  153.         Vector3 { x: 1f32, y: 1f32, z: 1f32 }
  154.     }
  155.    
  156.     pub fn project(&self, n: Vector3) -> Vector3
  157.     {
  158.         let dot = n.length_squared();
  159.         if dot < f32::EPSILON { return Vector3::zero(); }
  160.         n * self.dot(n) / dot
  161.     }
  162.    
  163.     pub fn project_on_plane(&self, pn: Vector3) -> Vector3
  164.     {
  165.         *self - self.project(pn)
  166.     }
  167.    
  168.     pub fn reflect(&self, n: Vector3) -> Vector3
  169.     {
  170.         *self - (n * self.dot(n) * 2f32)
  171.     }
  172.    
  173.     pub fn right() -> Vector3
  174.     {
  175.         Vector3 { x: 1f32, ..Default::default() }
  176.     }
  177.    
  178.     pub fn sqrt(&self) -> Vector3
  179.     {
  180.         Vector3
  181.         {
  182.             x: self.x.sqrt(),
  183.             y: self.y.sqrt(),
  184.             z: self.z.sqrt()
  185.         }
  186.     }
  187.    
  188.     pub fn transform(&self, r: Quaternion) -> Vector3
  189.     {
  190.         let rx = r.x + r.x;
  191.         let ry = r.y + r.y;
  192.         let rz = r.z + r.z;
  193.         let rwx = r.w * rx;
  194.         let rwy = r.w * ry;
  195.         let rwz = r.w * rz;
  196.         let rxx = r.x * rx;
  197.         let rxy = r.x * ry;
  198.         let rxz = r.x * rz;
  199.         let ryy = r.y * ry;
  200.         let ryz = r.y * rz;
  201.         let rzz = r.z * rz;
  202.        
  203.         Vector3
  204.         {
  205.             x: self.x*(1f32 - ryy - rzz) + self.y*(rxy - rwz) + self.z*(rxz + rwy),
  206.             y: self.x*(rxy + rwz) + self.y*(1f32 - rxx - rzz) + self.z*(ryz - rwx),
  207.             z: self.x*(rxz - rwy) + self.y*(ryz + rwx) + self.z*(1f32 - rxx - ryy)
  208.         }
  209.     }
  210.    
  211.     pub fn up() -> Vector3
  212.     {
  213.         Vector3 { y: 1f32, ..Default::default() }
  214.     }
  215.    
  216.     pub fn zero() -> Vector3
  217.     {
  218.         Vector3 { ..Default::default() }
  219.     }
  220. }
  221.  
  222. impl Hash for Vector3
  223. {
  224.     fn hash<H: Hasher>(&self, state: &mut H)
  225.     {
  226.         unsafe
  227.         {
  228.             state.write_u32(mem::transmute::<f32, u32>(self.x));
  229.             state.write_u32(mem::transmute::<f32, u32>(self.y));
  230.             state.write_u32(mem::transmute::<f32, u32>(self.z));
  231.         }
  232.     }
  233. }
  234.  
  235. impl Index<usize> for Vector3
  236. {
  237.     type Output = f32;
  238.    
  239.     fn index<'a>(&'a self, i: usize) -> &'a f32
  240.     {
  241.         match i
  242.         {
  243.             0 => &self.x,
  244.             1 => &self.y,
  245.             2 => &self.z,
  246.             _ => panic!("Vector3 index out of bounds: {}", i)
  247.         }
  248.     }
  249. }
  250.  
  251. impl IndexMut<usize> for Vector3
  252. {
  253.     fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut f32
  254.     {
  255.         match i
  256.         {
  257.             0 => &mut self.x,
  258.             1 => &mut self.y,
  259.             2 => &mut self.z,
  260.             _ => panic!("Vector3 index out of bounds: {}", i)
  261.         }
  262.     }
  263. }
  264.  
  265. impl PartialEq for Vector3
  266. {
  267.     fn eq(&self, other: &Vector3) -> bool
  268.     {
  269.         (self.x == other.x) && (self.y == other.y) && (self.z == other.z)
  270.     }
  271. }
  272.  
  273. impl PartialOrd for Vector3
  274. {
  275.     fn partial_cmp(&self, other: &Vector3) -> Option<Ordering>
  276.     {
  277.         if (self.x < other.x) && (self.y < other.y) && (self.z < other.z)
  278.         {
  279.             return Some(Ordering::Less);
  280.         }
  281.         else if (self.x > other.x) && (self.y > other.y) && (self.z > other.z)
  282.         {
  283.             return Some(Ordering::Greater);
  284.         }
  285.         else { return Some(Ordering::Equal) }
  286.     }
  287. }
  288.  
  289. impl Add for Vector3
  290. {
  291.     type Output = Vector3;
  292.    
  293.     fn add(self, rhs: Vector3) -> Vector3
  294.     {
  295.         Vector3
  296.         {
  297.             x: self.x + rhs.x,
  298.             y: self.y + rhs.y,
  299.             z: self.z + rhs.z
  300.         }
  301.     }
  302. }
  303.  
  304. impl Add<f32> for Vector3
  305. {
  306.     type Output = Vector3;
  307.    
  308.     fn add(self, rhs: f32) -> Vector3
  309.     {
  310.         Vector3
  311.         {
  312.             x: self.x + rhs,
  313.             y: self.y + rhs,
  314.             z: self.z + rhs
  315.         }
  316.     }
  317. }
  318.  
  319. impl AddAssign for Vector3
  320. {
  321.     fn add_assign(&mut self, rhs: Vector3)
  322.     {
  323.         *self = Vector3
  324.         {
  325.             x: self.x + rhs.x,
  326.             y: self.y + rhs.y,
  327.             z: self.z + rhs.z
  328.         };
  329.     }
  330. }
  331.  
  332. impl AddAssign<f32> for Vector3
  333. {
  334.     fn add_assign(&mut self, rhs: f32)
  335.     {
  336.         *self = Vector3
  337.         {
  338.             x: self.x + rhs,
  339.             y: self.y + rhs,
  340.             z: self.z + rhs
  341.         };
  342.     }
  343. }
  344.  
  345. impl Div for Vector3
  346. {
  347.     type Output = Vector3;
  348.    
  349.     fn div(self, rhs: Vector3) -> Vector3
  350.     {
  351.         Vector3
  352.         {
  353.             x: self.x / rhs.x,
  354.             y: self.y / rhs.y,
  355.             z: self.z / rhs.z
  356.         }
  357.     }
  358. }
  359.  
  360. impl Div<f32> for Vector3
  361. {
  362.     type Output = Vector3;
  363.    
  364.     fn div(self, rhs: f32) -> Vector3
  365.     {
  366.         Vector3
  367.         {
  368.             x: self.x / rhs,
  369.             y: self.y / rhs,
  370.             z: self.z / rhs
  371.         }
  372.     }
  373. }
  374.  
  375. impl DivAssign for Vector3
  376. {
  377.     fn div_assign(&mut self, rhs: Vector3)
  378.     {
  379.         *self = Vector3
  380.         {
  381.             x: self.x / rhs.x,
  382.             y: self.y / rhs.y,
  383.             z: self.z / rhs.z
  384.         };
  385.     }
  386. }
  387.  
  388. impl DivAssign<f32> for Vector3
  389. {
  390.     fn div_assign(&mut self, rhs: f32)
  391.     {
  392.         *self = Vector3
  393.         {
  394.             x: self.x / rhs,
  395.             y: self.y / rhs,
  396.             z: self.z / rhs
  397.         };
  398.     }
  399. }
  400.  
  401. impl Mul for Vector3
  402. {
  403.     type Output = Vector3;
  404.    
  405.     fn mul(self, rhs: Vector3) -> Vector3
  406.     {
  407.         Vector3
  408.         {
  409.             x: self.x * rhs.x,
  410.             y: self.y * rhs.y,
  411.             z: self.z * rhs.z
  412.         }
  413.     }
  414. }
  415.  
  416. impl Mul<f32> for Vector3
  417. {
  418.     type Output = Vector3;
  419.    
  420.     fn mul(self, rhs: f32) -> Vector3
  421.     {
  422.         Vector3
  423.         {
  424.             x: self.x * rhs,
  425.             y: self.y * rhs,
  426.             z: self.z * rhs
  427.         }
  428.     }
  429. }
  430.  
  431. impl MulAssign for Vector3
  432. {
  433.     fn mul_assign(&mut self, rhs: Vector3)
  434.     {
  435.         *self = Vector3
  436.         {
  437.             x: self.x * rhs.x,
  438.             y: self.y * rhs.y,
  439.             z: self.z * rhs.z
  440.         };
  441.     }
  442. }
  443.  
  444. impl MulAssign<f32> for Vector3
  445. {
  446.     fn mul_assign(&mut self, rhs: f32)
  447.     {
  448.         *self = Vector3
  449.         {
  450.             x: self.x * rhs,
  451.             y: self.y * rhs,
  452.             z: self.z * rhs
  453.         };
  454.     }
  455. }
  456.  
  457. impl Neg for Vector3
  458. {
  459.     type Output = Vector3;
  460.    
  461.     fn neg(self) -> Vector3
  462.     {
  463.         Vector3 { x: -self.x, y: -self.y, z: -self.z }
  464.     }
  465. }
  466.  
  467. impl Rem for Vector3
  468. {
  469.     type Output = Vector3;
  470.    
  471.     fn rem(self, rhs: Vector3) -> Vector3
  472.     {
  473.         Vector3
  474.         {
  475.             x: self.x % rhs.x,
  476.             y: self.y % rhs.y,
  477.             z: self.z % rhs.z
  478.         }
  479.     }
  480. }
  481.  
  482. impl Rem<f32> for Vector3
  483. {
  484.     type Output = Vector3;
  485.    
  486.     fn rem(self, rhs: f32) -> Vector3
  487.     {
  488.         Vector3
  489.         {
  490.             x: self.x % rhs,
  491.             y: self.y % rhs,
  492.             z: self.z % rhs
  493.         }
  494.     }
  495. }
  496.  
  497. impl RemAssign for Vector3
  498. {
  499.     fn rem_assign(&mut self, rhs: Vector3)
  500.     {
  501.         *self = Vector3
  502.         {
  503.             x: self.x % rhs.x,
  504.             y: self.y % rhs.y,
  505.             z: self.z % rhs.z
  506.         };
  507.     }
  508. }
  509.  
  510. impl RemAssign<f32> for Vector3
  511. {
  512.     fn rem_assign(&mut self, rhs: f32)
  513.     {
  514.         *self = Vector3
  515.         {
  516.             x: self.x % rhs,
  517.             y: self.y % rhs,
  518.             z: self.z % rhs
  519.         };
  520.     }
  521. }
  522.  
  523. impl Sub for Vector3
  524. {
  525.     type Output = Vector3;
  526.    
  527.     fn sub(self, rhs: Vector3) -> Vector3
  528.     {
  529.         Vector3
  530.         {
  531.             x: self.x - rhs.x,
  532.             y: self.y - rhs.y,
  533.             z: self.z - rhs.z
  534.         }
  535.     }
  536. }
  537.  
  538. impl Sub<f32> for Vector3
  539. {
  540.     type Output = Vector3;
  541.    
  542.     fn sub(self, rhs: f32) -> Vector3
  543.     {
  544.         Vector3
  545.         {
  546.             x: self.x - rhs,
  547.             y: self.y - rhs,
  548.             z: self.z - rhs
  549.         }
  550.     }
  551. }
  552.  
  553. impl SubAssign for Vector3
  554. {
  555.     fn sub_assign(&mut self, rhs: Vector3)
  556.     {
  557.         *self = Vector3
  558.         {
  559.             x: self.x - rhs.x,
  560.             y: self.y - rhs.y,
  561.             z: self.z - rhs.z
  562.         };
  563.     }
  564. }
  565.  
  566. impl SubAssign<f32> for Vector3
  567. {
  568.     fn sub_assign(&mut self, rhs: f32)
  569.     {
  570.         *self = Vector3
  571.         {
  572.             x: self.x - rhs,
  573.             y: self.y - rhs,
  574.             z: self.z - rhs
  575.         };
  576.     }
  577. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement