1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Vector4.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_VECTOR4_H_
17 #define NN_MATH_VECTOR4_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 VEC4;
29 
30 /* Please see man pages for details
31 
32 
33 */
34 
35 /* ------------------------------------------------------------------------
36     Function for VEC4
37    ------------------------------------------------------------------------ */
38 /*
39 
40 
41 
42 
43 
44 
45 
46  */
47 NN_MATH_INLINE VEC4* VEC4Add(VEC4* pOut, const VEC4* p1, const VEC4* p2);
48 
49 /*
50 
51 
52 
53 
54 
55 
56  */
57 NN_MATH_INLINE f32   VEC4DistSq(const VEC4* p1, const VEC4* p2);
58 
59 /*
60 
61 
62 
63 
64 
65 
66  */
67 NN_MATH_INLINE f32   VEC4Dot(const VEC4* p1, const VEC4* p2);
68 
69 /*
70 
71 
72 
73 
74 
75  */
76 NN_MATH_INLINE bool VEC4IsZero(const VEC4* p);
77 
78 /*
79 
80 
81 
82 
83 
84  */
85 NN_MATH_INLINE bool VEC4IsZeroWOne(const VEC4* p);
86 
87 /*
88 
89 
90 
91 
92 
93  */
94 NN_MATH_INLINE f32   VEC4Len(const VEC4* p);
95 
96 /*
97 
98 
99 
100 
101 
102  */
103 NN_MATH_INLINE f32   VEC4LenSq(const VEC4* p);
104 
105 /*
106 
107 
108 
109 
110 
111 
112 
113 
114  */
115 NN_MATH_INLINE VEC4* VEC4Lerp(VEC4* pOut, const VEC4* p1, const VEC4* p2, f32 t);
116 
117 /*
118 
119 
120 
121 
122 
123 
124 
125  */
126 NN_MATH_INLINE VEC4* VEC4Maximize(VEC4* pOut, const VEC4* p1, const VEC4* p2);
127 
128 /*
129 
130 
131 
132 
133 
134 
135 
136  */
137 NN_MATH_INLINE VEC4* VEC4Minimize(VEC4* pOut, const VEC4* p1, const VEC4* p2);
138 
139 /*
140 
141 
142 
143 
144 
145 
146 
147  */
148 NN_MATH_INLINE VEC4* VEC4Mult(VEC4* pOut, const VEC4* p1, const VEC4* p2);
149 
150 /*
151 
152 
153 
154 
155 
156 
157  */
158 NN_MATH_INLINE VEC4* VEC4Normalize(VEC4* pOut, const VEC4* p);
159 
160 /*
161 
162 
163 
164 
165 
166 
167 
168 
169  */
170 NN_MATH_INLINE VEC4* VEC4SafeNormalize(VEC4* pOut, const VEC4* p, const VEC4& alt);
171 
172 /*
173 
174 
175 
176 
177 
178 
179 
180  */
181 NN_MATH_INLINE VEC4* VEC4Scale(VEC4* pOut, const VEC4* p, f32 scale);
182 
183 /*
184 
185 
186 
187 
188 
189 
190 
191  */
192 NN_MATH_INLINE VEC4* VEC4Sub(VEC4* pOut, const VEC4* p1, const VEC4* p2);
193 
194 /*
195 
196 */
197 
198 /* =======================================================================
199         Class definitions
200    ======================================================================== */
201 /*
202 
203 
204  */
205 struct VEC4_
206 {
207     f32 x;  //
208     f32 y;  //
209     f32 z;  //
210     f32 w;  //
211 };
212 
213 
214 /*
215 
216 
217    */
218 class VEC4 : public VEC4_
219 {
220 public:
221     static const int DIMENSION = 4; //
222 
223     //
Zero()224     static const VEC4& Zero()
225     {
226         static const VEC4 zero(0.0f, 0.0f, 0.0f, 0.0f);
227 
228         return zero;
229     }
230 
231     //
ZeroWOne()232     static const VEC4& ZeroWOne()
233     {
234         static const VEC4 zero(0.0f, 0.0f, 0.0f, 1.0f);
235 
236         return zero;
237     }
238 
239     //
One()240     static const VEC4& One()
241     {
242         static const VEC4 one(1.0f, 1.0f, 1.0f, 1.0f);
243 
244         return one;
245     }
246 
247     typedef VEC4 self_type; //
248     typedef f32  value_type; //
249 public:
250 
251     //----------------------------------------
252     //
253     //
254 
255     //
VEC4()256     VEC4() {}
257     //
VEC4(const f32 * p)258     explicit VEC4(const f32* p) { x = p[0]; y = p[1]; z = p[2]; w = p[3]; }
259     //
VEC4(const VEC4_ & v)260     VEC4(const VEC4_& v) { x = v.x; y = v.y; z = v.z; w = v.w; }
261     //
VEC4(f32 fx,f32 fy,f32 fz,f32 fw)262     VEC4(f32 fx, f32 fy, f32 fz, f32 fw) { x = fx; y = fy; z = fz; w = fw; }
263     //
VEC4(const VEC3 & v)264     explicit VEC4(const VEC3& v) { x = v.x; y = v.y; z = v.z; w = 0.0f; }
265 
266     //
267 
268     //----------------------------------------
269     //
270     //
271 
272     //
273     operator f32*() { return &x; }
274 
275     //
276     operator const f32*() const { return &x; }
277 
278     //
279 
280     //----------------------------------------
281     //
282     //
283 
284     //
285     self_type& operator += (const self_type& rhs) { (void)VEC4Add(this, this, &rhs); return *this; }
286 
287     //
288     self_type& operator -= (const self_type& rhs) { (void)VEC4Sub(this, this, &rhs); return *this; }
289 
290     //
291     self_type& operator *= (f32 f) { (void)VEC4Scale(this, this, f); return *this; }
292 
293     //
294     self_type& operator *= (const self_type& rhs) { (void)VEC4Mult(this, this, &rhs); return *this; }
295 
296     //
297     self_type& operator /= (f32 f) { (void)VEC4Scale(this, this, 1/f); return *this; }
298 
299     //
300     self_type operator + () const { return *this; }
301 
302     //
303     self_type operator - () const { return self_type(-x, -y, -z, -w); }
304 
305     //
306     self_type operator + (const self_type& rhs) const { VEC4 tmp; (void)VEC4Add(&tmp, this, &rhs); return tmp; }
307 
308     //
309     self_type operator - (const self_type& rhs) const { VEC4 tmp; (void)VEC4Sub(&tmp, this, &rhs); return tmp; }
310 
311     //
312     self_type operator * (f32 f) const { VEC4 tmp; (void)VEC4Scale(&tmp, this, f); return tmp; }
313 
314     //
315     self_type operator / (f32 f) const { f32 r = 1.f / f; return operator*(r); }
316 
317     //
318     //
319     //
320     //
321     //
322     //
Lerp(const VEC4 & lhs,const VEC4 & rhs,f32 t)323     self_type& Lerp(const VEC4& lhs, const VEC4& rhs, f32 t)
324     {
325         return *VEC4Lerp(this, &lhs, &rhs, t);
326     }
327 
328     //
329     //
330     //
331     //
Dot(const VEC4 & vec)332     f32 Dot(const VEC4& vec) const
333     {
334         return VEC4Dot(this, &vec);
335     }
336 
337     //
338     //
339     //
LenSq()340     f32 LenSq() const { return VEC4LenSq(this); }
341 
342     //
LengthSquare()343     f32 LengthSquare() const { return VEC4LenSq(this); }
344 
345     //
Length()346     f32 Length() const { return VEC4Len(this); }
347 
348     //
Normalize()349     self_type& Normalize()
350     {
351         return *VEC4Normalize(this, this);
352     }
353 
354     //
355     //
356     //
357     //
SafeNormalize(const VEC4 & alt)358     self_type& SafeNormalize(const VEC4& alt)
359     {
360         return *VEC4SafeNormalize(this, this, alt);
361     }
362 
363     //
364     //
365     //
DistanceSquare(const VEC4 & vec)366     f32 DistanceSquare(const VEC4& vec)
367     {
368         return VEC4DistSq(this, &vec);
369     }
370 
371     //
372     //
373     //
374     //
Maximize(const VEC4 & lhs,const VEC4 & rhs)375     self_type& Maximize(const VEC4& lhs, const VEC4& rhs)
376     {
377         return *VEC4Maximize(this, &lhs, &rhs);
378     }
379 
380     //
381     //
382     //
383     //
Minimize(const VEC4 & lhs,const VEC4 & rhs)384     self_type& Minimize(const VEC4& lhs, const VEC4& rhs)
385     {
386         return *VEC4Minimize(this, &lhs, &rhs);
387     }
388 
389     //
390 
391     //----------------------------------------
392     //
393     //
394 
395     //
Set(f32 fx,f32 fy,f32 fz,f32 fw)396     void Set(f32 fx, f32 fy, f32 fz, f32 fw) { x = fx; y = fy; z = fz; w = fw; }
397 
398     //
399 
400     //----------------------------------------
401     //
402     //
403 
404     //
405     bool operator == (const self_type& rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; }
406 
407     //
408     bool operator != (const self_type& rhs) const { return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w; }
409 
410     //
IsZero()411     bool IsZero() const { return VEC4IsZero(this); }
412 
413     //
IsZeroWOne()414     bool IsZeroWOne() const { return VEC4IsZeroWOne(this); }
415     //
416 
417     //
418     void Report(bool bNewline = true, const char* name = NULL) const;
419 
420 private:
421     typedef void (self_type::*UnspecifiedBoolType)() const;
422     operator UnspecifiedBoolType() const;
423     operator UnspecifiedBoolType();
424 };
425 
426 //
427 //
428 
429 //
430 typedef class VEC4 Vector4;
431 
432 //
433 
434 inline VEC4
435 operator * (f32 f, const VEC4& rhs) { VEC4 tmp; (void)VEC4Scale(&tmp, &rhs, f); return tmp; }
436 
437 
438 }  // namespace math
439 }  // namespace nn
440 
441 
442 #if defined(NN_MATH_AS_INLINE)
443 #include <nn/math/inline/math_Vector4.ipp>
444 #endif
445 
446 namespace nn {
447 namespace math {
448 
449 //Overload referencing the -- const argument
VEC4IsZero(const VEC4 & v)450 inline bool VEC4IsZero(const VEC4& v){ return VEC4IsZero( &v ); }
VEC4IsZeroWOne(const VEC4 & v)451 inline bool VEC4IsZeroWOne(const VEC4& v){ return VEC4IsZeroWOne( &v ); }
VEC4Add(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)452 inline VEC4* VEC4Add(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Add( pOut, &v1, &v2 ); }
VEC4Sub(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)453 inline VEC4* VEC4Sub(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Sub( pOut, &v1, &v2 ); }
VEC4Mult(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)454 inline VEC4* VEC4Mult(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Mult( pOut, &v1, &v2 ); }
VEC4Scale(VEC4 * pOut,const VEC4 & v,f32 scale)455 inline VEC4* VEC4Scale(VEC4* pOut, const VEC4& v, f32 scale) { return VEC4Scale( pOut, &v, scale); }
VEC4Lerp(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2,f32 t)456 inline VEC4* VEC4Lerp(VEC4* pOut, const VEC4& v1, const VEC4& v2, f32 t) { return VEC4Lerp( pOut, &v1, &v2, t ); }
VEC4Dot(const VEC4 & v1,const VEC4 & v2)457 inline f32   VEC4Dot(const VEC4& v1, const VEC4& v2) { return VEC4Dot( &v1, &v2 ); }
VEC4LenSq(const VEC4 & v)458 inline f32   VEC4LenSq(const VEC4& v) { return VEC4LenSq( &v ); }
VEC4Len(const VEC4 & v)459 inline f32   VEC4Len(const VEC4& v) { return VEC4Len( &v ); }
VEC4Normalize(VEC4 * pOut,const VEC4 & v)460 inline VEC4* VEC4Normalize(VEC4* pOut, const VEC4& v) { return VEC4Normalize( pOut, &v ); }
VEC4SafeNormalize(VEC4 * pOut,const VEC4 & v,const VEC4 & alt)461 inline VEC4* VEC4SafeNormalize(VEC4* pOut, const VEC4& v, const VEC4& alt) { return VEC4SafeNormalize( pOut, &v, alt ); }
VEC4DistSq(const VEC4 & v1,const VEC4 & v2)462 inline f32   VEC4DistSq(const VEC4& v1, const VEC4& v2) { return VEC4DistSq( &v1, &v2 ); }
VEC4Maximize(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)463 inline VEC4* VEC4Maximize(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Maximize( pOut, &v1, &v2 ); }
VEC4Minimize(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)464 inline VEC4* VEC4Minimize(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Minimize( pOut, &v1, &v2 ); }
465 
466 
467 }  // namespace math
468 }  // namespace nn
469 
470 #pragma pop
471 
472 #endif
473