source: trunk/CLHEP/src/Rotation.cc@ 18

Last change on this file since 18 was 4, checked in by Pavel Demin, 17 years ago

first commit

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