CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
Rotation.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: Rotation.cc,v 1.4 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 the implementation of the parts of the the HepRotation class which
8// were present in the original CLHEP before the merge with ZOOM PhysicsVectors.
9//
10
11#include "CLHEP/Vector/defs.h"
12#include "CLHEP/Vector/Rotation.h"
13#include "CLHEP/Units/PhysicalConstants.h"
14
15#include <iostream>
16#include <cmath>
17
18namespace CLHEP {
19
20static inline double safe_acos (double x) {
21 if (std::abs(x) <= 1.0) return std::acos(x);
22 return ( (x>0) ? 0 : CLHEP::pi );
23}
24
25double HepRotation::operator() (int i, int j) const {
26 if (i == 0) {
27 if (j == 0) { return xx(); }
28 if (j == 1) { return xy(); }
29 if (j == 2) { return xz(); }
30 } else if (i == 1) {
31 if (j == 0) { return yx(); }
32 if (j == 1) { return yy(); }
33 if (j == 2) { return yz(); }
34 } else if (i == 2) {
35 if (j == 0) { return zx(); }
36 if (j == 1) { return zy(); }
37 if (j == 2) { return zz(); }
38 }
39 std::cerr << "HepRotation subscripting: bad indices "
40 << "(" << i << "," << j << ")" << std::endl;
41 return 0.0;
42}
43
44HepRotation & HepRotation::rotate(double a, const Hep3Vector& aaxis) {
45 if (a != 0.0) {
46 double ll = aaxis.mag();
47 if (ll == 0.0) {
48 ZMthrowC (ZMxpvZeroVector("HepRotation: zero axis"));
49 }else{
50 double sa = std::sin(a), ca = std::cos(a);
51 double dx = aaxis.x()/ll, dy = aaxis.y()/ll, dz = aaxis.z()/ll;
52 HepRotation m1(
53 ca+(1-ca)*dx*dx, (1-ca)*dx*dy-sa*dz, (1-ca)*dx*dz+sa*dy,
54 (1-ca)*dy*dx+sa*dz, ca+(1-ca)*dy*dy, (1-ca)*dy*dz-sa*dx,
55 (1-ca)*dz*dx-sa*dy, (1-ca)*dz*dy+sa*dx, ca+(1-ca)*dz*dz );
56 transform(m1);
57 }
58 }
59 return *this;
60}
61
63 double c1 = std::cos(a);
64 double s1 = std::sin(a);
65 double x1 = ryx, y1 = ryy, z1 = ryz;
66 ryx = c1*x1 - s1*rzx;
67 ryy = c1*y1 - s1*rzy;
68 ryz = c1*z1 - s1*rzz;
69 rzx = s1*x1 + c1*rzx;
70 rzy = s1*y1 + c1*rzy;
71 rzz = s1*z1 + c1*rzz;
72 return *this;
73}
74
76 double c1 = std::cos(a);
77 double s1 = std::sin(a);
78 double x1 = rzx, y1 = rzy, z1 = rzz;
79 rzx = c1*x1 - s1*rxx;
80 rzy = c1*y1 - s1*rxy;
81 rzz = c1*z1 - s1*rxz;
82 rxx = s1*x1 + c1*rxx;
83 rxy = s1*y1 + c1*rxy;
84 rxz = s1*z1 + c1*rxz;
85 return *this;
86}
87
89 double c1 = std::cos(a);
90 double s1 = std::sin(a);
91 double x1 = rxx, y1 = rxy, z1 = rxz;
92 rxx = c1*x1 - s1*ryx;
93 rxy = c1*y1 - s1*ryy;
94 rxz = c1*z1 - s1*ryz;
95 ryx = s1*x1 + c1*ryx;
96 ryy = s1*y1 + c1*ryy;
97 ryz = s1*z1 + c1*ryz;
98 return *this;
99}
100
102 const Hep3Vector &newY,
103 const Hep3Vector &newZ) {
104 double del = 0.001;
105 Hep3Vector w = newX.cross(newY);
106
107 if (std::abs(newZ.x()-w.x()) > del ||
108 std::abs(newZ.y()-w.y()) > del ||
109 std::abs(newZ.z()-w.z()) > del ||
110 std::abs(newX.mag2()-1.) > del ||
111 std::abs(newY.mag2()-1.) > del ||
112 std::abs(newZ.mag2()-1.) > del ||
113 std::abs(newX.dot(newY)) > del ||
114 std::abs(newY.dot(newZ)) > del ||
115 std::abs(newZ.dot(newX)) > del) {
116 std::cerr << "HepRotation::rotateAxes: bad axis vectors" << std::endl;
117 return *this;
118 }else{
119 return transform(HepRotation(newX.x(), newY.x(), newZ.x(),
120 newX.y(), newY.y(), newZ.y(),
121 newX.z(), newY.z(), newZ.z()));
122 }
123}
124
125double HepRotation::phiX() const {
126 return (yx() == 0.0 && xx() == 0.0) ? 0.0 : std::atan2(yx(),xx());
127}
128
129double HepRotation::phiY() const {
130 return (yy() == 0.0 && xy() == 0.0) ? 0.0 : std::atan2(yy(),xy());
131}
132
133double HepRotation::phiZ() const {
134 return (yz() == 0.0 && xz() == 0.0) ? 0.0 : std::atan2(yz(),xz());
135}
136
137double HepRotation::thetaX() const {
138 return safe_acos(zx());
139}
140
141double HepRotation::thetaY() const {
142 return safe_acos(zy());
143}
144
145double HepRotation::thetaZ() const {
146 return safe_acos(zz());
147}
148
149void HepRotation::getAngleAxis(double &angle, Hep3Vector &aaxis) const {
150 double cosa = 0.5*(xx()+yy()+zz()-1);
151 double cosa1 = 1-cosa;
152 if (cosa1 <= 0) {
153 angle = 0;
154 aaxis = Hep3Vector(0,0,1);
155 }else{
156 double x=0, y=0, z=0;
157 if (xx() > cosa) x = std::sqrt((xx()-cosa)/cosa1);
158 if (yy() > cosa) y = std::sqrt((yy()-cosa)/cosa1);
159 if (zz() > cosa) z = std::sqrt((zz()-cosa)/cosa1);
160 if (zy() < yz()) x = -x;
161 if (xz() < zx()) y = -y;
162 if (yx() < xy()) z = -z;
163 angle = (cosa < -1.) ? std::acos(-1.) : std::acos(cosa);
164 aaxis = Hep3Vector(x,y,z);
165 }
166}
167
169 return (rxx == 1.0 && rxy == 0.0 && rxz == 0.0 &&
170 ryx == 0.0 && ryy == 1.0 && ryz == 0.0 &&
171 rzx == 0.0 && rzy == 0.0 && rzz == 1.0) ? true : false;
172}
173
174int HepRotation::compare ( const HepRotation & r ) const {
175 if (rzz<r.rzz) return -1; else if (rzz>r.rzz) return 1;
176 else if (rzy<r.rzy) return -1; else if (rzy>r.rzy) return 1;
177 else if (rzx<r.rzx) return -1; else if (rzx>r.rzx) return 1;
178 else if (ryz<r.ryz) return -1; else if (ryz>r.ryz) return 1;
179 else if (ryy<r.ryy) return -1; else if (ryy>r.ryy) return 1;
180 else if (ryx<r.ryx) return -1; else if (ryx>r.ryx) return 1;
181 else if (rxz<r.rxz) return -1; else if (rxz>r.rxz) return 1;
182 else if (rxy<r.rxy) return -1; else if (rxy>r.rxy) return 1;
183 else if (rxx<r.rxx) return -1; else if (rxx>r.rxx) return 1;
184 else return 0;
185}
186
187
189
190} // namespace CLHEP
191
192
#define ZMthrowC(A)
Definition: ZMxpv.h:133
double z() const
double x() const
double mag2() const
double y() const
Hep3Vector cross(const Hep3Vector &) const
double dot(const Hep3Vector &) const
double mag() const
double operator()(int, int) const
Definition: Rotation.cc:25
double zz() const
double yz() const
HepRotation & rotateAxes(const Hep3Vector &newX, const Hep3Vector &newY, const Hep3Vector &newZ)
Definition: Rotation.cc:101
double zx() const
double thetaY() const
Definition: Rotation.cc:141
HepRotation & rotate(double delta, const Hep3Vector &axis)
Definition: Rotation.cc:44
bool isIdentity() const
Definition: Rotation.cc:168
HepRotation & transform(const HepRotation &r)
double phiY() const
Definition: Rotation.cc:129
double yx() const
static const HepRotation IDENTITY
Definition: Rotation.h:368
double zy() const
double thetaX() const
Definition: Rotation.cc:137
void getAngleAxis(double &delta, Hep3Vector &axis) const
Definition: Rotation.cc:149
double xx() const
HepRotation & rotateX(double delta)
Definition: Rotation.cc:62
double phiX() const
Definition: Rotation.cc:125
HepRotation & rotateZ(double delta)
Definition: Rotation.cc:88
double yy() const
double xz() const
HepRotation & rotateY(double delta)
Definition: Rotation.cc:75
double thetaZ() const
Definition: Rotation.cc:145
int compare(const HepRotation &r) const
Definition: Rotation.cc:174
double xy() const
double phiZ() const
Definition: Rotation.cc:133