Geant4  10.01.p02
UVector3.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UVector3
12 //
13 // 19.09.12 Marek Gayer
14 // Created from original implementation in CLHEP
15 // --------------------------------------------------------------------
16 
17 #include "UVector3.hh"
18 #include "UUtils.hh"
19 
20 //______________________________________________________________________________
21 UVector3::UVector3(double theta, double phi)
22 {
23  // Creates a unit vector based on theta and phi angles
24  x_ = std::sin(theta) * std::cos(phi);
25  y_ = std::sin(theta) * std::sin(phi);
26  z_ = std::cos(theta);
27 }
28 
29 //______________________________________________________________________________
30 double UVector3::Angle(const UVector3& q) const
31 {
32  // return the angle w.r.t. another 3-vector
33  double ptot2 = Mag2() * q.Mag2();
34  if (ptot2 <= 0)
35  {
36  return 0.0;
37  }
38  else
39  {
40  double arg = Dot(q) / std::sqrt(ptot2);
41  if (arg > 1.0) arg = 1.0;
42  if (arg < -1.0) arg = -1.0;
43  return UUtils::ACos(arg);
44  }
45 }
46 
47 //______________________________________________________________________________
48 double UVector3::Mag() const
49 {
50  // return the magnitude (rho in spherical coordinate system)
51 
52  return std::sqrt(Mag2());
53 }
54 
55 //______________________________________________________________________________
56 double UVector3::Perp() const
57 {
58  //return the transverse component (R in cylindrical coordinate system)
59 
60  return std::sqrt(Perp2());
61 }
62 
63 //______________________________________________________________________________
64 double UVector3::Phi() const
65 {
66  //return the azimuth angle. returns phi from -pi to pi
67  return x_ == 0.0 && y_ == 0.0 ? 0.0 : UUtils::ATan2(y_, x_);
68 }
69 
70 //______________________________________________________________________________
71 double UVector3::Theta() const
72 {
73  //return the polar angle from 0 to pi
74  double mag2 = Mag2();
75  if (mag2 == 0.0) return 0.0;
76  return UUtils::ACos(z_ / std::sqrt(mag2));
77 }
78 
79 //______________________________________________________________________________
81 {
82  // return unit vector parallel to this.
83  double tot = Mag2();
84  UVector3 p(x_, y_, z_);
85  return tot > 0.0 ? p *= (1.0 / std::sqrt(tot)) : p;
86 }
87 
88 //______________________________________________________________________________
90 {
91  // Normalize to unit. Return normalization factor.
92  double mag = Mag2();
93  if (mag == 0.0) return mag;;
94  mag = std::sqrt(mag);
95  x_ /= mag;
96  y_ /= mag;
97  z_ /= mag;
98  return mag;
99 }
100 
101 //______________________________________________________________________________
102 void UVector3::RotateX(double angle)
103 {
104  //rotate vector around X
105  double s = std::sin(angle);
106  double c = std::cos(angle);
107  double yy = y_;
108  y_ = c * yy - s * z_;
109  z_ = s * yy + c * z_;
110 }
111 
112 //______________________________________________________________________________
113 void UVector3::RotateY(double angle)
114 {
115  //rotate vector around Y
116  double s = std::sin(angle);
117  double c = std::cos(angle);
118  double zz = z_;
119  z_ = c * zz - s * x_;
120  x_ = s * zz + c * x_;
121 }
122 
123 //______________________________________________________________________________
124 void UVector3::RotateZ(double angle)
125 {
126  //rotate vector around Z
127  double s = std::sin(angle);
128  double c = std::cos(angle);
129  double xx = x_;
130  x_ = c * xx - s * y_;
131  y_ = s * xx + c * y_;
132 }
double Mag2() const
Definition: UVector3.hh:287
double ACos(double)
Definition: UUtils.hh:214
double y_
Definition: UVector3.hh:149
double Phi() const
Definition: UVector3.cc:64
double Normalize()
Definition: UVector3.cc:89
UVector3()
Definition: UVector3.hh:31
void RotateX(double)
Definition: UVector3.cc:102
void RotateZ(double)
Definition: UVector3.cc:124
static const double s
Definition: G4SIunits.hh:150
double ATan2(double, double)
Definition: UUtils.hh:222
double z_
Definition: UVector3.hh:150
double x_
Definition: UVector3.hh:148
double Dot(const UVector3 &) const
Definition: UVector3.hh:276
double Mag() const
Definition: UVector3.cc:48
double Angle(const UVector3 &) const
Definition: UVector3.cc:30
double Perp2() const
Definition: UVector3.hh:292
UVector3 Unit() const
Definition: UVector3.cc:80
void RotateY(double)
Definition: UVector3.cc:113
double Perp() const
Definition: UVector3.cc:56
double Theta() const
Definition: UVector3.cc:71