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