Skip to main content

binpack_3d/
vector.rs

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)]
20///
21pub struct Vector3<T>
22where
23    T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
24{
25    ///
26    pub x: T,
27    ///
28    pub y: T,
29    ///
30    pub z: T,
31}
32impl<T> Vector3<T>
33where
34    T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
35{
36    ///
37    pub const fn new(x: T, y: T, z: T) -> Self {
38        Self { x, y, z }
39    }
40    ///
41    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)]
48///
49pub struct Vector6<T>
50where
51    T: Serialize + PartialEq + PartialOrd + PrimInt + DivAssign + Div + Copy,
52{
53    ///
54    pub x: T,
55    ///
56    pub y: T,
57    ///
58    pub z: T,
59    ///
60    pub w: T,
61    ///
62    pub a: T,
63    ///
64    pub b: T,
65}
66impl<T> Vector6<T>
67where
68    T: Serialize + PartialEq + PartialOrd + Send + Sync + Num + PrimInt + DivAssign + Div + Copy,
69{
70    ///
71    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    ///
81    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    ///
98    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    ///
115    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    ///
132    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    ///
149    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    ///
159    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    ///
169    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    ///
179    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)]
186///
187pub struct Vector3u32 {
188    x: u32,
189    y: u32,
190    z: u32,
191}
192
193#[wasm_bindgen]
194impl Vector3u32 {
195    #[wasm_bindgen(constructor)]
196    ///
197    pub fn new(x: u32, y: u32, z: u32) -> Vector3u32 {
198        Vector3u32 { x, y, z }
199    }
200
201    #[wasm_bindgen(getter)]
202    ///
203    pub fn x(&self) -> u32 {
204        self.x
205    }
206    #[wasm_bindgen(getter)]
207    ///
208    pub fn y(&self) -> u32 {
209        self.y
210    }
211    #[wasm_bindgen(getter)]
212    ///
213    pub fn z(&self) -> u32 {
214        self.z
215    }
216
217    ///
218    pub fn divide_all(&mut self, d: u32) {
219        self.x /= d;
220        self.y /= d;
221        self.z /= d;
222    }
223}
224
225// optional conversions between wasm type and generic type
226impl From<Vector3u32> for Vector3<u32> {
227    ///
228    fn from(v: Vector3u32) -> Self {
229        Vector3::new(v.x, v.y, v.z)
230    }
231}
232impl From<Vector3<u32>> for Vector3u32 {
233    ///
234    fn from(v: Vector3<u32>) -> Self {
235        Vector3u32 {
236            x: v.x,
237            y: v.y,
238            z: v.z,
239        }
240    }
241}