Geant4  10.00.p03
RotationA.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // ---------------------------------------------------------------------------
3 //
4 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
5 //
6 // This is the implementation of those methods of the HepRotation class which
7 // were introduced when ZOOM PhysicsVectors was merged in, and which involve
8 // the angle/axis representation of a Rotation.
9 //
10 
11 #ifdef GNUPRAGMA
12 #pragma implementation
13 #endif
14 
15 #include "CLHEP/Vector/Rotation.h"
16 #include "CLHEP/Units/PhysicalConstants.h"
17 
18 #include <iostream>
19 #include <cmath>
20 
21 namespace CLHEP {
22 
23 // ---------- Constructors and Assignment:
24 
25 // axis and angle
26 
27 HepRotation & HepRotation::set( const Hep3Vector & aaxis, double ddelta ) {
28 
29  double sinDelta = std::sin(ddelta), cosDelta = std::cos(ddelta);
30  double oneMinusCosDelta = 1.0 - cosDelta;
31 
32  Hep3Vector u = aaxis.unit();
33 
34  double uX = u.getX();
35  double uY = u.getY();
36  double uZ = u.getZ();
37 
38  rxx = oneMinusCosDelta * uX * uX + cosDelta;
39  rxy = oneMinusCosDelta * uX * uY - sinDelta * uZ;
40  rxz = oneMinusCosDelta * uX * uZ + sinDelta * uY;
41 
42  ryx = oneMinusCosDelta * uY * uX + sinDelta * uZ;
43  ryy = oneMinusCosDelta * uY * uY + cosDelta;
44  ryz = oneMinusCosDelta * uY * uZ - sinDelta * uX;
45 
46  rzx = oneMinusCosDelta * uZ * uX - sinDelta * uY;
47  rzy = oneMinusCosDelta * uZ * uY + sinDelta * uX;
48  rzz = oneMinusCosDelta * uZ * uZ + cosDelta;
49 
50  return *this;
51 
52 } // HepRotation::set(axis, delta)
53 
54 HepRotation::HepRotation ( const Hep3Vector & aaxis, double ddelta )
55 {
56  set( aaxis, ddelta );
57 }
58 HepRotation & HepRotation::set( const HepAxisAngle & ax ) {
59  return set ( ax.axis(), ax.delta() );
60 }
61 HepRotation::HepRotation ( const HepAxisAngle & ax )
62 {
63  set ( ax.axis(), ax.delta() );
64 }
65 
66 double HepRotation::delta() const {
67 
68  double cosdelta = (rxx + ryy + rzz - 1.0) / 2.0;
69  if (cosdelta > 1.0) {
70  return 0;
71  } else if (cosdelta < -1.0) {
72  return CLHEP::pi;
73  } else {
74  return std::acos( cosdelta ); // Already safe due to the cosdelta > 1 check
75  }
76 
77 } // delta()
78 
79 Hep3Vector HepRotation::axis () const {
80 
81  // Determine 2*std::sin(delta) times the u components (I call this uX, uY, Uz)
82  // Normalization is not needed; it will be done when returning the 3-Vector
83 
84  double Uz = ryx - rxy;
85  double Uy = rxz - rzx;
86  double Ux = rzy - ryz;
87 
88  if ( (Uz==0) && (Uy==0) && (Ux==0) ) {
89  if ( rzz>0 ) {
90  return Hep3Vector(0,0,1);
91  } else if ( ryy>0 ) {
92  return Hep3Vector(0,1,0);
93  } else {
94  return Hep3Vector(1,0,0);
95  }
96  } else {
97  return Hep3Vector( Ux, Uy, Uz ).unit();
98  }
99 
100 } // axis()
101 
102 HepAxisAngle HepRotation::axisAngle() const {
103 
104  return HepAxisAngle (axis(), delta());
105 
106 } // axisAngle()
107 
108 
109 void HepRotation::setAxis (const Hep3Vector & aaxis) {
110  set ( aaxis, delta() );
111 }
112 
113 void HepRotation::setDelta (double ddelta) {
114  set ( axis(), ddelta );
115 }
116 
117 } // namespace CLHEP
const G4double pi