Geant4  10.01.p03
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 double& x();
71 
72  inline double x() const;
73 
74  inline double& y();
75 
76  inline double y() const;
77 
78  inline double& z();
79 
80  inline double z() const;
81 
82  inline UVector3& operator *= (double);
83  // Scaling with real numbers.
84 
85  inline UVector3& operator /= (double);
86  // Dividing with real numbers.
87 
88  inline double Dot(const UVector3&) const;
89  // Scalar product.
90 
91  inline UVector3 Cross(const UVector3&) const;
92  // Cross product.
93 
94  double Angle(const UVector3&) const;
95  // The angle w.r.t. another 3-vector.
96 
97  UVector3 Unit() const;
98  // Unit vector parallel to this.
99 
100  inline bool IsNull() const;
101  // Check if vector is null
102 
103  inline void SetNull();
104  // Set all components to 0.
105 
106  inline void Set(double xx, double yy, double zz);
107  // Assign values to components
108 
109  inline void Set(double xx);
110  // Assign value to all components
111 
112  double Normalize();
113  // Normalize to unit this vector
114 
115  double Phi() const;
116  // The azimuth angle. returns phi from -pi to pi
117 
118  double Theta() const;
119  // The polar angle.
120 
121  inline double CosTheta() const;
122  // Cosine of the polar angle.
123 
124  inline double Mag2() const;
125  // The magnitude squared (rho^2 in spherical coordinate system).
126 
127  double Mag() const;
128  // The magnitude (rho in spherical coordinate system).
129 
130  double Perp2() const;
131  // The transverse component (R^2 in cylindrical coordinate system).
132 
133  double Perp() const;
134  // The transverse component (R in cylindrical coordinate system).
135 
136  void RotateX(double);
137  // Rotates the vector around the x-axis.
138 
139  void RotateY(double);
140  // Rotates the vector around the y-axis.
141 
142  void RotateZ(double);
143  // Rotates the vector around the z-axis.
144 
145  inline UVector3& MultiplyByComponents(const UVector3& p);
146 
147  private:
148  double x_;
149  double y_;
150  double z_;
151 };
152 
153 
154 inline UVector3 operator + (const UVector3& a, const UVector3& b)
155 {
156  return UVector3(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
157 }
158 
159 inline UVector3 operator - (const UVector3& a, const UVector3& b)
160 {
161  return UVector3(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
162 }
163 
164 inline UVector3 operator * (const UVector3& p, double a)
165 {
166  return UVector3(a * p.x(), a * p.y(), a * p.z());
167 }
168 
169 inline UVector3 operator / (const UVector3& p, double a)
170 {
171  a = 1. / a;
172  return UVector3(a * p.x(), a * p.y(), a * p.z());
173 }
174 
175 inline UVector3 operator * (double a, const UVector3& p)
176 {
177  return UVector3(a * p.x(), a * p.y(), a * p.z());
178 }
179 
180 
181 //______________________________________________________________________________
183 {
184  // Assignment of a UVector3
185  x_ *= p.x_;
186  y_ *= p.y_;
187  z_ *= p.z_;
188  return *this;
189 }
190 
191 //______________________________________________________________________________
193 {
194  // Assignment of a UVector3
195  if (this == &p) { return *this; }
196  x_ = p.x_;
197  y_ = p.y_;
198  z_ = p.z_;
199  return *this;
200 }
201 
202 inline UVector3& UVector3::operator = (const double vect[3])
203 {
204  // Assignment of a C array
205  x_ = vect[0];
206  y_ = vect[1];
207  z_ = vect[2];
208  return *this;
209 }
210 
211 inline bool UVector3::operator == (const UVector3& v) const
212 {
213  return (v.x_ == x_ && v.y_ == y_ && v.z_ == z_) ? true : false;
214 }
215 
216 inline bool UVector3::operator != (const UVector3& v) const
217 {
218  return (v.x_ != x_ || v.y_ != y_ || v.z_ != z_) ? true : false;
219 }
220 
222 {
223  x_ += p.x_;
224  y_ += p.y_;
225  z_ += p.z_;
226  return *this;
227 }
228 
230 {
231  x_ -= p.x_;
232  y_ -= p.y_;
233  z_ -= p.z_;
234  return *this;
235 }
236 
238 {
239  return UVector3(-x_, -y_, -z_);
240 }
241 
243 {
244  x_ *= a;
245  y_ *= a;
246  z_ *= a;
247  return *this;
248 }
249 
251 {
252  a = 1. / a;
253  x_ *= a;
254  y_ *= a;
255  z_ *= a;
256  return *this;
257 }
258 
259 inline bool UVector3::IsNull() const
260 {
261  return ((std::abs(x_) + std::abs(y_) + std::abs(z_)) == 0.0) ? true : false;
262 }
263 
264 inline void UVector3::Set(double xx, double yy, double zz)
265 {
266  x_ = xx;
267  y_ = yy;
268  z_ = zz;
269 }
270 
271 inline void UVector3::Set(double xx)
272 {
273  x_ = y_ = z_ = xx;
274 }
275 
276 inline double UVector3::Dot(const UVector3& p) const
277 {
278  return x_ * p.x_ + y_ * p.y_ + z_ * p.z_;
279 }
280 
281 inline UVector3 UVector3::Cross(const UVector3& p) const
282 {
283  return UVector3(y_ * p.z_ - p.y_ * z_, z_ * p.x_ - p.z_ * x_,
284  x_ * p.y_ - p.x_ * y_);
285 }
286 
287 inline double UVector3::Mag2() const
288 {
289  return x_ * x_ + y_ * y_ + z_ * z_;
290 }
291 
292 inline double UVector3::Perp2() const
293 {
294  return x_ * x_ + y_ * y_;
295 }
296 
297 inline double UVector3::CosTheta() const
298 {
299  double ptot = Mag();
300  return ptot == 0.0 ? 1.0 : z_ / ptot;
301 }
302 
303 
304 inline double& UVector3::operator[](int index)
305 {
306  switch (index)
307  {
308  case 0:
309  return x_;
310  case 1:
311  return y_;
312  case 2:
313  return z_;
314  default:
315  return x_;
316  }
317 }
318 
319 inline double UVector3::operator[](int index) const
320 {
321  // return operator()(index);
322 
323  // TODO: test performance of both versions on Linux
324  // => first version is slightly faster
325  if (true)
326  {
327  double vec[3] = {x_, y_, z_};
328  return vec[index];
329  }
330 
331  switch (index)
332  {
333  case 0:
334  return x_;
335  case 1:
336  return y_;
337  case 2:
338  return z_;
339  default:
340  return 0;
341  }
342 }
343 
344 inline double& UVector3::x() { return x_; }
345 
346 inline double UVector3::x() const { return x_; }
347 
348 inline double& UVector3::y() { return y_; }
349 
350 inline double UVector3::y() const { return y_; }
351 
352 inline double& UVector3::z() { return z_; }
353 
354 inline double UVector3::z() const { return z_; }
355 
356 inline std::ostream& operator<< (std::ostream& os, const UVector3& v)
357 {
358  return os << "(" << v.x() << "," << v.y() << "," << v.z() << ")";
359 }
360 
361 #endif
double Mag2() const
Definition: UVector3.hh:287
double & z()
Definition: UVector3.hh:352
double & y()
Definition: UVector3.hh:348
double y_
Definition: UVector3.hh:149
double Phi() const
Definition: UVector3.cc:64
double Normalize()
Definition: UVector3.cc:89
UVector3 Cross(const UVector3 &) const
Definition: UVector3.hh:281
double & operator[](int index)
Definition: UVector3.hh:304
bool operator==(const UVector3 &) const
Definition: UVector3.hh:211
UVector3 operator/(const UVector3 &p, double a)
Definition: UVector3.hh:169
UVector3()
Definition: UVector3.hh:31
G4double a
Definition: TRTMaterials.hh:39
UVector3(const double coord[3])
Definition: UVector3.hh:42
UVector3 & operator+=(const UVector3 &)
Definition: UVector3.hh:221
void RotateX(double)
Definition: UVector3.cc:102
void RotateZ(double)
Definition: UVector3.cc:124
bool operator!=(const UVector3 &) const
Definition: UVector3.hh:216
UVector3 operator+(const UVector3 &a, const UVector3 &b)
Definition: UVector3.hh:154
UVector3 & operator*=(double)
Definition: UVector3.hh:242
bool IsNull() const
Definition: UVector3.hh:259
double & x()
Definition: UVector3.hh:344
double z_
Definition: UVector3.hh:150
std::ostream & operator<<(std::ostream &os, const UVector3 &v)
Definition: UVector3.hh:356
double x_
Definition: UVector3.hh:148
double Dot(const UVector3 &) const
Definition: UVector3.hh:276
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:292
void Set(double xx, double yy, double zz)
Definition: UVector3.hh:264
UVector3 operator-() const
Definition: UVector3.hh:237
UVector3 & operator=(const UVector3 &v)
Definition: UVector3.hh:192
UVector3 Unit() const
Definition: UVector3.cc:80
UVector3 & operator-=(const UVector3 &)
Definition: UVector3.hh:229
UVector3 operator-(const UVector3 &a, const UVector3 &b)
Definition: UVector3.hh:159
void RotateY(double)
Definition: UVector3.cc:113
UVector3 operator*(const UVector3 &p, double a)
Definition: UVector3.hh:164
double CosTheta() const
Definition: UVector3.hh:297
double Perp() const
Definition: UVector3.cc:56
UVector3 & operator/=(double)
Definition: UVector3.hh:250
double Theta() const
Definition: UVector3.cc:71
UVector3 & MultiplyByComponents(const UVector3 &p)
Definition: UVector3.hh:182