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
use std::{
    fmt::{self, Formatter},
    ops::{Add, AddAssign, Sub, SubAssign},
};

use crate::VecInner;

// TODO: parameterize by floating point length / fixed-point
// TODO: simd support?

#[derive(Clone, PartialEq, Copy)]
pub struct XYZVec<T> {
    inner: [T; 3],
}

impl<T: VecInner> XYZVec<T> {
    pub fn new(inner: [T; 3]) -> Self {
        Self { inner }
    }

    pub fn x(&self) -> T {
        self.inner[0]
    }

    pub fn y(&self) -> T {
        self.inner[1]
    }

    pub fn z(&self) -> T {
        self.inner[2]
    }

    pub fn scale_by(&self, d: T) -> Self {
        let x = self.x() * d;
        let y = self.y() * d;
        let z = self.z() * d;
        Self::new([x, y, z])
    }

    pub fn div_by(&self, d: T) -> Self {
        let x = self.x() / d;
        let y = self.y() / d;
        let z = self.z() / d;
        Self::new([x, y, z])
    }

    pub fn translated_by(&self, x: T, y: T, z: T) -> Self {
        let new_x = self.x() + x;
        let new_y = self.y() + y;
        let new_z = self.z() + z;
        Self {
            inner: [new_x, new_y, new_z],
        }
    }

    pub fn l1_norm(&self) -> T {
        self.x() + self.y() + self.z()
    }

    // pub fn l2_norm(&self) -> T {
    //     self.l2_norm_sqd().sqrt()
    // }

    pub fn l2_norm_sqd(&self) -> T {
        self.x() * self.x() + self.y() * self.y() + self.z() * self.z()
    }

    pub fn cross_prod(&self, other: Self) -> Self {
        let x: T = self.x() * other.y() - self.y() * other.x();
        let y: T = self.y() * other.z() - self.z() * other.y();
        let z: T = self.z() * other.x() - self.x() * other.z();
        Self::new([x, y, z])
    }

    pub fn dot_prod(&self, other: Self) -> T {
        self.x() * other.x() + self.y() * other.y() + self.z() * other.z()
    }

    pub fn cross_prod_magnitude_sqd(&self, other: Self) -> T {
        self.cross_prod(other).l2_norm_sqd()
    }
}

impl<T: VecInner> Add for XYZVec<T> {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        let x = self.x() + other.x();
        let y = self.y() + other.y();
        let z = self.z() + other.z();
        Self::new([x, y, z])
    }
}

impl<T: VecInner> AddAssign for XYZVec<T> {
    fn add_assign(&mut self, other: Self) {
        self.inner[0] += other.x();
        self.inner[1] += other.y();
        self.inner[2] += other.z();
    }
}

impl<T: VecInner> Sub for XYZVec<T> {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        let x = self.x() - other.x();
        let y = self.y() - other.y();
        let z = self.z() - other.z();
        Self::new([x, y, z])
    }
}

impl<T: VecInner> SubAssign for XYZVec<T> {
    fn sub_assign(&mut self, other: Self) {
        self.inner[0] -= other.x();
        self.inner[1] -= other.y();
        self.inner[2] -= other.z();
    }
}

impl<T: VecInner> fmt::Debug for XYZVec<T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "({:?}, {:?}, {:?})", self.x(), self.y(), self.z())
    }
}

impl<T: VecInner> fmt::Display for XYZVec<T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "({:.3}, {:.3}, {:.3})", self.x(), self.y(), self.z())
    }
}

impl XYZVec<f32> {
    pub fn l2_norm(&self) -> f32 {
        self.l2_norm_sqd().sqrt()
    }

    pub fn zeroes() -> Self {
        Self { inner: [0.0; 3] }
    }
}

impl XYZVec<f64> {
    pub fn l2_norm(&self) -> f64 {
        self.l2_norm_sqd().sqrt()
    }

    pub fn zeroes() -> Self {
        Self { inner: [0.0; 3] }
    }
}

// fn rotated_by_3d(&self, _other: Self, _theta: T) -> Self {
//     panic!("Reenable if we do 3d");
//     // let c  = theta.cos();
//     // let s = theta.sin();

//     // let cross_prod = self.cross_prod(other);
//     // let x = ((self.x() - other.x()*cross_prod))*(c - 1.0) + (other.z()*self.y() - other.y()*self.z())*s;
//     // let y = ((self.y() - other.y()*cross_prod))*(c - 1.0) + (other.x()*self.z() - other.z()*self.x())*s;
//     // let z = ((self.z() - other.z()*cross_prod))*(c - 1.0) + (other.y()*self.x() - other.x()*self.y())*s;
//     // Self::new([x,y,z])
// }