aboutsummaryrefslogtreecommitdiff
path: root/src/core/vector3.rs
blob: 1ae4db2cd4de8b1abd7b30652ee8fc4b5fe34859 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Implements 3d vectors
//!
//! Also add more 3d math things needed for shading and 3d calculations.
use crate::{Float, Number, NEAR_ZERO};
use std::ops::{Mul, Sub, Add, DivAssign, Neg, AddAssign};
use std::fmt;

#[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> Add<T> for Vector3<T> {
    type Output = Self;

    fn add(self, op: T) -> Self::Output {
        Self::new_xyz(
            self.x + op,
            self.y + op,
            self.z + op,
        )
    }
}

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

impl<T: Number> Neg for Vector3<T> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::Output::new_xyz(
            -self.x,
            -self.y,
            -self.z,
            )
    }
}

impl<T: Number> AddAssign<&Self> for Vector3<T> {
    fn add_assign(&mut self, op: &Self) {
        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<T: Number> fmt::Display for Vector3<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_fmt(format_args!("[{}, {}, {}]", self.x, self.y, self.z))
    }
}

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 rendering::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) {
        // TODO Experiment with checking for normality with len_squared
        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
    }

    pub fn cross(&self, op: &Self) -> Self {
        Self::new_xyz(
            self.y * op.z - self.z * op.y,
            self.z * op.x - self.x * op.z,
            self.x * op.y - self.y * op.x,
            )

    }

    /// Check if vector is close to [0, 0, 0]
    ///
    /// This is based on the NEAR_ZERO constant
    pub fn near_zero(&self) -> bool {
        (self.x.abs() < NEAR_ZERO) &&
            (self.y.abs() < NEAR_ZERO) &&
            (self.z.abs() < NEAR_ZERO)
    }
}