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

testBasicVector3D.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: testBasicVector3D.cc,v 1.5 2010/06/16 16:21:27 garren Exp $
00003 // ---------------------------------------------------------------------------
00004 
00005 #include <iostream>
00006 #include <assert.h>
00007 #include "CLHEP/Geometry/Point3D.h"
00008 #include "CLHEP/Geometry/Vector3D.h"
00009 #include "CLHEP/Geometry/Normal3D.h"
00010 #include "CLHEP/Geometry/Transform3D.h"
00011 #include "CLHEP/Units/PhysicalConstants.h"
00012 
00013 bool EQUAL(double a, double b) {
00014   double del = a - b;
00015   if (del < 0) del = -del;
00016   return del < 0.000001;
00017 }
00018 
00019 using namespace HepGeom;
00020 
00021 #define CHECK(point,type)                                         \
00022   /* Check default constructor */                                 \
00023   point p00;                                                      \
00024   assert(p00.x() == 0 && p00.y() == 0 && p00.z() == 0);           \
00025                                                                   \
00026   /* Check constructor from three numbers */                      \
00027   point p01(1,2,3);                                               \
00028   point p02(4.,5.,6.);                                            \
00029   assert(p01.x() == 1 && p01.y() == 2 && p01.z() == 3);           \
00030   assert(p02.x() == 4 && p02.y() == 5 && p02.z() == 6);           \
00031                                                                   \
00032   /* Check constructor from array */                              \
00033   float farray[] = {1,2,3};                                       \
00034   type  darray[] = {4,5,6};                                       \
00035   point p03(farray);                 assert(p03 == point(1,2,3)); \
00036   point p04(darray);                 assert(p04 == point(4,5,6)); \
00037                                                                   \
00038   /* Check conversion to array */                                 \
00039   const point p05(1,2,3);                                         \
00040   const type * a = p05;                                           \
00041   assert(a[0] == 1 && a[1] == 2 && a[2] == 3);                    \
00042   point p06(4,5,6);                                               \
00043   a = p06;                                                        \
00044   assert(a[0] == 4 && a[1] == 5 && a[2] == 6);                    \
00045   type * b = p06;                                                 \
00046   b[0] = 7;                                                       \
00047   b[1] = 8;                                                       \
00048   b[2] = 9;                          assert(p06 == point(7,8,9)); \
00049                                                                   \
00050   /* Check copy constructor */                                    \
00051   point p10(p01);                    assert(p10 == point(1,2,3)); \
00052   point p11(Point3D <float>(1,2,3)); assert(p11 == point(1,2,3)); \
00053   point p12(Vector3D<float>(4,5,6)); assert(p12 == point(4,5,6)); \
00054   point p13(Normal3D<float>(7,8,9)); assert(p13 == point(7,8,9)); \
00055   point p14(Point3D <type> (1,2,3)); assert(p14 == point(1,2,3)); \
00056   point p15(Vector3D<type> (4,5,6)); assert(p15 == point(4,5,6)); \
00057   point p16(Normal3D<type> (7,8,9)); assert(p16 == point(7,8,9)); \
00058                                                                   \
00059   /* Check assignment */                                          \
00060   point p20;                                                      \
00061   p20 = Point3D <float>(1,2,3);      assert(p20 == point(1,2,3)); \
00062   p20 = Vector3D<float>(4,5,6);      assert(p20 == point(4,5,6)); \
00063   p20 = Normal3D<float>(7,8,9);      assert(p20 == point(7,8,9)); \
00064   p20 = Point3D <type> (1,2,3);      assert(p20 == point(1,2,3)); \
00065   p20 = Vector3D<type> (4,5,6);      assert(p20 == point(4,5,6)); \
00066   p20 = Normal3D<type> (7,8,9);      assert(p20 == point(7,8,9)); \
00067                                                                   \
00068   /* Check arithmetic operations */                               \
00069   point p21(1,2,3);                                               \
00070   p21 += point(1,2,3);               assert(p21 == point(2,4,6)); \
00071   p21 += Point3D <float>(1,1,1);     assert(p21 == point(3,5,7)); \
00072   p21 += Vector3D<float>(1,1,1);     assert(p21 == point(4,6,8)); \
00073   p21 += Normal3D<float>(1,1,1);     assert(p21 == point(5,7,9)); \
00074   p21 -= point(1,2,3);               assert(p21 == point(4,5,6)); \
00075   p21 -= Point3D <type>(1,1,1);      assert(p21 == point(3,4,5)); \
00076   p21 -= Vector3D<type>(1,1,1);      assert(p21 == point(2,3,4)); \
00077   p21 -= Normal3D<type>(1,1,1);      assert(p21 == point(1,2,3)); \
00078   p21 *= 2;                          assert(p21 == point(2,4,6)); \
00079   p21 /= 2;                          assert(p21 == point(1,2,3)); \
00080   p21 *= 2.0f;                       assert(p21 == point(2,4,6)); \
00081   p21 /= 2.0f;                       assert(p21 == point(1,2,3)); \
00082   p21 *= 2.0;                        assert(p21 == point(2,4,6)); \
00083   p21 /= 2.0;                        assert(p21 == point(1,2,3)); \
00084                                                                   \
00085   /* Check subscripting */                                        \
00086   point p22(1,2,3);                                               \
00087   assert(p22(0) == 1 && p22(1) == 2 && p22(2) == 3);              \
00088   assert(p22[0] == 1 && p22[1] == 2 && p22[2] == 3);              \
00089   p22(0) = 4;                                                     \
00090   p22(1) = 5;                                                     \
00091   p22(2) = 6;                        assert(p22 == point(4,5,6)); \
00092   p22[0] = 7;                                                     \
00093   p22[1] = 8;                                                     \
00094   p22[2] = 9;                        assert(p22 == point(7,8,9)); \
00095                                                                   \
00096   /* Check carthesian coordinate system */                        \
00097   point p30;                                                      \
00098   p30.setX(1);                                                    \
00099   p30.setY(2);                                                    \
00100   p30.setZ(3);                       assert(p30 == point(1,2,3)); \
00101   p30.set(4,5,6);                                                 \
00102   assert(p30.x() == 4 && p30.y() == 5 && p30.z() == 6);           \
00103                                                                   \
00104   /* Check cylindrical coordinate system */                       \
00105   point p40(12,16,1);                assert(p40.perp2() == 400);  \
00106                                      assert(p40.perp()  ==  20);  \
00107                                      assert(p40.rho()   ==  20);  \
00108   p40.setPerp(5);                    assert(p40 == point(3,4,1)); \
00109   p40.set(0,0,1);                                                 \
00110   p40.setPerp(5);                    assert(p40 == point(0,0,1)); \
00111                                                                   \
00112   /* Check spherical coordinate system */                         \
00113   point p50(2,3,6);     assert(p50.mag2()     == 49);             \
00114                         assert(p50.mag()      ==  7);             \
00115                         assert(p50.r()        ==  7);             \
00116                         assert(p50.getR()     ==  7);             \
00117   point p51(0,0,1);     assert(p51.phi()      ==  0);             \
00118   point p52(2,4,5);     assert(EQUAL(p52.phi()     ,std::atan2(2.,1.))); \
00119                         assert(EQUAL(p52.getPhi()  ,std::atan2(2.,1.))); \
00120   point p53(0,0,0);     assert(p53.theta()    ==  0);             \
00121                         assert(p53.cosTheta() ==  1);             \
00122   point p54(3,4,10);    assert(EQUAL(p54.theta()   ,std::atan2(1.,2.))); \
00123                         assert(EQUAL(p54.getTheta(),std::atan2(1.,2.))); \
00124                         assert(EQUAL(p54.cosTheta(),std::sqrt(0.8)));  \
00125   point p55(2,3,6);                                               \
00126   p55.setMag(14);       assert(p55 == point(4,6,12));             \
00127   p55.setR(7);          assert(p55 == point(2,3,6));              \
00128   point p56 = p55;                                                \
00129   p56.setPhi(CLHEP::pi/6);   assert(EQUAL(p56.getPhi(),CLHEP::pi/6));       \
00130                         assert(EQUAL(p56.mag()   ,p55.mag()));    \
00131                         assert(EQUAL(p56.theta() ,p55.theta()));  \
00132   point p57 = p55;                                                \
00133   p57.setTheta(CLHEP::pi/3); assert(EQUAL(p57.cosTheta(),0.5));        \
00134                         assert(EQUAL(p57.mag()     ,p55.mag()));  \
00135                         assert(EQUAL(p57.phi()     ,p55.phi()));  \
00136                                                                   \
00137   /* Check pseudo-rapidity */                                     \
00138   point p60(2,3,6);                                               \
00139   point p61 = p60;                                                \
00140   p61.setEta(2);        assert(EQUAL(p61.pseudoRapidity(),2));    \
00141                         assert(EQUAL(p61.getEta()        ,2));    \
00142                         assert(EQUAL(p61.eta()           ,2));    \
00143                         assert(EQUAL(p61.mag()           ,7));    \
00144                         assert(EQUAL(p61.phi(),p60.phi()));       \
00145                                                                   \
00146   /* Check combination of two vectors */                          \
00147   point p70(1,2,3);     assert(p70.dot(p70)   == p70.mag2());     \
00148   point p71( 1,2, 3);                                             \
00149   point p72(-3,2,-1);   assert(p71.cross(p72) == point(-8,-8,8)); \
00150   point p73(3,4,0);     assert(p73.perp2(point(0,1,0)) == 9);     \
00151                         assert(p73.perp (point(1,0,0)) == 4);     \
00152   point p74(1,0,0);                                               \
00153   point p75(1,1,0);     assert(EQUAL(p74.angle(p75),CLHEP::pi/4));     \
00154                         assert(EQUAL(p75.angle(p00),CLHEP::pi/2));     \
00155                                                                   \
00156   /* Check related vectors */                                     \
00157   point p80(1,2,3);                                               \
00158   point p81 = p80.unit();       assert(EQUAL(p81.mag()   ,1));    \
00159   point p82 = p80.orthogonal(); assert(EQUAL(p82.dot(p81),0));    \
00160                                                                   \
00161   /* Check rotations */                                           \
00162   point p90(2,0.5,std::sqrt(3.)/2);                                     \
00163   p90.rotateX(CLHEP::pi/6);          assert(p90.x()    == 2);          \
00164                                 assert(EQUAL(p90.y(),0));         \
00165                                 assert(EQUAL(p90.z(),1));         \
00166   point p91(std::sqrt(3.)/2,2,0.5);                                     \
00167   p91.rotateY(CLHEP::pi/6);          assert(EQUAL(p91.x(),1));         \
00168                                 assert(p91.y()    == 2);          \
00169                                 assert(EQUAL(p91.z(),0));         \
00170   point p92(0.5,std::sqrt(3.)/2,2);                                     \
00171   p92.rotateZ(CLHEP::pi/6);          assert(EQUAL(p92.x(),0));         \
00172                                 assert(EQUAL(p92.y(),1));         \
00173                                 assert(p92.z()    == 2);          \
00174   point p93(1,1,std::sqrt(2.));                                         \
00175   p93.rotate(CLHEP::pi,Vector3D<float>(-1,-1,std::sqrt(2.)));                \
00176                                 assert(EQUAL(p93.x(),-1));        \
00177                                 assert(EQUAL(p93.y(),-1));        \
00178                                 assert(EQUAL(p93.z(),-std::sqrt(2.)));  \
00179                                                                   \
00180   /* Check transformations */                                     \
00181   point p100(1,1,std::sqrt(2.));                                        \
00182   Transform3D m;                                                  \
00183   m = Rotate3D(CLHEP::pi,Vector3D<float>(-1,-1,std::sqrt(2.)));              \
00184   p100.transform(m);            assert(EQUAL(p100.x(),-1));       \
00185                                 assert(EQUAL(p100.y(),-1));       \
00186                                 assert(EQUAL(p100.z(),-std::sqrt(2.))); \
00187                                                                   \
00188   /* Check input/output */                                        \
00189   point p110;                                                     \
00190   std::cin  >> p110;                                              \
00191   std::cout << p110 << std::endl;                                 \
00192                                                                   \
00193   /* Check non-member arithmetics */                              \
00194   point p120(-1,-2,-3);                                           \
00195   p120 = +p120;                 assert(p120 == point(-1,-2,-3));  \
00196   p120 = -p120;                 assert(p120 == point(1,2,3));     \
00197   point p121(1,2,3);                                              \
00198   p121 = p121 + Point3D <float>(1,1,1); assert(p121 == point(2,3,4));\
00199   p121 = p121 + Vector3D<float>(1,1,1); assert(p121 == point(3,4,5));\
00200   p121 = p121 + Normal3D<float>(1,1,1); assert(p121 == point(4,5,6));\
00201   p121 = p121 - Point3D <type> (1,1,1); assert(p121 == point(3,4,5));\
00202   p121 = p121 - Vector3D<type> (1,1,1); assert(p121 == point(2,3,4));\
00203   p121 = p121 - Normal3D<type> (1,1,1); assert(p121 == point(1,2,3));\
00204   p121 = p121 * 2;              assert(p121 == point(2,4,6));     \
00205   p121 = p121 / 2;              assert(p121 == point(1,2,3));     \
00206   p121 = p121 * 2.0f;           assert(p121 == point(2,4,6));     \
00207   p121 = p121 / 2.0f;           assert(p121 == point(1,2,3));     \
00208   p121 = p121 * 2.0;            assert(p121 == point(2,4,6));     \
00209   p121 = p121 / 2.0;            assert(p121 == point(1,2,3));     \
00210   p121 = 2    * p121;           assert(p121 == point(2,4,6));     \
00211   p121 = 0.5f * p121;           assert(p121 == point(1,2,3));     \
00212   p121 = 2.0  * p121;           assert(p121 == point(2,4,6));     \
00213   assert(p121 *  p121 == p121.mag2());                            \
00214   assert(p121 *  Point3D <float>(1,1,1) == 12);                   \
00215   assert(p121 *  Vector3D<float>(1,1,1) == 12);                   \
00216   assert(p121 *  Normal3D<float>(1,1,1) == 12);                   \
00217   assert(p121 == Point3D <float>(2,4,6));                         \
00218   assert(p121 == Vector3D<float>(2,4,6));                         \
00219   assert(p121 == Normal3D<float>(2,4,6));                         \
00220   assert(p121 != Point3D <type> (3,4,6));                         \
00221   assert(p121 != Vector3D<type> (2,5,6));                         \
00222   assert(p121 != Normal3D<type> (2,4,7));                         \
00223 
00224 void CheckPointFloat()   { CHECK(Point3D<float>  , float)  }
00225 void CheckVectorFloat()  { CHECK(Vector3D<float> , float)  }
00226 void CheckNormalFloat()  { CHECK(Normal3D<float> , float)  }
00227 void CheckPointDouble()  { CHECK(Point3D<double> , double) }
00228 void CheckVectorDouble() { CHECK(Vector3D<double>, double) }
00229 void CheckNormalDouble() { CHECK(Normal3D<double>, double) }
00230 
00231 int main()
00232 {
00233   CheckPointFloat();
00234   CheckVectorFloat();
00235   CheckNormalFloat();
00236   CheckPointDouble();
00237   CheckVectorDouble();
00238   CheckNormalDouble();
00239   return 0;
00240 }

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7