1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Matrix22.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: 18951 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_MATH_MATRIX22_H_
17 #define NN_MATH_MATRIX22_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 MTX22;
29 struct MTX23;
30 
31 /* ------------------------------------------------------------------------
32     MTX22用の関数
33    ------------------------------------------------------------------------ */
34 NN_MATH_INLINE MTX22* MTX22Identity(MTX22* pOut);
35 NN_MATH_INLINE bool   MTX22IsIdentity(const MTX22* p);
36 NN_MATH_INLINE MTX22* MTX22Copy(MTX22* pOut, const MTX22* p);
37 NN_MATH_INLINE MTX22* MTX22Zero(MTX22* pOut);
38 NN_MATH_INLINE MTX22* MTX23ToMTX22(MTX22* pOut, const MTX23* pM);
39 NN_MATH_INLINE MTX22* MTX22MAdd(MTX22* pOut, f32 t, const MTX22* p1, const MTX22* p2);
40 
41 /* =======================================================================
42         クラスの定義
43    ======================================================================== */
44 
45 struct MTX22_
46 {
47     struct BaseData
48     {
49         f32 _00, _01;
50         f32 _10, _11;
51     };
52 
53     union
54     {
55     #if defined(NN_MATH_USE_ANONYMOUS_STRUCT)
56         struct
57         {
58             f32 _00, _01;
59             f32 _10, _11;
60         };
61     #endif
62         BaseData f;
63         f32 m[2][2];
64         f32 a[4];
65         VEC2_ v[2];
66     };
67 };
68 
69 /*!--------------------------------------------------------------------------*
70   @brief        2行2列の行列クラスです
71  *---------------------------------------------------------------------------*/
72 struct MTX22 : public MTX22_
73 {
74 public:
75     static const int ROW_COUNT = 2; //!< 行数です。
76     static const int COLUMN_COUNT = 2; //!< 列数です。
77 
78     //! @brief 単位行列です。
IdentityMTX2279     static const MTX22& Identity()
80     {
81         static const MTX22 identity(
82             1.0f, 0.0f,
83             0.0f, 1.0f);
84 
85         return identity;
86     }
87 
88     typedef MTX22 self_type; //!< 自分の型です
89     typedef f32   value_type; //!< 要素の型です。
90 public:
91     //----------------------------------------
92     //! @name 作成
93     //@{
94 
95     //! @brief コンストラクタです。
MTX22MTX2296     MTX22() {}
97     //! @brief コンストラクタです。
98     MTX22(const f32* p);
99     //! @brief コンストラクタです。
100     MTX22(const MTX23& rhs);
101     //! @brief コンストラクタです。
MTX22MTX22102     MTX22(f32 x00, f32 x01,
103           f32 x10, f32 x11)
104     {
105         f._00 = x00; f._01 = x01;
106         f._10 = x10; f._11 = x11;
107     }
108 
109     //@}
110 
111     //----------------------------------------
112     //! @name 変換
113     //@{
114 
115     //! @brief f32 型へのキャストです。
116     operator f32*() { return this->a; }
117     //! @brief f32 型へのキャストです。
118     operator const f32*() const { return this->a; }
119 
120     //! @brief VEC2 型として行を取得します。
GetRowMTX22121     VEC2& GetRow(int index)
122     {
123         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
124         return *reinterpret_cast<VEC2*>(&this->v[index]);
125     }
126 
127     //! @brief VEC2 型として行を取得します。
GetRowMTX22128     const VEC2& GetRow(int index) const
129     {
130         NN_MATH_MINMAXLT_ASSERT(index, 0, ROW_COUNT);
131         return *reinterpret_cast<const VEC2*>(&this->v[index]);
132     }
133 
134     //! @brief VEC2 型として列を取得します。
GetColumnMTX22135     VEC2 GetColumn(int index) const
136     {
137         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
138         VEC2 column;
139         column.x = this->m[0][index];
140         column.y = this->m[1][index];
141         return column;
142     }
143 
144     //! @brief VEC2 型で列を設定します。
SetColumnMTX22145     void SetColumn(int index, const VEC2& column)
146     {
147         NN_MATH_MINMAXLT_ASSERT(index, 0, COLUMN_COUNT);
148         this->m[0][index] = column.x;
149         this->m[1][index] = column.y;
150     }
151     //@}
152 
153     //----------------------------------------
154     //! @name 設定
155     //@{
156 
157     //! @brief 単位行列に設定します。
SetupIdentityMTX22158     self_type& SetupIdentity() { return *MTX22Identity(this); }
159 
160     //@}
161 
162     //----------------------------------------
163     //! @name 比較
164     //@{
165 
166     //! @brief 同値であれば true を返します。
167     bool operator == (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX22)) == 0; }
168 
169     //! @brief 同値でなければ true を返します。
170     bool operator != (const self_type& rhs) const { return ::std::memcmp(this, &rhs, sizeof(MTX22)) != 0; }
171 
172     //! @brief 単位行列であれば true を返します。
IsIdentityMTX22173     bool IsIdentity() const { return MTX22IsIdentity(this); }
174 
175     //@}
176 
177     //! @brief 状態を出力します。
178     void Report(bool bNewline = true, const char* name = NULL) const;
179 };
180 
181 typedef struct MTX22 Matrix22;
182 
183 }  // namespace math
184 }  // namespace nn
185 
186 namespace nn {
187 namespace math {
188 
189 inline
MTX22(const f32 * p)190 MTX22::MTX22(const f32* p) { MTX22Copy(this, reinterpret_cast<const MTX22*>(p)); }
191 
192 inline
MTX22(const MTX23 & rhs)193 MTX22::MTX22(const MTX23& rhs) { MTX23ToMTX22(this, &rhs); }
194 
195 //-- const 引数を参照にしたオーバーロード
MTX22IsIdentity(const MTX22 & m)196 inline bool   MTX22IsIdentity(const MTX22& m) { return MTX22IsIdentity( &m ); }
MTX22Copy(MTX22 * pOut,const MTX22 & m)197 inline MTX22* MTX22Copy(MTX22* pOut, const MTX22& m) { return MTX22Copy(pOut, &m); }
MTX23ToMTX22(MTX22 * pOut,const MTX23 & m)198 inline MTX22* MTX23ToMTX22(MTX22* pOut, const MTX23& m) { return MTX23ToMTX22(pOut, &m); }
MTX22MAdd(MTX22 * pOut,f32 t,const MTX22 & m1,const MTX22 & m2)199 inline MTX22* MTX22MAdd(MTX22* pOut, f32 t, const MTX22& m1, const MTX22& m2) { return MTX22MAdd(pOut, t, &m1, &m2); }
200 
201 }  // namespace math
202 }  // namespace nn
203 
204 #if defined(NN_MATH_AS_INLINE)
205 #include <nn/math/inline/math_Matrix22.ipp>
206 #endif
207 
208 #pragma pop
209 
210 #endif
211