1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Matrix33.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: 47340 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_MATH_MATRIX33_H_
17 #define NN_MATH_MATRIX33_H_
18 
19 #include <cstring>
20 #include <nn/math/math_Config.h>
21 #include <nn/math/math_Vector3.h>
22 #include <nn/math/ARMv6/math_Matrix33.h>
23 
24 #pragma push
25 #pragma Otime
26 
27 namespace nn {
28 namespace math {
29 
30 struct MTX33;
31 struct MTX34;
32 
33 /* Please see man pages for details
34 
35 
36 */
37 /* ------------------------------------------------------------------------
38     Function for MTX33
39    ------------------------------------------------------------------------ */
40 /*
41 
42 
43 
44 
45 
46 
47  */
48 NN_FORCE_INLINE MTX33* MTX33Copy(MTX33* pOut, const MTX33* p);
49 
50 /*
51 
52 
53 
54 
55 
56  */
57 NN_MATH_INLINE MTX33* MTX33Identity(MTX33* pOut);
58 
59 /*
60 
61 
62 
63 
64 
65  */
66 NN_MATH_INLINE bool   MTX33IsIdentity(const MTX33* p);
67 
68 /*
69 
70 
71 
72 
73 
74 
75 
76 
77  */
78 NN_FORCE_INLINE MTX33* MTX33MAdd(MTX33* pOut, f32 t, const MTX33* p1, const MTX33* p2);
79 
80 /*
81 
82 
83 
84 
85 
86 
87 
88  */
89 template<typename TMatrix>
90 NN_FORCE_INLINE TMatrix* MTX33Mult(TMatrix* pOut, const TMatrix* p1, const TMatrix* p2);
91 
92 /*
93 
94 
95 
96 
97 
98  */
99 NN_MATH_INLINE MTX33* MTX33Zero(MTX33* pOut);
100 
101 /*
102 
103 */
104 
105 NN_MATH_INLINE MTX34* MTX33ToMTX34(MTX34* pOut, const MTX33* pM);
106 NN_MATH_INLINE MTX33* MTX34ToMTX33(MTX33* pOut, const MTX34* pM);
107 
108 NN_FORCE_INLINE VEC3* VEC3Transform(VEC3* pOut, const MTX33* pM, const VEC3* pV);
109 
110 /* =======================================================================
111         Class definitions
112    ======================================================================== */
113 /*
114 
115 
116  */
117 struct MTX33_
118 {
119     //
120     struct BaseData
121     {
122         f32 _00;    //
123         f32 _01;    //
124         f32 _02;    //
125         f32 _10;    //
126         f32 _11;    //
127         f32 _12;    //
128         f32 _20;    //
129         f32 _21;    //
130         f32 _22;    //
131     };
132 
133     union
134     {
135     //----------------------------------------
136     //
137     //
138     #if defined(NN_MATH_USE_ANONYMOUS_STRUCT)
139         //
140         struct
141         {
142             f32 _00, _01, _02;
143             f32 _10, _11, _12;
144             f32 _20, _21, _22;
145         };
146     #endif
147         BaseData f;     //
148         f32 m[3][3];    //
149         f32 a[9];       //
150         VEC3_ v[3];     //
151     //
152     };
153 };
154 
155 /*
156 
157 
158  */
159 // Exists mainly for the normal matrix.
160 // For now, only those things that are definitely necessary are implemented.
161 class MTX33 : public MTX33_
162 {
163 public:
164     static const int ROW_COUNT = 3; //
165     static const int COLUMN_COUNT = 3; //
166 
167     //
Identity()168     static const MTX33& Identity()
169     {
170         static const MTX33 identity(
171             1.0f, 0.0f, 0.0f,
172             0.0f, 1.0f, 0.0f,
173             0.0f, 0.0f, 1.0f);
174 
175         return identity;
176     }
177 
178     typedef MTX33 self_type; //
179     typedef f32   value_type; //
180 public:
181     //----------------------------------------
182     //
183     //
184 
185     //
MTX33()186     MTX33() {}
187     //
MTX33(const f32 * p)188     explicit MTX33(const f32* p) { MTX33Copy(this, reinterpret_cast<const MTX33*>(p)); }
189     //
MTX33(const MTX34 & rhs)190     explicit MTX33(const MTX34& rhs) { MTX34ToMTX33(this, &rhs); }
191     //
MTX33(f32 x00,f32 x01,f32 x02,f32 x10,f32 x11,f32 x12,f32 x20,f32 x21,f32 x22)192     MTX33(f32 x00, f32 x01, f32 x02,
193           f32 x10, f32 x11, f32 x12,
194           f32 x20, f32 x21, f32 x22)
195     {
196         f._00 = x00; f._01 = x01; f._02 = x02;
197         f._10 = x10; f._11 = x11; f._12 = x12;
198         f._20 = x20; f._21 = x21; f._22 = x22;
199     }
200     //
201 
202     //----------------------------------------
203     //
204     //
205 
206     //
207     operator f32*() { return this->a; }
208     //
209     operator const f32*() const { return this->a; }
210 
211     //
GetRow(int index)212     VEC3& GetRow(int index)
213     {
214         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
215         return *reinterpret_cast<VEC3*>(&this->v[index]);
216     }
217 
218     //
GetRow(int index)219     const VEC3& GetRow(int index) const
220     {
221         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
222         return *reinterpret_cast<const VEC3*>(&this->v[index]);
223     }
224 
225     //
GetColumn(int index)226     VEC3 GetColumn(int index) const
227     {
228         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
229         VEC3 column;
230         column.x = this->m[0][index];
231         column.y = this->m[1][index];
232         column.z = this->m[2][index];
233         return column;
234     }
235 
236     //
SetColumn(int index,const VEC3 & column)237     void SetColumn(int index, const VEC3& column)
238     {
239         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
240         this->m[0][index] = column.x;
241         this->m[1][index] = column.y;
242         this->m[2][index] = column.z;
243     }
244     //
245 
246     //----------------------------------------
247     //
248     //
249 
250     //
SetupIdentity()251     self_type& SetupIdentity() { return *MTX33Identity(this); }
252 
253     //
254 
255     //----------------------------------------
256     //
257     //
258 
259     //
260     bool operator == (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX33)) == 0; }
261 
262     //
263     bool operator != (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX33)) != 0; }
264 
265     //
IsIdentity()266     bool IsIdentity() const { return MTX33IsIdentity(this); }
267 
268     //
269 
270     //
271     void Report(bool bNewline = true, const char* name = NULL) const;
272 
273 private:
274     typedef void (self_type::*UnspecifiedBoolType)() const;
275     operator UnspecifiedBoolType() const;
276     operator UnspecifiedBoolType();
277 };
278 
279 //
280 //
281 
282 //
283 typedef class MTX33 Matrix33;
284 
285 //
286 
287 }  // namespace math
288 }  // namespace nn
289 
290 
291 namespace nn {
292 namespace math {
293 
294 template<typename TMatrix>
295 NN_FORCE_INLINE TMatrix*
MTX33Mult(TMatrix * pOut,const TMatrix * p1,const TMatrix * p2)296 MTX33Mult(TMatrix* pOut, const TMatrix* p1, const TMatrix* p2)
297 {
298 #if defined( NN_HARDWARE_CTR )
299     #if (MTX33MULT_CONFIG == D_ORG)
300         return ARMv6::MTX33MultC( pOut, p1, p2 );
301     #elif (MTX33MULT_CONFIG == D_FAST_C)
302     #elif (MTX33MULT_CONFIG == D_FAST_ASM)
303         return ARMv6::MTX33MultAsm( pOut, p1, p2 );
304     #elif (MTX33MULT_CONFIG == D_FAST_C_ALGO)
305     #elif (MTX33MULT_CONFIG == D_FAST_ASM_ALGO)
306     #endif
307 #else
308 #endif
309 }
310 
311 NN_FORCE_INLINE MTX33*
MTX33MAdd(MTX33 * pOut,f32 t,const MTX33 * p1,const MTX33 * p2)312 MTX33MAdd(MTX33* pOut, f32 t, const MTX33* p1, const MTX33* p2)
313 {
314 #if defined( NN_HARDWARE_CTR )
315     #if (MTX33MADD_CONFIG == D_ORG)
316         return ARMv6::MTX33MAddC(pOut, t, p1, p2);
317     #elif (MTX33MADD_CONFIG == D_FAST_C)
318         return ARMv6::MTX33MAddC_FAST(pOut, t, p1, p2);
319     #elif (MTX33MADD_CONFIG == D_FAST_ASM)
320         return ARMv6::MTX33MAddAsm(pOut, t, p1, p2);
321     #elif (MTX33MADD_CONFIG == D_FAST_C_ALGO)
322     #elif (MTX33MADD_CONFIG == D_FAST_ASM_ALGO)
323     #endif
324 #else
325 #endif // #if defined( NN_HARDWARE_CTR )
326 }
327 
328 NN_FORCE_INLINE MTX33*
MTX33Copy(MTX33 * pOut,const MTX33 * p)329 MTX33Copy(MTX33* pOut, const MTX33* p)
330 {
331 #if defined( NN_HARDWARE_CTR )
332     #if (MTX33COPY_CONFIG == D_ORG)
333         return ARMv6::MTX33CopyC(pOut, p);
334     #elif (MTX33COPY_CONFIG == D_FAST_C)
335     #elif (MTX33COPY_CONFIG == D_FAST_ASM)
336         return ARMv6::MTX33CopyAsm(pOut, p);
337     #elif (MTX33COPY_CONFIG == D_FAST_C_ALGO)
338     #elif (MTX33COPY_CONFIG == D_FAST_ASM_ALGO)
339     #endif
340 #else
341 #endif // #if defined( NN_HARDWARE_CTR )
342 }
343 
344 NN_FORCE_INLINE VEC3*
VEC3Transform(VEC3 * pOut,const MTX33 * pM,const VEC3 * pV)345 VEC3Transform(VEC3* pOut, const MTX33* pM, const VEC3* pV)
346 {
347 #if defined( NN_HARDWARE_CTR )
348     #if (VEC3TRANSFORM_33XVEC3_CONFIG == D_ORG)
349         return ARMv6::VEC3TransformC(pOut, pM, pV);
350     #elif (VEC3TRANSFORM_33XVEC3_CONFIG == D_FAST_C)
351     #elif (VEC3TRANSFORM_33XVEC3_CONFIG == D_FAST_ASM)
352         return ARMv6::VEC3TransformAsm(pOut, pM, pV);
353     #elif (VEC3TRANSFORM_33XVEC3_CONFIG == D_FAST_C_ALGO)
354     #elif (VEC3TRANSFORM_33XVEC3_CONFIG == D_FAST_ASM_ALGO)
355     #endif
356 #else
357 #endif // #if defined( NN_HARDWARE_CTR )
358 }
359 
360 //Overload referencing the -- const argument
361 template<typename TMatrix>
MTX33Mult(TMatrix * pOut,const TMatrix & m1,const TMatrix & m2)362 inline TMatrix* MTX33Mult(TMatrix* pOut, const TMatrix& m1, const TMatrix& m2) { return MTX33Mult( pOut, &m1, &m2 ); }
MTX33IsIdentity(const MTX33 & m)363 inline bool   MTX33IsIdentity(const MTX33& m) { return MTX33IsIdentity( &m ); }
MTX33Copy(MTX33 * pOut,const MTX33 & m)364 inline MTX33* MTX33Copy(MTX33* pOut, const MTX33& m) { return MTX33Copy( pOut, &m ); }
MTX33MAdd(MTX33 * pOut,f32 t,const MTX33 & m1,const MTX33 & m2)365 inline MTX33* MTX33MAdd(MTX33* pOut, f32 t, const MTX33& m1, const MTX33& m2) { return MTX33MAdd( pOut, t, &m1, &m2 ); }
366 
367 }  // namespace math
368 }  // namespace nn
369 
370 
371 #if defined(NN_MATH_AS_INLINE)
372 #include <nn/math/inline/math_Matrix33.ipp>
373 #include <nn/math/ARMv6/inline/math_Matrix33.ipp>
374 #endif
375 
376 #pragma pop
377 
378 #endif
379