NW4F Sys
Loading...
Searching...
No Matches
math_Vector2.h
Go to the documentation of this file.
1#ifndef NW_MATH_VECTOR2_H_
2#define NW_MATH_VECTOR2_H_
3
4#include <cstring>
5#include <nw/math/math_Config.h>
6#include <nw/math/math_Arithmetic.h>
7
8namespace nw { namespace math {
9
10struct VEC2;
11
12namespace internal { namespace standard {
13
14 VEC2* VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
15 VEC2* VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
16
17} } // namespace internal::standard
18
19#if defined(NW_MATH_ENABLE_INTRINSICS)
20
21namespace internal { namespace intrinsics {
22
23 VEC2* VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
24 VEC2* VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
25
26} } // namespace internal::intrinsics
27
28#endif // NW_MATH_ENABLE_INTRINSICS
29
31
32NW_MATH_INLINE VEC2* VEC2Lerp(VEC2* pOut, const VEC2* p1, const VEC2* p2, f32 t);
33NW_MATH_INLINE f32 VEC2Dot(const VEC2* p1, const VEC2* p2);
34NW_MATH_INLINE VEC2* VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
35NW_MATH_INLINE VEC2* VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
36/* NW_MATH_INLINE */ extern VEC2* VEC2Normalize(VEC2* pOut, const VEC2* p);
37/* NW_MATH_INLINE */ extern VEC2* VEC2FastNormalize(VEC2* pOut, const VEC2* p);
38/* NW_MATH_INLINE */ extern VEC2* VEC2SafeNormalize(VEC2* pOut, const VEC2* p, const VEC2& alt);
39/* NW_MATH_INLINE */ extern VEC2* VEC2FastSafeNormalize(VEC2* pOut, const VEC2* p, const VEC2& alt);
40NW_MATH_INLINE f32 VEC2DistSq(const VEC2* p1, const VEC2* p2);
41
42struct VEC2_
43{
46};
47
48struct VEC2 : public VEC2_
49{
50public:
51 static const int DIMENSION = 2;
52
53 static const VEC2& Zero()
54 {
55 static const VEC2 zero(0.0f, 0.0f);
56
57 return zero;
58 }
59
60 typedef VEC2 self_type;
61 typedef f32 value_type;
62public:
63 VEC2() {}
64 explicit VEC2(const f32* p) { x = p[0]; y = p[1]; }
65 explicit VEC2(const VEC2_& v) { x = v.x; y = v.y; }
66 VEC2(f32 fx, f32 fy) { x = fx; y = fy; }
67
68 operator f32*() { return &x; }
69 operator const f32*() const { return &x; }
70
71 f32* ToF32() { return &x; }
72 const f32* ToF32() const { return &x; }
73
74 template <typename ToPtr>
76 {
77 return reinterpret_cast<ToPtr>( this );
78 }
79
80 template <typename ToPtr>
81 ToPtr Cast() const
82 {
83 return reinterpret_cast<ToPtr>( this );
84 }
85
86 self_type& operator += (const self_type& rhs) { x += rhs.x; y += rhs.y; return *this; }
87 self_type& operator -= (const self_type& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
88 self_type& operator *= (f32 f) { x *= f; y *= f; return *this; }
89 self_type& operator /= (f32 f) { f32 r = 1.f / f; x *= r; y *= r; return *this; }
90
91 self_type operator + () const { return *this; }
92 self_type operator - () const { return self_type(-x, -y); }
93
94 self_type operator + (const self_type& rhs) const { return self_type(x + rhs.x, y + rhs.y); }
95 self_type operator - (const self_type& rhs) const { return self_type(x - rhs.x, y - rhs.y); }
96 self_type operator * (f32 f) const { return self_type(f * x, f * y); }
97 self_type operator / (f32 f) const { f32 r = 1.f / f; return self_type(r * x, r * y); }
98
99 self_type& Lerp(const VEC2& lhs, const VEC2& rhs, f32 t)
100 {
101 return *VEC2Lerp(this, &lhs, &rhs, t);
102 }
103
104 f32 Dot(const VEC2& vec) const
105 {
106 return VEC2Dot(this, &vec);
107 }
108
109 f32 LengthSquare() const { return x * x + y * y; }
110 f32 Length() const { return FSqrt(this->x * this->x + this->y * this->y); }
111
113 {
114 return *VEC2Normalize(this, this);
115 }
116
118 {
119 return *VEC2Normalize(this, &src);
120 }
121
123 {
124 return *VEC2SafeNormalize(this, this, alt);
125 }
126
128 {
129 return *VEC2SafeNormalize(this, &src, alt);
130 }
131
133 {
134 return VEC2DistSq(this, &vec);
135 }
136
138 {
139 return *VEC2Maximize(this, &lhs, &rhs);
140 }
141
143 {
144 return *VEC2Minimize(this, &lhs, &rhs);
145 }
146
147 void Set(f32 fx, f32 fy) { x = fx; y = fy; }
148
149#pragma clang diagnostic push
150#pragma clang diagnostic ignored "-Wfloat-equal"
151
152 bool operator == (const self_type& rhs) const { return x == rhs.x && y == rhs.y; }
153 bool operator != (const self_type& rhs) const { return x != rhs.x || y != rhs.y; }
154
155#pragma clang diagnostic pop
156
157 bool IsZero() const { return VEC2IsZero(this); }
158};
159
160typedef struct VEC2 Vector2;
161
162namespace internal { namespace standard {
163
165VEC2Add(VEC2* pOut, const VEC2* p1, const VEC2* p2)
166{
167 pOut->x = p1->x + p2->x; pOut->y = p1->y + p2->y;
168 return pOut;
169}
170
172VEC2Sub(VEC2* pOut, const VEC2* p1, const VEC2* p2)
173{
174 pOut->x = p1->x - p2->x; pOut->y = p1->y - p2->y;
175 return pOut;
176}
177
179VEC2Scale(VEC2* pOut, const VEC2* p, f32 scale)
180{
181 pOut->x = p->x * scale; pOut->y = p->y * scale;
182 return pOut;
183}
184
186VEC2Lerp(VEC2* pOut, const VEC2* p1, const VEC2* p2, f32 t)
187{
188 // (1-t)*p1 + t*p2
189 pOut->x = p1->x + t * (p2->x - p1->x);
190 pOut->y = p1->y + t * (p2->y - p1->y);
191 return pOut;
192}
193
195VEC2Dot(const VEC2* p1, const VEC2* p2)
196{
197 return p1->x * p2->x + p1->y * p2->y;
198}
199
202{
203 return p->x * p->x + p->y * p->y;
204}
205
207VEC2DistSq(const VEC2* p1, const VEC2* p2)
208{
209 VEC2 tmp;
210 return standard::VEC2LenSq(VEC2Sub(&tmp, p1, p2));
211}
212
213} } // namespace internal::standard
214
215#if defined(NW_MATH_ENABLE_INTRINSICS)
216
217namespace internal { namespace intrinsics {
218
220VEC2Add(VEC2* pOut, const VEC2* p1, const VEC2* p2)
221{
223
224 return pOut;
225}
226
228VEC2Sub(VEC2* pOut, const VEC2* p1, const VEC2* p2)
229{
231
232 return pOut;
233}
234
236VEC2Scale(VEC2* pOut, const VEC2* p, f32 scale)
237{
239
240 return pOut;
241}
242
244VEC2Lerp(VEC2* pOut, const VEC2* p1, const VEC2* p2, f32 t)
245{
246 // (1-t)*p1 + t*p2
247
248 f32x2 tt = __PS_FDUP(t);
249 f32x2 f0 = tof32x2(p1->x);
250 f0 = __PS_NMSUB(f0, tt, f0);
252
253 return pOut;
254}
255
257VEC2Dot(const VEC2* p1, const VEC2* p2)
258{
260 f0 = __PS_SUM0(f0, f0, f0);
261
262 return f0[0];
263}
264
266VEC2LenSq(const VEC2* p)
267{
268 f32x2 f0 = tof32x2(p->x);
269 f0 =__PS_MUL(f0, f0);
270 f0 = __PS_SUM0(f0, f0, f0);
271
272 return f0[0];
273}
274
276VEC2DistSq(const VEC2* p1, const VEC2* p2)
277{
279 f0 =__PS_MUL(f0, f0);
280 f0 = __PS_SUM0(f0, f0, f0);
281
282 return f0[0];
283}
284
285} } // namespace internal::intrinsics
286
287#endif // NW_MATH_ENABLE_INTRINSICS
288
289inline VEC2*
290VEC2Add(VEC2* pOut, const VEC2* p1, const VEC2* p2)
291{
292 return NW_MATH_IMPL_NS::VEC2Add(pOut, p1, p2);
293}
294
295inline VEC2*
296VEC2Sub(VEC2* pOut, const VEC2* p1, const VEC2* p2)
297{
298 return NW_MATH_IMPL_NS::VEC2Sub(pOut, p1, p2);
299}
300
301inline VEC2*
302VEC2Scale(VEC2* pOut, const VEC2* p, f32 scale)
303{
304 return NW_MATH_IMPL_NS::VEC2Scale(pOut, p, scale);
305}
306
307inline VEC2*
308VEC2Lerp(VEC2* pOut, const VEC2* p1, const VEC2* p2, f32 t)
309{
310 return NW_MATH_IMPL_NS::VEC2Lerp(pOut, p1, p2, t);
311}
312
313inline f32
314VEC2Dot(const VEC2* p1, const VEC2* p2)
315{
316 return NW_MATH_IMPL_NS::VEC2Dot(p1, p2);
317}
318
319inline f32
321{
322 return internal::standard::VEC2LenSq(p);
323}
324
325inline f32
326VEC2Len(const VEC2* p) { return FSqrt(p->x * p->x + p->y * p->y); }
327
328inline f32
329VEC2DistSq(const VEC2* p1, const VEC2* p2)
330{
331 return NW_MATH_IMPL_NS::VEC2DistSq(p1, p2);
332}
333
334inline VEC2
335operator * (f32 f, const VEC2& rhs) { return VEC2(f * rhs.x, f * rhs.y); }
336
337} } // namespace nw::math
338
339#if defined(NW_MATH_AS_INLINE)
340
341namespace nw { namespace math {
342
343namespace internal { namespace standard {
344
346VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2)
347{
348 pOut->x = (p1->x > p2->x) ? p1->x : p2->x;
349 pOut->y = (p1->y > p2->y) ? p1->y : p2->y;
350
351 return pOut;
352}
353
355VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2)
356{
357 pOut->x = (p1->x < p2->x) ? p1->x : p2->x;
358 pOut->y = (p1->y < p2->y) ? p1->y : p2->y;
359
360 return pOut;
361}
362
363} } // namespace internal::standard
364
365#if defined(NW_MATH_ENABLE_INTRINSICS)
366
367namespace internal { namespace intrinsics {
368
370VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2)
371{
372 f32x2 f0 = tof32x2(p1->x);
373 f32x2 f1 = tof32x2(p2->x);
375
376 return pOut;
377}
378
380VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2)
381{
382 f32x2 f0 = tof32x2(p1->x);
383 f32x2 f1 = tof32x2(p2->x);
385
386 return pOut;
387}
388
389} } // namespace internal::intrinsics
390
391#endif // NW_MATH_ENABLE_INTRINSICS
392
394VEC2IsZero(const VEC2* p)
395{
396 return p->x == 0.f && p->y == 0.f;
397}
398
400VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2)
401{
402 return NW_MATH_IMPL_NS::VEC2Maximize(pOut, p1, p2);
403}
404
406VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2)
407{
408 return NW_MATH_IMPL_NS::VEC2Minimize(pOut, p1, p2);
409}
410
411// NW_MATH_INLINE VEC2*
412// VEC2Normalize(VEC2* pOut, const VEC2* p)
413// {
414// (void)VEC2Scale(pOut, p, FrSqrt(p->x * p->x + p->y * p->y));
415//
416// return pOut;
417// }
418
419// NW_MATH_INLINE VEC2*
420// VEC2FastNormalize(VEC2* pOut, const VEC2* p)
421// {
422// (void)VEC2Scale(pOut, p, FrFastSqrt(p->x * p->x + p->y * p->y));
423//
424// return pOut;
425// }
426
427// NW_MATH_INLINE VEC2*
428// VEC2SafeNormalize(VEC2* pOut, const VEC2* p, const VEC2& alt)
429// {
430// f32 mag = (p->x * p->x) + (p->y * p->y);
431//
432// if (mag == 0 /* || mag == F_INF || isnan(mag) */)
433// {
434// *pOut = alt;
435//
436// return pOut;
437// }
438//
439// (void)VEC2Scale(pOut, p, FrSqrt(mag));
440//
441// return pOut;
442// }
443
444// NW_MATH_INLINE VEC2*
445// VEC2FastSafeNormalize(VEC2* pOut, const VEC2* p, const VEC2& alt)
446// {
447// f32 mag = (p->x * p->x) + (p->y * p->y);
448//
449// if (mag == 0 /* || mag == F_INF || isnan(mag) */)
450// {
451// *pOut = alt;
452//
453// return pOut;
454// }
455//
456// (void)VEC2Scale(pOut, p, FrFastSqrt(mag));
457//
458// return pOut;
459// }
460
461} } // namespace nw::math
462
463#endif
464
465namespace nw { namespace math {
466
467inline bool VEC2IsZero(const VEC2& v){ return VEC2IsZero( &v ); }
468inline VEC2* VEC2Add(VEC2* pOut, const VEC2& p1, const VEC2& p2) { return VEC2Add(pOut, &p1, &p2); }
469inline VEC2* VEC2Sub(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Sub(pOut, &v1, &v2); }
470inline VEC2* VEC2Scale(VEC2* pOut, const VEC2& v, f32 scale) { return VEC2Scale(pOut, &v, scale); }
471inline VEC2* VEC2Lerp(VEC2* pOut, const VEC2& v1, const VEC2& v2, f32 t) { return VEC2Lerp(pOut, &v1, &v2, t); }
472inline f32 VEC2Dot(const VEC2& v1, const VEC2& v2) { return VEC2Dot(&v1, &v2); }
473inline f32 VEC2LenSq(const VEC2& v) { return VEC2LenSq( &v ); }
474inline f32 VEC2Len(const VEC2& v) { return VEC2Len( &v ); }
475inline f32 VEC2DistSq(const VEC2& v1, const VEC2& v2) { return VEC2DistSq( &v1, &v2 ); }
476
477inline VEC2* VEC2Maximize(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Maximize( pOut, &v1, &v2 ); }
478inline VEC2* VEC2Minimize(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Minimize( pOut, &v1, &v2 ); }
479inline VEC2* VEC2Normalize(VEC2* pOut, const VEC2& v) { return VEC2Normalize( pOut, &v ); }
480inline VEC2* VEC2FastNormalize(VEC2* pOut, const VEC2& v) { return VEC2FastNormalize( pOut, &v ); }
481inline VEC2* VEC2SafeNormalize(VEC2* pOut, const VEC2& v, const VEC2& alt) { return VEC2SafeNormalize( pOut, &v, alt ); }
482inline VEC2* VEC2FastSafeNormalize(VEC2* pOut, const VEC2& v, const VEC2& alt) { return VEC2FastSafeNormalize( pOut, &v, alt ); }
483
484} } // namespace nw::math
485
486#endif
#define NW_MATH_AS_INLINE
Definition math_Config.h:6
#define NW_MATH_INLINE
Definition math_Config.h:7
#define NW_MATH_IMPL_NS
Definition math_Config.h:14
Definition math_Matrix34.h:11
NW_INLINE f32 VEC2DistSq(const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:207
VEC2 * VEC2Minimize(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
NW_INLINE VEC2 * VEC2Add(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:165
NW_INLINE f32 VEC2LenSq(const VEC2 *p)
Definition math_Vector2.h:201
NW_INLINE VEC2 * VEC2Lerp(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2, f32 t)
Definition math_Vector2.h:186
NW_INLINE VEC2 * VEC2Sub(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:172
NW_INLINE VEC2 * VEC2Scale(VEC2 *pOut, const VEC2 *p, f32 scale)
Definition math_Vector2.h:179
VEC2 * VEC2Maximize(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
NW_INLINE f32 VEC2Dot(const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:195
Definition math_Triangular.cpp:3
Definition math_Constant.cpp:5
f32 VEC2Len(const VEC2 *p)
Definition math_Vector2.h:326
VEC2 * VEC2Scale(VEC2 *pOut, const VEC2 &v, f32 scale)
Definition math_Vector2.h:470
NW_MATH_INLINE VEC2 * VEC2Lerp(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2, f32 t)
Definition math_Vector2.h:308
NW_MATH_INLINE VEC2 * VEC2Maximize(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
f32 VEC2LenSq(const VEC2 *p)
Definition math_Vector2.h:320
VEC2 * VEC2Sub(VEC2 *pOut, const VEC2 &v1, const VEC2 &v2)
Definition math_Vector2.h:469
VEC2 * VEC2FastNormalize(VEC2 *pOut, const VEC2 &v)
Definition math_Vector2.h:480
VEC2 * VEC2Lerp(VEC2 *pOut, const VEC2 &v1, const VEC2 &v2, f32 t)
Definition math_Vector2.h:471
VEC2 * VEC2FastNormalize(VEC2 *pOut, const VEC2 *p)
VEC2 * VEC2Normalize(VEC2 *pOut, const VEC2 &v)
Definition math_Vector2.h:479
VEC2 * VEC2Add(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:290
VEC2 * VEC2Add(VEC2 *pOut, const VEC2 &p1, const VEC2 &p2)
Definition math_Vector2.h:468
VEC2 * VEC2Minimize(VEC2 *pOut, const VEC2 &v1, const VEC2 &v2)
Definition math_Vector2.h:478
VEC2 * VEC2Sub(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:296
struct VEC2 Vector2
Definition math_Vector2.h:160
f32 VEC2Len(const VEC2 &v)
Definition math_Vector2.h:474
NW_MATH_INLINE bool VEC2IsZero(const VEC2 *p)
f32 VEC2DistSq(const VEC2 &v1, const VEC2 &v2)
Definition math_Vector2.h:475
VEC2 * VEC2Scale(VEC2 *pOut, const VEC2 *p, f32 scale)
Definition math_Vector2.h:302
bool VEC2IsZero(const VEC2 &v)
Definition math_Vector2.h:467
VEC2 * VEC2Normalize(VEC2 *pOut, const VEC2 *p)
VEC2 * VEC2Maximize(VEC2 *pOut, const VEC2 &v1, const VEC2 &v2)
Definition math_Vector2.h:477
VEC2 * VEC2FastSafeNormalize(VEC2 *pOut, const VEC2 *p, const VEC2 &alt)
NW_MATH_INLINE f32 VEC2Dot(const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:314
VEC2 * VEC2SafeNormalize(VEC2 *pOut, const VEC2 &v, const VEC2 &alt)
Definition math_Vector2.h:481
VEC2 * VEC2SafeNormalize(VEC2 *pOut, const VEC2 *p, const VEC2 &alt)
NW_MATH_INLINE f32 VEC2DistSq(const VEC2 *p1, const VEC2 *p2)
Definition math_Vector2.h:329
NW_MATH_INLINE VEC2 * VEC2Minimize(VEC2 *pOut, const VEC2 *p1, const VEC2 *p2)
f32 VEC2Dot(const VEC2 &v1, const VEC2 &v2)
Definition math_Vector2.h:472
VEC2 * VEC2FastSafeNormalize(VEC2 *pOut, const VEC2 &v, const VEC2 &alt)
Definition math_Vector2.h:482
VEC2 operator*(f32 f, const VEC2 &rhs)
Definition math_Vector2.h:335
f32 VEC2LenSq(const VEC2 &v)
Definition math_Vector2.h:473
Definition math_Constant.cpp:5
Definition math_Vector2.h:43
f32 y
Definition math_Vector2.h:45
f32 x
Definition math_Vector2.h:44
Definition math_Vector2.h:49
f32 value_type
Definition math_Vector2.h:61
VEC2(const VEC2_ &v)
Definition math_Vector2.h:65
VEC2 self_type
Definition math_Vector2.h:60
VEC2(const f32 *p)
Definition math_Vector2.h:64
VEC2()
Definition math_Vector2.h:63
static const VEC2 & Zero()
Definition math_Vector2.h:53
static const int DIMENSION
Definition math_Vector2.h:51
VEC2(f32 fx, f32 fy)
Definition math_Vector2.h:66
#define NW_INLINE
Definition types.h:14