メインページ | ネームスペース一覧 | クラス階層 | 構成 | Directories | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

TinyMat.h

00001 #ifndef TINYMAT_H
00002 #define TINYMAT_H
00003 
00004 #include <Base/TMatrixUtility.h>
00005 #include <Base/TMatrix.h>
00006 #include <Base/BaseDebug.h>
00007 
00008 #ifndef PTM_PACK    //  単体で使用する場合は,namespace に入れない
00009 namespace Spr{;
00010 #endif
00011 #ifdef _WIN32
00012  #pragma pack(push,4)
00013 #endif
00014 //-----------------------------------------------------------------------------
00015 //      TMatrix2
00016 /** 2x2行列クラス.  */
00017 template <class T>
00018 class TMatrix2:public PTM::TMatrixBase<DIMENC(2),DIMENC(2),
00019     PTM::TMatrixDescCol< TMatrix2<T>, PTM::TMatrixRow<2,2,T>, 2,2,2,T> >{
00020 public:
00021     typedef PTM::TMatrixDescCol< TMatrix2<T>, PTM::TMatrixRow<2,2,T>, 2,2,2,T> desc;
00022     typedef PTM::TMatrixBase<DIMENC(2),DIMENC(2),desc> base_type;
00023     /// 基本的なメンバの定義 @see ::DEF_MATRIX_BASIC_MEMBER
00024     DEF_MATRIX_BASIC_MEMBER(TMatrix2);
00025     union{
00026         element_type data[2][2];
00027         struct {
00028             element_type xx, xy, yx, yy;
00029         };
00030     };
00031     /// 要素のアクセス
00032     element_type& item_impl(size_t i, size_t j){ return data[j][i]; }
00033     const element_type& item_impl(size_t i, size_t j) const { return data[j][i]; }
00034 
00035     /**@name    基底ベクトルへのアクセス    */
00036     //@{
00037     /// 
00038     TVec2<element_type>& Ex(){ return *(TVec2<element_type>*)&col(0); }
00039     /// 
00040     const TVec2<element_type>& Ex() const{ return *(TVec2<element_type>*)&col(0); }
00041     /// 
00042     /// 
00043     TVec2<element_type>& Ey(){ return *(TVec2<element_type>*)&col(1); }
00044     /// 
00045     const TVec2<element_type>& Ey() const{ return *(TVec2<element_type>*)&col(1); }
00046     //@}
00047     
00048     ///@name 初期化と構築
00049     //@{
00050     /// コンストラクタ
00051     void set_default(){*this = Unit();}
00052     /// コンストラクタ(直接指定)
00053     TMatrix2(const TVec2<T>& exi, const TVec2<T>& eyi){
00054         Ex().X() = exi.X();
00055         Ex().Y() = exi.Y();
00056         Ey().X() = eyi.X();
00057         Ey().Y() = eyi.Y();
00058     }
00059     TMatrix2(T m11, T m12, T m21, T m22){
00060         Ex().X() = m11;
00061         Ex().Y() = m21;
00062         Ey().X() = m12;
00063         Ey().Y() = m22;
00064     }
00065     ///零行列
00066     static TMatrix2<T> Zero(){return TMatrix2<T>(0, 0, 0, 0);}
00067     ///単位行列
00068     static TMatrix2<T> Unit(){return TMatrix2<T>(1, 0, 0, 1);}
00069     ///対角行列
00070     static TMatrix2<T> Diag(element_type x, element_type y){return TMatrix2<T>(x, 0, 0, y);}
00071     ///回転行列
00072     ///回転角を指定
00073     static TMatrix2<T> Rot(element_type rad){
00074         TMatrix2<T> m;
00075         PTM::init_rot(m, rad);
00076         return m;
00077     }
00078     ///x/y軸の向きを指定
00079     template<typename BUFA>
00080     static TMatrix2<T> Rot(const PTM::TVectorBase<DIMENC(2), BUFA>& a, char axis){
00081         TMatrix2<T> m;
00082         PTM::init_rot(m, a, axis);
00083         return m;
00084     }
00085     //@}
00086 
00087     /**@name 要素へのアクセス */
00088     //@{
00089     /// 
00090     const element_type& ExX() const {return EX().X();}
00091     element_type& ExX() {return EX().X();}
00092     /// 
00093     const element_type& ExY() const {return Ex().Y();}
00094     element_type& ExY() {return Ex().Y();}
00095     /// 
00096     const element_type& EyX() const {return Ey().X();}
00097     element_type& EyX() {return Ey().X();}
00098     /// 
00099     const element_type& EyY() const {return Ey().Y();}
00100     element_type& EyY() {return Ey().Y();}
00101     //@}
00102 
00103     ///@name 演算
00104     //@{
00105     /// 回転行列の回転角を返す
00106     element_type angle() const{
00107         return atan2(Ex().Y(), Ex().X());
00108     }
00109     //@}
00110 };
00111 
00112 //-----------------------------------------------------------------------------
00113 //  TMatrix3
00114 
00115 /// 3x3行列クラス.
00116 template <class T>
00117 class TMatrix3:public PTM::TMatrixBase<DIMENC(3),DIMENC(3),
00118     PTM::TMatrixDescCol< TMatrix3<T>, PTM::TMatrixRow<3,3,T>, 3,3,3,T> >{
00119 public:
00120     typedef PTM::TMatrixDescCol< TMatrix3<T>, PTM::TMatrixRow<3,3,T>, 3,3,3,T> desc;
00121     typedef PTM::TMatrixBase<DIMENC(3),DIMENC(3),desc> base_type;
00122     /// 基本的なメンバの定義 @see ::DEF_MATRIX_BASIC_MEMBER
00123     DEF_MATRIX_BASIC_MEMBER(TMatrix3);
00124     union{
00125         element_type data[3][3];
00126         struct {
00127             element_type xx, xy, xz;
00128             element_type yx, yy, yz;
00129             element_type zx, zy, zz;
00130         };
00131     };
00132     /// 要素のアクセス
00133     element_type& item_impl(size_t i, size_t j){ return data[j][i]; }
00134     const element_type& item_impl(size_t i, size_t j) const { return data[j][i]; }
00135 
00136     /**@name    基底ベクトルへのアクセス    */
00137     //@{
00138     /// 
00139     TVec3<element_type>& Ex(){ return *(TVec3<element_type>*) &item(0,0); }
00140     ///
00141     const TVec3<element_type>& Ex() const { return *(TVec3<element_type>*) &item(0,0); }
00142     /// 
00143     TVec3<element_type>& Ey(){ return *(TVec3<element_type>*) &item(0,1); }
00144     ///
00145     const TVec3<element_type>& Ey() const { return *(TVec3<element_type>*) &item(0,1); }
00146     /// 
00147     TVec3<element_type>& Ez(){ return *(TVec3<element_type>*) &item(0,2); }
00148     ///
00149     const TVec3<element_type>& Ez() const { return *(TVec3<element_type>*) &item(0,2); }
00150     //@}
00151     /**@name 要素へのアクセス */
00152     //@{
00153     /// 
00154     const element_type& ExX() const {return Ex().X();}
00155     element_type& ExX() {return Ex().X();}
00156     /// 
00157     const element_type& ExY() const {return Ex().Y();}
00158     element_type& ExY() {return Ex().Y();}
00159     /// 
00160     const element_type& ExZ() const {return Ex().Z();}
00161     element_type& ExZ() {return Ex().Z();}
00162     /// 
00163     const element_type& EyX() const {return Ey().X();}
00164     element_type& EyX() {return Ey().X();}
00165     /// 
00166     const element_type& EyY() const {return Ey().Y();}
00167     element_type& EyY() {return Ey().Y();}
00168     /// 
00169     const element_type& EyZ() const {return Ey().Z();}
00170     element_type& EyZ() {return Ey().Z();}
00171     /// 
00172     const element_type& EzX() const {return Ez().X();}
00173     element_type& EzX() {return Ez().X();}
00174     /// 
00175     const element_type& EzY() const {return Ez().Y();}
00176     element_type& EzY() {return Ez().Y();}
00177     /// 
00178     const element_type& EzZ() const {return Ez().Z();}
00179     element_type& EzZ() {return Ez().Z();}
00180     //@}
00181     
00182     //---------------------------------
00183     /**@name    初期化と構築    */
00184     //@{
00185     ///デフォルトコンストラクタ
00186     void set_default(){*this = Unit();}
00187     ///コンストラクタ(基底ベクトルを指定)
00188     template <class BX, class BY, class BZ>
00189     TMatrix3(const PTM::TVectorBase<DIMENC(3), BX>& exi, const PTM::TVectorBase<DIMENC(3), BY>&eyi, const PTM::TVectorBase<DIMENC(3), BZ>&ezi){
00190         Ex() = exi; Ey() = eyi; Ez() = ezi;
00191     }
00192     ///コンストラクタ(成分を指定)
00193     TMatrix3(T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33)
00194     {
00195         Ex().X() = m11; Ey().X() = m12; Ez().X() = m13;
00196         Ex().Y() = m21; Ey().Y() = m22; Ez().Y() = m23;
00197         Ex().Z() = m31; Ey().Z() = m32; Ez().Z() = m33;
00198     }
00199 
00200     ///零行列
00201     static TMatrix3<T> Zero(){TMatrix3<T> m; m.clear(); return m;}
00202     ///単位行列
00203     static TMatrix3<T> Unit(){return TMatrix3<T>(1, 0, 0, 0, 1, 0, 0, 0, 1);}
00204     ///対角行列
00205     static TMatrix3<T> Diag(element_type x, element_type y, element_type z){
00206         return TMatrix3<T>(x, 0, 0, 0, y, 0, 0, 0, z);
00207     }
00208     ///回転行列
00209     ///方向ベクトルで指定
00210     template <class BA, class BB>
00211     static TMatrix3<T> Rot(const PTM::TVectorBase<DIMENC(3),BA>& a, const PTM::TVectorBase<DIMENC(3),BB>&b, char axis = 'x') {
00212         TMatrix3<T> m;
00213         PTM::init_rot(m, a, b, axis);
00214         return m;
00215     }
00216     ///回転軸で指定
00217     static TMatrix3<T> Rot(element_type th, char axis){
00218         TMatrix3<T> m;
00219         PTM::init_rot(m, th, axis);
00220         return m;
00221     }
00222     /** 任意軸まわり回転
00223         +                                                                      +
00224         |u^2+(1-u^2)cos(th)      uv(1-cos(th))-wsin(th)  wu(1-cos(th))+vsin(th)|
00225     R = |uv(1-cos(th))+wsin(th)  v^2+(1-v^2)cos(th)      vw(1-cos(th))-usin(th)|
00226         |wu(1-cos(th))-vsin(th)  vw(1-cos(th))+usin(th)  w^2+(1-w^2)cos(th)    |
00227         +                                                                      +
00228     */
00229     template <class B>
00230     static TMatrix3<T> Rot(element_type th, const PTM::TVectorBase<DIMENC(3), B>& axis){
00231         TMatrix3<T> m;
00232         PTM::init_rot(m, th, axis.unit());
00233         return m;
00234     }
00235     /// Qutaternionで指定
00236     template <class B>
00237     static TMatrix3<T> Rot(const PTM::TVectorBase<DIMENC(4), B>& q){
00238         TMatrix3<T> m;
00239         PTM::init_rot(m, q);
00240         return m;
00241     }
00242     
00243     ///外積と等価な行列
00244     template <class B>
00245     static TMatrix3<T> Cross(const PTM::TVectorBase<DIMENC(3), B>& v){
00246         TMatrix3<T> m;
00247         PTM::init_cross(m, v);
00248         return m;
00249     }
00250     
00251     //@}
00252 };
00253 
00254 /// float版2×2行列.
00255 typedef TMatrix2<float> Matrix2f;
00256 /// double版2×2行列.
00257 typedef TMatrix2<double> Matrix2d;
00258 /// float版3×3行列.
00259 typedef TMatrix3<float> Matrix3f;
00260 /// double版3×3行列.
00261 typedef TMatrix3<double> Matrix3d;
00262 
00263 
00264 template <class T>
00265 bool IsUnitary(TMatrix3<T> r){
00266     bool bGood = true;
00267 #ifdef _DEBUG
00268     if(abs(r.Ex().norm()-1) > 1e-1) bGood = false;
00269     if(abs(r.Ey().norm()-1) > 1e-1) bGood = false;
00270     if(abs(r.Ez().norm()-1) > 1e-1) bGood = false;
00271     if(abs(r.Ex() * r.Ey()) > 1e-1) bGood = false;
00272     if(abs(r.Ey() * r.Ez()) > 1e-1) bGood = false;
00273     if(abs(r.Ez() * r.Ex()) > 1e-1) bGood = false;
00274     if (!bGood){
00275         DSTR << "PHSolid::SetRotation: " << r;
00276         DSTR << "x:" << r.Ex().norm() << " " << r.Ex()*r.Ey() << std::endl;
00277         DSTR << "y:" << r.Ey().norm() << " " << r.Ey()*r.Ez() << std::endl;
00278         DSTR << "z:" << r.Ez().norm() << " " << r.Ez()*r.Ex() << std::endl;
00279     }
00280 #endif
00281     return bGood;
00282 }
00283 
00284 #ifdef _WIN32
00285  #pragma pack(pop)
00286 #endif
00287 #ifndef PTM_PACK    //  単体で使用する場合は,namespace に入れない
00288 }
00289 #endif
00290 
00291 #endif

Springheadに対してSun Apr 16 01:57:55 2006に生成されました。  doxygen 1.4.1