CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testRotation.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: testRotation.cc,v 1.3 2003/08/13 20:00:14 garren Exp $
00003 // ---------------------------------------------------------------------------
00004 //
00005 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00006 //
00007 // This is a test for HepRotation class.
00008 //
00009 #include "CLHEP/Units/GlobalSystemOfUnits.h"    // to see shadowing problems
00010 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00011 #include "CLHEP/Vector/Rotation.h"
00012 #include "CLHEP/Vector/ThreeVector.h"
00013 #include <assert.h>
00014 #include <cmath>
00015 #include <stdlib.h>
00016 
00017 using namespace CLHEP;
00018 
00019 typedef HepRotation Rotation;
00020 typedef Hep3Vector  Vector;
00021 
00022 #define DEL 10.e-16
00023 
00024 int main() {
00025   int i,k;  
00026   double angA=CLHEP::pi/3, angB=CLHEP::pi/4, angC=CLHEP::pi/6; 
00027   double cosA=std::cos(angA), sinA=std::sin(angA);
00028   double cosB=std::cos(angB), sinB=std::sin(angB);
00029   double cosC=std::cos(angC), sinC=std::sin(angC);
00030 
00031   Rotation R;                   // default constructor
00032   assert ( R.xx() == 1 );
00033   assert ( R.xy() == 0 );
00034   assert ( R.xz() == 0 );
00035   assert ( R.yx() == 0 );
00036   assert ( R.yy() == 1 );
00037   assert ( R.yz() == 0 );
00038   assert ( R.zx() == 0 );
00039   assert ( R.zy() == 0 );
00040   assert ( R.zz() == 1 );
00041 
00042   assert( R.isIdentity() );     // isIdentity()
00043 
00044   R = Rotation();               // rotateX() 
00045   R.rotateX(angA);
00046   assert ( R.xx() == 1    );
00047   assert ( R.xy() == 0    );
00048   assert ( R.xz() == 0    );
00049   assert ( R.yx() == 0    );
00050   assert ( R.yy() == cosA );
00051   assert ( R.yz() ==-sinA );
00052   assert ( R.zx() == 0    );
00053   assert ( R.zy() == sinA );
00054   assert ( R.zz() == cosA );
00055 
00056   R = Rotation();               // rotateY() 
00057   R.rotateY(angB);
00058   assert ( R.xx() == cosB );
00059   assert ( R.xy() == 0    );
00060   assert ( R.xz() == sinB );
00061   assert ( R.yx() == 0    );
00062   assert ( R.yy() == 1    );
00063   assert ( R.yz() == 0    );
00064   assert ( R.zx() ==-sinB );
00065   assert ( R.zy() == 0    );
00066   assert ( R.zz() == cosB );
00067 
00068   R = Rotation();               // rotateZ() 
00069   R.rotateZ(angC);
00070   assert ( R.xx() == cosC );
00071   assert ( R.xy() ==-sinC );
00072   assert ( R.xz() == 0    );
00073   assert ( R.yx() == sinC );
00074   assert ( R.yy() == cosC );
00075   assert ( R.yz() == 0    );
00076   assert ( R.zx() == 0    );
00077   assert ( R.zy() == 0    );
00078   assert ( R.zz() == 1    );
00079 
00080   R = Rotation();               // copy constructor
00081   R.rotateZ(angC);
00082   R.rotateY(angB);
00083   R.rotateZ(angA);
00084   Rotation RR(R);
00085 
00086   assert ( std::abs(RR.xx() - cosA*cosB*cosC + sinA*sinC) < DEL );
00087   assert ( std::abs(RR.xy() + cosA*cosB*sinC + sinA*cosC) < DEL );
00088   assert ( std::abs(RR.xz() - cosA*sinB)                  < DEL );
00089   assert ( std::abs(RR.yx() - sinA*cosB*cosC - cosA*sinC) < DEL );
00090   assert ( std::abs(RR.yy() + sinA*cosB*sinC - cosA*cosC) < DEL );
00091   assert ( std::abs(RR.yz() - sinA*sinB)                  < DEL );
00092   assert ( std::abs(RR.zx() + sinB*cosC)                  < DEL );
00093   assert ( std::abs(RR.zy() - sinB*sinC)                  < DEL );
00094   assert ( std::abs(RR.zz() - cosB)                       < DEL );
00095 
00096   RR = Rotation();              // operator=, operator!=, operator==
00097   assert ( RR != R );
00098   RR = R;
00099   assert ( RR == R );
00100 
00101   assert ( R(0,0) == R.xx() );  // operator(i,j), operator[i][j] 
00102   assert ( R(0,1) == R.xy() );
00103   assert ( R(0,2) == R.xz() );
00104   assert ( R(1,0) == R.yx() );
00105   assert ( R(1,1) == R.yy() );
00106   assert ( R(1,2) == R.yz() );
00107   assert ( R(2,0) == R.zx() );
00108   assert ( R(2,1) == R.zy() );
00109   assert ( R(2,2) == R.zz() );
00110 
00111   for(i=0; i<3; i++) { 
00112     for(k=0; k<3; k++) { 
00113       assert ( RR(i,k) == R[i][k] );
00114     }
00115   }
00116 
00117   Rotation A, B ,C;                                // operator*= 
00118   A.rotateZ(angA);
00119   B.rotateY(angB);
00120   C.rotateZ(angC);
00121   R  = A; R *= B; R *= C;
00122 
00123   Vector V(1,2,3);                                 // operator* (Vector) 
00124   V = R * V;
00125   assert ( std::abs(V.x()-R.xx()-2.*R.xy()-3.*R.xz()) < DEL );
00126   assert ( std::abs(V.y()-R.yx()-2.*R.yy()-3.*R.yz()) < DEL );
00127   assert ( std::abs(V.z()-R.zx()-2.*R.zy()-3.*R.zz()) < DEL );
00128 
00129   R = A * B * C;                                  // operator*(Matrix)
00130   assert ( std::abs(RR.xx() - R.xx()) < DEL );
00131   assert ( std::abs(RR.xy() - R.xy()) < DEL );
00132   assert ( std::abs(RR.xz() - R.xz()) < DEL );
00133   assert ( std::abs(RR.yx() - R.yx()) < DEL );
00134   assert ( std::abs(RR.yy() - R.yy()) < DEL );
00135   assert ( std::abs(RR.yz() - R.yz()) < DEL );
00136   assert ( std::abs(RR.zx() - R.zx()) < DEL );
00137   assert ( std::abs(RR.zy() - R.zy()) < DEL );
00138   assert ( std::abs(RR.zz() - R.zz()) < DEL );
00139 
00140   R = C;                                           // transform()
00141   R.transform(B);
00142   R.transform(A); 
00143   assert ( std::abs(RR.xx() - R.xx()) < DEL );
00144   assert ( std::abs(RR.xy() - R.xy()) < DEL );
00145   assert ( std::abs(RR.xz() - R.xz()) < DEL );
00146   assert ( std::abs(RR.yx() - R.yx()) < DEL );
00147   assert ( std::abs(RR.yy() - R.yy()) < DEL );
00148   assert ( std::abs(RR.yz() - R.yz()) < DEL );
00149   assert ( std::abs(RR.zx() - R.zx()) < DEL );
00150   assert ( std::abs(RR.zy() - R.zy()) < DEL );
00151   assert ( std::abs(RR.zz() - R.zz()) < DEL );
00152 
00153   R = RR.inverse();                                // inverse()
00154   for(i=0; i<3; i++) { 
00155     for(k=0; k<3; k++) { 
00156       assert ( RR(i,k) == R[k][i] );
00157     }
00158   }
00159 
00160   R.invert();                                      // invert() 
00161   assert ( RR == R );
00162 
00163   R = Rotation();                                  // rotateAxes()
00164   R.rotateAxes( Vector(RR.xx(), RR.yx(), RR.zx()),
00165                 Vector(RR.xy(), RR.yy(), RR.zy()),
00166                 Vector(RR.xz(), RR.yz(), RR.zz()) );
00167   assert ( RR == R );
00168 
00169   double ang=CLHEP::twopi/9.;                           // rotate()
00170   R = Rotation();
00171   R.rotate(ang, V);
00172 
00173   RR = Rotation();
00174   RR.rotateZ(-(V.phi()));
00175   RR.rotateY(-(V.theta()));
00176   RR.rotateZ(ang);
00177   RR.rotateY(V.theta());
00178   RR.rotateZ(V.phi());
00179 
00180   assert ( std::abs(RR.xx() - R.xx()) < DEL );
00181   assert ( std::abs(RR.xy() - R.xy()) < DEL );
00182   assert ( std::abs(RR.xz() - R.xz()) < DEL );
00183   assert ( std::abs(RR.yx() - R.yx()) < DEL );
00184   assert ( std::abs(RR.yy() - R.yy()) < DEL );
00185   assert ( std::abs(RR.yz() - R.yz()) < DEL );
00186   assert ( std::abs(RR.zx() - R.zx()) < DEL );
00187   assert ( std::abs(RR.zy() - R.zy()) < DEL );
00188   assert ( std::abs(RR.zz() - R.zz()) < DEL );
00189 
00190   Vector Vu = V.unit();                           // getAngleAxis
00191   R.getAngleAxis(ang, V);
00192   assert ( std::abs(ang   - CLHEP::twopi/9.) < DEL );
00193   assert ( std::abs(V.x() - Vu.x())     < DEL );
00194   assert ( std::abs(V.y() - Vu.y())     < DEL );
00195   assert ( std::abs(V.z() - Vu.z())     < DEL );
00196 
00197   assert ( std::abs(RR.phiX()-std::atan2(RR.yx(),RR.xx())) < DEL ); // phiX()
00198   assert ( std::abs(RR.phiY()-std::atan2(RR.yy(),RR.xy())) < DEL ); // phiY()
00199   assert ( std::abs(RR.phiZ()-std::atan2(RR.yz(),RR.xz())) < DEL ); // phiZ()
00200 
00201   assert ( std::abs(RR.thetaX()-std::acos(RR.zx())) < DEL );        // thetaX()
00202   assert ( std::abs(RR.thetaY()-std::acos(RR.zy())) < DEL );        // thetaY()
00203   assert ( std::abs(RR.thetaZ()-std::acos(RR.zz())) < DEL );        // thetaZ()
00204 
00205   return 0;
00206 }           

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7