1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: math_Vector4.h
4
5 Copyright (C)2009-2010 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 $Revision: 34617 $
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 /* ------------------------------------------------------------------------
31 Function for VEC4
32 ------------------------------------------------------------------------ */
33 NN_MATH_INLINE bool VEC4IsZero(const VEC4* p);
34 NN_MATH_INLINE bool VEC4IsZeroWOne(const VEC4* p);
35 NN_MATH_INLINE VEC4* VEC4Add(VEC4* pOut, const VEC4* p1, const VEC4* p2);
36 NN_MATH_INLINE VEC4* VEC4Sub(VEC4* pOut, const VEC4* p1, const VEC4* p2);
37 NN_MATH_INLINE VEC4* VEC4Mult(VEC4* pOut, const VEC4* p1, const VEC4* p2);
38 NN_MATH_INLINE VEC4* VEC4Scale(VEC4* pOut, const VEC4* p, f32 scale);
39 NN_MATH_INLINE VEC4* VEC4Lerp(VEC4* pOut, const VEC4* p1, const VEC4* p2, f32 t);
40 NN_MATH_INLINE f32 VEC4Dot(const VEC4* p1, const VEC4* p2);
41 NN_MATH_INLINE f32 VEC4LenSq(const VEC4* p);
42 NN_MATH_INLINE f32 VEC4Len(const VEC4* p);
43 NN_MATH_INLINE VEC4* VEC4Normalize(VEC4* pOut, const VEC4* p);
44 NN_MATH_INLINE VEC4* VEC4SafeNormalize(VEC4* pOut, const VEC4* p, const VEC4& alt);
45 NN_MATH_INLINE f32 VEC4DistSq(const VEC4* p1, const VEC4* p2);
46 NN_MATH_INLINE VEC4* VEC4Maximize(VEC4* pOut, const VEC4* p1, const VEC4* p2);
47 NN_MATH_INLINE VEC4* VEC4Minimize(VEC4* pOut, const VEC4* p1, const VEC4* p2);
48
49 /* =======================================================================
50 Class definitions
51 ======================================================================== */
52 struct VEC4_
53 {
54 f32 x;
55 f32 y;
56 f32 z;
57 f32 w;
58 };
59
60
61 /*
62
63 */
64 struct VEC4 : public VEC4_
65 {
66 public:
67 static const int DIMENSION = 4; //
68
69 //
ZeroVEC470 static const VEC4& Zero()
71 {
72 static const VEC4 zero(0.0f, 0.0f, 0.0f, 0.0f);
73
74 return zero;
75 }
76
77 //
ZeroWOneVEC478 static const VEC4& ZeroWOne()
79 {
80 static const VEC4 zero(0.0f, 0.0f, 0.0f, 1.0f);
81
82 return zero;
83 }
84
85 //
OneVEC486 static const VEC4& One()
87 {
88 static const VEC4 one(1.0f, 1.0f, 1.0f, 1.0f);
89
90 return one;
91 }
92
93 typedef VEC4 self_type; //
94 typedef f32 value_type; //
95 public:
96
97 //----------------------------------------
98 //
99 //
100
101 //
VEC4VEC4102 VEC4() {}
103 //
VEC4VEC4104 explicit VEC4(const f32* p) { x = p[0]; y = p[1]; z = p[2]; w = p[3]; }
105 //
VEC4VEC4106 VEC4(const VEC4_& v) { x = v.x; y = v.y; z = v.z; w = v.w; }
107 //
VEC4VEC4108 VEC4(f32 fx, f32 fy, f32 fz, f32 fw) { x = fx; y = fy; z = fz; w = fw; }
109 //
VEC4VEC4110 explicit VEC4(const VEC3& v) { x = v.x; y = v.y; z = v.z; w = 0.0f; }
111
112 //
113
114 //----------------------------------------
115 //
116 //
117
118 //
119 operator f32*() { return &x; }
120
121 //
122 operator const f32*() const { return &x; }
123
124 //
125
126 //----------------------------------------
127 //
128 //
129 self_type& operator += (const self_type& rhs) { (void)VEC4Add(this, this, &rhs); return *this; }
130 self_type& operator -= (const self_type& rhs) { (void)VEC4Sub(this, this, &rhs); return *this; }
131 self_type& operator *= (const self_type& rhs) { (void)VEC4Mult(this, this, &rhs); return *this; }
132 self_type& operator *= (f32 f) { (void)VEC4Scale(this, this, f); return *this; }
133 self_type& operator /= (f32 f) { (void)VEC4Scale(this, this, 1/f); return *this; }
134
135 self_type operator + () const { return *this; }
136 self_type operator - () const { return self_type(-x, -y, -z, -w); }
137
138 self_type operator + (const self_type& rhs) const { VEC4 tmp; (void)VEC4Add(&tmp, this, &rhs); return tmp; }
139 self_type operator - (const self_type& rhs) const { VEC4 tmp; (void)VEC4Sub(&tmp, this, &rhs); return tmp; }
140 self_type operator * (f32 f) const { VEC4 tmp; (void)VEC4Scale(&tmp, this, f); return tmp; }
141 self_type operator / (f32 f) const { f32 r = 1.f / f; return operator*(r); }
142
143 //
144 //
145 //
146 //
147 //
148 //
LerpVEC4149 self_type& Lerp(const VEC4& lhs, const VEC4& rhs, f32 t)
150 {
151 return *VEC4Lerp(this, &lhs, &rhs, t);
152 }
153
154 //
155 //
156 //
157 //
DotVEC4158 f32 Dot(const VEC4& vec) const
159 {
160 return VEC4Dot(this, &vec);
161 }
162
163 //
164 //
165 //
LenSqVEC4166 f32 LenSq() const { return VEC4LenSq(this); }
167
168 //
LengthSquareVEC4169 f32 LengthSquare() const { return VEC4LenSq(this); }
170
171 //
LengthVEC4172 f32 Length() const { return VEC4Len(this); }
173
174 //
NormalizeVEC4175 self_type& Normalize()
176 {
177 return *VEC4Normalize(this, this);
178 }
179
180 //
181 //
182 //
183 //
SafeNormalizeVEC4184 self_type& SafeNormalize(const VEC4& alt)
185 {
186 return *VEC4SafeNormalize(this, this, alt);
187 }
188
189 //
190 //
191 //
DistanceSquareVEC4192 f32 DistanceSquare(const VEC4& vec)
193 {
194 return VEC4DistSq(this, &vec);
195 }
196
197 //
198 //
199 //
200 //
MaximizeVEC4201 self_type& Maximize(const VEC4& lhs, const VEC4& rhs)
202 {
203 return *VEC4Maximize(this, &lhs, &rhs);
204 }
205
206 //
207 //
208 //
209 //
MinimizeVEC4210 self_type& Minimize(const VEC4& lhs, const VEC4& rhs)
211 {
212 return *VEC4Minimize(this, &lhs, &rhs);
213 }
214
215 //
216
217 //----------------------------------------
218 //
219 //
220
221 //
SetVEC4222 void Set(f32 fx, f32 fy, f32 fz, f32 fw) { x = fx; y = fy; z = fz; w = fw; }
223
224 //
225
226 //----------------------------------------
227 //
228 //
229
230 //
231 bool operator == (const self_type& rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; }
232
233 //
234 bool operator != (const self_type& rhs) const { return x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w; }
235
236 //
IsZeroVEC4237 bool IsZero() const { return VEC4IsZero(this); }
238
239 //
IsZeroWOneVEC4240 bool IsZeroWOne() const { return VEC4IsZeroWOne(this); }
241 //
242
243 //
244 void Report(bool bNewline = true, const char* name = NULL) const;
245 };
246
247 typedef struct VEC4 Vector4;
248
249 inline VEC4
250 operator * (f32 f, const VEC4& rhs) { VEC4 tmp; (void)VEC4Scale(&tmp, &rhs, f); return tmp; }
251
252
253 } // namespace math
254 } // namespace nn
255
256
257 #if defined(NN_MATH_AS_INLINE)
258 #include <nn/math/inline/math_Vector4.ipp>
259 #endif
260
261 namespace nn {
262 namespace math {
263
264 //-- Overloads that reference const type arguments
VEC4IsZero(const VEC4 & v)265 inline bool VEC4IsZero(const VEC4& v){ return VEC4IsZero( &v ); }
VEC4IsZeroWOne(const VEC4 & v)266 inline bool VEC4IsZeroWOne(const VEC4& v){ return VEC4IsZeroWOne( &v ); }
VEC4Add(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)267 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)268 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)269 inline VEC4* VEC4Mult(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Mult( pOut, &v1, &v2 ); }
VEC4Scale(VEC4 * pOut,const VEC4 & v,f32 scale)270 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)271 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)272 inline f32 VEC4Dot(const VEC4& v1, const VEC4& v2) { return VEC4Dot( &v1, &v2 ); }
VEC4LenSq(const VEC4 & v)273 inline f32 VEC4LenSq(const VEC4& v) { return VEC4LenSq( &v ); }
VEC4Len(const VEC4 & v)274 inline f32 VEC4Len(const VEC4& v) { return VEC4Len( &v ); }
VEC4Normalize(VEC4 * pOut,const VEC4 & v)275 inline VEC4* VEC4Normalize(VEC4* pOut, const VEC4& v) { return VEC4Normalize( pOut, &v ); }
VEC4SafeNormalize(VEC4 * pOut,const VEC4 & v,const VEC4 & alt)276 inline VEC4* VEC4SafeNormalize(VEC4* pOut, const VEC4& v, const VEC4& alt) { return VEC4SafeNormalize( pOut, &v, alt ); }
VEC4DistSq(const VEC4 & v1,const VEC4 & v2)277 inline f32 VEC4DistSq(const VEC4& v1, const VEC4& v2) { return VEC4DistSq( &v1, &v2 ); }
VEC4Maximize(VEC4 * pOut,const VEC4 & v1,const VEC4 & v2)278 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)279 inline VEC4* VEC4Minimize(VEC4* pOut, const VEC4& v1, const VEC4& v2) { return VEC4Minimize( pOut, &v1, &v2 ); }
280
281
282 } // namespace math
283 } // namespace nn
284
285 #pragma pop
286
287 #endif
288