1 // -*- C++ -*-
2 // $Id: testRotation.cc,v 1.3 2003/08/13 20:00:14 garren Exp $
3 // ---------------------------------------------------------------------------
4 //
5 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
6 //
7 // This is a test for HepRotation class.
8 //
9 #include "CLHEP/Units/GlobalSystemOfUnits.h"	// to see shadowing problems
10 #include "CLHEP/Units/GlobalPhysicalConstants.h"
11 #include "CLHEP/Vector/Rotation.h"
12 #include "CLHEP/Vector/ThreeVector.h"
13 #include <cassert>
14 #include <cmath>
15 #include <stdlib.h>
16 
17 using namespace CLHEP;
18 
19 typedef HepRotation Rotation;
20 typedef Hep3Vector  Vector;
21 
22 #define DEL 10.e-16
23 
24 // don't generate warnings about unused variables inside assert
main()25 int main() {
26   int i,k;
27   double angA=CLHEP::pi/3, angB=CLHEP::pi/4, angC=CLHEP::pi/6;
28   double cosA =std::cos(angA), sinA =std::sin(angA);
29   double cosB =std::cos(angB), sinB =std::sin(angB);
30   double cosC =std::cos(angC), sinC =std::sin(angC);
31 
32   Rotation R;                   // default constructor
33   assert ( R.xx() == 1 );
34   assert ( R.xy() == 0 );
35   assert ( R.xz() == 0 );
36   assert ( R.yx() == 0 );
37   assert ( R.yy() == 1 );
38   assert ( R.yz() == 0 );
39   assert ( R.zx() == 0 );
40   assert ( R.zy() == 0 );
41   assert ( R.zz() == 1 );
42 
43   assert( R.isIdentity() );     // isIdentity()
44 
45   R = Rotation();               // rotateX()
46   R.rotateX(angA);
47   assert ( R.xx() == 1    );
48   assert ( R.xy() == 0    );
49   assert ( R.xz() == 0    );
50   assert ( R.yx() == 0    );
51   assert ( R.yy() == cosA );
52   assert ( R.yz() ==-sinA );
53   assert ( R.zx() == 0    );
54   assert ( R.zy() == sinA );
55   assert ( R.zz() == cosA );
56 
57   R = Rotation();               // rotateY()
58   R.rotateY(angB);
59   assert ( R.xx() == cosB );
60   assert ( R.xy() == 0    );
61   assert ( R.xz() == sinB );
62   assert ( R.yx() == 0    );
63   assert ( R.yy() == 1    );
64   assert ( R.yz() == 0    );
65   assert ( R.zx() ==-sinB );
66   assert ( R.zy() == 0    );
67   assert ( R.zz() == cosB );
68 
69   R = Rotation();               // rotateZ()
70   R.rotateZ(angC);
71   assert ( R.xx() == cosC );
72   assert ( R.xy() ==-sinC );
73   assert ( R.xz() == 0    );
74   assert ( R.yx() == sinC );
75   assert ( R.yy() == cosC );
76   assert ( R.yz() == 0    );
77   assert ( R.zx() == 0    );
78   assert ( R.zy() == 0    );
79   assert ( R.zz() == 1    );
80 
81   R = Rotation();               // copy constructor
82   R.rotateZ(angC);
83   R.rotateY(angB);
84   R.rotateZ(angA);
85   Rotation RR(R);
86 
87   assert ( std::abs(RR.xx() - cosA*cosB*cosC + sinA*sinC) < DEL );
88   assert ( std::abs(RR.xy() + cosA*cosB*sinC + sinA*cosC) < DEL );
89   assert ( std::abs(RR.xz() - cosA*sinB)                  < DEL );
90   assert ( std::abs(RR.yx() - sinA*cosB*cosC - cosA*sinC) < DEL );
91   assert ( std::abs(RR.yy() + sinA*cosB*sinC - cosA*cosC) < DEL );
92   assert ( std::abs(RR.yz() - sinA*sinB)                  < DEL );
93   assert ( std::abs(RR.zx() + sinB*cosC)                  < DEL );
94   assert ( std::abs(RR.zy() - sinB*sinC)                  < DEL );
95   assert ( std::abs(RR.zz() - cosB)                       < DEL );
96 
97   RR = Rotation();              // operator=, operator!=, operator==
98   assert ( RR != R );
99   RR = R;
100   assert ( RR == R );
101 
102   assert ( R(0,0) == R.xx() );  // operator(i,j), operator[i][j]
103   assert ( R(0,1) == R.xy() );
104   assert ( R(0,2) == R.xz() );
105   assert ( R(1,0) == R.yx() );
106   assert ( R(1,1) == R.yy() );
107   assert ( R(1,2) == R.yz() );
108   assert ( R(2,0) == R.zx() );
109   assert ( R(2,1) == R.zy() );
110   assert ( R(2,2) == R.zz() );
111 
112   for(i=0; i<3; i++) {
113     for(k=0; k<3; k++) {
114       assert ( RR(i,k) == R[i][k] );
115     }
116   }
117 
118   Rotation A, B ,C;                                // operator*=
119   A.rotateZ(angA);
120   B.rotateY(angB);
121   C.rotateZ(angC);
122   R  = A; R *= B; R *= C;
123 
124   Vector V(1,2,3);                                 // operator* (Vector)
125   V = R * V;
126   assert ( std::abs(V.x()-R.xx()-2.*R.xy()-3.*R.xz()) < DEL );
127   assert ( std::abs(V.y()-R.yx()-2.*R.yy()-3.*R.yz()) < DEL );
128   assert ( std::abs(V.z()-R.zx()-2.*R.zy()-3.*R.zz()) < DEL );
129 
130   R = A * B * C;                                  // operator*(Matrix)
131   assert ( std::abs(RR.xx() - R.xx()) < DEL );
132   assert ( std::abs(RR.xy() - R.xy()) < DEL );
133   assert ( std::abs(RR.xz() - R.xz()) < DEL );
134   assert ( std::abs(RR.yx() - R.yx()) < DEL );
135   assert ( std::abs(RR.yy() - R.yy()) < DEL );
136   assert ( std::abs(RR.yz() - R.yz()) < DEL );
137   assert ( std::abs(RR.zx() - R.zx()) < DEL );
138   assert ( std::abs(RR.zy() - R.zy()) < DEL );
139   assert ( std::abs(RR.zz() - R.zz()) < DEL );
140 
141   R = C;                                           // transform()
142   R.transform(B);
143   R.transform(A);
144   assert ( std::abs(RR.xx() - R.xx()) < DEL );
145   assert ( std::abs(RR.xy() - R.xy()) < DEL );
146   assert ( std::abs(RR.xz() - R.xz()) < DEL );
147   assert ( std::abs(RR.yx() - R.yx()) < DEL );
148   assert ( std::abs(RR.yy() - R.yy()) < DEL );
149   assert ( std::abs(RR.yz() - R.yz()) < DEL );
150   assert ( std::abs(RR.zx() - R.zx()) < DEL );
151   assert ( std::abs(RR.zy() - R.zy()) < DEL );
152   assert ( std::abs(RR.zz() - R.zz()) < DEL );
153 
154   R = RR.inverse();                                // inverse()
155   for(i=0; i<3; i++) {
156     for(k=0; k<3; k++) {
157       assert ( RR(i,k) == R[k][i] );
158     }
159   }
160 
161   R.invert();                                      // invert()
162   assert ( RR == R );
163 
164   R = Rotation();                                  // rotateAxes()
165   R.rotateAxes( Vector(RR.xx(), RR.yx(), RR.zx()),
166 		Vector(RR.xy(), RR.yy(), RR.zy()),
167 		Vector(RR.xz(), RR.yz(), RR.zz()) );
168   assert ( RR == R );
169 
170   double ang=CLHEP::twopi/9.;                           // rotate()
171   R = Rotation();
172   R.rotate(ang, V);
173 
174   RR = Rotation();
175   RR.rotateZ(-(V.phi()));
176   RR.rotateY(-(V.theta()));
177   RR.rotateZ(ang);
178   RR.rotateY(V.theta());
179   RR.rotateZ(V.phi());
180 
181   assert ( std::abs(RR.xx() - R.xx()) < DEL );
182   assert ( std::abs(RR.xy() - R.xy()) < DEL );
183   assert ( std::abs(RR.xz() - R.xz()) < DEL );
184   assert ( std::abs(RR.yx() - R.yx()) < DEL );
185   assert ( std::abs(RR.yy() - R.yy()) < DEL );
186   assert ( std::abs(RR.yz() - R.yz()) < DEL );
187   assert ( std::abs(RR.zx() - R.zx()) < DEL );
188   assert ( std::abs(RR.zy() - R.zy()) < DEL );
189   assert ( std::abs(RR.zz() - R.zz()) < DEL );
190 
191   Vector Vu = V.unit();                           // getAngleAxis
192   R.getAngleAxis(ang, V);
193   assert ( std::abs(ang   - CLHEP::twopi/9.) < DEL );
194   assert ( std::abs(V.x() - Vu.x())     < DEL );
195   assert ( std::abs(V.y() - Vu.y())     < DEL );
196   assert ( std::abs(V.z() - Vu.z())     < DEL );
197 
198   assert ( std::abs(RR.phiX()-std::atan2(RR.yx(),RR.xx())) < DEL ); // phiX()
199   assert ( std::abs(RR.phiY()-std::atan2(RR.yy(),RR.xy())) < DEL ); // phiY()
200   assert ( std::abs(RR.phiZ()-std::atan2(RR.yz(),RR.xz())) < DEL ); // phiZ()
201 
202   assert ( std::abs(RR.thetaX()-std::acos(RR.zx())) < DEL );        // thetaX()
203   assert ( std::abs(RR.thetaY()-std::acos(RR.zy())) < DEL );        // thetaY()
204   assert ( std::abs(RR.thetaZ()-std::acos(RR.zz())) < DEL );        // thetaZ()
205 
206   return 0;
207 }
208