123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- // ┏━╸┏━╸┏━┓┏┳┓┏━╸╺┳╸┏━┓╻ ╻
- // ┃╺┓┣╸ ┃ ┃┃┃┃┣╸ ┃ ┣┳┛┗┳┛
- // ┗━┛┗━╸┗━┛╹ ╹┗━╸ ╹ ╹┗╸ ╹
- #[derive(Copy, Clone, Debug)]
- pub struct Vec3 {
- pub x: f32,
- pub y: f32,
- pub z: f32,
- }
- impl Vec3 {
- pub fn new(x: f32, y: f32, z: f32) -> Vec3 {
- return Vec3 {
- x: x,
- y: y,
- z: z
- };
- }
- pub fn length(&self) -> f32 {
- return (*self * *self).sqrt();
- }
-
- pub fn distance_to(&self, other: Vec3) -> f32 {
- return (*self - other).length();
- }
- pub fn normalize(&mut self) {
- let l = self.length();
- self.x /= l;
- self.y /= l;
- self.z /= l;
- }
- pub fn normalized(&self) -> Vec3 {
- let mut other = self.clone();
- other.normalize();
- return other;
- }
- pub fn cross(&self, _rhs: Vec3) -> Vec3 {
- return Vec3 {
- 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,
- };
- }
- pub fn select(&self, a: Axis) -> f32 {
- match a {
- x => self.x,
- y => self.y,
- z => self.z
- }
- }
- }
- impl std::fmt::Display for Vec3 {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "({}, {}, {})", self.x, self.y, self.z);
- Ok(())
- }
- }
- impl std::ops::Add<Vec3> for Vec3 {
- type Output = Vec3;
- fn add(self, _rhs: Vec3) -> Vec3 {
- return Vec3 {
- x: self.x + _rhs.x,
- y: self.y + _rhs.y,
- z: self.z + _rhs.z,
- };
- }
- }
- impl std::ops::AddAssign for Vec3 {
- fn add_assign(&mut self, other: Vec3) {
- self.x += other.x;
- self.y += other.y;
- self.z += other.z;
- }
- }
- impl std::ops::Neg for Vec3 {
- type Output = Vec3;
- fn neg(self) -> Vec3 {
- return self * -1.0f32;
- }
- }
- impl std::ops::Sub for Vec3 {
- type Output = Vec3;
- fn sub(self, _rhs: Vec3) -> Vec3 {
- return self + (-_rhs);
- }
- }
- impl std::ops::Sub for &Vec3 {
- type Output = Vec3;
- fn sub(self, _rhs: &Vec3) -> Vec3 {
- return *self + (-*_rhs);
- }
- }
- // Dot product
- impl std::ops::Mul<Vec3> for Vec3 {
- type Output = f32;
- fn mul(self, _rhs: Vec3) -> f32 {
- self.x * _rhs.x + self.y * _rhs.y + self.z * _rhs.z
- }
- }
- // Scalar product
- impl std::ops::Mul<f32> for Vec3 {
- type Output = Vec3;
- fn mul(self, _rhs: f32) -> Vec3 {
- return Vec3 {
- x: _rhs * self.x,
- y: _rhs * self.y,
- z: _rhs * self.z
- };
- }
- }
- impl std::ops::Mul<Vec3> for f32 {
- type Output = Vec3;
- fn mul(self, _rhs: Vec3) -> Vec3 {
- return _rhs * self;
- }
- }
- /**
- * Coordinate space axis
- */
- #[derive(Clone,Copy)]
- pub enum Axis {
- x,
- y,
- z
- }
- impl Axis {
- pub fn next(&self) -> Axis {
- match self {
- Axis::x => Axis::y,
- Axis::y => Axis::z,
- Axis::z => Axis::x
- }
- }
- }
- pub struct Ray {
- pub origin: Vec3,
- pub direction: Vec3,
- pub n_inv: Vec3
- }
- impl Ray {
- pub fn calc_n_inv(&mut self) {
- self.n_inv = Vec3::new(1.0 / self.direction.x, 1.0 / self.direction.y, 1.0 / self.direction.z);
- }
- }
|