geometry.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // ┏━╸┏━╸┏━┓┏┳┓┏━╸╺┳╸┏━┓╻ ╻
  2. // ┃╺┓┣╸ ┃ ┃┃┃┃┣╸ ┃ ┣┳┛┗┳┛
  3. // ┗━┛┗━╸┗━┛╹ ╹┗━╸ ╹ ╹┗╸ ╹
  4. #[derive(Copy, Clone, Debug)]
  5. pub struct Vec3 {
  6. pub x: f32,
  7. pub y: f32,
  8. pub z: f32,
  9. }
  10. impl Vec3 {
  11. pub fn new(x: f32, y: f32, z: f32) -> Vec3 {
  12. return Vec3 {
  13. x: x,
  14. y: y,
  15. z: z
  16. };
  17. }
  18. pub fn length(&self) -> f32 {
  19. return (*self * *self).sqrt();
  20. }
  21. pub fn distance_to(&self, other: Vec3) -> f32 {
  22. return (*self - other).length();
  23. }
  24. pub fn normalize(&mut self) {
  25. let l = self.length();
  26. self.x /= l;
  27. self.y /= l;
  28. self.z /= l;
  29. }
  30. pub fn normalized(&self) -> Vec3 {
  31. let mut other = self.clone();
  32. other.normalize();
  33. return other;
  34. }
  35. pub fn cross(&self, _rhs: Vec3) -> Vec3 {
  36. return Vec3 {
  37. x: self.y * _rhs.z - self.z * _rhs.y,
  38. y: self.z * _rhs.x - self.x * _rhs.z,
  39. z: self.x * _rhs.y - self.y * _rhs.x,
  40. };
  41. }
  42. pub fn select(&self, a: Axis) -> f32 {
  43. match a {
  44. x => self.x,
  45. y => self.y,
  46. z => self.z
  47. }
  48. }
  49. }
  50. impl std::fmt::Display for Vec3 {
  51. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  52. write!(f, "({}, {}, {})", self.x, self.y, self.z);
  53. Ok(())
  54. }
  55. }
  56. impl std::ops::Add<Vec3> for Vec3 {
  57. type Output = Vec3;
  58. fn add(self, _rhs: Vec3) -> Vec3 {
  59. return Vec3 {
  60. x: self.x + _rhs.x,
  61. y: self.y + _rhs.y,
  62. z: self.z + _rhs.z,
  63. };
  64. }
  65. }
  66. impl std::ops::AddAssign for Vec3 {
  67. fn add_assign(&mut self, other: Vec3) {
  68. self.x += other.x;
  69. self.y += other.y;
  70. self.z += other.z;
  71. }
  72. }
  73. impl std::ops::Neg for Vec3 {
  74. type Output = Vec3;
  75. fn neg(self) -> Vec3 {
  76. return self * -1.0f32;
  77. }
  78. }
  79. impl std::ops::Sub for Vec3 {
  80. type Output = Vec3;
  81. fn sub(self, _rhs: Vec3) -> Vec3 {
  82. return self + (-_rhs);
  83. }
  84. }
  85. impl std::ops::Sub for &Vec3 {
  86. type Output = Vec3;
  87. fn sub(self, _rhs: &Vec3) -> Vec3 {
  88. return *self + (-*_rhs);
  89. }
  90. }
  91. // Dot product
  92. impl std::ops::Mul<Vec3> for Vec3 {
  93. type Output = f32;
  94. fn mul(self, _rhs: Vec3) -> f32 {
  95. self.x * _rhs.x + self.y * _rhs.y + self.z * _rhs.z
  96. }
  97. }
  98. // Scalar product
  99. impl std::ops::Mul<f32> for Vec3 {
  100. type Output = Vec3;
  101. fn mul(self, _rhs: f32) -> Vec3 {
  102. return Vec3 {
  103. x: _rhs * self.x,
  104. y: _rhs * self.y,
  105. z: _rhs * self.z
  106. };
  107. }
  108. }
  109. impl std::ops::Mul<Vec3> for f32 {
  110. type Output = Vec3;
  111. fn mul(self, _rhs: Vec3) -> Vec3 {
  112. return _rhs * self;
  113. }
  114. }
  115. /**
  116. * Coordinate space axis
  117. */
  118. #[derive(Clone,Copy)]
  119. pub enum Axis {
  120. x,
  121. y,
  122. z
  123. }
  124. impl Axis {
  125. pub fn next(&self) -> Axis {
  126. match self {
  127. Axis::x => Axis::y,
  128. Axis::y => Axis::z,
  129. Axis::z => Axis::x
  130. }
  131. }
  132. }
  133. pub struct Ray {
  134. pub origin: Vec3,
  135. pub direction: Vec3,
  136. pub n_inv: Vec3
  137. }
  138. impl Ray {
  139. pub fn calc_n_inv(&mut self) {
  140. self.n_inv = Vec3::new(1.0 / self.direction.x, 1.0 / self.direction.y, 1.0 / self.direction.z);
  141. }
  142. }