Belle II Software  release-08-01-10
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 #include <Math/Vector3D.h>
15 
16 #include <string>
17 #include <iosfwd>
18 #include <cmath>
19 
20 class TVector3;
21 
22 namespace Belle2 {
27  namespace TrackFindingCDC {
28 
30 
33  class Vector3D {
34 
35  public:
38  : m_xy(0.0, 0.0)
39  , m_z(0.0)
40  {
41  }
42 
44  explicit Vector3D(const TVector3& tVector3);
45 
47  explicit Vector3D(const B2Vector3D& b2Vector3);
48 
50  explicit Vector3D(const ROOT::Math::XYZVector& xyzVector3);
51 
53  Vector3D(double x, double y, double z)
54  : m_xy(x, y)
55  , m_z(z)
56  {
57  }
58 
60  explicit Vector3D(const Vector2D& xy)
61  : m_xy(xy)
62  , m_z(0.0)
63  {
64  }
65 
67  Vector3D(const Vector2D& xy, double z)
68  : m_xy(xy)
69  , m_z(z)
70  {
71  }
72 
74  Vector3D& operator=(const TVector3& tVector3);
75 
77  Vector3D& operator=(const B2Vector3D& b2Vector3);
78 
80  Vector3D& operator=(const ROOT::Math::XYZVector& xyzVector3);
81 
83 
87  static Vector3D average(const Vector3D& one, const Vector3D& two)
88  {
89  if (one.hasNAN()) {
90  return two;
91  } else if (two.hasNAN()) {
92  return one;
93  } else {
94  return Vector3D((one.x() + two.x()) / 2.0,
95  (one.y() + two.y()) / 2.0,
96  (one.z() + two.z()) / 2.0);
97  }
98  }
99 
101 
106  static Vector3D average(const Vector3D& one, const Vector3D& two, const Vector3D& three)
107  {
108 
109  if (one.hasNAN()) {
110  return average(two, three);
111  } else if (two.hasNAN()) {
112  return average(one, three);
113  } else if (three.hasNAN()) {
114  return average(one, two);
115  } else {
116  return Vector3D((one.x() + two.x() + three.x()) / 3.0,
117  (one.y() + two.y() + three.y()) / 3.0,
118  (one.z() + two.z() + three.z()) / 3.0);
119  }
120  }
121 
123  operator const TVector3() const;
124 
126  operator const B2Vector3D() const;
127 
129  operator const ROOT::Math::XYZVector() const;
130 
132  bool operator==(const Vector3D& rhs) const
133  {
134  return x() == rhs.x() and y() == rhs.y() and z() == rhs.z();
135  }
136 
139 
145  bool operator<(const Vector3D& rhs) const
146  {
147  return norm() < rhs.norm() or (norm() == rhs.norm() and
148  (z() < rhs.z() or (z() == rhs.z() and (phi() < rhs.phi()))));
149  }
150 
152 
154  {
155  return Vector3D(0.0, 0.0, 0.0);
156  }
157 
159  bool isNull() const
160  {
161  return x() == 0.0 and y() == 0.0 and z() == 0.0;
162  }
163 
165  bool hasNAN() const
166  {
167  return std::isnan(x()) or std::isnan(y()) or std::isnan(z());
168  }
169 
171  std::string __str__() const;
172 
174  double dot(const Vector3D& rhs) const
175  {
176  return x() * rhs.x() + y() * rhs.y() + z() * rhs.z();
177  }
178 
180  double dotXY(const Vector3D& rhs) const
181  {
182  return x() * rhs.x() + y() * rhs.y();
183  }
184 
186  Vector3D cross(const Vector3D& rhs) const
187  {
188  return Vector3D(y() * rhs.z() - z() * rhs.y(),
189  z() * rhs.x() - x() * rhs.z(),
190  x() * rhs.y() - y() * rhs.x());
191  }
192 
194  double crossXY(const Vector3D& rhs) const
195  {
196  return xy().cross(rhs.xy());
197  }
198 
200  double crossXY(const Vector2D& rhs) const
201  {
202  return xy().cross(rhs);
203  }
204 
206  double normSquared() const
207  {
208  return x() * x() + y() * y() + z() * z();
209  }
210 
212  double norm() const
213  {
214  return hypot3(x(), y(), z());
215  }
216 
225  double cosWith(const Vector3D& rhs) const
226  {
227  return dot(rhs) / (norm() * rhs.norm());
228  }
229 
231  double sinWith(const Vector3D& rhs) const
232  {
233  return cross(rhs).norm() / (norm() * rhs.norm());
234  }
235 
237  double angleWith(const Vector3D& rhs) const
238  {
239  return atan2(sinWith(rhs), cosWith(rhs));
240  }
242 
244  double distance(const Vector3D& rhs = Vector3D(0.0, 0.0, 0.0)) const
245  {
246  double deltaX = x() - rhs.x();
247  double deltaY = y() - rhs.y();
248  double deltaZ = z() - rhs.z();
249  return hypot3(deltaX, deltaY, deltaZ);
250  }
251 
253  Vector3D& scale(const double factor)
254  {
255  m_xy.scale(factor);
256  m_z *= factor;
257  return *this;
258  }
259 
261  Vector3D& operator*=(const double factor)
262  {
263  return scale(factor);
264  }
265 
267  Vector3D scaled(const double factor) const
268  {
269  return Vector3D(xy().scaled(factor), z() * factor);
270  }
271 
273  Vector3D operator*(const double factor) const
274  {
275  return scaled(factor);
276  }
277 
279  Vector3D& divide(const double denominator)
280  {
281  m_xy.divide(denominator);
282  m_z /= denominator;
283  return *this;
284  }
285 
287  Vector3D& operator/=(const double denominator)
288  {
289  return divide(denominator);
290  }
291 
293  Vector3D divided(const double denominator) const
294  {
295  return Vector3D(xy().divided(denominator), z() / denominator);
296  }
297 
299  Vector3D operator/(const double denominator) const
300  {
301  return divided(denominator);
302  }
303 
305  Vector3D& add(const Vector3D& rhs)
306  {
307  m_xy.add(rhs.xy());
308  m_z += rhs.z();
309  return *this;
310  }
311 
313  Vector3D& add(const Vector2D& rhs)
314  {
315  m_xy.add(rhs);
316  return *this;
317  }
318 
321  {
322  return add(rhs);
323  }
324 
327  {
328  return add(rhs);
329  }
330 
333  {
334  m_xy.subtract(rhs.xy());
335  m_z -= rhs.z();
336  return *this;
337  }
338 
341  {
342  m_xy.subtract(rhs);
343  return *this;
344  }
345 
348  {
349  return subtract(rhs);
350  }
351 
354  {
355  return subtract(rhs);
356  }
357 
359  Vector3D unit() const
360  {
361  return isNull() ? Vector3D(0.0, 0.0, 0.0) : divided(norm());
362  }
363 
365 
367  double normalize()
368  {
369  double result = norm();
370  if (result != 0.0) divide(result);
371  return result;
372  }
373 
375 
377  double normalizeTo(const double toLength)
378  {
379  double result = norm();
380  if (result != 0.0) scale(toLength / result);
381  return result;
382  }
383 
386  {
387  scale(-1.0);
388  return *this;
389  }
390 
393  {
394  return scaled(-1.0);
395  }
396 
399  {
400  return reversed();
401  }
402 
404  Vector3D operator+(const Vector3D& rhs) const
405  {
406  return Vector3D(xy() + rhs.xy(), z() + rhs.z());
407  }
408 
410  Vector3D operator-(const Vector3D& rhs) const
411  {
412  return Vector3D(xy() - rhs.xy(), z() - rhs.z());
413  }
414 
416  double parallelComp(const Vector3D& relativTo) const
417  {
418  return relativTo.dot(*this) / relativTo.norm();
419  }
420 
422  Vector3D parallelVector(const Vector3D& relativTo) const
423  {
424  return relativTo.scaled(relativTo.dot(*this) / relativTo.normSquared());
425  }
426 
428 
430  double unnormalizedParallelComp(const Vector3D& relativTo) const
431  {
432  return relativTo.dot(*this);
433  }
434 
436 
440  double orthogonalComp(const Vector3D& relativTo) const
441  {
442  return relativTo.cross(*this).norm() / relativTo.norm();
443  }
444 
446  Vector3D orthogonalVector(const Vector3D& relativTo) const
447  {
448  return *this - parallelVector(relativTo);
449  }
450 
452 
454  double unnormalizedOrthogonalComp(const Vector3D& relativTo) const
455  {
456  return relativTo.cross(*this).norm();
457  }
458 
460  void passiveMoveBy(const Vector3D& by)
461  {
462  subtract(by);
463  }
464 
467  {
468  return *this - by;
469  }
470 
472  double x() const
473  {
474  return m_xy.x();
475  }
476 
478  void setX(const double x)
479  {
480  m_xy.setX(x);
481  }
482 
484  double y() const
485  {
486  return m_xy.y();
487  }
488 
490  void setY(const double y)
491  {
492  m_xy.setY(y);
493  }
494 
496  double z() const
497  {
498  return m_z;
499  }
500 
502  void setZ(const double z)
503  {
504  m_z = z;
505  }
506 
508  const Vector2D& xy() const
509  {
510  return m_xy;
511  }
512 
514  void setXY(const Vector2D& xy)
515  {
516  m_xy = xy;
517  }
518 
520  void set(const double first, const double second, const double third)
521  {
522  setX(first);
523  setY(second);
524  setZ(third);
525  }
526 
528  double cylindricalRSquared() const
529  {
530  return xy().normSquared();
531  }
532 
534  double cylindricalR() const
535  {
536  return xy().norm();
537  }
538 
540  double phi() const
541  {
542  return xy().phi();
543  }
544 
546  double theta() const
547  {
548  return atan2(cylindricalR(), z());
549  }
550 
552  double lambda() const
553  {
554  return atan2(z(), cylindricalR());
555  }
556 
558  double cotTheta() const
559  {
560  return z() / cylindricalR();
561  }
562 
564  double tanLambda() const
565  {
566  return z() / cylindricalR();
567  }
568 
569  private:
572 
574  double m_z;
575  };
576 
578  std::ostream& operator<<(std::ostream& output, const Vector3D& vector3D);
579  }
581 }
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:33
Vector3D operator-(const Vector3D &rhs) const
Returns a new vector as differenc of this and rhs.
Definition: Vector3D.h:410
double normalize()
Normalizes the vector to unit length.
Definition: Vector3D.h:367
Vector3D passiveMovedBy(const Vector3D &by)
Passivelly moves the vector inplace by the given vector.
Definition: Vector3D.h:466
bool operator<(const Vector3D &rhs) const
Total ordering based on cylindrical radius first the z component second and azimuth angle third.
Definition: Vector3D.h:145
Vector3D & operator=(const TVector3 &tVector3)
Assignment translating from a TVector3 instance.
Definition: Vector3D.cc:38
std::string __str__() const
Output operator for python.
Definition: Vector3D.cc:82
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:244
Vector3D & scale(const double factor)
Scales the vector in place by the given factor.
Definition: Vector3D.h:253
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition: Vector3D.h:377
double unnormalizedOrthogonalComp(const Vector3D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition: Vector3D.h:454
double cotTheta() const
Getter for the cotangent of the polar angle.
Definition: Vector3D.h:558
double sinWith(const Vector3D &rhs) const
Sine of the angle between this and rhs.
Definition: Vector3D.h:231
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:340
static Vector3D getLowest()
Getter for the lowest possible vector.
Definition: Vector3D.h:153
static Vector3D average(const Vector3D &one, const Vector3D &two, const Vector3D &three)
Constructs the average of three vectors.
Definition: Vector3D.h:106
double dotXY(const Vector3D &rhs) const
Calculates the two dimensional dot product in xy projection.
Definition: Vector3D.h:180
double lambda() const
Getter for lambda.
Definition: Vector3D.h:552
Vector3D cross(const Vector3D &rhs) const
Calculated the three dimensional cross product.
Definition: Vector3D.h:186
Vector3D orthogonalVector(const Vector3D &relativTo) const
Calculates the part of this vector that is parallel to the given vector.
Definition: Vector3D.h:446
double cosWith(const Vector3D &rhs) const
Definition: Vector3D.h:225
double orthogonalComp(const Vector3D &relativTo) const
Calculates the component orthogonal to the given vector.
Definition: Vector3D.h:440
Vector3D(const Vector2D &xy, double z)
Constructor augmeting a Vector2D to a Vector3D setting z explicitly.
Definition: Vector3D.h:67
Vector3D parallelVector(const Vector3D &relativTo) const
Calculates the part of this vector that is parallel to the given vector.
Definition: Vector3D.h:422
double cylindricalR() const
Getter for the cylindrical radius ( xy projected norm )
Definition: Vector3D.h:534
Vector2D m_xy
Memory for the first and second coordinate available as a vector.
Definition: Vector3D.h:571
Vector3D(const Vector2D &xy)
Constructor augmeting a Vector2D to a Vector3D setting z to zero.
Definition: Vector3D.h:60
Vector3D operator+(const Vector3D &rhs) const
Returns a new vector as sum of this and rhs.
Definition: Vector3D.h:404
void passiveMoveBy(const Vector3D &by)
Passivelly moves the vector inplace by the given vector.
Definition: Vector3D.h:460
Vector3D operator*(const double factor) const
Same as scaled()
Definition: Vector3D.h:273
double dot(const Vector3D &rhs) const
Calculates the three dimensional dot product.
Definition: Vector3D.h:174
double parallelComp(const Vector3D &relativTo) const
Calculates the component parallel to the given vector.
Definition: Vector3D.h:416
Vector3D unit() const
Returns a unit vector colaligned with this.
Definition: Vector3D.h:359
double tanLambda() const
Getter for the tangent of lambda equivalent to cotTheta()
Definition: Vector3D.h:564
Vector3D operator-() const
Same as reversed()
Definition: Vector3D.h:398
double x() const
Getter for the x coordinate.
Definition: Vector3D.h:472
Vector3D & subtract(const Vector3D &rhs)
Subtracts a vector to this in place.
Definition: Vector3D.h:332
static Vector3D average(const Vector3D &one, const Vector3D &two)
Constructs the average of two vectors.
Definition: Vector3D.h:87
Vector3D & operator+=(const Vector2D &rhs)
Same as add()
Definition: Vector3D.h:326
Vector3D & operator+=(const Vector3D &rhs)
Same as add()
Definition: Vector3D.h:320
double crossXY(const Vector2D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition: Vector3D.h:200
Vector3D(double x, double y, double z)
Constructor from three coordinates.
Definition: Vector3D.h:53
Vector3D & operator*=(const double factor)
Same as scale()
Definition: Vector3D.h:261
const Vector2D & xy() const
Getter for the xy projected vector ( reference ! )
Definition: Vector3D.h:508
Vector3D & add(const Vector3D &rhs)
Adds a vector to this in place.
Definition: Vector3D.h:305
void setZ(const double z)
Setter for the z coordinate.
Definition: Vector3D.h:502
double angleWith(const Vector3D &rhs) const
The angle between this and rhs.
Definition: Vector3D.h:237
double phi() const
Getter for the azimuth angle.
Definition: Vector3D.h:540
double cylindricalRSquared() const
Getter for the squared cylindrical radius ( xy projected squared norm )
Definition: Vector3D.h:528
Vector3D operator/(const double denominator) const
Same as divided()
Definition: Vector3D.h:299
void setXY(const Vector2D &xy)
Setter for the xy projected vector.
Definition: Vector3D.h:514
double normSquared() const
Calculates the squared length of the vector.
Definition: Vector3D.h:206
bool isNull() const
Checks if the vector is the null vector.
Definition: Vector3D.h:159
Vector3D & divide(const double denominator)
Divides all coordinates by a common denominator in place.
Definition: Vector3D.h:279
double unnormalizedParallelComp(const Vector3D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition: Vector3D.h:430
Vector3D scaled(const double factor) const
Returns a scaled copy of the vector.
Definition: Vector3D.h:267
bool hasNAN() const
Checks if one of the coordinates is NAN.
Definition: Vector3D.h:165
bool operator==(const Vector3D &rhs) const
Equality comparison with all three coordinates.
Definition: Vector3D.h:132
void setY(const double y)
Setter for the y coordinate.
Definition: Vector3D.h:490
double y() const
Getter for the y coordinate.
Definition: Vector3D.h:484
double m_z
Memory for the third coordinate.
Definition: Vector3D.h:574
Vector3D & reverse()
Reverses the direction of the vector in place.
Definition: Vector3D.h:385
Vector3D & operator-=(const Vector2D &rhs)
Same as subtract()
Definition: Vector3D.h:353
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:313
void set(const double first, const double second, const double third)
Setter for all three coordinates.
Definition: Vector3D.h:520
double crossXY(const Vector3D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition: Vector3D.h:194
double norm() const
Calculates the length of the vector.
Definition: Vector3D.h:212
double z() const
Getter for the z coordinate.
Definition: Vector3D.h:496
void setX(const double x)
Setter for the x coordinate.
Definition: Vector3D.h:478
double theta() const
Getter for the polar angle.
Definition: Vector3D.h:546
Vector3D & operator-=(const Vector3D &rhs)
Same as subtract()
Definition: Vector3D.h:347
Vector3D reversed() const
Returns a vector pointing in the opposite direction.
Definition: Vector3D.h:392
Vector3D()
Default constructor for ROOT compatibility.
Definition: Vector3D.h:37
Vector3D & operator/=(const double denominator)
Same as divide()
Definition: Vector3D.h:287
Vector3D divided(const double denominator) const
Returns a copy where all coordinates got divided by a common denominator.
Definition: Vector3D.h:293
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition: B2Vector3.h:516
Abstract base class for different kinds of events.