Geant4  10.00.p01
UVector3.hh
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 // Class description:
14 //
15 // Bucket type for Vector type.
16 //
17 // 19.09.12 Marek Gayer
18 // Created from original implementation in CLHEP
19 // --------------------------------------------------------------------
20 
21 #ifndef USOLIDS_UVector3
22 #define USOLIDS_UVector3
23 
24 #include <cmath>
25 #include <iostream>
26 #include <fstream>
27 
28 struct UVector3
29 {
30  public:
32  {
33  x = y = z = 0.0;
34  }
35  UVector3(double xval, double yval, double zval)
36  {
37  x = xval;
38  y = yval;
39  z = zval;
40  }
41  UVector3(double theta, double phi);
42  UVector3(const double coord[3])
43  {
44  x = coord[0];
45  y = coord[1];
46  z = coord[2];
47  }
48 
49  inline UVector3& operator = (const UVector3& v);
50  inline UVector3& operator = (const double* vect);
51  // Assignments
52 
53  inline bool operator == (const UVector3&) const;
54  inline bool operator != (const UVector3&) const;
55  // Comparisons.
56 
57  inline UVector3 operator - () const;
58  // Unary minus.
59 
60  inline UVector3& operator += (const UVector3&);
61  // Addition.
62 
63  inline UVector3& operator -= (const UVector3&);
64  // Subtraction.
65 
66  inline double& operator[](int index);
67 
68  inline double operator[](int index) const;
69 
70  inline UVector3& operator *= (double);
71  // Scaling with real numbers.
72 
73  inline UVector3& operator /= (double);
74  // Dividing with real numbers.
75 
76  inline double Dot(const UVector3&) const;
77  // Scalar product.
78 
79  inline UVector3 Cross(const UVector3&) const;
80  // Cross product.
81 
82  double Angle(const UVector3&) const;
83  // The angle w.r.t. another 3-vector.
84 
85  UVector3 Unit() const;
86  // Unit vector parallel to this.
87 
88  inline bool IsNull() const;
89  // Check if vector is null
90 
91  inline void SetNull();
92  // Set all components to 0.
93 
94  inline void Set(double xx, double yy, double zz);
95  // Assign values to components
96 
97  inline void Set(double xx);
98  // Assign value to all components
99 
100  double Normalize();
101  // Normalize to unit this vector
102 
103  double Phi() const;
104  // The azimuth angle. returns phi from -pi to pi
105 
106  double Theta() const;
107  // The polar angle.
108 
109  inline double CosTheta() const;
110  // Cosine of the polar angle.
111 
112  inline double Mag2() const;
113  // The magnitude squared (rho^2 in spherical coordinate system).
114 
115  double Mag() const;
116  // The magnitude (rho in spherical coordinate system).
117 
118  double Perp2() const;
119  // The transverse component (R^2 in cylindrical coordinate system).
120 
121  double Perp() const;
122  // The transverse component (R in cylindrical coordinate system).
123 
124  void RotateX(double);
125  // Rotates the vector around the x-axis.
126 
127  void RotateY(double);
128  // Rotates the vector around the y-axis.
129 
130  void RotateZ(double);
131  // Rotates the vector around the z-axis.
132 
133  inline UVector3& MultiplyByComponents(const UVector3& p);
134 
135  public:
136  double x;
137  double y;
138  double z;
139 };
140 
141 UVector3 operator + (const UVector3&, const UVector3&);
142 // Addition of 3-vectors.
143 
144 UVector3 operator - (const UVector3&, const UVector3&);
145 // Subtraction of 3-vectors.
146 
147 double operator * (const UVector3&, const UVector3&);
148 // Scalar product of 3-vectors.
149 
150 UVector3 operator * (const UVector3&, double a);
151 UVector3 operator / (const UVector3&, double a);
152 UVector3 operator * (double a, const UVector3&);
153 
154 // Scaling of 3-vectors with a real number
155 
156 //______________________________________________________________________________
158 {
159  // Assignment of a UVector3
160  x *= p.x;
161  y *= p.y;
162  z *= p.z;
163  return *this;
164 }
165 
166 //______________________________________________________________________________
168 {
169  // Assignment of a UVector3
170  if (this == &p) { return *this; }
171  x = p.x;
172  y = p.y;
173  z = p.z;
174  return *this;
175 }
176 
177 inline UVector3& UVector3::operator = (const double vect[3])
178 {
179  // Assignment of a C array
180  x = vect[0];
181  y = vect[1];
182  z = vect[2];
183  return *this;
184 }
185 
186 inline bool UVector3::operator == (const UVector3& v) const
187 {
188  return (v.x == x && v.y == y && v.z == z) ? true : false;
189 }
190 
191 inline bool UVector3::operator != (const UVector3& v) const
192 {
193  return (v.x != x || v.y != y || v.z != z) ? true : false;
194 }
195 
197 {
198  x += p.x;
199  y += p.y;
200  z += p.z;
201  return *this;
202 }
203 
205 {
206  x -= p.x;
207  y -= p.y;
208  z -= p.z;
209  return *this;
210 }
211 
213 {
214  return UVector3(-x, -y, -z);
215 }
216 
218 {
219  x *= a;
220  y *= a;
221  z *= a;
222  return *this;
223 }
224 
226 {
227  a = 1. / a;
228  x *= a;
229  y *= a;
230  z *= a;
231  return *this;
232 }
233 
234 inline bool UVector3::IsNull() const
235 {
236  return ((std::abs(x) + std::abs(y) + std::abs(z)) == 0.0) ? true : false;
237 }
238 
239 /*
240 inline void UVector3::SetNull() {
241 x = y = z = 0.0;
242 }
243 */
244 
245 inline void UVector3::Set(double xx, double yy, double zz)
246 {
247  x = xx;
248  y = yy;
249  z = zz;
250 }
251 
252 inline void UVector3::Set(double xx)
253 {
254  x = y = z = xx;
255 }
256 
257 inline double UVector3::Dot(const UVector3& p) const
258 {
259  return x * p.x + y * p.y + z * p.z;
260 }
261 
262 inline UVector3 UVector3::Cross(const UVector3& p) const
263 {
264  return UVector3(y * p.z - p.y * z, z * p.x - p.z * x, x * p.y - p.x * y);
265 }
266 
267 inline double UVector3::Mag2() const
268 {
269  return x * x + y * y + z * z;
270 }
271 
272 inline double UVector3::Perp2() const
273 {
274  return x * x + y * y;
275 }
276 
277 inline double UVector3::CosTheta() const
278 {
279  double ptot = Mag();
280  return ptot == 0.0 ? 1.0 : z / ptot;
281 }
282 
283 
284 inline double& UVector3::operator[](int index)
285 {
286  switch (index)
287  {
288  case 0:
289  return x;
290  case 1:
291  return y;
292  case 2:
293  return z;
294  default:
295  return x;
296  }
297 }
298 
299 inline double UVector3::operator[](int index) const
300 {
301  // return operator()(index);
302 
303  // TODO: test performance of both versions on Linux
304  // => first version is slightly faster
305  if (true)
306  {
307  double vec[3] = {x, y, z};
308  return vec[index];
309  }
310 
311  switch (index)
312  {
313  case 0:
314  return x;
315  case 1:
316  return y;
317  case 2:
318  return z;
319  default:
320  return 0;
321  }
322 }
323 
324 inline std::ostream& operator<< (std::ostream& os, const UVector3& v)
325 {
326  return os << "(" << v.x << "," << v.y << "," << v.z << ")";
327 }
328 
329 #endif
double Mag2() const
Definition: UVector3.hh:267
double Phi() const
Definition: UVector3.cc:64
double Normalize()
Definition: UVector3.cc:89
UVector3 Cross(const UVector3 &) const
Definition: UVector3.hh:262
UVector3 operator/(const UVector3 &, double a)
Definition: UVector3.cc:149
double & operator[](int index)
Definition: UVector3.hh:284
bool operator==(const UVector3 &) const
Definition: UVector3.hh:186
UVector3 operator+(const UVector3 &, const UVector3 &)
Definition: UVector3.cc:134
UVector3()
Definition: UVector3.hh:31
G4double a
Definition: TRTMaterials.hh:39
UVector3(const double coord[3])
Definition: UVector3.hh:42
double operator*(const UVector3 &, const UVector3 &)
Definition: UVector3.cc:160
UVector3 & operator+=(const UVector3 &)
Definition: UVector3.hh:196
double x
Definition: UVector3.hh:136
void RotateX(double)
Definition: UVector3.cc:102
void RotateZ(double)
Definition: UVector3.cc:124
bool operator!=(const UVector3 &) const
Definition: UVector3.hh:191
UVector3 & operator*=(double)
Definition: UVector3.hh:217
bool IsNull() const
Definition: UVector3.hh:234
std::ostream & operator<<(std::ostream &os, const UVector3 &v)
Definition: UVector3.hh:324
UVector3 operator-(const UVector3 &, const UVector3 &)
Definition: UVector3.cc:139
double Dot(const UVector3 &) const
Definition: UVector3.hh:257
void SetNull()
double Mag() const
Definition: UVector3.cc:48
UVector3(double xval, double yval, double zval)
Definition: UVector3.hh:35
double Angle(const UVector3 &) const
Definition: UVector3.cc:30
double Perp2() const
Definition: UVector3.hh:272
void Set(double xx, double yy, double zz)
Definition: UVector3.hh:245
UVector3 operator-() const
Definition: UVector3.hh:212
UVector3 & operator=(const UVector3 &v)
Definition: UVector3.hh:167
UVector3 Unit() const
Definition: UVector3.cc:80
UVector3 & operator-=(const UVector3 &)
Definition: UVector3.hh:204
void RotateY(double)
Definition: UVector3.cc:113
double CosTheta() const
Definition: UVector3.hh:277
double Perp() const
Definition: UVector3.cc:56
double z
Definition: UVector3.hh:138
UVector3 & operator/=(double)
Definition: UVector3.hh:225
double y
Definition: UVector3.hh:137
double Theta() const
Definition: UVector3.cc:71
UVector3 & MultiplyByComponents(const UVector3 &p)
Definition: UVector3.hh:157