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
// Copyright (c) 2020 rose garden cult All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
//===----------------------------------------------------------------------===//
//
// Four point vector representation
//
//===----------------------------------------------------------------------===//

use crate::vector::Base;
use crate::vector::Component;

use std::fmt;
use std::ops::*;

#[doc = "A vector with x, y, z, and w components"]
#[doc = "derives traits: Default, Debug, PartialEq, Eq, Copy, Clone, Hash"]
#[derive(Default, Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub struct Vector4<T: Component> {
  pub x: T,
  pub y: T,
  pub z: T,
  pub w: T,
}

impl<T: Component + fmt::Display> fmt::Display for Vector4<T> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    return write!(
      f,
      "Vector4 - (x:{0}, y:{1}, z:{2}, w:{3})",
      self.x, self.y, self.z, self.w
    );
  }
}

impl<T: Component> Base<T> for Vector4<T> {
  #[doc = "Return the magnitude of the vector, sqrt(x^2 + y^2 + z^2 + w^2)"]
  fn magnitude(&self) -> T {
    return (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt();
  }

  #[doc = "Shrink vector to have magnitude of 1"]
  fn normalize(&mut self) -> Vector4<T> {
    *self = *self / self.magnitude();
    return *self;
  }
}

impl<T: Component> Add<Vector4<T>> for Vector4<T> {
  type Output = Vector4<T>;

  #[doc = "Add a Vector4 to another Vector4"]
  fn add(self, rhs: Vector4<T>) -> Vector4<T> {
    return Vector4::<T> {
      x: self.x + rhs.x,
      y: self.y + rhs.y,
      z: self.z + rhs.z,
      w: self.w + rhs.w,
    };
  }
}

impl<T: Component> Sub<Vector4<T>> for Vector4<T> {
  type Output = Vector4<T>;

  #[doc = "Subtract a Vector4 from another Vector4"]
  fn sub(self, rhs: Vector4<T>) -> Vector4<T> {
    return Vector4::<T> {
      x: self.x - rhs.x,
      y: self.y - rhs.y,
      z: self.z - rhs.z,
      w: self.w - rhs.w,
    };
  }
}

impl<T: Component + Neg<Output = T>> Neg for Vector4<T> {
  type Output = Vector4<T>;

  #[doc = "Reverse the sign of the vector's components"]
  fn neg(self) -> Vector4<T> {
    return Vector4::<T> {
      x: -self.x,
      y: -self.y,
      z: -self.z,
      w: -self.w,
    };
  }
}

impl<T: Component> Mul<T> for Vector4<T> {
  type Output = Vector4<T>;

  #[doc = "Multiply a Vector4 by a scalar"]
  fn mul(self, rhs: T) -> Vector4<T> {
    return Vector4 {
      x: self.x * rhs,
      y: self.y * rhs,
      z: self.z * rhs,
      w: self.w * rhs,
    };
  }
}

impl<T: Component> Div<T> for Vector4<T> {
  type Output = Vector4<T>;

  #[doc = "Divide a Vector4 by a scalar"]
  fn div(self, rhs: T) -> Vector4<T> {
    return Vector4 {
      x: self.x / rhs,
      y: self.y / rhs,
      z: self.z / rhs,
      w: self.w / rhs,
    };
  }
}