CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testTransform3D.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: testTransform3D.cc,v 1.3 2003/10/24 21:39:45 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 the HepGeom::Transform3D class.
00008 //
00009 #include <assert.h>
00010 #include "CLHEP/Geometry/Transform3D.h"
00011 #include "CLHEP/Vector/Rotation.h"
00012 #include "CLHEP/Vector/ThreeVector.h"
00013 #include "CLHEP/Units/PhysicalConstants.h"
00014 
00015 typedef HepGeom::Scale3D           Scale;
00016 typedef HepGeom::Rotate3D          Rotation;
00017 typedef HepGeom::Translate3D       Translation;
00018 typedef HepGeom::Transform3D       Transformation;
00019 typedef HepGeom::Point3D<double>   Point;
00020 typedef HepGeom::Vector3D<double>  Vector;
00021 typedef HepGeom::Normal3D<double>  Normal;
00022 
00023 #define DEL 10.e-16
00024 
00025 int main() {
00026   int i,k;  
00027   double E[4][4] = {
00028     { 1, 0, 0, 0},
00029     { 0, 1, 0, 0},
00030     { 0, 0, 1, 0},
00031     { 0, 0, 0, 1}
00032   };
00033 
00034   // Default constructor
00035 
00036   Transformation M;
00037   for (i=0; i<4; i++) {
00038     for (k=0; k<4; k++) {
00039       assert ( M[i][k] == E[i][k] );
00040     }
00041   }
00042  assert ( M == Transformation::Identity );
00043 
00044   // Rotation + Translation
00045 
00046   HepRotation R;
00047   double angA=CLHEP::pi/3, angB=CLHEP::pi/4, angC=CLHEP::pi/6; 
00048 
00049   R.rotateX(angA); R.rotateY(angB); R.rotateZ(angC);
00050   const Hep3Vector D(1, 2, 3);
00051   M = Transformation(R,D);
00052 
00053   for (i=0; i<3; i++) {
00054     for (k=0; k<3; k++) { assert ( M[i][k] == R[i][k] ); }
00055   }
00056   assert ( M(0,3) == D.x() );
00057   assert ( M(1,3) == D.y() );
00058   assert ( M(2,3) == D.z() );
00059 
00060   // Transformation of point, vector, normal
00061 
00062   const Point  p0(1,1,1);
00063   const Vector v0(1,1,1);
00064   const Normal n0(1,1,1);
00065 
00066   Point p1 = M * p0;
00067   Point p2 = R*Hep3Vector(1,1,1) + D;
00068   assert( abs(p1.x()-p2.x()) < DEL );
00069   assert( abs(p1.y()-p2.y()) < DEL );
00070   assert( abs(p1.z()-p2.z()) < DEL );
00071 
00072   Vector v1 = M * v0;
00073   Normal n1 = M * n0;
00074   assert( abs(v1.x()-n1.x()) < DEL );
00075   assert( abs(v1.y()-n1.y()) < DEL );
00076   assert( abs(v1.z()-n1.z()) < DEL );
00077 
00078   // Transformation of basis
00079 
00080   p1 = Point(M[0][0]+D.x(), M[1][0]+D.y(), M[2][0]+D.z());
00081   p2 = Point(M[0][1]+D.x(), M[1][1]+D.y(), M[2][1]+D.z());
00082   Transformation T(Point(0,0,0), Point(1,0,0), Point(0,1,0), D, p1, p2);
00083 
00084   for (i=0; i<4; i++) {
00085     for (k=0; k<4; k++) { assert ( abs(M[i][k] - T[i][k]) < DEL ); }
00086   }
00087 
00088   // Set Identity
00089 
00090   T.setIdentity();
00091   for (i=0; i<4; i++) {
00092     for (k=0; k<4; k++) { assert ( T[i][k] == E[i][k] ); }
00093   }
00094 
00095   // Assignment, fortran-style subscripting 
00096 
00097   T = M;
00098   assert (T == M);
00099   for (i=0; i<4; i++) {
00100     for (k=0; k<4; k++) { assert ( T(i,k) == M[i][k] ); }
00101   }
00102 
00103   // Inversion
00104 
00105   T = M.inverse();
00106   assert (T != M);
00107   T = M * T;
00108   for (i=0; i<4; i++) {
00109     for (k=0; k<4; k++) { assert ( abs(T[i][k] - E[i][k]) < DEL ); }
00110   }
00111  
00112   T = M.inverse();
00113   T = T * M;
00114   for (i=0; i<4; i++) {
00115     for (k=0; k<4; k++) { assert ( abs(T[i][k] - E[i][k]) < DEL ); }
00116   }
00117 
00118   // Get Rotation
00119 
00120   HepRotation Q;
00121   Q = M.getRotation();
00122   for (i=0; i<3; i++) {
00123     for (k=0; k<3; k++) { assert ( R[i][k] == Q[i][k] ); }
00124   }
00125 
00126   // Get Translation
00127 
00128   Hep3Vector C;
00129   C = M.getTranslation();
00130   assert ( C.x() == D.x() );
00131   assert ( C.y() == D.y() );
00132   assert ( C.z() == D.z() );
00133 
00134   // Compound transformation
00135   // Get Decomposition
00136 
00137   Scale S(-2,3,4);
00138   M = Transformation(R,D)*S;
00139 
00140   Scale       SS;
00141   Rotation    RR;
00142   Translation TT;
00143   M.getDecomposition(SS,RR,TT);
00144 
00145   S = HepGeom::Scale3D(2,3,-4);
00146   T = TT*RR*SS;
00147   for (i=0; i<4; i++) {
00148     for (k=0; k<4; k++) {
00149       assert ( abs(S[i][k] - SS[i][k]) < DEL );
00150       assert ( abs(M[i][k] - T[i][k])  < DEL );
00151     }
00152   }
00153 
00154   // test for isNear()
00155 
00156   assert ( T.isNear(M, DEL) );
00157   S = HepGeom::Scale3D(2.01,3,-4);
00158   T = TT*RR*S;
00159   assert ( !T.isNear(M) );
00160 
00161   // Different conversions
00162 
00163   Hep3Vector www(1,2,3);
00164   Vector     vvv;
00165   Point      ppp(3,2,1);
00166   Normal     nnn;
00167 
00168   vvv = www;
00169   www = vvv;
00170   nnn = ppp;
00171 
00172   assert (vvv.x() == nnn.z()); 
00173   assert (vvv.y() == nnn.y()); 
00174   assert (vvv.z() == nnn.x()); 
00175 
00176   nnn = Normal(ppp);
00177   www = Hep3Vector(vvv);
00178 
00179   return 0;
00180 }           

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