Geant4  10.00.p01
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 }
133 
135 {
136  return UVector3(a.x + b.x, a.y + b.y, a.z + b.z);
137 }
138 
140 {
141  return UVector3(a.x - b.x, a.y - b.y, a.z - b.z);
142 }
143 
144 UVector3 operator * (const UVector3& p, double a)
145 {
146  return UVector3(a * p.x, a * p.y, a * p.z);
147 }
148 
149 UVector3 operator / (const UVector3& p, double a)
150 {
151  a = 1. / a;
152  return UVector3(a * p.x, a * p.y, a * p.z);
153 }
154 
155 UVector3 operator * (double a, const UVector3& p)
156 {
157  return UVector3(a * p.x, a * p.y, a * p.z);
158 }
159 
160 double operator * (const UVector3& a, const UVector3& b)
161 {
162  return a.Dot(b);
163 }
164 
double Mag2() const
Definition: UVector3.hh:267
double ACos(double)
Definition: UUtils.hh:218
double Phi() const
Definition: UVector3.cc:64
double Normalize()
Definition: UVector3.cc:89
UVector3()
Definition: UVector3.hh:31
G4double a
Definition: TRTMaterials.hh:39
UVector3 operator/(const UVector3 &p, double a)
Definition: UVector3.cc:149
double x
Definition: UVector3.hh:136
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:226
double Dot(const UVector3 &) const
Definition: UVector3.hh:257
UVector3 operator+(const UVector3 &a, const UVector3 &b)
Definition: UVector3.cc:134
double Mag() const
Definition: UVector3.cc:48
double Angle(const UVector3 &) const
Definition: UVector3.cc:30
double Perp2() const
Definition: UVector3.hh:272
UVector3 Unit() const
Definition: UVector3.cc:80
void RotateY(double)
Definition: UVector3.cc:113
double Perp() const
Definition: UVector3.cc:56
double z
Definition: UVector3.hh:138
double y
Definition: UVector3.hh:137
double Theta() const
Definition: UVector3.cc:71
UVector3 operator-(const UVector3 &a, const UVector3 &b)
Definition: UVector3.cc:139
UVector3 operator*(const UVector3 &p, double a)
Definition: UVector3.cc:144