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: 24035 $
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     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     @brief        2行3列の行列クラスです。
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     //! @brief 単位行列です。
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     //! @name 作成
99     //@{
100 
101     //! @brief コンストラクタです。
MTX23MTX23102     MTX23() {}
103     //! @brief コンストラクタです。
MTX23MTX23104     MTX23(const f32* p) { (void)MTX23Copy(this, reinterpret_cast<const MTX23*>(p)); }
105     //! @brief コンストラクタです。
MTX23MTX23106     MTX23(const MTX22& rhs) { MTX22ToMTX23(this, &rhs); }
107     //! @brief コンストラクタです。
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     //! @name 変換
119     //@{
120 
121     //! @brief f32 型へのキャストです。
122     operator f32*() { return this->a; }
123 
124     //! @brief f32 型へのキャストです。
125     operator const f32*() const { return this->a; }
126 
127     //! @brief VEC2 型として行を取得します。
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     //! @brief VEC2 型として行を取得します。
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     //! @brief VEC3 型として列を取得します。
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     //! @brief VEC3 型で列を設定します。
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     //! @name 設定
162     //@{
163 
164     //! @brief 単位行列に設定します。
SetupIdentityMTX23165     self_type& SetupIdentity() { return *MTX23Identity(this); }
166 
167     //@}
168 
169     //----------------------------------------
170     //! @name 演算
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     //! @name 設定
196     //@{
197 
198     //! @brief スケール変換行列に設定します。
199     //!
200     //! @param[in] matrix 元となる行列です。
201     //! @param[in] scale スケール値です。
202     //!
SetupScaleMTX23203     self_type& SetupScale(const MTX23& matrix, const VEC2& scale)
204     {
205         return *MTX23Scale(this, &matrix, &scale);
206     }
207 
208     //! @brief 平行移動行列に設定します。
209     //!
210     //! @param[in] matrix 元となる行列です。
211     //! @param[in] translate 平行移動値です。
212     //!
SetupTranslateMTX23213     self_type& SetupTranslate(const MTX23& matrix, const VEC2& translate)
214     {
215         return *MTX23Translate(this, &matrix, &translate);
216     }
217 
218     //! @brief XYZ 順の回転行列に設定します。
219     //!
220     //! @param[in] rotate 回転値です。単位はラジアンです。
221     //!
SetupRotateMTX23222     self_type& SetupRotate(f32 rotate)
223     {
224         return *MTX23RotFIdx(this, NN_MATH_RAD_TO_FIDX(rotate));
225 
226     }
227 
228     //! @brief 指定した軸周りの回転行列に設定します。
229     //!
230     //! @param[in] center 基準となる座標です。
231     //! @param[in] rotate 回転させる角度です。単位はラジアンです。
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     //! @name 比較
242     //@{
243 
244     //! @brief 同値であれば true を返します。
245     bool operator == (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX23)) == 0; }
246 
247     //! @brief 同値でなければ true を返します。
248     bool operator != (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX23)) != 0; }
249 
250     //! @brief 単位行列であれば true を返します。
IsIdentityMTX23251     bool IsIdentity() const { return MTX23IsIdentity(this); }
252     //@}
253 
254     //! @brief 状態を出力します。
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 //-- const 引数を参照にしたオーバーロード
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