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

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