1use std::ops::{
2 Add,
3 Div,
4 DivAssign,
5 Mul,
6 Sub,
7};
8
9use num_traits::{
10 Num,
11 PrimInt,
12};
13use serde::{
14 Deserialize,
15 Serialize,
16};
17use wasm_bindgen::prelude::wasm_bindgen;
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
20pub struct Vector3<T>
22where
23 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
24{
25 pub x: T,
27 pub y: T,
29 pub z: T,
31}
32impl<T> Vector3<T>
33where
34 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
35{
36 pub const fn new(x: T, y: T, z: T) -> Self {
38 Self { x, y, z }
39 }
40 pub fn divide_all(&mut self, divide: T, minus: T) {
42 self.x = (self.x.saturating_sub(minus)) / divide;
43 self.y = (self.y.saturating_sub(minus)) / divide;
44 self.z = (self.z.saturating_sub(minus)) / divide;
45 }
46}
47#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
48pub struct Vector6<T>
50where
51 T: Serialize + PartialEq + PartialOrd + PrimInt + DivAssign + Div + Copy,
52{
53 pub x: T,
55 pub y: T,
57 pub z: T,
59 pub w: T,
61 pub a: T,
63 pub b: T,
65}
66impl<T> Vector6<T>
67where
68 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
69{
70 pub const fn new(x: T, y: T, z: T, w: T, a: T, b: T) -> Self {
72 Self { x, y, z, w, a, b }
73 }
74}
75impl<T> Add for Vector6<T>
76where
77 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
78{
79 type Output = Self;
80 fn add(self, o: Self) -> Self {
82 Self::new(
83 self.x + o.x,
84 self.y + o.y,
85 self.z + o.z,
86 self.w + o.w,
87 self.a + o.a,
88 self.b + o.b,
89 )
90 }
91}
92impl<T> Sub for Vector6<T>
93where
94 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div,
95{
96 type Output = Self;
97 fn sub(self, o: Self) -> Self {
99 Self::new(
100 self.x - o.x,
101 self.y - o.y,
102 self.z - o.z,
103 self.w - o.w,
104 self.a - o.a,
105 self.b - o.b,
106 )
107 }
108}
109impl<T> Mul for Vector6<T>
110where
111 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div,
112{
113 type Output = Self;
114 fn mul(self, o: Self) -> Self {
116 Self::new(
117 self.x * o.x,
118 self.y * o.y,
119 self.z * o.z,
120 self.w * o.w,
121 self.a * o.a,
122 self.b * o.b,
123 )
124 }
125}
126impl<T> Div for Vector6<T>
127where
128 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div,
129{
130 type Output = Self;
131 fn div(self, o: Self) -> Self {
133 Self::new(
134 self.x / o.x,
135 self.y / o.y,
136 self.z / o.z,
137 self.w / o.w,
138 self.a / o.a,
139 self.b / o.b,
140 )
141 }
142}
143impl<T> Add for Vector3<T>
144where
145 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
146{
147 type Output = Self;
148 fn add(self, o: Self) -> Self {
150 Self::new(self.x + o.x, self.y + o.y, self.z + o.z)
151 }
152}
153impl<T> Sub for Vector3<T>
154where
155 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div,
156{
157 type Output = Self;
158 fn sub(self, o: Self) -> Self {
160 Self::new(self.x - o.x, self.y - o.y, self.z - o.z)
161 }
162}
163impl<T> Mul for Vector3<T>
164where
165 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div,
166{
167 type Output = Self;
168 fn mul(self, o: Self) -> Self {
170 Self::new(self.x * o.x, self.y * o.y, self.z * o.z)
171 }
172}
173impl<T> Div for Vector3<T>
174where
175 T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div,
176{
177 type Output = Self;
178 fn div(self, o: Self) -> Self {
180 Self::new(self.x / o.x, self.y / o.y, self.z / o.z)
181 }
182}
183
184#[wasm_bindgen]
185#[derive(Debug, Clone, Copy)]
186pub struct Vector3u32 {
188 x: u32,
189 y: u32,
190 z: u32,
191}
192
193#[wasm_bindgen]
194impl Vector3u32 {
195 #[wasm_bindgen(constructor)]
196 pub fn new(x: u32, y: u32, z: u32) -> Vector3u32 {
198 Vector3u32 { x, y, z }
199 }
200
201 #[wasm_bindgen(getter)]
202 pub fn x(&self) -> u32 {
204 self.x
205 }
206 #[wasm_bindgen(getter)]
207 pub fn y(&self) -> u32 {
209 self.y
210 }
211 #[wasm_bindgen(getter)]
212 pub fn z(&self) -> u32 {
214 self.z
215 }
216
217 pub fn divide_all(&mut self, d: u32) {
219 self.x /= d;
220 self.y /= d;
221 self.z /= d;
222 }
223}
224
225impl From<Vector3u32> for Vector3<u32> {
227 fn from(v: Vector3u32) -> Self {
229 Vector3::new(v.x, v.y, v.z)
230 }
231}
232impl From<Vector3<u32>> for Vector3u32 {
233 fn from(v: Vector3<u32>) -> Self {
235 Vector3u32 {
236 x: v.x,
237 y: v.y,
238 z: v.z,
239 }
240 }
241}