sead
Loading...
Searching...
No Matches
seadVectorCalcCommon.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef cafe
4
5#include <cafe.h>
6
7#ifndef DISABLE_PS
8#include <ppc_ps.h>
9
10inline f32x2& tof32x2(f32& x)
11{
12 return *reinterpret_cast<f32x2*>(&x);
13}
14
15inline const f32x2& tof32x2(const f32& x)
16{
17 return *reinterpret_cast<const f32x2*>(&x);
18}
19#endif // DISABLE_PS
20#endif // cafe
21
22#include <math/seadMathCalcCommon.h>
23
24namespace sead {
25
26template <typename T>
27inline void
28Vector2CalcCommon<T>::add(Base& o, const Base& a, const Base& b)
29{
30 o.x = a.x + b.x;
31 o.y = a.y + b.y;
32}
33
34template <typename T>
35inline void
36Vector2CalcCommon<T>::sub(Base& o, const Base& a, const Base& b)
37{
38 o.x = a.x - b.x;
39 o.y = a.y - b.y;
40}
41
42template <typename T>
43inline void
44Vector2CalcCommon<T>::div(Base& o, const Base& a, const Base& b)
45{
46 o.x = a.x / b.x;
47 o.y = a.y / b.y;
48}
49
50template <typename T>
51inline bool
53{
54 return a.x == b.x &&
55 a.y == b.y;
56}
57
58template <typename T>
59inline T
60Vector2CalcCommon<T>::dot(const Base& a, const Base& b)
61{
62 return a.x * b.x + a.y * b.y;
63}
64
65template <typename T>
66inline T
68{
69 return v.x * v.x + v.y * v.y;
70}
71
72template <typename T>
73inline T
78
79template <typename T>
80inline T
82{
83 T dx = v.x - t.x;
84 T dy = v.y - t.y;
85 return dx * dx + dy * dy;
86}
87
88template <typename T>
89inline T
91{
93}
94
95template <typename T>
96inline void
98{
99 o.x = MathCalcCommon<T>::lerp(a.x, b.x, ratio);
100 o.y = MathCalcCommon<T>::lerp(a.y, b.y, ratio);
101}
102
103template <typename T>
104inline void
106{
107 o.x = v.x * t;
108 o.y = v.y * t;
109}
110
111template <typename T>
112inline void
114{
116 multScalar(o, v, inv_t);
117}
118
119template <typename T>
120inline void
122{
123 o.x = MathCalcCommon<T>::neg(v.x);
124 o.y = MathCalcCommon<T>::neg(v.y);
125}
126
127template <typename T>
129{
130 const T len = length(v);
131 if (len > 0)
132 {
133 const T inv_len = 1 / len;
134 v.x *= inv_len;
135 v.y *= inv_len;
136 }
137
138 return len;
139}
140
141template <typename T>
143{
144 const T len = length(v);
145 if (len > 0)
146 {
147 const T inv_len = 1 / len;
148 o.x = v.x * inv_len;
149 o.y = v.y * inv_len;
150 }
151
152 return len;
153}
154
155template <typename T>
156inline constexpr void
158{
159 o.x = v.x;
160 o.y = v.y;
161}
162
163template <typename T>
164inline constexpr void
166{
167 v.x = x;
168 v.y = y;
169}
170
171template <typename T>
172inline void
173Vector3CalcCommon<T>::add(Base& o, const Base& a, const Base& b)
174{
175 o.x = a.x + b.x;
176 o.y = a.y + b.y;
177 o.z = a.z + b.z;
178}
179
180#ifdef cafe
181#ifndef DISABLE_PS
182template <>
183inline void
184Vector3CalcCommon<f32>::add(Base& o, const Base& a, const Base& b)
185{
187 o.z = a.z + b.z;
188}
189#endif // DISABLE_PS
190#endif // cafe
191
192template <typename T>
193inline void
194Vector3CalcCommon<T>::sub(Base& o, const Base& a, const Base& b)
195{
196 o.x = a.x - b.x;
197 o.y = a.y - b.y;
198 o.z = a.z - b.z;
199}
200
201#ifdef cafe
202#ifndef DISABLE_PS
203template <>
204inline void
205Vector3CalcCommon<f32>::sub(Base& o, const Base& a, const Base& b)
206{
208 o.z = a.z - b.z;
209}
210#endif // DISABLE_PS
211#endif // cafe
212
213template <typename T>
214inline bool
216{
217 return a.x == b.x &&
218 a.y == b.y &&
219 a.z == b.z;
220}
221
222template <typename T>
223inline void
224Vector3CalcCommon<T>::cross(Base& o, const Base& a, const Base& b)
225{
226 Base v;
227
228 v.x = a.y * b.z - a.z * b.y;
229 v.y = a.z * b.x - a.x * b.z;
230 v.z = a.x * b.y - a.y * b.x;
231
232 o.x = v.x;
233 o.y = v.y;
234 o.z = v.z;
235}
236
237#ifdef cafe
238
239template <>
240inline void
242{
243 ASM_VECCrossProduct((const Vec*)&a, (const Vec*)&b, (Vec*)&o);
244}
245
246#endif // cafe
247
248template <typename T>
249inline T
250Vector3CalcCommon<T>::dot(const Base& a, const Base& b)
251{
252 return a.x * b.x + a.y * b.y + a.z * b.z;
253}
254
255#ifdef cafe
256#ifndef DISABLE_PS
257template <>
258inline f32
260{
261 f32x2 f0;
262 f0 = __PS_MUL(tof32x2(a.x), tof32x2(b.x));
263 f0 = __PS_SUM0(f0, f0, f0);
264
265 return f0[0] + a.z * b.z;
266}
267#endif // DISABLE_PS
268#endif // cafe
269
270template <typename T>
271inline T
273{
274 return v.x * v.x + v.y * v.y + v.z * v.z;
275}
276
277#ifdef cafe
278
279template <>
280inline f32
282{
283 return ASM_VECSquareMag((const Vec*)&v);
284}
285
286#endif // cafe
287
288template <typename T>
289inline T
291{
293}
294
295#ifdef cafe
296
297template <>
298inline f32
300{
301 return ASM_VECMag((const Vec*)&v);
302}
303
304#endif // cafe
305
306template <typename T>
307inline T
309{
310 T dx = v.x - t.x;
311 T dy = v.y - t.y;
312 T dz = v.z - t.z;
313 return dx * dx + dy * dy + dz * dz;
314}
315
316#ifdef cafe
317
318template <>
319inline f32
321{
322 return ASM_VECSquareDistance((const Vec*)&v, (const Vec*)&t);
323}
324
325#endif // cafe
326
327template <typename T>
328inline T
330{
332}
333
334#ifdef cafe
335
336template <>
337inline f32
339{
340 return ASM_VECDistance((const Vec*)&v, (const Vec*)&t);
341}
342
343#endif // cafe
344
345template <typename T>
346inline void
348{
349 o.x = MathCalcCommon<T>::lerp(a.x, b.x, ratio);
350 o.y = MathCalcCommon<T>::lerp(a.y, b.y, ratio);
351 o.z = MathCalcCommon<T>::lerp(a.z, b.z, ratio);
352}
353
354#ifdef cafe
355
356template <>
357inline void
359{
360 ASM_MTXMultVec(const_cast<f32(*)[4]>(m.m), (const Vec*)&v, (Vec*)&o);
361}
362
363template <>
364inline void
366{
367 ASM_MTXMultVecSR(const_cast<f32(*)[4]>(m.m), (const Vec*)&v, (Vec*)&o);
368}
369
370#endif // cafe
371
372template <typename T>
373inline void
375{
376 o.x = v.x * t;
377 o.y = v.y * t;
378 o.z = v.z * t;
379}
380
381#ifdef cafe
382#ifndef DISABLE_PS
383template <>
384inline void
386{
388 o.z = v.z * t;
389}
390#endif // DISABLE_PS
391#endif // cafe
392
393template <typename T>
394inline void
396{
397 o.x = a.x * t + b.x;
398 o.y = a.y * t + b.y;
399 o.z = a.z * t + b.z;
400}
401
402#ifdef cafe
403#ifndef DISABLE_PS
404template <>
405inline void
407{
409 o.z = a.z * t + b.z;
410}
411#endif // DISABLE_PS
412#endif // cafe
413
414template <typename T>
415inline void
417{
419 multScalar(o, v, inv_t);
420}
421
422template <typename T>
423inline void
425{
426 o.x = MathCalcCommon<T>::neg(v.x);
427 o.y = MathCalcCommon<T>::neg(v.y);
428 o.z = MathCalcCommon<T>::neg(v.z);
429}
430
431template <typename T>
433{
434 const T len = length(v);
435 if (len > 0)
436 {
437 const T inv_len = 1 / len;
438 v.x *= inv_len;
439 v.y *= inv_len;
440 v.z *= inv_len;
441 }
442
443 return len;
444}
445
446template <typename T>
448{
449 const T len = length(v);
450 if (len > 0)
451 {
452 const T inv_len = 1 / len;
453 o.x = v.x * inv_len;
454 o.y = v.y * inv_len;
455 o.z = v.z * inv_len;
456 }
457
458 return len;
459}
460
461template <typename T>
462inline constexpr void
464{
465 o.x = v.x;
466 o.y = v.y;
467 o.z = v.z;
468}
469
470template <typename T>
471inline constexpr void
473{
474 v.x = x;
475 v.y = y;
476 v.z = z;
477}
478
479template <typename T>
480inline void
481Vector4CalcCommon<T>::add(Base& o, const Base& a, const Base& b)
482{
483 o.x = a.x + b.x;
484 o.y = a.y + b.y;
485 o.z = a.z + b.z;
486 o.w = a.w + b.w;
487}
488
489template <typename T>
490inline void
491Vector4CalcCommon<T>::sub(Base& o, const Base& a, const Base& b)
492{
493 o.x = a.x - b.x;
494 o.y = a.y - b.y;
495 o.z = a.z - b.z;
496 o.w = a.w - b.w;
497}
498
499template <typename T>
500inline bool
502{
503 return a.x == b.x &&
504 a.y == b.y &&
505 a.z == b.z &&
506 a.w == b.w;
507}
508
509template <typename T>
510inline T
511Vector4CalcCommon<T>::dot(const Base& a, const Base& b)
512{
513 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
514}
515
516template <typename T>
517inline T
519{
520 return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
521}
522
523template <typename T>
524inline T
526{
528}
529
530template <typename T>
531inline T
533{
534 T dx = v.x - t.x;
535 T dy = v.y - t.y;
536 T dz = v.z - t.z;
537 T dw = v.w - t.w;
538 return dx * dx + dy * dy + dz * dz + dw * dw;
539}
540
541template <typename T>
542inline T
544{
546}
547
548template <typename T>
549inline void
551{
552 o.x = MathCalcCommon<T>::lerp(a.x, b.x, ratio);
553 o.y = MathCalcCommon<T>::lerp(a.y, b.y, ratio);
554 o.z = MathCalcCommon<T>::lerp(a.z, b.z, ratio);
555 o.w = MathCalcCommon<T>::lerp(a.w, b.w, ratio);
556}
557
558template <typename T>
559inline void
561{
562 o.x = v.x * t;
563 o.y = v.y * t;
564 o.z = v.z * t;
565 o.w = v.w * t;
566}
567
568template <typename T>
569inline void
571{
573 multScalar(o, v, inv_t);
574}
575
576template <typename T>
577inline void
579{
580 o.x = MathCalcCommon<T>::neg(v.x);
581 o.y = MathCalcCommon<T>::neg(v.y);
582 o.z = MathCalcCommon<T>::neg(v.z);
583 o.w = MathCalcCommon<T>::neg(v.w);
584}
585
586template <typename T>
588{
589 const T len = length(v);
590 if (len > 0)
591 {
592 const T inv_len = 1 / len;
593 v.x *= inv_len;
594 v.y *= inv_len;
595 v.z *= inv_len;
596 v.w *= inv_len;
597 }
598
599 return len;
600}
601
602template <typename T>
604{
605 const T len = length(v);
606 if (len > 0)
607 {
608 const T inv_len = 1 / len;
609 o.x = v.x * inv_len;
610 o.y = v.y * inv_len;
611 o.z = v.z * inv_len;
612 o.w = v.w * inv_len;
613 }
614
615 return len;
616}
617
618template <typename T>
619inline constexpr void
621{
622 o.x = v.x;
623 o.y = v.y;
624 o.z = v.z;
625 o.w = v.w;
626}
627
628template <typename T>
629inline constexpr void
631{
632 v.x = x;
633 v.y = y;
634 v.z = z;
635 v.w = w;
636}
637
638} // namespace sead
Definition seadAssert.h:44