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 methods of the HepRotationY class which
7 // were introduced when ZOOM PhysicsVectors was merged in.
8 //
9 
10 #ifdef GNUPRAGMA
11 #pragma implementation
12 #endif
13 
14 #include "CLHEP/Vector/defs.h"
15 #include "CLHEP/Vector/RotationY.h"
16 #include "CLHEP/Vector/AxisAngle.h"
17 #include "CLHEP/Vector/EulerAngles.h"
18 #include "CLHEP/Vector/LorentzRotation.h"
19 #include "CLHEP/Units/PhysicalConstants.h"
20 
21 #include <cmath>
22 #include <stdlib.h>
23 #include <iostream>
24 
25 namespace CLHEP  {
26 
safe_acos(double x)27 static inline double safe_acos (double x) {
28   if (std::abs(x) <= 1.0) return std::acos(x);
29   return ( (x>0) ? 0 : CLHEP::pi );
30 }
31 
HepRotationY(double ddelta)32 HepRotationY::HepRotationY(double ddelta) :
33 		its_d(proper(ddelta)), its_s(std::sin(ddelta)), its_c(std::cos(ddelta))
34 {}
35 
set(double ddelta)36 HepRotationY & HepRotationY::set ( double ddelta ) {
37   its_d = proper(ddelta);
38   its_s = std::sin(its_d);
39   its_c = std::cos(its_d);
40   return *this;
41 }
42 
phi() const43 double  HepRotationY::phi() const {
44   if ( its_d == 0 ) {
45     return 0;
46   } else if ( (its_d < 0) || (its_d == CLHEP::pi) )  {
47     return +CLHEP::halfpi;
48   } else {
49     return -CLHEP::halfpi;
50   }
51 }  // HepRotationY::phi()
52 
theta() const53 double  HepRotationY::theta() const {
54   return  std::fabs( its_d );
55 }  // HepRotationY::theta()
56 
psi() const57 double  HepRotationY::psi() const {
58   if ( its_d == 0 ) {
59     return 0;
60   } else if ( (its_d < 0) || (its_d == CLHEP::pi) )  {
61     return -CLHEP::halfpi;
62   } else {
63     return +CLHEP::halfpi;
64   }
65 }  // HepRotationY::psi()
66 
eulerAngles() const67 HepEulerAngles HepRotationY::eulerAngles() const {
68   return HepEulerAngles(  phi(),  theta(),  psi() );
69 }  // HepRotationY::eulerAngles()
70 
71 
72 // From the defining code in the implementation of CLHEP (in Rotation.cc)
73 // it is clear that thetaX, phiX form the polar angles in the original
74 // coordinate system of the new X axis (and similarly for phiY and phiZ).
75 //
76 // This code is taken directly from the original CLHEP. However, there are as
77 // shown opportunities for significant speed improvement.
78 
phiX() const79 double HepRotationY::phiX() const {
80   return (yx() == 0.0 && xx() == 0.0) ? 0.0 : std::atan2(yx(),xx());
81   		// or ---- return 0;
82 }
83 
phiY() const84 double HepRotationY::phiY() const {
85   return (yy() == 0.0 && xy() == 0.0) ? 0.0 : std::atan2(yy(),xy());
86 		// or ----  return CLHEP::halfpi;
87 }
88 
phiZ() const89 double HepRotationY::phiZ() const {
90   return (yz() == 0.0 && xz() == 0.0) ? 0.0 : std::atan2(yz(),xz());
91 		// or ----  return 0;
92 }
93 
thetaX() const94 double HepRotationY::thetaX() const {
95   return safe_acos(zx());
96 }
97 
thetaY() const98 double HepRotationY::thetaY() const {
99   return safe_acos(zy());
100 		// or ----  return CLHEP::halfpi;
101 }
102 
thetaZ() const103 double HepRotationY::thetaZ() const {
104   return safe_acos(zz());
105 		// or ---- return d;
106 }
107 
setDelta(double ddelta)108 void HepRotationY::setDelta ( double ddelta ) {
109   set(ddelta);
110 }
111 
decompose(HepAxisAngle & rotation,Hep3Vector & boost) const112 void HepRotationY::decompose
113 	(HepAxisAngle & rotation, Hep3Vector & boost) const {
114   boost.set(0,0,0);
115   rotation = axisAngle();
116 }
117 
decompose(Hep3Vector & boost,HepAxisAngle & rotation) const118 void HepRotationY::decompose
119 	(Hep3Vector & boost, HepAxisAngle & rotation) const {
120   boost.set(0,0,0);
121   rotation = axisAngle();
122 }
123 
decompose(HepRotation & rotation,HepBoost & boost) const124 void HepRotationY::decompose
125         (HepRotation & rotation, HepBoost & boost) const {
126   boost.set(0,0,0);
127   rotation = HepRotation(*this);
128 }
129 
decompose(HepBoost & boost,HepRotation & rotation) const130 void HepRotationY::decompose
131         (HepBoost & boost, HepRotation & rotation) const {
132   boost.set(0,0,0);
133   rotation = HepRotation(*this);
134 }
135 
distance2(const HepRotationY & r) const136 double HepRotationY::distance2( const HepRotationY & r  ) const {
137   double answer = 2.0 * ( 1.0 - ( its_s * r.its_s + its_c * r.its_c ) ) ;
138   return (answer >= 0) ? answer : 0;
139 }
140 
distance2(const HepRotation & r) const141 double HepRotationY::distance2( const HepRotation & r  ) const {
142   double sum =        xx() * r.xx()          +  xz() * r.xz()
143 		   		       + r.yy()
144                        + zx() * r.zx()          + zz() * r.zz();
145   double answer = 3.0 - sum;
146   return (answer >= 0 ) ? answer : 0;
147 }
148 
distance2(const HepLorentzRotation & lt) const149 double HepRotationY::distance2( const HepLorentzRotation & lt  ) const {
150   HepAxisAngle a;
151   Hep3Vector   b;
152   lt.decompose(b, a);
153   double bet = b.beta();
154   double bet2 = bet*bet;
155   HepRotation r(a);
156   return bet2/(1-bet2) + distance2(r);
157 }
158 
distance2(const HepBoost & lt) const159 double HepRotationY::distance2( const HepBoost & lt ) const {
160   return distance2( HepLorentzRotation(lt));
161 }
162 
howNear(const HepRotationY & r) const163 double HepRotationY::howNear( const HepRotationY & r ) const {
164   return std::sqrt(distance2(r));
165 }
howNear(const HepRotation & r) const166 double HepRotationY::howNear( const HepRotation & r ) const {
167   return std::sqrt(distance2(r));
168 }
howNear(const HepBoost & lt) const169 double HepRotationY::howNear( const HepBoost & lt ) const {
170   return std::sqrt(distance2(lt));
171 }
howNear(const HepLorentzRotation & lt) const172 double HepRotationY::howNear( const HepLorentzRotation & lt ) const {
173   return std::sqrt(distance2(lt));
174 }
isNear(const HepRotationY & r,double epsilon) const175 bool HepRotationY::isNear(const HepRotationY & r,double epsilon)const{
176   return (distance2(r) <= epsilon*epsilon);
177 }
isNear(const HepRotation & r,double epsilon) const178 bool HepRotationY::isNear(const HepRotation & r,double epsilon)const {
179   return (distance2(r) <= epsilon*epsilon);
180 }
isNear(const HepBoost & lt,double epsilon) const181 bool HepRotationY::isNear( const HepBoost & lt,double epsilon) const {
182   return (distance2(lt) <= epsilon*epsilon);
183 }
isNear(const HepLorentzRotation & lt,double epsilon) const184 bool HepRotationY::isNear( const HepLorentzRotation & lt,
185                                      double epsilon) const {
186   return (distance2(lt) <= epsilon*epsilon);
187 }
188 
norm2() const189 double HepRotationY::norm2() const {
190   return 2.0 - 2.0 * its_c;
191 }
192 
print(std::ostream & os) const193 std::ostream & HepRotationY::print( std::ostream & os ) const {
194   os << "\nRotation about Y (" << its_d <<
195                 ") [cos d = " << its_c << " sin d = " << its_s << "]\n";
196   return os;
197 }
198 
199 }  // namespace CLHEP
200