1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Vector2.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 48334 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_MATH_VECTOR2_H_
17 #define NN_MATH_VECTOR2_H_
18 
19 #include <cstring>
20 #include <nn/math/math_Config.h>
21 
22 #pragma push
23 #pragma Otime
24 
25 namespace nn {
26 namespace math {
27 
28 struct VEC2;
29 struct MTX23;
30 
31 /* Please see man pages for details
32 
33 
34 */
35 /*
36 
37 
38 
39 
40 
41 
42 
43  */
44 NN_MATH_INLINE VEC2* VEC2Add(VEC2* pOut, const VEC2* p1, const VEC2* p2);
45 
46 /*
47 
48 
49 
50 
51 
52 
53  */
54 NN_MATH_INLINE f32 VEC2DistSq(const VEC2* p1, const VEC2* p2);
55 
56 /*
57 
58 
59 
60 
61 
62 
63  */
64 NN_MATH_INLINE f32 VEC2Dot(const VEC2* p1, const VEC2* p2);
65 
66 /*
67 
68 
69 
70 
71 
72  */
73 NN_MATH_INLINE bool VEC2IsZero(const VEC2* p);
74 
75 /*
76 
77 
78 
79 
80 
81  */
82 NN_MATH_INLINE f32 VEC2Len(const VEC2* p);
83 
84 /*
85 
86 
87 
88 
89 
90  */
91 NN_MATH_INLINE f32 VEC2LenSq(const VEC2* p);
92 
93 /*
94 
95 
96 
97 
98 
99 
100 
101 
102  */
103 NN_MATH_INLINE VEC2* VEC2Lerp(VEC2* pOut, const VEC2* p1, const VEC2* p2, f32 t);
104 
105 /*
106 
107 
108 
109 
110 
111 
112 
113  */
114 NN_MATH_INLINE VEC2* VEC2Maximize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
115 
116 /*
117 
118 
119 
120 
121 
122 
123 
124  */
125 NN_MATH_INLINE VEC2* VEC2Minimize(VEC2* pOut, const VEC2* p1, const VEC2* p2);
126 
127 /*
128 
129 
130 
131 
132 
133 
134 
135  */
136 NN_MATH_INLINE VEC2* VEC2Mult(VEC2* pOut, const VEC2* p1, const VEC2* p2);
137 
138 /*
139 
140 
141 
142 
143 
144 
145  */
146 NN_MATH_INLINE VEC2* VEC2Normalize(VEC2* pOut, const VEC2* p);
147 
148 /*
149 
150 
151 
152 
153 
154 
155 
156 
157  */
158 NN_MATH_INLINE VEC2* VEC2SafeNormalize(VEC2* pOut, const VEC2* p, const VEC2& alt);
159 
160 /*
161 
162 
163 
164 
165 
166 
167 
168  */
169 NN_MATH_INLINE VEC2* VEC2Scale(VEC2* pOut, const VEC2* p, f32 scale);
170 
171 /*
172 
173 
174 
175 
176 
177 
178 
179  */
180 NN_MATH_INLINE VEC2* VEC2Sub(VEC2* pOut, const VEC2* p1, const VEC2* p2);
181 
182 /*
183 
184 */
185 
186 NN_MATH_INLINE VEC2* VEC2Transform(VEC2* pOut, const MTX23* pM, const VEC2* pV);
187 
188 /* =======================================================================
189         Class definitions
190    ======================================================================== */
191 /*
192 
193 
194  */
195 struct VEC2_
196 {
197     f32 x;  //
198     f32 y;  //
199 };
200 
201 #pragma diag_suppress 2530
202 /*
203 
204 
205    */
206 class VEC2 : public VEC2_
207 {
208 public:
209     static const int DIMENSION = 2; //
210 
211     //
Zero()212     static const VEC2& Zero()
213     {
214         static const VEC2 zero(0.0f, 0.0f);
215 
216         return zero;
217     }
218 
219     //
One()220     static const VEC2& One()
221     {
222         static const VEC2 one(1.0f, 1.0f);
223 
224         return one;
225     }
226 
227     typedef VEC2 self_type; //
228     typedef f32  value_type; //
229 public:
230 
231     //----------------------------------------
232     //
233     //
234 
235     //
VEC2()236     VEC2() {}
237     //
VEC2(const f32 * p)238     explicit VEC2(const f32* p) { x = p[0]; y = p[1]; }
239     //
VEC2(const VEC2_ & v)240     VEC2(const VEC2_& v) { x = v.x; y = v.y; }
241     //
VEC2(f32 fx,f32 fy)242     VEC2(f32 fx, f32 fy) { x = fx; y = fy; }
243 
244     //
245 
246     //----------------------------------------
247     //
248     //
249     //
250     operator f32*() { return &x; }
251 
252     //
253     operator const f32*() const { return &x; }
254     //
255 
256     //----------------------------------------
257     //
258     //
259 
260     //
261     self_type& operator += (const self_type& rhs) { x += rhs.x; y += rhs.y; return *this; }
262 
263     //
264     self_type& operator -= (const self_type& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
265 
266     //
267     self_type& operator *= (f32 f) { x *= f; y *= f; return *this; }
268 
269     //
270     self_type& operator *= (const self_type& rhs) { x *= rhs.x; y *= rhs.y; return *this; }
271 
272     //
273     self_type& operator /= (f32 f) { f32 r = 1.f / f; x *= r; y *= r; return *this; }
274 
275     //
276     self_type operator + () const { return *this; }
277 
278     //
279     self_type operator - () const { return self_type(-x, -y); }
280 
281     //
282     self_type operator + (const self_type& rhs) const { return self_type(x + rhs.x, y + rhs.y); }
283 
284     //
285     self_type operator - (const self_type& rhs) const { return self_type(x - rhs.x, y - rhs.y); }
286 
287     //
288     self_type operator * (f32 f) const { return self_type(f * x, f * y); }
289 
290     //
291     self_type operator / (f32 f) const { f32 r = 1.f / f; return self_type(r * x, r * y); }
292 
293     //
294     //
295     //
296     //
297     //
298     //
Lerp(const VEC2 & lhs,const VEC2 & rhs,f32 t)299     self_type& Lerp(const VEC2& lhs, const VEC2& rhs, f32 t)
300     {
301         return *VEC2Lerp(this, &lhs, &rhs, t);
302     }
303 
304     //
305     //
306     //
307     //
Dot(const VEC2 & vec)308     f32 Dot(const VEC2& vec) const
309     {
310         return VEC2Dot(this, &vec);
311     }
312 
313     //
LenSq()314     f32 LenSq() const { return x * x + y * y; }
315 
316     //
LengthSquare()317     f32 LengthSquare() const { return x * x + y * y; }
318 
319     //
Length()320     f32 Length() const { return FSqrt(this->x * this->x + this->y * this->y); }
321 
322     //
Normalize()323     self_type& Normalize()
324     {
325         return *VEC2Normalize(this, this);
326     }
327 
328     //
329     //
330     //
331     //
SafeNormalize(const VEC2 & alt)332     self_type& SafeNormalize(const VEC2& alt)
333     {
334         return *VEC2SafeNormalize(this, this, alt);
335     }
336 
337     //
338     //
339     //
DistanceSquare(const VEC2 & vec)340     f32 DistanceSquare(const VEC2& vec)
341     {
342         return VEC2DistSq(this, &vec);
343     }
344 
345     //
346     //
347     //
348     //
Maximize(const VEC2 & lhs,const VEC2 & rhs)349     self_type& Maximize(const VEC2& lhs, const VEC2& rhs)
350     {
351         return *VEC2Maximize(this, &lhs, &rhs);
352     }
353 
354     //
355     //
356     //
357     //
Minimize(const VEC2 & lhs,const VEC2 & rhs)358     self_type& Minimize(const VEC2& lhs, const VEC2& rhs)
359     {
360         return *VEC2Minimize(this, &lhs, &rhs);
361     }
362 
363     //
364 
365     //----------------------------------------
366     //
367     //
368 
369     //
Set(f32 fx,f32 fy)370     void Set(f32 fx, f32 fy) { x = fx; y = fy; }
371 
372     //
373 
374     //----------------------------------------
375     //
376     //
377 
378     //
379     bool operator == (const self_type& rhs) const { return x == rhs.x && y == rhs.y; }
380 
381     //
382     bool operator != (const self_type& rhs) const { return x != rhs.x || y != rhs.y; }
383 
384     //
IsZero()385     bool IsZero() const { return VEC2IsZero(this); }
386     //
387 
388     //
389     void Report(bool bNewline = true, const char* name = NULL) const;
390 
391 private:
392     typedef void (self_type::*UnspecifiedBoolType)() const;
393     operator UnspecifiedBoolType() const;
394     operator UnspecifiedBoolType();
395 };
396 #pragma diag_default 2530
397 
398 //
399 //
400 
401 //
402 typedef class VEC2 Vector2;
403 
404 //
405 
406 /* ------------------------------------------------------------------------
407     Function for VEC2
408    ------------------------------------------------------------------------ */
409 NN_MATH_INLINE VEC2*
VEC2Add(VEC2 * pOut,const VEC2 * p1,const VEC2 * p2)410 VEC2Add(VEC2* pOut, const VEC2* p1, const VEC2* p2)
411 {
412     NN_NULL_ASSERT( pOut );
413     NN_NULL_ASSERT( p1 );
414     NN_NULL_ASSERT( p2 );
415 
416     pOut->x = p1->x + p2->x; pOut->y = p1->y + p2->y;
417     return pOut;
418 }
419 
420 NN_MATH_INLINE VEC2*
VEC2Sub(VEC2 * pOut,const VEC2 * p1,const VEC2 * p2)421 VEC2Sub(VEC2* pOut, const VEC2* p1, const VEC2* p2)
422 {
423     NN_NULL_ASSERT( pOut );
424     NN_NULL_ASSERT( p1 );
425     NN_NULL_ASSERT( p2 );
426 
427     pOut->x = p1->x - p2->x; pOut->y = p1->y - p2->y;
428     return pOut;
429 }
430 
431 NN_MATH_INLINE VEC2*
VEC2Mult(VEC2 * pOut,const VEC2 * p1,const VEC2 * p2)432 VEC2Mult(VEC2* pOut, const VEC2* p1, const VEC2* p2)
433 {
434     pOut->x = p1->x * p2->x;
435     pOut->y = p1->y * p2->y;
436     return pOut;
437 }
438 
439 
440 NN_MATH_INLINE VEC2*
VEC2Scale(VEC2 * pOut,const VEC2 * p,f32 scale)441 VEC2Scale(VEC2* pOut, const VEC2* p, f32 scale)
442 {
443     NN_NULL_ASSERT( pOut );
444     NN_NULL_ASSERT( p );
445 
446     pOut->x = p->x * scale; pOut->y = p->y * scale;
447     return pOut;
448 }
449 
450 NN_MATH_INLINE VEC2*
VEC2Lerp(VEC2 * pOut,const VEC2 * p1,const VEC2 * p2,f32 t)451 VEC2Lerp(VEC2* pOut, const VEC2* p1, const VEC2* p2, f32 t)
452 {
453     // (1-t)*p1 + t*p2
454     pOut->x = p1->x + t * (p2->x - p1->x);
455     pOut->y = p1->y + t * (p2->y - p1->y);
456     return pOut;
457 }
458 
459 NN_MATH_INLINE f32
VEC2Dot(const VEC2 * p1,const VEC2 * p2)460 VEC2Dot(const VEC2* p1, const VEC2* p2)
461 {
462     NN_NULL_ASSERT( p1 );
463     NN_NULL_ASSERT( p2 );
464 
465     return p1->x * p2->x + p1->y * p2->y;
466 }
467 
468 NN_MATH_INLINE f32
VEC2LenSq(const VEC2 * p)469 VEC2LenSq(const VEC2* p)
470 {
471     NN_NULL_ASSERT( p );
472     return p->x * p->x + p->y * p->y;
473 }
474 
475 NN_MATH_INLINE f32
VEC2Len(const VEC2 * p)476 VEC2Len(const VEC2* p) { return FSqrt(p->x * p->x + p->y * p->y); }
477 
478 NN_MATH_INLINE f32
VEC2DistSq(const VEC2 * p1,const VEC2 * p2)479 VEC2DistSq(const VEC2* p1, const VEC2* p2) { VEC2 tmp; return VEC2LenSq(VEC2Sub(&tmp, p1, p2)); }
480 
481 NN_MATH_INLINE VEC2
482 operator * (f32 f, const VEC2& rhs) { return VEC2(f * rhs.x, f * rhs.y); }
483 
484 }  // namespace math
485 }  // namespace nn
486 
487 #if defined(NN_MATH_AS_INLINE)
488 #include <nn/math/inline/math_Vector2.ipp>
489 #endif
490 
491 namespace nn {
492 namespace math {
493 
494 //Overload referencing the -- const argument
VEC2IsZero(const VEC2 & v)495 inline bool VEC2IsZero(const VEC2& v){ return VEC2IsZero( &v ); }
VEC2Add(VEC2 * pOut,const VEC2 & p1,const VEC2 & p2)496 inline VEC2* VEC2Add(VEC2* pOut, const VEC2& p1, const VEC2& p2) { return VEC2Add(pOut, &p1, &p2); }
VEC2Sub(VEC2 * pOut,const VEC2 & v1,const VEC2 & v2)497 inline VEC2* VEC2Sub(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Sub(pOut, &v1, &v2); }
VEC2Mult(VEC2 * pOut,const VEC2 & v1,const VEC2 & v2)498 inline VEC2* VEC2Mult(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Mult(pOut, &v1, &v2); }
VEC2Scale(VEC2 * pOut,const VEC2 & v,f32 scale)499 inline VEC2* VEC2Scale(VEC2* pOut, const VEC2& v, f32 scale) { return VEC2Scale(pOut, &v, scale); }
VEC2Lerp(VEC2 * pOut,const VEC2 & v1,const VEC2 & v2,f32 t)500 inline VEC2* VEC2Lerp(VEC2* pOut, const VEC2& v1, const VEC2& v2, f32 t) { return VEC2Lerp(pOut, &v1, &v2, t); }
VEC2Dot(const VEC2 & v1,const VEC2 & v2)501 inline f32 VEC2Dot(const VEC2& v1, const VEC2& v2) { return VEC2Dot(&v1, &v2); }
VEC2LenSq(const VEC2 & v)502 inline f32 VEC2LenSq(const VEC2& v) { return VEC2LenSq( &v ); }
VEC2Len(const VEC2 & v)503 inline f32 VEC2Len(const VEC2& v) { return VEC2Len( &v ); }
VEC2DistSq(const VEC2 & v1,const VEC2 & v2)504 inline f32 VEC2DistSq(const VEC2& v1, const VEC2& v2) { return VEC2DistSq( &v1, &v2 ); }
505 
VEC2Maximize(VEC2 * pOut,const VEC2 & v1,const VEC2 & v2)506 inline VEC2* VEC2Maximize(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Maximize( pOut, &v1, &v2 ); }
VEC2Minimize(VEC2 * pOut,const VEC2 & v1,const VEC2 & v2)507 inline VEC2* VEC2Minimize(VEC2* pOut, const VEC2& v1, const VEC2& v2) { return VEC2Minimize( pOut, &v1, &v2 ); }
VEC2Normalize(VEC2 * pOut,const VEC2 & v)508 inline VEC2* VEC2Normalize(VEC2* pOut, const VEC2& v) { return VEC2Normalize( pOut, &v ); }
VEC2SafeNormalize(VEC2 * pOut,const VEC2 & v,const VEC2 & alt)509 inline VEC2* VEC2SafeNormalize(VEC2* pOut, const VEC2& v, const VEC2& alt) { return VEC2SafeNormalize( pOut, &v, alt ); }
510 
511 }  // namespace math
512 }  // namespace nn
513 
514 #pragma pop
515 
516 #endif
517