1use std::ops;
2
3#[derive(Debug, Copy, Clone, PartialEq)]
4pub struct Vec3 {
5 pub x: f64,
6 pub y: f64,
7 pub z: f64,
8}
9
10impl Vec3 {
11 pub fn new(x: f64, y: f64, z: f64) -> Self {
12 Self {
13 x: x,
14 y: y,
15 z: z
16 }
17 }
18
19 pub fn cofm1() -> Self {
20 Self {
21 x: 0.0,
22 y: 0.0,
23 z: 0.0
24 }
25 }
26
27 pub fn cofm2() -> Self {
28 Self {
29 x: 1.0,
30 y: 0.0,
31 z: 0.0
32 }
33 }
34
35 pub fn set(&mut self, x: f64, y: f64, z: f64) -> () {
36 self.x = x;
37 self.y = y;
38 self.z = z;
39 }
40
41 pub fn unit(&mut self) {
43 let norm_squared = self.x.powi(2) + self.y.powi(2) + self.z.powi(2);
44 let norm = norm_squared.sqrt();
45 self.x /= norm;
46 self.y /= norm;
47 self.z /= norm;
48 }
49
50 pub fn length(&self) -> f64 {
52 (self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
53 }
54
55 pub fn sqr(&self) -> f64 {
57 self.x.powi(2) + self.y.powi(2) + self.z.powi(2)
58 }
59
60 pub fn dot(&self, other: &Vec3) -> f64 {
62 self.x*other.x + self.y*other.y + self.z*other.z
63 }
64
65 pub fn cross(&self, other: &Vec3) -> Vec3 {
67 let temp_x = self.y*other.z - self.z*other.y;
68 let temp_y = self.z*other.x - self.x*other.z;
69 let temp_z = self.x*other.y - self.y*other.x;
70 Vec3::new(temp_x, temp_y, temp_z)
71 }
72
73}
74
75impl ops::MulAssign<f64> for Vec3 {
77 fn mul_assign(&mut self, rhs: f64) {
78 self.x *= rhs;
79 self.y *= rhs;
80 self.z *= rhs;
81 }
82}
83
84impl ops::DivAssign<f64> for Vec3 {
86 fn div_assign(&mut self, rhs: f64) {
87 self.x /= rhs;
88 self.y /= rhs;
89 self.z /= rhs;
90 }
91}
92
93impl ops::AddAssign<Vec3> for Vec3 {
95 fn add_assign(&mut self, rhs: Vec3) {
96 self.x += rhs.x;
97 self.y += rhs.y;
98 self.z += rhs.z;
99 }
100}
101
102
103impl ops::SubAssign<Vec3> for Vec3 {
105 fn sub_assign(&mut self, rhs: Vec3) {
106 self.x -= rhs.x;
107 self.y -= rhs.y;
108 self.z -= rhs.z;
109 }
110}
111
112impl ops::Add for Vec3 {
114 type Output = Self;
115 fn add(self, other: Self) -> Self {
116 Self {
117 x: self.x + other.x,
118 y: self.y + other.y,
119 z: self.z + other.z,
120 }
121 }
122}
123
124impl ops::Sub for Vec3 {
126 type Output = Self;
127 fn sub(self, other: Self) -> Self {
128 Self {
129 x: self.x - other.x,
130 y: self.y - other.y,
131 z: self.z - other.z,
132 }
133 }
134}
135
136impl ops::Mul<f64> for Vec3 {
138 type Output = Self;
139
140 fn mul(self, rhs: f64) -> Self {
141 Self{
142 x: self.x * rhs,
143 y: self.y * rhs,
144 z: self.z * rhs,
145 }
146 }
147}
148
149impl ops::Mul<Vec3> for f64 {
151 type Output = Vec3;
152
153 fn mul(self, rhs: Vec3) -> Vec3 {
154 Vec3{
155 x: self * rhs.x,
156 y: self * rhs.y,
157 z: self * rhs.z,
158 }
159 }
160}
161
162impl ops::Div<f64> for Vec3 {
164 type Output = Self;
165
166 fn div(self, rhs: f64) -> Self {
167 Self{
168 x: self.x / rhs,
169 y: self.y / rhs,
170 z: self.z / rhs,
171 }
172 }
173}
174
175impl ops::Div<Vec3> for f64 {
177 type Output = Vec3;
178
179 fn div(self, rhs: Vec3) -> Vec3 {
180 Vec3{
181 x: self / rhs.x,
182 y: self / rhs.y,
183 z: self / rhs.z,
184 }
185 }
186}
187
188impl ops::Neg for Vec3 {
190 type Output = Self;
191
192 fn neg(self) -> Self {
193 Vec3{
194 x: -self.x,
195 y: -self.y,
196 z: -self.z,
197 }
198 }
199}