aboutsummaryrefslogtreecommitdiff
path: root/src/core/vector3.rs
blob: 915765d99053860e6742bd698aeae2bd6a49fb4e (plain)
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
//! Implements 3d vectors
//!
//! Also add more 3d math things needed for shading and 3d calculations.
use crate::{Float, Number};
use std::ops::{Sub, Add, DivAssign};

#[derive(Clone, Copy)]
pub struct Vector3<T: Number> {
    pub x: T,
    pub y: T,
    pub z: T,
}

pub type Vector3f = Vector3<Float>;

impl<T: Number> Vector3<T> {
    pub fn new(initial: T) -> Vector3<T> {
        Vector3 { 
            x: initial,
            y: initial,
            z: initial,
        }
    }

    pub fn new_xyz(x: T, y: T, z: T) -> Vector3<T> {
        Vector3 { x, y, z}
    }
}

impl<T: Number> Sub for Vector3<T> {
    type Output = Self;
    fn sub(self, op: Self) -> Self::Output {
        Self::new_xyz(
            self.x - op.x,
            self.y - op.y,
            self.z - op.z,
        )
    }
}

impl<T: Number> Add for Vector3<T> {
    type Output = Self;
    fn add(self, op: Self) -> Self::Output {
        Self::new_xyz(
            self.x + op.x,
            self.y + op.y,
            self.z + op.z,
        )
    }
}

impl<T: Number> DivAssign<T> for Vector3<T> {
    fn div_assign(&mut self, op: T) {
        self.x /= op;
        self.y /= op;
        self.z /= op;
    }
}

impl Vector3f {
    /// Calculates the length times itself
    ///
    /// This is faster than using len * len as the square is ommited
    pub fn len_squared(&self) -> Float {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    pub fn len(&self) -> Float {
        self.len_squared().sqrt()
    }

    pub fn dot(&self, op: &Self) -> Float {
        self.x * op.x + self.y * op.y + self.z * op.z
    }

    /// Inplace normal instead of creating a new vector
    ///
    /// # Example
    ///
    /// ```
    /// use pathtrace::core::Vector3f;
    /// let mut v = Vector3f::new_xyz(10.0, 0.0, 0.0);
    /// v.norm_in();
    /// assert!(v.x == 1.0);
    /// ```
    pub fn norm_in(&mut self) {
        let len = self.len();
        if len == 0.0 {
            *self = Self::new(0.0);
        }

        *self /= len;
    }

    pub fn norm(&self) -> Self {
        let mut new = self.clone();
        new.norm_in();
        new
    }
}