CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testRotationAxis.cc
Go to the documentation of this file.
1#include "CLHEP/Units/SystemOfUnits.h"
2#include "CLHEP/Vector/ThreeVector.h"
3#include "CLHEP/Vector/Rotation.h"
4#include <assert.h>
5#include <cmath>
6#include <iostream>
7
8bool equal(double a, double b) {
9 const double eps = 1e-9;
10 return (std::abs(a - b) < eps);
11}
12
13bool equal(const CLHEP::Hep3Vector& a, const CLHEP::Hep3Vector& b) {
14 const double eps = 1e-10;
15 return (std::abs(a.x() - b.x()) < eps &&
16 std::abs(a.y() - b.y()) < eps &&
17 std::abs(a.z() - b.z()) < eps);
18}
19
20using namespace CLHEP;
21
22int main()
23{
24 std::cout << "\n=== Check rotation around specified axes" << std::endl;
25
26 HepRotation rot;
27 Hep3Vector axis;
28
29 std::cout << "Zero rotation - OK" << std::endl;
30 axis.set(0,0,1);
31 rot.set(HepRep3x3( 1, 0, 0,
32 0, 1, 0,
33 0, 0, 1 ));
34 assert(equal(rot.delta(),0));
35 assert(equal(rot.axis(),axis));
36
37 std::cout << "Rotation by 180 degrees around X - OK" << std::endl;
38 axis.set(1,0,0);
39 rot.set(HepRep3x3( 1, 0, 0,
40 0,-1, 0,
41 0, 0,-1 ));
42 assert(equal(rot.delta(),pi));
43 assert(equal(rot.axis(),axis) || equal(rot.axis(),-axis));
44
45 std::cout << "Rotation by 180 degrees around Y - OK" << std::endl;
46 axis.set(0,1,0);
47 rot.set(HepRep3x3(-1, 0, 0,
48 0, 1, 0,
49 0, 0,-1 ));
50 assert(equal(rot.delta(),pi));
51 assert(equal(rot.axis(),axis) || equal(rot.axis(),-axis));
52
53 std::cout << "Rotation by 180 degrees around Z - OK" << std::endl;
54 axis.set(0,0,1);
55 rot.set(HepRep3x3(-1, 0, 0,
56 0,-1, 0,
57 0, 0, 1 ));
58 assert(equal(rot.delta(),pi));
59 assert(equal(rot.axis(),axis) || equal(rot.axis(),-axis));
60
61 std::cout << "Rotation by 180 degrees around XY - OK" << std::endl;
62 axis = Hep3Vector(1,1,0).unit();
63 rot.set(HepRep3x3( 0, 1, 0,
64 1, 0, 0,
65 0, 0,-1 ));
66 assert(equal(rot.delta(),pi));
67 assert(equal(rot.axis(),axis) || equal(rot.axis(),-axis));
68
69 std::cout << "Rotation by 180 degrees around YZ - OK" << std::endl;
70 axis = Hep3Vector(0,1,1).unit();
71 rot.set(HepRep3x3(-1, 0, 0,
72 0, 0, 1,
73 0, 1, 0 ));
74 assert(equal(rot.delta(),pi));
75 assert(equal(rot.axis(),axis) || equal(rot.axis(),-axis));
76
77 std::cout << "Rotation by 180 degrees around ZX - OK" << std::endl;
78 axis = Hep3Vector(1,0,1).unit();
79 rot.set(HepRep3x3( 0, 0, 1,
80 0,-1, 0,
81 1, 0, 0 ));
82 assert(equal(rot.delta(),pi));
83 assert(equal(rot.axis(),axis) || equal(rot.axis(),-axis));
84
85 //-------------------------------------------------------
86 //
87 CLHEP::HepRotation matrix;
88 CLHEP::Hep3Vector vinput, voutput;
89
90 int step = 1;
91 std::cout << "\n=== Check all possible axes and angles (step = " << step << " degree)" << std::endl;
92
93 for ( int the = 0; the <= 180; the += step) {
94 for ( int phi = 0; phi < 360; phi += step) {
95 for (int a = 0; a < 360; a += step) {
96 double z = std::cos(the*deg);
97 double y = std::sin(the*deg)*sin(phi*deg);
98 double x = std::sin(the*deg)*cos(phi*deg);
99 double ang = a*deg;
100
101 // Set rotation matrix
102 vinput.set(x, y, z);
103 matrix.set(vinput, ang);
104
105 // Get axis/angle
106 voutput = matrix.axis();
107 double del = matrix.delta();
108 if (a > 180) {
109 del = CLHEP::twopi - del;
110 voutput = -voutput;
111 }
112
113 // Check angle
114 if (a != 180) assert( equal(ang,del) ); // at 180, precision can be less than eps
115
116 // Check axis
117 if (a == 0) continue; // no check, any axis is good
118 if (a == 180 ) {
119 assert( equal(vinput,voutput) || equal(-vinput,voutput));
120 } else {
121 assert( equal(vinput,voutput) );
122 }
123 }
124 }
125 }
126 std::cout << "OK" << std::endl;
127
128 //-------------------------------------------------------
129 //
130 int istep = 10000;
131 std::cout << "\n=== Check angles near 0 (step = 1/" << istep << " degree)" << std::endl;
132
133 for ( int the = 0; the <= 180; the += 5) {
134 for ( int phi = 0; phi < 360; phi += 5) {
135 for (int a = -istep; a <= istep; a += 1) {
136 double z = std::cos(the*deg);
137 double y = std::sin(the*deg)*sin(phi*deg);
138 double x = std::sin(the*deg)*cos(phi*deg);
139 double ang = 0. + a * (deg/istep);
140
141 // Set rotation matrix
142 vinput.set(x, y, z);
143 matrix.set(vinput, ang);
144
145 // Get axis/angle
146 voutput = matrix.axis();
147 double del = matrix.delta();
148 if (a < 0) {
149 del = -del;
150 voutput = -voutput;
151 }
152
153 // Check angle
154 assert( equal(ang,del) );
155
156 // Check axis
157 if (a == 0) continue; // no check, any axis is good
158 assert( equal(vinput,voutput) );
159 }
160 }
161 }
162 std::cout << "OK" << std::endl;
163
164 //-------------------------------------------------------
165 //
166 istep = 10000;
167 std::cout << "\n=== Check angles near 180 (step = 1/" << istep << " degree)" << std::endl;
168
169 for ( int the = 0; the <= 180; the += 5) {
170 for ( int phi = 0; phi < 360; phi += 5) {
171 for (int a = -istep; a <= istep; a += 1) {
172 double z = std::cos(the*deg);
173 double y = std::sin(the*deg)*sin(phi*deg);
174 double x = std::sin(the*deg)*cos(phi*deg);
175 double ang = pi + a * (deg/istep);
176
177 // Set rotation matrix
178 vinput.set(x, y, z);
179 matrix.set(vinput, ang);
180
181 // Get axis/angle
182 voutput = matrix.axis();
183 double del = matrix.delta();
184 if (a > 0) {
185 del = CLHEP::twopi - del;
186 voutput = -voutput;
187 }
188
189 // Check angle
190 if (a != 0) assert( equal(ang,del) ); // at 180, precision can be less than eps
191
192 // Check axis
193 if (a == 0 ) {
194 assert( equal(vinput,voutput) || equal(-vinput,voutput));
195 } else {
196 assert( equal(vinput,voutput) );
197 }
198 }
199 }
200 }
201 std::cout << "OK" << std::endl;
202
203 std::cout << std::endl;
204 return 0 ;
205}
double z() const
Hep3Vector unit() const
double x() const
double y() const
void set(double x, double y, double z)
Hep3Vector axis() const
Definition: RotationA.cc:76
double delta() const
Definition: RotationA.cc:63
HepRotation & set(const Hep3Vector &axis, double delta)
Definition: RotationA.cc:24
bool equal(double a, double b)
int main()