Belle II Software prerelease-11-00-00a
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:
38 Vector3D() = default;
39
41 explicit Vector3D(const TVector3& tVector3);
42
44 explicit Vector3D(const B2Vector3D& b2Vector3);
45
47 Vector3D(const ROOT::Math::XYZVector& xyzVector);
48
50 Vector3D(double x, double y, double z)
51 : m_xy(x, y)
52 , m_z(z)
53 {
54 }
55
57 explicit Vector3D(const Vector2D& xy)
58 : m_xy(xy)
59 , m_z(0.0)
60 {
61 }
62
64 Vector3D(const Vector2D& xy, double z)
65 : m_xy(xy)
66 , m_z(z)
67 {
68 }
69
71 Vector3D& operator=(const TVector3& tVector3);
72
74 Vector3D& operator=(const B2Vector3D& b2Vector3);
75
77 Vector3D& operator=(const ROOT::Math::XYZVector& xyzVector);
78
80
84 static Vector3D average(const Vector3D& one, const Vector3D& two)
85 {
86 if (one.hasNAN()) {
87 return two;
88 } else if (two.hasNAN()) {
89 return one;
90 } else {
91 return Vector3D((one.x() + two.x()) / 2.0,
92 (one.y() + two.y()) / 2.0,
93 (one.z() + two.z()) / 2.0);
94 }
95 }
96
98
103 static Vector3D average(const Vector3D& one, const Vector3D& two, const Vector3D& three)
104 {
105
106 if (one.hasNAN()) {
107 return average(two, three);
108 } else if (two.hasNAN()) {
109 return average(one, three);
110 } else if (three.hasNAN()) {
111 return average(one, two);
112 } else {
113 return Vector3D((one.x() + two.x() + three.x()) / 3.0,
114 (one.y() + two.y() + three.y()) / 3.0,
115 (one.z() + two.z() + three.z()) / 3.0);
116 }
117 }
118
120 operator const TVector3() const;
121
123 operator const B2Vector3D() const;
124
126 operator const ROOT::Math::XYZVector() const;
127
129 bool operator==(const Vector3D& rhs) const
130 {
131 return x() == rhs.x() and y() == rhs.y() and z() == rhs.z();
132 }
133
136
142 bool operator<(const Vector3D& rhs) const
143 {
144 return norm() < rhs.norm() or (norm() == rhs.norm() and
145 (z() < rhs.z() or (z() == rhs.z() and (phi() < rhs.phi()))));
146 }
147
149
151 {
152 return Vector3D(0.0, 0.0, 0.0);
153 }
154
156 bool isNull() const
157 {
158 return x() == 0.0 and y() == 0.0 and z() == 0.0;
159 }
160
162 bool hasNAN() const
163 {
164 return std::isnan(x()) or std::isnan(y()) or std::isnan(z());
165 }
166
168 std::string __str__() const;
169
171 double dot(const Vector3D& rhs) const
172 {
173 return x() * rhs.x() + y() * rhs.y() + z() * rhs.z();
174 }
175
176 double Dot(const Vector3D& rhs) const
177 {
178 return dot(rhs);
179 }
180
182 double dotXY(const Vector3D& rhs) const
183 {
184 return x() * rhs.x() + y() * rhs.y();
185 }
186
188 Vector3D cross(const Vector3D& rhs) const
189 {
190 return Vector3D(y() * rhs.z() - z() * rhs.y(),
191 z() * rhs.x() - x() * rhs.z(),
192 x() * rhs.y() - y() * rhs.x());
193 }
194
196 double crossXY(const Vector3D& rhs) const
197 {
198 return xy().cross(rhs.xy());
199 }
200
202 double crossXY(const Vector2D& rhs) const
203 {
204 return xy().cross(rhs);
205 }
206
208 double normSquared() const
209 {
210 return x() * x() + y() * y() + z() * z();
211 }
212
213 double Mag2() const
214 {
215 return normSquared();
216 }
217
219 double norm() const
220 {
221 return hypot3(x(), y(), z());
222 }
223
232 double cosWith(const Vector3D& rhs) const
233 {
234 return dot(rhs) / (norm() * rhs.norm());
235 }
236
238 double sinWith(const Vector3D& rhs) const
239 {
240 return cross(rhs).norm() / (norm() * rhs.norm());
241 }
242
244 double angleWith(const Vector3D& rhs) const
245 {
246 return atan2(sinWith(rhs), cosWith(rhs));
247 }
248
249
251 double distance(const Vector3D& rhs = Vector3D(0.0, 0.0, 0.0)) const
252 {
253 double deltaX = x() - rhs.x();
254 double deltaY = y() - rhs.y();
255 double deltaZ = z() - rhs.z();
256 return hypot3(deltaX, deltaY, deltaZ);
257 }
258
260 Vector3D& scale(const double factor)
261 {
262 m_xy.scale(factor);
263 m_z *= factor;
264 return *this;
265 }
266
268 Vector3D& operator*=(const double factor)
269 {
270 return scale(factor);
271 }
272
274 Vector3D scaled(const double factor) const
275 {
276 return Vector3D(xy().scaled(factor), z() * factor);
277 }
278
280 Vector3D operator*(const double factor) const
281 {
282 return scaled(factor);
283 }
284
286 Vector3D& divide(const double denominator)
287 {
288 m_xy.divide(denominator);
289 m_z /= denominator;
290 return *this;
291 }
292
294 Vector3D& operator/=(const double denominator)
295 {
296 return divide(denominator);
297 }
298
300 Vector3D divided(const double denominator) const
301 {
302 return Vector3D(xy().divided(denominator), z() / denominator);
303 }
304
306 Vector3D operator/(const double denominator) const
307 {
308 return divided(denominator);
309 }
310
312 Vector3D& add(const Vector3D& rhs)
313 {
314 m_xy.add(rhs.xy());
315 m_z += rhs.z();
316 return *this;
317 }
318
320 Vector3D& add(const Vector2D& rhs)
321 {
322 m_xy.add(rhs);
323 return *this;
324 }
325
328 {
329 return add(rhs);
330 }
331
334 {
335 return add(rhs);
336 }
337
340 {
341 m_xy.subtract(rhs.xy());
342 m_z -= rhs.z();
343 return *this;
344 }
345
348 {
349 m_xy.subtract(rhs);
350 return *this;
351 }
352
355 {
356 return subtract(rhs);
357 }
358
361 {
362 return subtract(rhs);
363 }
364
367 {
368 return isNull() ? Vector3D(0.0, 0.0, 0.0) : divided(norm());
369 }
370
372
374 double normalize()
375 {
376 double result = norm();
377 if (result != 0.0) divide(result);
378 return result;
379 }
380
382
384 double normalizeTo(const double toLength)
385 {
386 double result = norm();
387 if (result != 0.0) scale(toLength / result);
388 return result;
389 }
390
393 {
394 scale(-1.0);
395 return *this;
396 }
397
400 {
401 return scaled(-1.0);
402 }
403
406 {
407 return reversed();
408 }
409
411 Vector3D operator+(const Vector3D& rhs) const
412 {
413 return Vector3D(xy() + rhs.xy(), z() + rhs.z());
414 }
415
417 Vector3D operator-(const Vector3D& rhs) const
418 {
419 return Vector3D(xy() - rhs.xy(), z() - rhs.z());
420 }
421
423 double parallelComp(const Vector3D& relativTo) const
424 {
425 return relativTo.dot(*this) / relativTo.norm();
426 }
427
429
431 double unnormalizedParallelComp(const Vector3D& relativTo) const
432 {
433 return relativTo.dot(*this);
434 }
435
437
441 double orthogonalComp(const Vector3D& relativTo) const
442 {
443 return relativTo.cross(*this).norm() / relativTo.norm();
444 }
445
447 Vector3D orthogonalVector(const Vector3D& relativTo) const
448 {
449 return *this - Belle2::VectorUtil::parallelVector(*this, relativTo);
450 }
451
453
455 double unnormalizedOrthogonalComp(const Vector3D& relativTo) const
456 {
457 return relativTo.cross(*this).norm();
458 }
459
461 void passiveMoveBy(const Vector3D& by)
462 {
463 subtract(by);
464 }
465
468 {
469 return *this - by;
470 }
471
473 double x() const
474 {
475 return m_xy.x();
476 }
477
479 void setX(const double x)
480 {
481 m_xy.setX(x);
482 }
483
485 double y() const
486 {
487 return m_xy.y();
488 }
489
491 void setY(const double y)
492 {
493 m_xy.setY(y);
494 }
495
497 double z() const
498 {
499 return m_z;
500 }
501
503 void setZ(const double z)
504 {
505 m_z = z;
506 }
507
509 const Vector2D& xy() const
510 {
511 return m_xy;
512 }
513
515 void setXY(const Vector2D& xy)
516 {
517 m_xy = xy;
518 }
519
521 void set(const double first, const double second, const double third)
522 {
523 setX(first);
524 setY(second);
525 setZ(third);
526 }
527
529 double cylindricalRSquared() const
530 {
531 return xy().normSquared();
532 }
533
535 double cylindricalR() const
536 {
537 return xy().norm();
538 }
539
541 double phi() const
542 {
543 return xy().phi();
544 }
545
547 double theta() const
548 {
549 return atan2(cylindricalR(), z());
550 }
551
553 double lambda() const
554 {
555 return atan2(z(), cylindricalR());
556 }
557
559 double cotTheta() const
560 {
561 return z() / cylindricalR();
562 }
563
565 double tanLambda() const
566 {
567 return z() / cylindricalR();
568 }
569
570 private:
572 Vector2D m_xy{0.0, 0.0};
573
575 double m_z = 0.0;
576 };
577
579 template<class Vector>
580 Vector3D operator- (const Vector& a, const Vector3D& b)
581 {
582 return Vector3D(a.X() - b.x(), a.Y() - b.y(), a.Z() - b.z());
583 }
584
586 std::ostream& operator<<(std::ostream& output, const Vector3D& vector3D);
587 }
589}
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:185
double phi() const
Gives the azimuth angle being the angle to the x axes ( range -M_PI to M_PI )
Definition Vector2D.h:596
double normSquared() const
Calculates .
Definition Vector2D.h:191
double norm() const
Calculates the length of the vector.
Definition Vector2D.h:202
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:417
double normalize()
Normalizes the vector to unit length.
Definition Vector3D.h:374
Vector3D passiveMovedBy(const Vector3D &by)
Passively moves the vector inplace by the given vector.
Definition Vector3D.h:467
bool operator<(const Vector3D &rhs) const
Total ordering based on cylindrical radius first the z component second and azimuth angle third.
Definition Vector3D.h:142
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:509
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:251
Vector3D & operator+=(const Vector3D &rhs)
Same as add()
Definition Vector3D.h:327
Vector3D & operator+=(const Vector2D &rhs)
Same as add()
Definition Vector3D.h:333
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition Vector3D.h:384
double unnormalizedOrthogonalComp(const Vector3D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition Vector3D.h:455
double cotTheta() const
Getter for the cotangent of the polar angle.
Definition Vector3D.h:559
double sinWith(const Vector3D &rhs) const
Sine of the angle between this and rhs.
Definition Vector3D.h:238
Vector3D & scale(const double factor)
Scales the vector in place by the given factor.
Definition Vector3D.h:260
static Vector3D getLowest()
Getter for the lowest possible vector.
Definition Vector3D.h:150
static Vector3D average(const Vector3D &one, const Vector3D &two, const Vector3D &three)
Constructs the average of three vectors.
Definition Vector3D.h:103
double dotXY(const Vector3D &rhs) const
Calculates the two dimensional dot product in xy projection.
Definition Vector3D.h:182
double lambda() const
Getter for lambda.
Definition Vector3D.h:553
Vector3D cross(const Vector3D &rhs) const
Calculated the three dimensional cross product.
Definition Vector3D.h:188
Vector3D orthogonalVector(const Vector3D &relativTo) const
Calculates the part of this vector that is orthogonal to the given vector.
Definition Vector3D.h:447
Vector3D & operator/=(const double denominator)
Same as divide()
Definition Vector3D.h:294
double cosWith(const Vector3D &rhs) const
Definition Vector3D.h:232
double orthogonalComp(const Vector3D &relativTo) const
Calculates the component orthogonal to the given vector.
Definition Vector3D.h:441
Vector3D & add(const Vector3D &rhs)
Adds a vector to this in place.
Definition Vector3D.h:312
Vector3D(const Vector2D &xy, double z)
Constructor augmeting a Vector2D to a Vector3D setting z explicitly.
Definition Vector3D.h:64
double cylindricalR() const
Getter for the cylindrical radius ( xy projected norm )
Definition Vector3D.h:535
Vector2D m_xy
Memory for the first and second coordinate available as a vector.
Definition Vector3D.h:572
Vector3D(const Vector2D &xy)
Constructor augmeting a Vector2D to a Vector3D setting z to zero.
Definition Vector3D.h:57
Vector3D operator+(const Vector3D &rhs) const
Returns a new vector as sum of this and rhs.
Definition Vector3D.h:411
void passiveMoveBy(const Vector3D &by)
Passively moves the vector inplace by the given vector.
Definition Vector3D.h:461
Vector3D operator*(const double factor) const
Same as scaled()
Definition Vector3D.h:280
double dot(const Vector3D &rhs) const
Calculates the three dimensional dot product.
Definition Vector3D.h:171
double parallelComp(const Vector3D &relativTo) const
Calculates the component parallel to the given vector.
Definition Vector3D.h:423
Vector3D unit() const
Returns a unit vector colaligned with this.
Definition Vector3D.h:366
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:320
double tanLambda() const
Getter for the tangent of lambda equivalent to cotTheta()
Definition Vector3D.h:565
Vector3D operator-() const
Same as reversed()
Definition Vector3D.h:405
double x() const
Getter for the x coordinate.
Definition Vector3D.h:473
static Vector3D average(const Vector3D &one, const Vector3D &two)
Constructs the average of two vectors.
Definition Vector3D.h:84
double Dot(const Vector3D &rhs) const
Calculates the three dimensional dot product, ROOT::Math compatible.
Definition Vector3D.h:176
Vector3D & divide(const double denominator)
Divides all coordinates by a common denominator in place.
Definition Vector3D.h:286
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:347
double crossXY(const Vector2D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition Vector3D.h:202
Vector3D(double x, double y, double z)
Constructor from three coordinates.
Definition Vector3D.h:50
Vector3D & operator*=(const double factor)
Same as scale()
Definition Vector3D.h:268
void setZ(const double z)
Setter for the z coordinate.
Definition Vector3D.h:503
double angleWith(const Vector3D &rhs) const
The angle between this and rhs.
Definition Vector3D.h:244
Vector3D()=default
Default constructor for ROOT compatibility.
double phi() const
Getter for the azimuth angle.
Definition Vector3D.h:541
double cylindricalRSquared() const
Getter for the squared cylindrical radius ( xy projected squared norm )
Definition Vector3D.h:529
Vector3D operator/(const double denominator) const
Same as divided()
Definition Vector3D.h:306
double Mag2() const
Alias for normSquared.
Definition Vector3D.h:213
void setXY(const Vector2D &xy)
Setter for the xy projected vector.
Definition Vector3D.h:515
double normSquared() const
Calculates the squared length of the vector.
Definition Vector3D.h:208
bool isNull() const
Checks if the vector is the null vector.
Definition Vector3D.h:156
double unnormalizedParallelComp(const Vector3D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition Vector3D.h:431
Vector3D scaled(const double factor) const
Returns a scaled copy of the vector.
Definition Vector3D.h:274
bool hasNAN() const
Checks if one of the coordinates is NAN.
Definition Vector3D.h:162
bool operator==(const Vector3D &rhs) const
Equality comparison with all three coordinates.
Definition Vector3D.h:129
void setY(const double y)
Setter for the y coordinate.
Definition Vector3D.h:491
double y() const
Getter for the y coordinate.
Definition Vector3D.h:485
Vector3D & subtract(const Vector3D &rhs)
Subtracts a vector to this in place.
Definition Vector3D.h:339
double m_z
Memory for the third coordinate.
Definition Vector3D.h:575
void set(const double first, const double second, const double third)
Setter for all three coordinates.
Definition Vector3D.h:521
double crossXY(const Vector3D &rhs) const
Calculates the two dimensional cross product in xy projection.
Definition Vector3D.h:196
double norm() const
Calculates the length of the vector.
Definition Vector3D.h:219
double z() const
Getter for the z coordinate.
Definition Vector3D.h:497
void setX(const double x)
Setter for the x coordinate.
Definition Vector3D.h:479
double theta() const
Getter for the polar angle.
Definition Vector3D.h:547
Vector3D & operator-=(const Vector3D &rhs)
Same as subtract()
Definition Vector3D.h:354
Vector3D & operator-=(const Vector2D &rhs)
Same as subtract()
Definition Vector3D.h:360
Vector3D reversed() const
Returns a vector pointing in the opposite direction.
Definition Vector3D.h:399
Vector3D & reverse()
Reverses the direction of the vector in place.
Definition Vector3D.h:392
Vector3D divided(const double denominator) const
Returns a copy where all coordinates got divided by a common denominator.
Definition Vector3D.h:300
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.