quaternion_wrapper/
ops.rs

1//! 演算子オーバーロードを実装
2
3use core::ops::{Add, AddAssign, Sub, SubAssign, Neg, Mul, MulAssign, Div, DivAssign};
4use super::{quat, QuaternionWrapper, Vector3Wrapper, ScalarWrapper};
5use super::Float;
6
7
8// ------------------------ Quaternion ------------------------ //
9// H + H
10impl<T: Float> Add for QuaternionWrapper<T> {
11    type Output = Self;
12    fn add(self, other: QuaternionWrapper<T>) -> Self {
13        Self( quat::add(self.0, other.0) )
14    }
15}
16
17// H += H
18impl<T: Float + Copy> AddAssign for QuaternionWrapper<T> {
19    fn add_assign(&mut self, other: QuaternionWrapper<T>) {
20        *self = Self( quat::add(self.0, other.0) );
21    }
22}
23
24// H + R^3
25impl<T: Float> Add<Vector3Wrapper<T>> for QuaternionWrapper<T> {
26    type Output = Self;
27    fn add(self, other: Vector3Wrapper<T>) -> Self {
28        Self(( (self.0).0, quat::add((self.0).1, other.0) ))
29    }
30}
31
32// H + R
33impl<T: Float> Add<ScalarWrapper<T>> for QuaternionWrapper<T> {
34    type Output = Self;
35    fn add(self, other: ScalarWrapper<T>) -> Self {
36        Self(( (self.0).0 + other.0, (self.0).1 ))
37    }
38}
39
40// H - H
41impl<T: Float> Sub for QuaternionWrapper<T> {
42    type Output = Self;
43    fn sub(self, other: QuaternionWrapper<T>) -> Self {
44        Self( quat::sub(self.0, other.0) )
45    }
46}
47
48// H -= H
49impl<T: Float + Copy> SubAssign for QuaternionWrapper<T> {
50    fn sub_assign(&mut self, other: QuaternionWrapper<T>) {
51        *self = Self( quat::sub(self.0, other.0) );
52    }
53}
54
55// -H
56impl<T: Float> Neg for QuaternionWrapper<T> {
57    type Output = Self;
58    fn neg(self) -> Self {
59        Self( quat::negate(self.0) )
60    }
61}
62
63// H - R^3
64impl<T: Float> Sub<Vector3Wrapper<T>> for QuaternionWrapper<T> {
65    type Output = Self;
66    fn sub(self, other: Vector3Wrapper<T>) -> Self {
67        Self(( (self.0).0, quat::sub((self.0).1, other.0) ))
68    }
69}
70
71// H - R
72impl<T: Float> Sub<ScalarWrapper<T>> for QuaternionWrapper<T> {
73    type Output = Self;
74    fn sub(self, other: ScalarWrapper<T>) -> Self {
75        Self(( (self.0).0 - other.0, (self.0).1 ))
76    }
77}
78
79// H * H
80impl<T: Float> Mul for QuaternionWrapper<T> {
81    type Output = Self;
82    fn mul(self, other: QuaternionWrapper<T>) -> Self {
83        Self( quat::mul(self.0, other.0) )
84    }
85}
86
87// H *= H
88impl<T: Float> MulAssign for QuaternionWrapper<T> {
89    fn mul_assign(&mut self, other: QuaternionWrapper<T>) {
90        *self = Self( quat::mul(self.0, other.0) );
91    }
92}
93
94// H * R^3
95impl<T: Float> Mul<Vector3Wrapper<T>> for QuaternionWrapper<T> {
96    type Output = Self;
97    fn mul(self, other: Vector3Wrapper<T>) -> Self {
98        Self((
99            -quat::dot((self.0).1, other.0), 
100            quat::scale_add((self.0).0, other.0, quat::cross((self.0).1, other.0)) 
101        ))
102    }
103}
104
105// H * R
106impl<T: Float> Mul<ScalarWrapper<T>> for QuaternionWrapper<T> {
107    type Output = Self;
108    fn mul(self, other: ScalarWrapper<T>) -> Self {
109        Self( quat::scale(other.0, self.0) )
110    }
111}
112
113// H / R
114impl<T: Float> Div<ScalarWrapper<T>> for QuaternionWrapper<T> {
115    type Output = Self;
116    fn div(self, other: ScalarWrapper<T>) -> Self {
117        Self( quat::scale(other.0.recip(), self.0) )
118    }
119}
120
121// -------------------------- Vector3 ------------------------- //
122// R^3 + R^3
123impl<T: Float> Add for Vector3Wrapper<T> {
124    type Output = Self;
125    fn add(self, other: Vector3Wrapper<T>) -> Self {
126        Self( quat::add(self.0, other.0) )
127    }
128}
129
130// R^3 += R^3
131impl<T: Float> AddAssign for Vector3Wrapper<T> {
132    fn add_assign(&mut self, other: Vector3Wrapper<T>) {
133        *self = Self( quat::add(self.0, other.0) )
134    }
135}
136
137// R^3 + H
138impl<T: Float> Add<QuaternionWrapper<T>> for Vector3Wrapper<T> {
139    type Output = QuaternionWrapper<T>;
140    fn add(self, other: QuaternionWrapper<T>) -> QuaternionWrapper<T> {
141        QuaternionWrapper(( (other.0).0, quat::add(self.0, (other.0).1) ))
142    }
143}
144
145// R^3 + R
146impl<T: Float> Add<ScalarWrapper<T>> for Vector3Wrapper<T> {
147    type Output = QuaternionWrapper<T>;
148    fn add(self, other: ScalarWrapper<T>) -> QuaternionWrapper<T> {
149        QuaternionWrapper(( other.0, self.0 ))
150    }
151}
152
153// R^3 - R^3
154impl<T: Float> Sub for Vector3Wrapper<T> {
155    type Output = Self;
156    fn sub(self, other: Vector3Wrapper<T>) -> Self {
157        Self( quat::sub(self.0, other.0) )
158    }
159}
160
161// R^3 -= R^3
162impl<T: Float> SubAssign for Vector3Wrapper<T> {
163    fn sub_assign(&mut self, other: Vector3Wrapper<T>) {
164        *self = Self( quat::sub(self.0, other.0) )
165    }
166}
167
168// -R
169impl<T: Float> Neg for Vector3Wrapper<T> {
170    type Output = Self;
171    fn neg(self) -> Self {
172        Self( quat::negate(self.0) )
173    }
174}
175
176// R^3 - H
177impl<T: Float> Sub<QuaternionWrapper<T>> for Vector3Wrapper<T> {
178    type Output = QuaternionWrapper<T>;
179    fn sub(self, other: QuaternionWrapper<T>) -> QuaternionWrapper<T> {
180        QuaternionWrapper(( -(other.0).0, quat::sub(self.0, (other.0).1) ))
181    }
182}
183
184// R^3 - R
185impl<T: Float> Sub<ScalarWrapper<T>> for Vector3Wrapper<T> {
186    type Output = QuaternionWrapper<T>;
187    fn sub(self, other: ScalarWrapper<T>) -> QuaternionWrapper<T> {
188        QuaternionWrapper(( -other.0, self.0 ))
189    }
190}
191
192// R^3 * R^3
193impl<T: Float> Mul for Vector3Wrapper<T> {
194    type Output = QuaternionWrapper<T>;
195    fn mul(self, other: Vector3Wrapper<T>) -> QuaternionWrapper<T> {
196        QuaternionWrapper( quat::mul(self.0, other.0) )
197    }
198}
199
200// R^3 * H
201impl<T: Float> Mul<QuaternionWrapper<T>> for Vector3Wrapper<T> {
202    type Output = QuaternionWrapper<T>;
203    fn mul(self, other: QuaternionWrapper<T>) -> QuaternionWrapper<T> {
204        QuaternionWrapper((
205            -quat::dot(self.0, (other.0).1),
206            quat::scale_add( (other.0).0, self.0, quat::cross(self.0, (other.0).1) )
207        ))
208    }
209}
210
211// R^3 * R
212impl<T: Float> Mul<ScalarWrapper<T>> for Vector3Wrapper<T> {
213    type Output = Self;
214    fn mul(self, other: ScalarWrapper<T>) -> Self {
215        Self( quat::scale(other.0, self.0) )
216    }
217}
218
219// R^3 / R
220impl<T: Float> Div<ScalarWrapper<T>> for Vector3Wrapper<T> {
221    type Output = Self;
222    fn div(self, other: ScalarWrapper<T>) -> Self {
223        Self( quat::scale(other.0.recip(), self.0) )
224    }
225}
226
227// --------------------------- Scalar ------------------------- //
228// R + R
229impl<T: Float> Add for ScalarWrapper<T> {
230    type Output = Self;
231    fn add(self, other: ScalarWrapper<T>) -> Self {
232        Self(self.0 + other.0)
233    }
234}
235
236// R += R
237impl<T: Float> AddAssign for ScalarWrapper<T> {
238    fn add_assign(&mut self, other: ScalarWrapper<T>) {
239        *self = Self(self.0 + other.0)
240    }
241}
242
243// R - R
244impl<T: Float> Sub for ScalarWrapper<T> {
245    type Output = Self;
246    fn sub(self, other: ScalarWrapper<T>) -> Self {
247        Self(self.0 - other.0)
248    }
249}
250
251// -R
252impl<T: Float> Neg for ScalarWrapper<T> {
253    type Output = Self;
254    fn neg(self) -> Self {
255        Self(-self.0)
256    }
257}
258
259// R -= R
260impl<T: Float> SubAssign for ScalarWrapper<T> {
261    fn sub_assign(&mut self, other: ScalarWrapper<T>) {
262        *self = Self(self.0 - other.0)
263    }
264}
265
266// R * R
267impl<T: Float> Mul for ScalarWrapper<T> {
268    type Output = Self;
269    fn mul(self, other: ScalarWrapper<T>) -> Self {
270        Self(self.0 * other.0)
271    }
272}
273
274// R *= R
275impl<T: Float> MulAssign for ScalarWrapper<T> {
276    fn mul_assign(&mut self, other: ScalarWrapper<T>) {
277        *self = Self(self.0 * other.0)
278    }
279}
280
281// R / R
282impl<T: Float> Div for ScalarWrapper<T> {
283    type Output = Self;
284    fn div(self, other: ScalarWrapper<T>) -> Self {
285        Self(self.0 / other.0)
286    }
287}
288
289// R /= R
290impl<T: Float> DivAssign for ScalarWrapper<T> {
291    fn div_assign(&mut self, other: ScalarWrapper<T>) {
292        *self = Self(self.0 / other.0)
293    }
294}
295
296// R + H
297impl<T: Float> Add<QuaternionWrapper<T>> for ScalarWrapper<T> {
298    type Output = QuaternionWrapper<T>;
299    fn add(self, other: QuaternionWrapper<T>) -> QuaternionWrapper<T> {
300        QuaternionWrapper(( self.0 + (other.0).0, (other.0).1 ))
301    }
302}
303
304// R - H
305impl<T: Float> Sub<QuaternionWrapper<T>> for ScalarWrapper<T> {
306    type Output = QuaternionWrapper<T>;
307    fn sub(self, other: QuaternionWrapper<T>) -> QuaternionWrapper<T> {
308        QuaternionWrapper(( self.0 - (other.0).0, quat::negate((other.0).1) ))
309    }
310}
311
312// R * H
313impl<T: Float> Mul<QuaternionWrapper<T>> for ScalarWrapper<T> {
314    type Output = QuaternionWrapper<T>;
315    fn mul(self, other: QuaternionWrapper<T>) -> QuaternionWrapper<T> {
316        QuaternionWrapper( quat::scale(self.0, other.0) )
317    }
318}
319
320// R + R^3
321impl<T: Float> Add<Vector3Wrapper<T>> for ScalarWrapper<T> {
322    type Output = QuaternionWrapper<T>;
323    fn add(self, other: Vector3Wrapper<T>) -> QuaternionWrapper<T> {
324        QuaternionWrapper( (self.0, other.0) )
325    }
326}
327
328// R - R^3
329impl<T: Float> Sub<Vector3Wrapper<T>> for ScalarWrapper<T> {
330    type Output = QuaternionWrapper<T>;
331    fn sub(self, other: Vector3Wrapper<T>) -> QuaternionWrapper<T> {
332        QuaternionWrapper( (self.0, quat::negate(other.0)) )
333    }
334}
335
336// R * R^3
337impl<T: Float> Mul<Vector3Wrapper<T>> for ScalarWrapper<T> {
338    type Output = Vector3Wrapper<T>;
339    fn mul(self, other: Vector3Wrapper<T>) -> Vector3Wrapper<T> {
340        Vector3Wrapper( quat::scale(self.0, other.0) )
341    }
342}