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

Generated on Thu Jul 1 22:02:31 2010 for CLHEP by  doxygen 1.4.7