00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00021 #include<ImLib3D/Affine3DTransform.hpp>
00022 #include<ImLib3D/ImLib3DFile.hpp>
00023 #include <math.h>
00024 #include<ImLib3D/Vect3D.hpp>
00025
00026
00027
00028 Affine3DTransform::Affine3DTransform(const string &fname)
00029 {
00030 Reset();
00031 ReadFromFile(fname);
00032 }
00033
00034 void
00035 Affine3DTransform::Read( ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node)
00036 {
00037 node=CreateReadNode("Affine3DTransform",file,parentNode,node);
00038 xmlpp::Element *mNode=file->GetChild(node,"M");
00039 matrix(0,0)=file->GetDoubleAttribute(mNode,"a11");
00040 matrix(1,0)=file->GetDoubleAttribute(mNode,"a21");
00041 matrix(2,0)=file->GetDoubleAttribute(mNode,"a31");
00042 matrix(0,1)=file->GetDoubleAttribute(mNode,"a12");
00043 matrix(1,1)=file->GetDoubleAttribute(mNode,"a22");
00044 matrix(2,1)=file->GetDoubleAttribute(mNode,"a32");
00045 matrix(0,2)=file->GetDoubleAttribute(mNode,"a13");
00046 matrix(1,2)=file->GetDoubleAttribute(mNode,"a23");
00047 matrix(2,2)=file->GetDoubleAttribute(mNode,"a33");
00048 xmlpp::Element *tNode=file->GetChild(node,"T");
00049 translation(0)=file->GetDoubleAttribute(tNode,"t1");
00050 translation(1)=file->GetDoubleAttribute(tNode,"t2");
00051 translation(2)=file->GetDoubleAttribute(tNode,"t3");
00052 }
00053
00054
00055 void
00056 Affine3DTransform::Write(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node) const
00057 {
00058 node=CreateWriteNode("Affine3DTransform",file,parentNode,node);
00059 xmlpp::Element *mNode=node->add_child("M");
00060
00061 mNode->set_attribute("a11",SPrintf("%10.10f",matrix(0,0)));
00062 mNode->set_attribute("a21",SPrintf("%10.10f",matrix(1,0)));
00063 mNode->set_attribute("a31",SPrintf("%10.10f",matrix(2,0)));
00064 mNode->set_attribute("a12",SPrintf("%10.10f",matrix(0,1)));
00065 mNode->set_attribute("a22",SPrintf("%10.10f",matrix(1,1)));
00066 mNode->set_attribute("a32",SPrintf("%10.10f",matrix(2,1)));
00067 mNode->set_attribute("a13",SPrintf("%10.10f",matrix(0,2)));
00068 mNode->set_attribute("a23",SPrintf("%10.10f",matrix(1,2)));
00069 mNode->set_attribute("a33",SPrintf("%10.10f",matrix(2,2)));
00070 xmlpp::Element *tNode=node->add_child("T");
00071 tNode->set_attribute("t1",SPrintf("%10.10f",translation(0)));
00072 tNode->set_attribute("t2",SPrintf("%10.10f",translation(1)));
00073 tNode->set_attribute("t3",SPrintf("%10.10f",translation(2)));
00074 }
00075
00076 void Affine3DTransform::Inverse()
00077 {
00078 matrix.Inverse();
00079 translation=-(matrix*translation);
00080 }
00082 Affine3DTransform operator*(const Affine3DTransform& a1,const Affine3DTransform& a2)
00083 {
00084 Affine3DTransform res;
00085 res.translation=a1.matrix*a2.translation+a1.translation;
00086 res.matrix=a1.matrix*a2.matrix;
00087 return res;
00088 }
00089
00091 Affine3DTransform Affine3DTransform::operator*=(const Affine3DTransform& other)
00092 {
00093 translation=matrix*other.translation+translation;
00094 matrix*=other.matrix;
00095 return *this;
00096 }
00097 void Affine3DTransform::SetRotation(float theta,Vect3Df direction,
00098 const Vect3Df ¢er)
00099 {
00100 if(direction.Norm2()==0){SetRotationX(0);}
00101
00102
00103 direction.Unit();
00104 Vect3Df U=direction;
00105 float initS[]={ 0.0 , -direction.z , direction.y ,
00106 direction.z , 0.0 , -direction.x ,
00107 -direction.y , direction.x , 0.0 };
00108 Matrix S(initS);
00109
00110 Matrix I;
00111 I.SetToIdentity();
00112
00113 Matrix UUt;
00114 for(int i=0;i<3;i++)
00115 for(int j=0;j<3;j++)
00116 {
00117 UUt(i,j)=U(i)*U(j);
00118 }
00119
00120 matrix=UUt+cos(theta)*(I-UUt)+sin(theta)*S;
00121
00122 translation=center-matrix*center;
00123 }
00124 void Affine3DTransform::SetRotationX(float theta,const Vect3Df ¢er)
00125 {
00126 float c1 = cos(theta);
00127 float s1 = sin(theta);
00128
00129 float initmatrix[]={
00130 1 , 0 , 0,
00131 0 , c1 , -s1,
00132 0 , s1 , c1 };
00133 matrix=Matrix(initmatrix);
00134
00135 translation=center-matrix*center;
00136 }
00137
00138 void Affine3DTransform::SetRotationY(float theta,const Vect3Df ¢er)
00139 {
00140 float c1 = cos(theta);
00141 float s1 = sin(theta);
00142 float initMatrix[]={
00143 c1 , 0 , s1,
00144 0 , 1 , 0,
00145 (-s1) , 0 , c1};
00146 matrix=Matrix(initMatrix);
00147 translation=center-matrix*center;
00148 }
00149
00150 void Affine3DTransform::SetRotationZ(float theta,const Vect3Df ¢er)
00151 {
00152 float c1 = cos(theta);
00153 float s1 = sin(theta);
00154
00155 float initMatrix[]={
00156 c1 , (-s1) , 0,
00157 s1 , c1 , 0,
00158 0 , 0 , 1};
00159
00160 matrix=Matrix(initMatrix);
00161 translation=center-matrix*center;
00162 }
00163
00165
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187