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
00016
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
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
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
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
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
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
00104
00105
00106 element_type angle() const{
00107 return atan2(Ex().Y(), Ex().X());
00108 }
00109
00110 };
00111
00112
00113
00114
00115
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
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
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
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
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
00225
00226
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
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
00255 typedef TMatrix2<float> Matrix2f;
00256
00257 typedef TMatrix2<double> Matrix2d;
00258
00259 typedef TMatrix3<float> Matrix3f;
00260
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