Belle II Software development
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/trackingUtilities/geometry/Vector2D.h>
11#include <tracking/trackingUtilities/numerics/Quadratic.h>
12
13#include <framework/geometry/B2Vector3.h>
14#include <framework/geometry/VectorUtil.h>
15#include <Math/Vector3D.h>
16
17#include <string>
18#include <iosfwd>
19#include <cmath>
20
21class TVector3;
22
23namespace Belle2 {
28 namespace TrackingUtilities {
29
31
34 class Vector3D {
35
36 public:
39 : m_xy(0.0, 0.0)
40 , m_z(0.0)
41 {
42 }
43
45 explicit Vector3D(const TVector3& tVector3);
46
48 explicit Vector3D(const B2Vector3D& b2Vector3);
49
51 Vector3D(const ROOT::Math::XYZVector& xyzVector);
52
54 Vector3D(double x, double y, double z)
55 : m_xy(x, y)
56 , m_z(z)
57 {
58 }
59
61 explicit Vector3D(const Vector2D& xy)
62 : m_xy(xy)
63 , m_z(0.0)
64 {
65 }
66
68 Vector3D(const Vector2D& xy, double z)
69 : m_xy(xy)
70 , m_z(z)
71 {
72 }
73
75 Vector3D& operator=(const TVector3& tVector3);
76
78 Vector3D& operator=(const B2Vector3D& b2Vector3);
79
81 Vector3D& operator=(const ROOT::Math::XYZVector& xyzVector);
82
84
88 static Vector3D average(const Vector3D& one, const Vector3D& two)
89 {
90 if (one.hasNAN()) {
91 return two;
92 } else if (two.hasNAN()) {
93 return one;
94 } else {
95 return Vector3D((one.x() + two.x()) / 2.0,
96 (one.y() + two.y()) / 2.0,
97 (one.z() + two.z()) / 2.0);
98 }
99 }
100
102
107 static Vector3D average(const Vector3D& one, const Vector3D& two, const Vector3D& three)
108 {
109
110 if (one.hasNAN()) {
111 return average(two, three);
112 } else if (two.hasNAN()) {
113 return average(one, three);
114 } else if (three.hasNAN()) {
115 return average(one, two);
116 } else {
117 return Vector3D((one.x() + two.x() + three.x()) / 3.0,
118 (one.y() + two.y() + three.y()) / 3.0,
119 (one.z() + two.z() + three.z()) / 3.0);
120 }
121 }
122
124 operator const TVector3() const;
125
127 operator const B2Vector3D() const;
128
130 operator const ROOT::Math::XYZVector() const;
131
133 bool operator==(const Vector3D& rhs) const
134 {
135 return x() == rhs.x() and y() == rhs.y() and z() == rhs.z();
136 }
137
140
146 bool operator<(const Vector3D& rhs) const
147 {
148 return norm() < rhs.norm() or (norm() == rhs.norm() and
149 (z() < rhs.z() or (z() == rhs.z() and (phi() < rhs.phi()))));
150 }
151
153
155 {
156 return Vector3D(0.0, 0.0, 0.0);
157 }
158
160 bool isNull() const
161 {
162 return x() == 0.0 and y() == 0.0 and z() == 0.0;
163 }
164
166 bool hasNAN() const
167 {
168 return std::isnan(x()) or std::isnan(y()) or std::isnan(z());
169 }
170
172 std::string __str__() const;
173
175 double dot(const Vector3D& rhs) const
176 {
177 return x() * rhs.x() + y() * rhs.y() + z() * rhs.z();
178 }
179
180 double Dot(const Vector3D& rhs) const
181 {
182 return dot(rhs);
183 }
184
186 double dotXY(const Vector3D& rhs) const
187 {
188 return x() * rhs.x() + y() * rhs.y();
189 }
190
192 Vector3D cross(const Vector3D& rhs) const
193 {
194 return Vector3D(y() * rhs.z() - z() * rhs.y(),
195 z() * rhs.x() - x() * rhs.z(),
196 x() * rhs.y() - y() * rhs.x());
197 }
198
200 double crossXY(const Vector3D& rhs) const
201 {
202 return xy().cross(rhs.xy());
203 }
204
206 double crossXY(const Vector2D& rhs) const
207 {
208 return xy().cross(rhs);
209 }
210
212 double normSquared() const
213 {
214 return x() * x() + y() * y() + z() * z();
215 }
216
217 double Mag2() const
218 {
219 return normSquared();
220 }
221
223 double norm() const
224 {
225 return hypot3(x(), y(), z());
226 }
227
236 double cosWith(const Vector3D& rhs) const
237 {
238 return dot(rhs) / (norm() * rhs.norm());
239 }
240
242 double sinWith(const Vector3D& rhs) const
243 {
244 return cross(rhs).norm() / (norm() * rhs.norm());
245 }
246
248 double angleWith(const Vector3D& rhs) const
249 {
250 return atan2(sinWith(rhs), cosWith(rhs));
251 }
252
253
255 double distance(const Vector3D& rhs = Vector3D(0.0, 0.0, 0.0)) const
256 {
257 double deltaX = x() - rhs.x();
258 double deltaY = y() - rhs.y();
259 double deltaZ = z() - rhs.z();
260 return hypot3(deltaX, deltaY, deltaZ);
261 }
262
264 Vector3D& scale(const double factor)
265 {
266 m_xy.scale(factor);
267 m_z *= factor;
268 return *this;
269 }
270
272 Vector3D& operator*=(const double factor)
273 {
274 return scale(factor);
275 }
276
278 Vector3D scaled(const double factor) const
279 {
280 return Vector3D(xy().scaled(factor), z() * factor);
281 }
282
284 Vector3D operator*(const double factor) const
285 {
286 return scaled(factor);
287 }
288
290 Vector3D& divide(const double denominator)
291 {
292 m_xy.divide(denominator);
293 m_z /= denominator;
294 return *this;
295 }
296
298 Vector3D& operator/=(const double denominator)
299 {
300 return divide(denominator);
301 }
302
304 Vector3D divided(const double denominator) const
305 {
306 return Vector3D(xy().divided(denominator), z() / denominator);
307 }
308
310 Vector3D operator/(const double denominator) const
311 {
312 return divided(denominator);
313 }
314
316 Vector3D& add(const Vector3D& rhs)
317 {
318 m_xy.add(rhs.xy());
319 m_z += rhs.z();
320 return *this;
321 }
322
324 Vector3D& add(const Vector2D& rhs)
325 {
326 m_xy.add(rhs);
327 return *this;
328 }
329
332 {
333 return add(rhs);
334 }
335
338 {
339 return add(rhs);
340 }
341
344 {
345 m_xy.subtract(rhs.xy());
346 m_z -= rhs.z();
347 return *this;
348 }
349
352 {
353 m_xy.subtract(rhs);
354 return *this;
355 }
356
359 {
360 return subtract(rhs);
361 }
362
365 {
366 return subtract(rhs);
367 }
368
371 {
372 return isNull() ? Vector3D(0.0, 0.0, 0.0) : divided(norm());
373 }
374
376
378 double normalize()
379 {
380 double result = norm();
381 if (result != 0.0) divide(result);
382 return result;
383 }
384
386
388 double normalizeTo(const double toLength)
389 {
390 double result = norm();
391 if (result != 0.0) scale(toLength / result);
392 return result;
393 }
394
397 {
398 scale(-1.0);
399 return *this;
400 }
401
404 {
405 return scaled(-1.0);
406 }
407
410 {
411 return reversed();
412 }
413
415 Vector3D operator+(const Vector3D& rhs) const
416 {
417 return Vector3D(xy() + rhs.xy(), z() + rhs.z());
418 }
419
421 Vector3D operator-(const Vector3D& rhs) const
422 {
423 return Vector3D(xy() - rhs.xy(), z() - rhs.z());
424 }
425
427 double parallelComp(const Vector3D& relativTo) const
428 {
429 return relativTo.dot(*this) / relativTo.norm();
430 }
431
433
435 double unnormalizedParallelComp(const Vector3D& relativTo) const
436 {
437 return relativTo.dot(*this);
438 }
439
441
445 double orthogonalComp(const Vector3D& relativTo) const
446 {
447 return relativTo.cross(*this).norm() / relativTo.norm();
448 }
449
451 Vector3D orthogonalVector(const Vector3D& relativTo) const
452 {
453 return *this - Belle2::VectorUtil::parallelVector(*this, relativTo);
454 }
455
457
459 double unnormalizedOrthogonalComp(const Vector3D& relativTo) const
460 {
461 return relativTo.cross(*this).norm();
462 }
463
465 void passiveMoveBy(const Vector3D& by)
466 {
467 subtract(by);
468 }
469
472 {
473 return *this - by;
474 }
475
477 double x() const
478 {
479 return m_xy.x();
480 }
481
483 void setX(const double x)
484 {
485 m_xy.setX(x);
486 }
487
489 double y() const
490 {
491 return m_xy.y();
492 }
493
495 void setY(const double y)
496 {
497 m_xy.setY(y);
498 }
499
501 double z() const
502 {
503 return m_z;
504 }
505
507 void setZ(const double z)
508 {
509 m_z = z;
510 }
511
513 const Vector2D& xy() const
514 {
515 return m_xy;
516 }
517
519 void setXY(const Vector2D& xy)
520 {
521 m_xy = xy;
522 }
523
525 void set(const double first, const double second, const double third)
526 {
527 setX(first);
528 setY(second);
529 setZ(third);
530 }
531
533 double cylindricalRSquared() const
534 {
535 return xy().normSquared();
536 }
537
539 double cylindricalR() const
540 {
541 return xy().norm();
542 }
543
545 double phi() const
546 {
547 return xy().phi();
548 }
549
551 double theta() const
552 {
553 return atan2(cylindricalR(), z());
554 }
555
557 double lambda() const
558 {
559 return atan2(z(), cylindricalR());
560 }
561
563 double cotTheta() const
564 {
565 return z() / cylindricalR();
566 }
567
569 double tanLambda() const
570 {
571 return z() / cylindricalR();
572 }
573
574 private:
577
579 double m_z;
580 };
581
583 template<class Vector>
584 Vector3D operator- (const Vector& a, const Vector3D& b)
585 {
586 return Vector3D(a.X() - b.x(), a.Y() - b.y(), a.Z() - b.z());
587 }
588
590 std::ostream& operator<<(std::ostream& output, const Vector3D& vector3D);
591 }
593}
A two dimensional vector which is equipped with functions for correct handling of orientation relate...
Definition Vector2D.h:36
double cross(const Vector2D &rhs) const
Calculated the two dimensional cross product.
Definition Vector2D.h:189
double phi() const
Gives the azimuth angle being the angle to the x axes ( range -M_PI to M_PI )
Definition Vector2D.h:600
double normSquared() const
Calculates .
Definition Vector2D.h:195
double norm() const
Calculates the length of the vector.
Definition Vector2D.h:206
A three dimensional vector.
Definition Vector3D.h:34
Vector3D operator-(const Vector3D &rhs) const
Returns a new vector as difference of this and rhs.
Definition Vector3D.h:421
double normalize()
Normalizes the vector to unit length.
Definition Vector3D.h:378
Vector3D passiveMovedBy(const Vector3D &by)
Passively moves the vector inplace by the given vector.
Definition Vector3D.h:471
bool operator<(const Vector3D &rhs) const
Total ordering based on cylindrical radius first the z component second and azimuth angle third.
Definition Vector3D.h:146
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
const Vector2D & xy() const
Getter for the xy projected vector ( reference ! )
Definition Vector3D.h:513
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:255
Vector3D & operator+=(const Vector3D &rhs)
Same as add()
Definition Vector3D.h:331
Vector3D & operator+=(const Vector2D &rhs)
Same as add()
Definition Vector3D.h:337
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition Vector3D.h:388
double unnormalizedOrthogonalComp(const Vector3D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition Vector3D.h:459
double cotTheta() const
Getter for the cotangent of the polar angle.
Definition Vector3D.h:563
double sinWith(const Vector3D &rhs) const
Sine of the angle between this and rhs.
Definition Vector3D.h:242
Vector3D & scale(const double factor)
Scales the vector in place by the given factor.
Definition Vector3D.h:264
static Vector3D getLowest()
Getter for the lowest possible vector.
Definition Vector3D.h:154
static Vector3D average(const Vector3D &one, const Vector3D &two, const Vector3D &three)
Constructs the average of three vectors.
Definition Vector3D.h:107
double dotXY(const Vector3D &rhs) const
Calculates the two dimensional dot product in xy projection.
Definition Vector3D.h:186
double lambda() const
Getter for lambda.
Definition Vector3D.h:557
Vector3D cross(const Vector3D &rhs) const
Calculated the three dimensional cross product.
Definition Vector3D.h:192
Vector3D orthogonalVector(const Vector3D &relativTo) const
Calculates the part of this vector that is orthogonal to the given vector.
Definition Vector3D.h:451
Vector3D & operator/=(const double denominator)
Same as divide()
Definition Vector3D.h:298
double cosWith(const Vector3D &rhs) const
Definition Vector3D.h:236
double orthogonalComp(const Vector3D &relativTo) const
Calculates the component orthogonal to the given vector.
Definition Vector3D.h:445
Vector3D & add(const Vector3D &rhs)
Adds a vector to this in place.
Definition Vector3D.h:316
Vector3D(const Vector2D &xy, double z)
Constructor augmeting a Vector2D to a Vector3D setting z explicitly.
Definition Vector3D.h:68
double cylindricalR() const
Getter for the cylindrical radius ( xy projected norm )
Definition Vector3D.h:539
Vector2D m_xy
Memory for the first and second coordinate available as a vector.
Definition Vector3D.h:576
Vector3D(const Vector2D &xy)
Constructor augmeting a Vector2D to a Vector3D setting z to zero.
Definition Vector3D.h:61
Vector3D operator+(const Vector3D &rhs) const
Returns a new vector as sum of this and rhs.
Definition Vector3D.h:415
void passiveMoveBy(const Vector3D &by)
Passively moves the vector inplace by the given vector.
Definition Vector3D.h:465
Vector3D operator*(const double factor) const
Same as scaled()
Definition Vector3D.h:284
double dot(const Vector3D &rhs) const
Calculates the three dimensional dot product.
Definition Vector3D.h:175
double parallelComp(const Vector3D &relativTo) const
Calculates the component parallel to the given vector.
Definition Vector3D.h:427
Vector3D unit() const
Returns a unit vector colaligned with this.
Definition Vector3D.h:370
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:324
double tanLambda() const
Getter for the tangent of lambda equivalent to cotTheta()
Definition Vector3D.h:569
Vector3D operator-() const
Same as reversed()
Definition Vector3D.h:409
double x() const
Getter for the x coordinate.
Definition Vector3D.h:477
static Vector3D average(const Vector3D &one, const Vector3D &two)
Constructs the average of two vectors.
Definition Vector3D.h:88
double Dot(const Vector3D &rhs) const
Calculates the three dimensional dot product, ROOT::Math compatible.
Definition Vector3D.h:180
Vector3D & divide(const double denominator)
Divides all coordinates by a common denominator in place.
Definition Vector3D.h:290
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:351
double crossXY(const Vector2D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition Vector3D.h:206
Vector3D(double x, double y, double z)
Constructor from three coordinates.
Definition Vector3D.h:54
Vector3D & operator*=(const double factor)
Same as scale()
Definition Vector3D.h:272
void setZ(const double z)
Setter for the z coordinate.
Definition Vector3D.h:507
double angleWith(const Vector3D &rhs) const
The angle between this and rhs.
Definition Vector3D.h:248
double phi() const
Getter for the azimuth angle.
Definition Vector3D.h:545
double cylindricalRSquared() const
Getter for the squared cylindrical radius ( xy projected squared norm )
Definition Vector3D.h:533
Vector3D operator/(const double denominator) const
Same as divided()
Definition Vector3D.h:310
double Mag2() const
Alias for normSquared.
Definition Vector3D.h:217
void setXY(const Vector2D &xy)
Setter for the xy projected vector.
Definition Vector3D.h:519
double normSquared() const
Calculates the squared length of the vector.
Definition Vector3D.h:212
bool isNull() const
Checks if the vector is the null vector.
Definition Vector3D.h:160
double unnormalizedParallelComp(const Vector3D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition Vector3D.h:435
Vector3D scaled(const double factor) const
Returns a scaled copy of the vector.
Definition Vector3D.h:278
bool hasNAN() const
Checks if one of the coordinates is NAN.
Definition Vector3D.h:166
bool operator==(const Vector3D &rhs) const
Equality comparison with all three coordinates.
Definition Vector3D.h:133
void setY(const double y)
Setter for the y coordinate.
Definition Vector3D.h:495
double y() const
Getter for the y coordinate.
Definition Vector3D.h:489
Vector3D & subtract(const Vector3D &rhs)
Subtracts a vector to this in place.
Definition Vector3D.h:343
double m_z
Memory for the third coordinate.
Definition Vector3D.h:579
void set(const double first, const double second, const double third)
Setter for all three coordinates.
Definition Vector3D.h:525
double crossXY(const Vector3D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition Vector3D.h:200
double norm() const
Calculates the length of the vector.
Definition Vector3D.h:223
double z() const
Getter for the z coordinate.
Definition Vector3D.h:501
void setX(const double x)
Setter for the x coordinate.
Definition Vector3D.h:483
double theta() const
Getter for the polar angle.
Definition Vector3D.h:551
Vector3D & operator-=(const Vector3D &rhs)
Same as subtract()
Definition Vector3D.h:358
Vector3D & operator-=(const Vector2D &rhs)
Same as subtract()
Definition Vector3D.h:364
Vector3D reversed() const
Returns a vector pointing in the opposite direction.
Definition Vector3D.h:403
Vector3D()
Default constructor for ROOT compatibility.
Definition Vector3D.h:38
Vector3D & reverse()
Reverses the direction of the vector in place.
Definition Vector3D.h:396
Vector3D divided(const double denominator) const
Returns a copy where all coordinates got divided by a common denominator.
Definition Vector3D.h:304
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition B2Vector3.h:516
HepGeom::Vector3D< double > Vector3D
3D Vector
Definition Cell.h:34
Abstract base class for different kinds of events.