1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Matrix23.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_MATRIX23_H_
17 #define NN_MATH_MATRIX23_H_
18 
19 #include <cstring>
20 #include <nn/math/math_Config.h>
21 #include <nn/math/math_Vector2.h>
22 #include <nn/math/math_Vector3.h>
23 
24 #pragma push
25 #pragma Otime
26 
27 namespace nn {
28 namespace math {
29 
30 struct MTX23;
31 struct MTX22;
32 
33 /* Please see man pages for details
34 
35 
36 */
37 
38 /* ------------------------------------------------------------------------
39     Function for MTX23
40    ------------------------------------------------------------------------ */
41 /*
42 
43 
44 
45 
46 
47 
48 
49  */
50 NN_MATH_INLINE MTX23* MTX23Add(MTX23* pOut, const MTX23* p1, const MTX23* p2);
51 
52 /*
53 
54 
55 
56 
57 
58 
59  */
60 NN_MATH_INLINE MTX23* MTX23Copy(MTX23* pOut, const MTX23* p);
61 
62 /*
63 
64 
65 
66 
67 
68  */
69 NN_MATH_INLINE MTX23* MTX23Identity(MTX23* pOut);
70 
71 /*
72 
73 
74 
75 
76 
77  */
78 NN_MATH_INLINE bool   MTX23IsIdentity(const MTX23* p);
79 
80 /*
81 
82 
83 
84 
85 
86 
87 
88 
89  */
90 NN_MATH_INLINE MTX23* MTX23MAdd(MTX23* pOut, f32 t, const MTX23* p1, const MTX23* p2);
91 
92 /*
93 
94 
95 
96 
97 
98 
99 
100  */
101 NN_MATH_INLINE MTX23* MTX23Mult(MTX23* pOut, const MTX23* p, f32 f);
102 
103 /*
104 
105 
106 
107 
108 
109 
110 
111  */
112 NN_MATH_INLINE MTX23* MTX23Mult(MTX23* pOut, const MTX23* p1, const MTX23* p2);
113 
114 /*
115 
116 
117 
118 
119 
120 
121 
122  */
123 NN_MATH_INLINE MTX23* MTX23MultTranslate(MTX23* pOut, const MTX23* pM, const VEC2* pT);
124 
125 /*
126 
127 
128 
129 
130 
131 
132 
133  */
134 NN_MATH_INLINE MTX23* MTX23MultTranslate(MTX23* pOut, const VEC2* pT, const MTX23* pM);
135 
136 /*
137 
138 
139 
140 
141 
142 
143 
144  */
145 NN_MATH_INLINE MTX23* MTX23RotCenterFIdx(MTX23* pOut, const VEC2* pCenter, f32 fIdx);
146 
147 /*
148 
149 
150 
151 
152 
153 
154  */
155 NN_MATH_INLINE MTX23* MTX23RotFIdx(MTX23* pOut, f32 fIdx);
156 
157 /*
158 
159 
160 
161 
162 
163 
164 
165  */
166 NN_MATH_INLINE MTX23* MTX23Scale(MTX23* pOut, const MTX23* pM, const VEC2* pS);
167 
168 /*
169 
170 
171 
172 
173 
174 
175 
176  */
177 NN_MATH_INLINE MTX23* MTX23Sub(MTX23* pOut, const MTX23* p1, const MTX23* p2);
178 
179 /*
180 
181 
182 
183 
184 
185 
186  */
187 NN_MATH_INLINE MTX23* MTX23Translate(MTX23* pOut, const VEC2* pT);
188 
189 /*
190 
191 
192 
193 
194 
195  */
196 NN_MATH_INLINE MTX23* MTX23Zero(MTX23* pOut);
197 
198 /*
199 
200 */
201 
202 NN_MATH_INLINE MTX23* MTX22ToMTX23(MTX23* pOut, const MTX22* pM);
203 
204 /*
205 
206 
207  */
208 struct MTX23_
209 {
210     //
211     struct BaseData
212     {
213         f32 _00;    //
214         f32 _01;    //
215         f32 _02;    //
216         f32 _10;    //
217         f32 _11;    //
218         f32 _12;    //
219     };
220 
221     union
222     {
223     //----------------------------------------
224     //
225     //
226     #if defined(NN_MATH_USE_ANONYMOUS_STRUCT)
227         //
228         struct
229         {
230             f32 _00, _01, _02;
231             f32 _10, _11, _12;
232         };
233     #endif
234         BaseData f;     //
235         f32 m[2][3];    //
236         f32 a[6];       //
237         VEC3_ v[2];     //
238     //
239     };
240 };
241 
242 /*
243 
244 
245  */
246 class MTX23 : public MTX23_
247 {
248 public:
249     static const int ROW_COUNT = 2; //
250     static const int COLUMN_COUNT = 3; //
251 
252     //
Identity()253     static const MTX23& Identity()
254     {
255         static const MTX23 identity(
256             1.0f, 0.0f, 0.0f,
257             0.0f, 1.0f, 0.0f);
258 
259         return identity;
260     }
261 
262     typedef MTX23 self_type; //
263     typedef f32   value_type; //
264 public:
265     //----------------------------------------
266     //
267     //
268 
269     //
MTX23()270     MTX23() {}
271     //
MTX23(const f32 * p)272     explicit MTX23(const f32* p) { (void)MTX23Copy(this, reinterpret_cast<const MTX23*>(p)); }
273     //
MTX23(const MTX22 & rhs)274     explicit MTX23(const MTX22& rhs) { MTX22ToMTX23(this, &rhs); }
275     //
MTX23(f32 x00,f32 x01,f32 x02,f32 x10,f32 x11,f32 x12)276     MTX23(f32 x00, f32 x01, f32 x02,
277           f32 x10, f32 x11, f32 x12)
278     {
279         f._00 = x00; f._01 = x01; f._02 = x02;
280         f._10 = x10; f._11 = x11; f._12 = x12;
281     }
282 
283     //
284 
285     //----------------------------------------
286     //
287     //
288 
289     //
290     operator f32*() { return this->a; }
291 
292     //
293     operator const f32*() const { return this->a; }
294 
295     //
GetRow(int index)296     VEC3& GetRow(int index)
297     {
298         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
299         return *reinterpret_cast<VEC3*>(&this->v[index]);
300     }
301 
302     //
GetRow(int index)303     const VEC3& GetRow(int index) const
304     {
305         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
306         return *reinterpret_cast<const VEC3*>(&this->v[index]);
307     }
308 
309     //
GetColumn(int index)310     VEC2 GetColumn(int index) const
311     {
312         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
313         VEC2 column;
314         column.x = this->m[0][index];
315         column.y = this->m[1][index];
316         return column;
317     }
318 
319     //
SetColumn(int index,const VEC2 & column)320     void SetColumn(int index, const VEC2& column)
321     {
322         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
323         this->m[0][index] = column.x;
324         this->m[1][index] = column.y;
325     }
326     //
327 
328     //----------------------------------------
329     //
330     //
331 
332     //
SetupIdentity()333     self_type& SetupIdentity() { return *MTX23Identity(this); }
334 
335     //
336 
337     //----------------------------------------
338     //
339     //
340 
341     //
342     self_type& operator += (const self_type& rhs) { return *MTX23Add(this, this, &rhs); }
343 
344     //
345     self_type& operator -= (const self_type& rhs) { return *MTX23Sub(this, this, &rhs); }
346 
347     //
348     self_type& operator *= (f32 f) { return *MTX23Mult(this, this, f); }
349 
350     //
351     self_type& operator /= (f32 f) { return operator*=(1.f / f); }
352 
353     //
354     self_type operator + () const { return *this; }
355 
356     //
357     self_type operator - () const
358     {
359         return MTX23(-f._00, -f._01, -f._02,
360                      -f._10, -f._11, -f._12);
361     }
362 
363     //
364     self_type operator + (const self_type& rhs) const { MTX23 tmp; return *MTX23Add(&tmp, this, &rhs); }
365 
366     //
367     self_type operator - (const self_type& rhs) const { MTX23 tmp; return *MTX23Sub(&tmp, this, &rhs); }
368 
369     //
370     self_type operator * (f32 f) const { MTX23 tmp; return *MTX23Mult(&tmp, this, f); }
371 
372     //
373     self_type operator / (f32 f) const { return *this * (1.f / f); }
374 
375     //
376 
377     //----------------------------------------
378     //
379     //
380 
381     //
382     //
383     //
384     //
385     //
SetupScale(const MTX23 & matrix,const VEC2 & scale)386     self_type& SetupScale(const MTX23& matrix, const VEC2& scale)
387     {
388         return *MTX23Scale(this, &matrix, &scale);
389     }
390 
391     //
392     //
393     //
394     //
395     //
SetupTranslate(const MTX23 & matrix,const VEC2 & translate)396     self_type& SetupTranslate(const MTX23& matrix, const VEC2& translate)
397     {
398         return *MTX23MultTranslate(this, &matrix, &translate);
399     }
400 
401     //
402     //
403     //
404     //
SetupRotate(f32 rotate)405     self_type& SetupRotate(f32 rotate)
406     {
407         return *MTX23RotFIdx(this, NN_MATH_RAD_TO_FIDX(rotate));
408 
409     }
410 
411     //
412     //
413     //
414     //
415     //
SetupRotate(const VEC2 & center,f32 rotate)416     self_type& SetupRotate(const VEC2& center, f32 rotate)
417     {
418         return *MTX23RotCenterFIdx(this, &center, NN_MATH_RAD_TO_FIDX(rotate));
419     }
420 
421     //
422 
423     //----------------------------------------
424     //
425     //
426 
427     //
428     bool operator == (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX23)) == 0; }
429 
430     //
431     bool operator != (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX23)) != 0; }
432 
433     //
IsIdentity()434     bool IsIdentity() const { return MTX23IsIdentity(this); }
435     //
436 
437     //
438     void Report(bool bNewline = true, const char* name = NULL) const;
439 
440 private:
441     typedef void (self_type::*UnspecifiedBoolType)() const;
442     operator UnspecifiedBoolType() const;
443     operator UnspecifiedBoolType();
444 };
445 
446 //
447 //
448 
449 //
450 typedef class MTX23 Matrix23;
451 
452 //
453 
454 }  // namespace math
455 }  // namespace nn
456 
457 namespace nn {
458 namespace math {
459 
460 //Overload referencing the -- const argument
461 
MTX23Copy(MTX23 * pOut,const MTX23 & m)462 inline MTX23* MTX23Copy(MTX23* pOut, const MTX23& m) { return MTX23Copy( pOut, &m ); }
MTX23IsIdentity(const MTX23 & m)463 inline bool   MTX23IsIdentity(const MTX23& m) { return MTX23IsIdentity( &m ); }
MTX23Add(MTX23 * pOut,const MTX23 & m1,const MTX23 & m2)464 inline MTX23* MTX23Add(MTX23* pOut, const MTX23& m1, const MTX23& m2) { return MTX23Add( pOut, &m1, &m2 ); }
MTX23Sub(MTX23 * pOut,const MTX23 & m1,const MTX23 & m2)465 inline MTX23* MTX23Sub(MTX23* pOut, const MTX23& m1, const MTX23& m2) { return MTX23Sub( pOut, &m1, &m2 ); }
MTX23Mult(MTX23 * pOut,const MTX23 & m,f32 f)466 inline MTX23* MTX23Mult(MTX23* pOut, const MTX23& m, f32 f) { return MTX23Mult( pOut, &m, f ); }
MTX23Mult(MTX23 * pOut,const MTX23 & m1,const MTX23 & m2)467 inline MTX23* MTX23Mult(MTX23* pOut, const MTX23& m1, const MTX23& m2) { return MTX23Mult( pOut, &m1, &m2); }
MTX23Scale(MTX23 * pOut,const MTX23 & m,const VEC2 & vS)468 inline MTX23* MTX23Scale(MTX23* pOut, const MTX23& m, const VEC2& vS) { return MTX23Scale( pOut, &m, &vS); }
MTX23Translate(MTX23 * pOut,const VEC2 & vT)469 inline MTX23* MTX23Translate(MTX23* pOut, const VEC2& vT) { return MTX23Translate( pOut, &vT); }
MTX23MultTranslate(MTX23 * pOut,const MTX23 & m,const VEC2 & vT)470 inline MTX23* MTX23MultTranslate(MTX23* pOut, const MTX23& m, const VEC2& vT) { return MTX23MultTranslate( pOut, &m, &vT); }
MTX23MultTranslate(MTX23 * pOut,const VEC2 & vT,const MTX23 & m)471 inline MTX23* MTX23MultTranslate(MTX23* pOut, const VEC2& vT, const MTX23& m) { return MTX23MultTranslate( pOut, &vT, &m); }
MTX23RotCenterFIdx(MTX23 * pOut,const VEC2 & vCenter,f32 fIdx)472 inline MTX23* MTX23RotCenterFIdx(MTX23* pOut, const VEC2& vCenter, f32 fIdx) { return MTX23RotCenterFIdx( pOut, &vCenter, fIdx ); }
MTX23MAdd(MTX23 * pOut,f32 t,const MTX23 & m1,const MTX23 & m2)473 inline MTX23* MTX23MAdd(MTX23* pOut, f32 t, const MTX23& m1, const MTX23& m2) { return MTX23MAdd( pOut, t, &m1, &m2 ); }
474 
475 }  // namespace math
476 }  // namespace nn
477 
478 #if defined(NN_MATH_AS_INLINE)
479 #include <nn/math/inline/math_Matrix23.ipp>
480 #endif
481 
482 #pragma pop
483 
484 #endif
485