Belle II Software  release-06-01-15
Vector3D.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingCDC/geometry/Vector2D.h>
11 #include <tracking/trackFindingCDC/numerics/Quadratic.h>
12 
13 #include <framework/geometry/B2Vector3.h>
14 
15 #include <string>
16 #include <iosfwd>
17 #include <cmath>
18 
19 class TVector3;
20 
21 namespace Belle2 {
26  namespace TrackFindingCDC {
27 
29 
32  class Vector3D {
33 
34  public:
37  : m_xy(0.0, 0.0)
38  , m_z(0.0)
39  {
40  }
41 
43  explicit Vector3D(const TVector3& tVector3);
44 
46  explicit Vector3D(const B2Vector3D& b2Vector3);
47 
49  Vector3D(double x, double y, double z)
50  : m_xy(x, y)
51  , m_z(z)
52  {
53  }
54 
56  explicit Vector3D(const Vector2D& xy)
57  : m_xy(xy)
58  , m_z(0.0)
59  {
60  }
61 
63  Vector3D(const Vector2D& xy, double z)
64  : m_xy(xy)
65  , m_z(z)
66  {
67  }
68 
70  Vector3D& operator=(const TVector3& tVector3);
71 
73  Vector3D& operator=(const B2Vector3D& b2Vector3);
74 
76 
80  static Vector3D average(const Vector3D& one, const Vector3D& two)
81  {
82  if (one.hasNAN()) {
83  return two;
84  } else if (two.hasNAN()) {
85  return one;
86  } else {
87  return Vector3D((one.x() + two.x()) / 2.0,
88  (one.y() + two.y()) / 2.0,
89  (one.z() + two.z()) / 2.0);
90  }
91  }
92 
94 
99  static Vector3D average(const Vector3D& one, const Vector3D& two, const Vector3D& three)
100  {
101 
102  if (one.hasNAN()) {
103  return average(two, three);
104  } else if (two.hasNAN()) {
105  return average(one, three);
106  } else if (three.hasNAN()) {
107  return average(one, two);
108  } else {
109  return Vector3D((one.x() + two.x() + three.x()) / 3.0,
110  (one.y() + two.y() + three.y()) / 3.0,
111  (one.z() + two.z() + three.z()) / 3.0);
112  }
113  }
114 
116  operator const TVector3() const;
117 
119  operator const B2Vector3D() const;
120 
122  bool operator==(const Vector3D& rhs) const
123  {
124  return x() == rhs.x() and y() == rhs.y() and z() == rhs.z();
125  }
126 
129 
135  bool operator<(const Vector3D& rhs) const
136  {
137  return norm() < rhs.norm() or (norm() == rhs.norm() and
138  (z() < rhs.z() or (z() == rhs.z() and (phi() < rhs.phi()))));
139  }
140 
142 
144  {
145  return Vector3D(0.0, 0.0, 0.0);
146  }
147 
149  bool isNull() const
150  {
151  return x() == 0.0 and y() == 0.0 and z() == 0.0;
152  }
153 
155  bool hasNAN() const
156  {
157  return std::isnan(x()) or std::isnan(y()) or std::isnan(z());
158  }
159 
161  std::string __str__() const;
162 
164  double dot(const Vector3D& rhs) const
165  {
166  return x() * rhs.x() + y() * rhs.y() + z() * rhs.z();
167  }
168 
170  double dotXY(const Vector3D& rhs) const
171  {
172  return x() * rhs.x() + y() * rhs.y();
173  }
174 
176  Vector3D cross(const Vector3D& rhs) const
177  {
178  return Vector3D(y() * rhs.z() - z() * rhs.y(),
179  z() * rhs.x() - x() * rhs.z(),
180  x() * rhs.y() - y() * rhs.x());
181  }
182 
184  double crossXY(const Vector3D& rhs) const
185  {
186  return xy().cross(rhs.xy());
187  }
188 
190  double crossXY(const Vector2D& rhs) const
191  {
192  return xy().cross(rhs);
193  }
194 
196  double normSquared() const
197  {
198  return x() * x() + y() * y() + z() * z();
199  }
200 
202  double norm() const
203  {
204  return hypot3(x(), y(), z());
205  }
206 
215  double cosWith(const Vector3D& rhs) const
216  {
217  return dot(rhs) / (norm() * rhs.norm());
218  }
219 
221  double sinWith(const Vector3D& rhs) const
222  {
223  return cross(rhs).norm() / (norm() * rhs.norm());
224  }
225 
227  double angleWith(const Vector3D& rhs) const
228  {
229  return atan2(sinWith(rhs), cosWith(rhs));
230  }
232 
234  double distance(const Vector3D& rhs = Vector3D(0.0, 0.0, 0.0)) const
235  {
236  double deltaX = x() - rhs.x();
237  double deltaY = y() - rhs.y();
238  double deltaZ = z() - rhs.z();
239  return hypot3(deltaX, deltaY, deltaZ);
240  }
241 
243  Vector3D& scale(const double factor)
244  {
245  m_xy.scale(factor);
246  m_z *= factor;
247  return *this;
248  }
249 
251  Vector3D& operator*=(const double factor)
252  {
253  return scale(factor);
254  }
255 
257  Vector3D scaled(const double factor) const
258  {
259  return Vector3D(xy().scaled(factor), z() * factor);
260  }
261 
263  Vector3D operator*(const double factor) const
264  {
265  return scaled(factor);
266  }
267 
269  Vector3D& divide(const double denominator)
270  {
271  m_xy.divide(denominator);
272  m_z /= denominator;
273  return *this;
274  }
275 
277  Vector3D& operator/=(const double denominator)
278  {
279  return divide(denominator);
280  }
281 
283  Vector3D divided(const double denominator) const
284  {
285  return Vector3D(xy().divided(denominator), z() / denominator);
286  }
287 
289  Vector3D operator/(const double denominator) const
290  {
291  return divided(denominator);
292  }
293 
295  Vector3D& add(const Vector3D& rhs)
296  {
297  m_xy.add(rhs.xy());
298  m_z += rhs.z();
299  return *this;
300  }
301 
303  Vector3D& add(const Vector2D& rhs)
304  {
305  m_xy.add(rhs);
306  return *this;
307  }
308 
311  {
312  return add(rhs);
313  }
314 
317  {
318  return add(rhs);
319  }
320 
323  {
324  m_xy.subtract(rhs.xy());
325  m_z -= rhs.z();
326  return *this;
327  }
328 
331  {
332  m_xy.subtract(rhs);
333  return *this;
334  }
335 
338  {
339  return subtract(rhs);
340  }
341 
344  {
345  return subtract(rhs);
346  }
347 
349  Vector3D unit() const
350  {
351  return isNull() ? Vector3D(0.0, 0.0, 0.0) : divided(norm());
352  }
353 
355 
357  double normalize()
358  {
359  double result = norm();
360  if (result != 0.0) divide(result);
361  return result;
362  }
363 
365 
367  double normalizeTo(const double toLength)
368  {
369  double result = norm();
370  if (result != 0.0) scale(toLength / result);
371  return result;
372  }
373 
376  {
377  scale(-1.0);
378  return *this;
379  }
380 
383  {
384  return scaled(-1.0);
385  }
386 
389  {
390  return reversed();
391  }
392 
394  Vector3D operator+(const Vector3D& rhs) const
395  {
396  return Vector3D(xy() + rhs.xy(), z() + rhs.z());
397  }
398 
400  Vector3D operator-(const Vector3D& rhs) const
401  {
402  return Vector3D(xy() - rhs.xy(), z() - rhs.z());
403  }
404 
406  double parallelComp(const Vector3D& relativTo) const
407  {
408  return relativTo.dot(*this) / relativTo.norm();
409  }
410 
412  Vector3D parallelVector(const Vector3D& relativTo) const
413  {
414  return relativTo.scaled(relativTo.dot(*this) / relativTo.normSquared());
415  }
416 
418 
420  double unnormalizedParallelComp(const Vector3D& relativTo) const
421  {
422  return relativTo.dot(*this);
423  }
424 
426 
430  double orthogonalComp(const Vector3D& relativTo) const
431  {
432  return relativTo.cross(*this).norm() / relativTo.norm();
433  }
434 
436  Vector3D orthogonalVector(const Vector3D& relativTo) const
437  {
438  return *this - parallelVector(relativTo);
439  }
440 
442 
444  double unnormalizedOrthogonalComp(const Vector3D& relativTo) const
445  {
446  return relativTo.cross(*this).norm();
447  }
448 
450  void passiveMoveBy(const Vector3D& by)
451  {
452  subtract(by);
453  }
454 
457  {
458  return *this - by;
459  }
460 
462  double x() const
463  {
464  return m_xy.x();
465  }
466 
468  void setX(const double x)
469  {
470  m_xy.setX(x);
471  }
472 
474  double y() const
475  {
476  return m_xy.y();
477  }
478 
480  void setY(const double y)
481  {
482  m_xy.setY(y);
483  }
484 
486  double z() const
487  {
488  return m_z;
489  }
490 
492  void setZ(const double z)
493  {
494  m_z = z;
495  }
496 
498  const Vector2D& xy() const
499  {
500  return m_xy;
501  }
502 
504  void setXY(const Vector2D& xy)
505  {
506  m_xy = xy;
507  }
508 
510  void set(const double first, const double second, const double third)
511  {
512  setX(first);
513  setY(second);
514  setZ(third);
515  }
516 
518  double cylindricalRSquared() const
519  {
520  return xy().normSquared();
521  }
522 
524  double cylindricalR() const
525  {
526  return xy().norm();
527  }
528 
530  double phi() const
531  {
532  return xy().phi();
533  }
534 
536  double theta() const
537  {
538  return atan2(cylindricalR(), z());
539  }
540 
542  double lambda() const
543  {
544  return atan2(z(), cylindricalR());
545  }
546 
548  double cotTheta() const
549  {
550  return z() / cylindricalR();
551  }
552 
554  double tanLambda() const
555  {
556  return z() / cylindricalR();
557  }
558 
559  private:
562 
564  double m_z;
565  };
566 
568  std::ostream& operator<<(std::ostream& output, const Vector3D& vector3D);
569  }
571 }
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:35
Vector2D & add(const Vector2D &rhs)
Adds a vector to this in place.
Definition: Vector2D.h:274
Vector2D & subtract(const Vector2D &rhs)
Subtracts a vector from this in place.
Definition: Vector2D.h:288
double cross(const Vector2D &rhs) const
Calculated the two dimensional cross product.
Definition: Vector2D.h:175
Vector2D & divide(const double denominator)
Divides all coordinates by a common denominator in place.
Definition: Vector2D.h:249
double x() const
Getter for the x coordinate.
Definition: Vector2D.h:607
double phi() const
Gives the azimuth angle being the angle to the x axes ( range -M_PI to M_PI )
Definition: Vector2D.h:581
double normSquared() const
Calculates .
Definition: Vector2D.h:181
Vector2D & scale(const double factor)
Scales the vector in place by the given factor.
Definition: Vector2D.h:224
void setY(const double y)
Setter for the y coordinate.
Definition: Vector2D.h:622
double y() const
Getter for the y coordinate.
Definition: Vector2D.h:617
double norm() const
Calculates the length of the vector.
Definition: Vector2D.h:187
void setX(const double x)
Setter for the x coordinate.
Definition: Vector2D.h:612
A three dimensional vector.
Definition: Vector3D.h:32
Vector3D operator-(const Vector3D &rhs) const
Returns a new vector as differenc of this and rhs.
Definition: Vector3D.h:400
double normalize()
Normalizes the vector to unit length.
Definition: Vector3D.h:357
Vector3D passiveMovedBy(const Vector3D &by)
Passivelly moves the vector inplace by the given vector.
Definition: Vector3D.h:456
bool operator<(const Vector3D &rhs) const
Total ordering based on cylindrical radius first the z component second and azimuth angle third.
Definition: Vector3D.h:135
Vector3D & operator=(const TVector3 &tVector3)
Assignment translating from a TVector3 instance.
Definition: Vector3D.cc:32
std::string __str__() const
Output operator for python.
Definition: Vector3D.cc:63
double distance(const Vector3D &rhs=Vector3D(0.0, 0.0, 0.0)) const
Calculates the distance of this point to the rhs.
Definition: Vector3D.h:234
Vector3D & scale(const double factor)
Scales the vector in place by the given factor.
Definition: Vector3D.h:243
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition: Vector3D.h:367
double unnormalizedOrthogonalComp(const Vector3D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition: Vector3D.h:444
double cotTheta() const
Getter for the cotangent of the polar angle.
Definition: Vector3D.h:548
double sinWith(const Vector3D &rhs) const
Sine of the angle between this and rhs.
Definition: Vector3D.h:221
Vector3D & subtract(const Vector2D &rhs)
Subtracts a two dimensional vector from this in place taking z of the given vector as zero.
Definition: Vector3D.h:330
static Vector3D getLowest()
Getter for the lowest possible vector.
Definition: Vector3D.h:143
static Vector3D average(const Vector3D &one, const Vector3D &two, const Vector3D &three)
Constructs the average of three vectors.
Definition: Vector3D.h:99
double dotXY(const Vector3D &rhs) const
Calculates the two dimensional dot product in xy projection.
Definition: Vector3D.h:170
double lambda() const
Getter for lambda.
Definition: Vector3D.h:542
Vector3D cross(const Vector3D &rhs) const
Calculated the three dimensional cross product.
Definition: Vector3D.h:176
Vector3D orthogonalVector(const Vector3D &relativTo) const
Calculates the part of this vector that is parallel to the given vector.
Definition: Vector3D.h:436
double cosWith(const Vector3D &rhs) const
Definition: Vector3D.h:215
double orthogonalComp(const Vector3D &relativTo) const
Calculates the component orthogonal to the given vector.
Definition: Vector3D.h:430
Vector3D(const Vector2D &xy, double z)
Constructor augmeting a Vector2D to a Vector3D setting z explicitly.
Definition: Vector3D.h:63
Vector3D parallelVector(const Vector3D &relativTo) const
Calculates the part of this vector that is parallel to the given vector.
Definition: Vector3D.h:412
double cylindricalR() const
Getter for the cylindrical radius ( xy projected norm )
Definition: Vector3D.h:524
Vector2D m_xy
Memory for the first and second coordinate available as a vector.
Definition: Vector3D.h:561
Vector3D(const Vector2D &xy)
Constructor augmeting a Vector2D to a Vector3D setting z to zero.
Definition: Vector3D.h:56
Vector3D operator+(const Vector3D &rhs) const
Returns a new vector as sum of this and rhs.
Definition: Vector3D.h:394
void passiveMoveBy(const Vector3D &by)
Passivelly moves the vector inplace by the given vector.
Definition: Vector3D.h:450
Vector3D operator*(const double factor) const
Same as scaled()
Definition: Vector3D.h:263
double dot(const Vector3D &rhs) const
Calculates the three dimensional dot product.
Definition: Vector3D.h:164
double parallelComp(const Vector3D &relativTo) const
Calculates the component parallel to the given vector.
Definition: Vector3D.h:406
Vector3D unit() const
Returns a unit vector colaligned with this.
Definition: Vector3D.h:349
double tanLambda() const
Getter for the tangent of lambda equivalent to cotTheta()
Definition: Vector3D.h:554
Vector3D operator-() const
Same as reversed()
Definition: Vector3D.h:388
double x() const
Getter for the x coordinate.
Definition: Vector3D.h:462
Vector3D & subtract(const Vector3D &rhs)
Subtracts a vector to this in place.
Definition: Vector3D.h:322
static Vector3D average(const Vector3D &one, const Vector3D &two)
Constructs the average of two vectors.
Definition: Vector3D.h:80
Vector3D & operator+=(const Vector2D &rhs)
Same as add()
Definition: Vector3D.h:316
Vector3D & operator+=(const Vector3D &rhs)
Same as add()
Definition: Vector3D.h:310
double crossXY(const Vector2D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition: Vector3D.h:190
Vector3D(double x, double y, double z)
Constructor from three coordinates.
Definition: Vector3D.h:49
Vector3D & operator*=(const double factor)
Same as scale()
Definition: Vector3D.h:251
const Vector2D & xy() const
Getter for the xy projected vector ( reference ! )
Definition: Vector3D.h:498
Vector3D & add(const Vector3D &rhs)
Adds a vector to this in place.
Definition: Vector3D.h:295
void setZ(const double z)
Setter for the z coordinate.
Definition: Vector3D.h:492
double angleWith(const Vector3D &rhs) const
The angle between this and rhs.
Definition: Vector3D.h:227
double phi() const
Getter for the azimuth angle.
Definition: Vector3D.h:530
double cylindricalRSquared() const
Getter for the squared cylindrical radius ( xy projected squared norm )
Definition: Vector3D.h:518
Vector3D operator/(const double denominator) const
Same as divided()
Definition: Vector3D.h:289
void setXY(const Vector2D &xy)
Setter for the xy projected vector.
Definition: Vector3D.h:504
double normSquared() const
Calculates the squared length of the vector.
Definition: Vector3D.h:196
bool isNull() const
Checks if the vector is the null vector.
Definition: Vector3D.h:149
Vector3D & divide(const double denominator)
Divides all coordinates by a common denominator in place.
Definition: Vector3D.h:269
double unnormalizedParallelComp(const Vector3D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition: Vector3D.h:420
Vector3D scaled(const double factor) const
Returns a scaled copy of the vector.
Definition: Vector3D.h:257
bool hasNAN() const
Checks if one of the coordinates is NAN.
Definition: Vector3D.h:155
bool operator==(const Vector3D &rhs) const
Equality comparison with all three coordinates.
Definition: Vector3D.h:122
void setY(const double y)
Setter for the y coordinate.
Definition: Vector3D.h:480
double y() const
Getter for the y coordinate.
Definition: Vector3D.h:474
double m_z
Memory for the third coordinate.
Definition: Vector3D.h:564
Vector3D & reverse()
Reverses the direction of the vector in place.
Definition: Vector3D.h:375
Vector3D & operator-=(const Vector2D &rhs)
Same as subtract()
Definition: Vector3D.h:343
Vector3D & add(const Vector2D &rhs)
Adds a two dimensional vector to this in place taking z of the given vector as zero.
Definition: Vector3D.h:303
void set(const double first, const double second, const double third)
Setter for all three coordinates.
Definition: Vector3D.h:510
double crossXY(const Vector3D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition: Vector3D.h:184
double norm() const
Calculates the length of the vector.
Definition: Vector3D.h:202
double z() const
Getter for the z coordinate.
Definition: Vector3D.h:486
void setX(const double x)
Setter for the x coordinate.
Definition: Vector3D.h:468
double theta() const
Getter for the polar angle.
Definition: Vector3D.h:536
Vector3D & operator-=(const Vector3D &rhs)
Same as subtract()
Definition: Vector3D.h:337
Vector3D reversed() const
Returns a vector pointing in the opposite direction.
Definition: Vector3D.h:382
Vector3D()
Default constructor for ROOT compatibility.
Definition: Vector3D.h:36
Vector3D & operator/=(const double denominator)
Same as divide()
Definition: Vector3D.h:277
Vector3D divided(const double denominator) const
Returns a copy where all coordinates got divided by a common denominator.
Definition: Vector3D.h:283
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition: B2Vector3.h:493
Abstract base class for different kinds of events.