1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Matrix23.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_MATRIX23_H_
17 #define NN_MATH_MATRIX23_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 MTX23;
29 struct MTX22;
30 
31 /* ------------------------------------------------------------------------
32     Function for MTX23
33    ------------------------------------------------------------------------ */
34 NN_MATH_INLINE MTX23* MTX23Copy(MTX23* pOut, const MTX23* p);
35 NN_MATH_INLINE MTX23* MTX23Zero(MTX23* pOut);
36 NN_MATH_INLINE MTX23* MTX23Identity(MTX23* pOut);
37 NN_MATH_INLINE bool   MTX23IsIdentity(const MTX23* p);
38 NN_MATH_INLINE MTX23* MTX23Add(MTX23* pOut, const MTX23* p1, const MTX23* p2);
39 NN_MATH_INLINE MTX23* MTX23Sub(MTX23* pOut, const MTX23* p1, const MTX23* p2);
40 NN_MATH_INLINE MTX23* MTX23Mult(MTX23* pOut, const MTX23* p, f32 f);
41 NN_MATH_INLINE MTX23* MTX23Mult(MTX23* pOut, const MTX23* p1, const MTX23* p2);
42 NN_MATH_INLINE MTX23* MTX23Scale(MTX23* pOut, const MTX23* pM, const VEC2* pS);
43 NN_MATH_INLINE MTX23* MTX23Translate(MTX23* pOut, const MTX23* pM, const VEC2* pT);
44 NN_MATH_INLINE MTX23* MTX23RotFIdx(MTX23* pOut, f32 fIdx);
45 NN_MATH_INLINE MTX23* MTX23RotCenterFIdx(MTX23* pOut, const VEC2* pCenter, f32 fIdx);
46 
47 NN_MATH_INLINE MTX23* MTX23MAdd(MTX23* pOut, f32 t, const MTX23* p1, const MTX23* p2);
48 NN_MATH_INLINE MTX23* MTX22ToMTX23(MTX23* pOut, const MTX22* pM);
49 
50 
51 struct MTX23_
52 {
53     struct BaseData
54     {
55         f32 _00, _01, _02;
56         f32 _10, _11, _12;
57     };
58 
59     union
60     {
61     #if defined(NN_MATH_USE_ANONYMOUS_STRUCT)
62         struct
63         {
64             f32 _00, _01, _02;
65             f32 _10, _11, _12;
66         };
67     #endif
68         BaseData f;
69         f32 m[2][3];
70         f32 a[6];
71         VEC3_ v[2];
72     };
73 };
74 
75 /*
76 
77  */
78 struct MTX23 : public MTX23_
79 {
80 public:
81     static const int ROW_COUNT = 2; //
82     static const int COLUMN_COUNT = 3; //
83 
84     //
IdentityMTX2385     static const MTX23& Identity()
86     {
87         static const MTX23 identity(
88             1.0f, 0.0f, 0.0f,
89             0.0f, 1.0f, 0.0f);
90 
91         return identity;
92     }
93 
94     typedef MTX23 self_type; //
95     typedef f32   value_type; //
96 public:
97     //----------------------------------------
98     //
99     //
100 
101     //
MTX23MTX23102     MTX23() {}
103     //
MTX23MTX23104     explicit MTX23(const f32* p) { (void)MTX23Copy(this, reinterpret_cast<const MTX23*>(p)); }
105     //
MTX23MTX23106     explicit MTX23(const MTX22& rhs) { MTX22ToMTX23(this, &rhs); }
107     //
MTX23MTX23108     MTX23(f32 x00, f32 x01, f32 x02,
109           f32 x10, f32 x11, f32 x12)
110     {
111         f._00 = x00; f._01 = x01; f._02 = x02;
112         f._10 = x10; f._11 = x11; f._12 = x12;
113     }
114 
115     //
116 
117     //----------------------------------------
118     //
119     //
120 
121     //
122     operator f32*() { return this->a; }
123 
124     //
125     operator const f32*() const { return this->a; }
126 
127     //
GetRowMTX23128     VEC3& GetRow(int index)
129     {
130         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
131         return *reinterpret_cast<VEC3*>(&this->v[index]);
132     }
133 
134     //
GetRowMTX23135     const VEC3& GetRow(int index) const
136     {
137         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
138         return *reinterpret_cast<const VEC3*>(&this->v[index]);
139     }
140 
141     //
GetColumnMTX23142     VEC2 GetColumn(int index) const
143     {
144         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
145         VEC2 column;
146         column.x = this->m[0][index];
147         column.y = this->m[1][index];
148         return column;
149     }
150 
151     //
SetColumnMTX23152     void SetColumn(int index, const VEC2& column)
153     {
154         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
155         this->m[0][index] = column.x;
156         this->m[1][index] = column.y;
157     }
158     //
159 
160     //----------------------------------------
161     //
162     //
163 
164     //
SetupIdentityMTX23165     self_type& SetupIdentity() { return *MTX23Identity(this); }
166 
167     //
168 
169     //----------------------------------------
170     //
171     //
172 
173     self_type& operator += (const self_type& rhs) { return *MTX23Add(this, this, &rhs); }
174     self_type& operator -= (const self_type& rhs) { return *MTX23Sub(this, this, &rhs); }
175 
176     self_type& operator *= (f32 f) { return *MTX23Mult(this, this, f); }
177     self_type& operator /= (f32 f) { return operator*=(1.f / f); }
178 
179     self_type operator + () const { return *this; }
180     self_type operator - () const
181     {
182         return MTX23(-f._00, -f._01, -f._02,
183                      -f._10, -f._11, -f._12);
184     }
185 
186     self_type operator + (const self_type& rhs) const { MTX23 tmp; return *MTX23Add(&tmp, this, &rhs); }
187     self_type operator - (const self_type& rhs) const { MTX23 tmp; return *MTX23Sub(&tmp, this, &rhs); }
188 
189     self_type operator * (f32 f) const { MTX23 tmp; return *MTX23Mult(&tmp, this, f); }
190     self_type operator / (f32 f) const { return *this * (1.f / f); }
191 
192     //
193 
194     //----------------------------------------
195     //
196     //
197 
198     //
199     //
200     //
201     //
202     //
SetupScaleMTX23203     self_type& SetupScale(const MTX23& matrix, const VEC2& scale)
204     {
205         return *MTX23Scale(this, &matrix, &scale);
206     }
207 
208     //
209     //
210     //
211     //
212     //
SetupTranslateMTX23213     self_type& SetupTranslate(const MTX23& matrix, const VEC2& translate)
214     {
215         return *MTX23Translate(this, &matrix, &translate);
216     }
217 
218     //
219     //
220     //
221     //
SetupRotateMTX23222     self_type& SetupRotate(f32 rotate)
223     {
224         return *MTX23RotFIdx(this, NN_MATH_RAD_TO_FIDX(rotate));
225 
226     }
227 
228     //
229     //
230     //
231     //
232     //
SetupRotateMTX23233     self_type& SetupRotate(const VEC2& center, f32 rotate)
234     {
235         return *MTX23RotCenterFIdx(this, &center, NN_MATH_RAD_TO_FIDX(rotate));
236     }
237 
238     //
239 
240     //----------------------------------------
241     //
242     //
243 
244     //
245     bool operator == (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX23)) == 0; }
246 
247     //
248     bool operator != (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX23)) != 0; }
249 
250     //
IsIdentityMTX23251     bool IsIdentity() const { return MTX23IsIdentity(this); }
252     //
253 
254     //
255     void Report(bool bNewline = true, const char* name = NULL) const;
256 };
257 
258 typedef struct MTX23 Matrix23;
259 
260 }  // namespace math
261 }  // namespace nn
262 
263 namespace nn {
264 namespace math {
265 
266 //-- Overloads that reference const type arguments
267 
MTX23Copy(MTX23 * pOut,const MTX23 & m)268 inline MTX23* MTX23Copy(MTX23* pOut, const MTX23& m) { return MTX23Copy( pOut, &m ); }
MTX23IsIdentity(const MTX23 & m)269 inline bool   MTX23IsIdentity(const MTX23& m) { return MTX23IsIdentity( &m ); }
MTX23Add(MTX23 * pOut,const MTX23 & m1,const MTX23 & m2)270 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)271 inline MTX23* MTX23Sub(MTX23* pOut, const MTX23& m1, const MTX23& m2) { return MTX23Sub( pOut, &m1, &m2 ); }
MTX23Mult(MTX23 * pOut,const MTX23 & m,f32 f)272 inline MTX23* MTX23Mult(MTX23* pOut, const MTX23& m, f32 f) { return MTX23Mult( pOut, &m, f ); }
MTX23Mult(MTX23 * pOut,const MTX23 & m1,const MTX23 & m2)273 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)274 inline MTX23* MTX23Scale(MTX23* pOut, const MTX23& m, const VEC2& vS) { return MTX23Scale( pOut, &m, &vS); }
MTX23Translate(MTX23 * pOut,const MTX23 & m,const VEC2 & vT)275 inline MTX23* MTX23Translate(MTX23* pOut, const MTX23& m, const VEC2& vT) { return MTX23Translate( pOut, &m, &vT); }
MTX23RotCenterFIdx(MTX23 * pOut,const VEC2 & vCenter,f32 fIdx)276 inline MTX23* MTX23RotCenterFIdx(MTX23* pOut, const VEC2& vCenter, f32 fIdx) { return MTX23RotCenterFIdx( pOut, &vCenter, fIdx ); }
277 
MTX23MAdd(MTX23 * pOut,f32 t,const MTX23 & m1,const MTX23 & m2)278 inline MTX23* MTX23MAdd(MTX23* pOut, f32 t, const MTX23& m1, const MTX23& m2) { return MTX23MAdd( pOut, t, &m1, &m2 ); }
MTX22ToMTX23(MTX23 * pOut,const MTX22 & m)279 inline MTX23* MTX22ToMTX23(MTX23* pOut, const MTX22& m) { return MTX22ToMTX23( pOut, &m ); }
280 
281 }  // namespace math
282 }  // namespace nn
283 
284 #if defined(NN_MATH_AS_INLINE)
285 #include <nn/math/inline/math_Matrix23.ipp>
286 #endif
287 
288 #pragma pop
289 
290 #endif
291