Belle II Software development
Vector2D.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/numerics/Quadratic.h>
11
12#include <tracking/trackFindingCDC/numerics/EForwardBackward.h>
13#include <tracking/trackFindingCDC/numerics/ERightLeft.h>
14#include <tracking/trackFindingCDC/numerics/ERotation.h>
15#include <tracking/trackFindingCDC/numerics/ESign.h>
16
17#include <utility>
18#include <string>
19#include <iosfwd>
20#include <cmath>
21
22namespace Belle2 {
27 namespace TrackFindingCDC {
32 class Vector2D {
33
34 public:
37 : m_x(0.0)
38 , m_y(0.0)
39 {
40 }
41
43 Vector2D(const double x, const double y)
44 : m_x(x)
45 , m_y(y)
46 {
47 }
48
55 Vector2D(const Vector2D& coordinateVec, const double parallelCoor, const double orthoCoor)
56 : m_x(coordinateVec.x() * parallelCoor - coordinateVec.y() * orthoCoor)
57 , m_y(coordinateVec.y() * parallelCoor + coordinateVec.x() * orthoCoor)
58 {
59 }
60
62 static Vector2D Phi(const double phi)
63 {
64 return std::isnan(phi) ? Vector2D(0.0, 0.0) : Vector2D(cos(phi), sin(phi));
65 }
66
69
73 static Vector2D
74 compose(const Vector2D& coordinateVec, const double parallelCoor, const double orthoCoor)
75 {
76 return Vector2D(coordinateVec, parallelCoor, orthoCoor);
77 }
78
80
84 static Vector2D average(const Vector2D& one, const Vector2D& two)
85 {
86 if (one.hasNAN()) {
87 return two;
88 } else if (two.hasNAN()) {
89 return one;
90 } else {
91 return Vector2D((one.x() + two.x()) / 2.0, (one.y() + two.y()) / 2.0);
92 }
93 }
94
96
101 static Vector2D average(const Vector2D& one, const Vector2D& two, const Vector2D& three)
102 {
103
104 if (one.hasNAN()) {
105 return average(two, three);
106 } else if (two.hasNAN()) {
107 return average(one, three);
108 } else if (three.hasNAN()) {
109 return average(one, two);
110 } else {
111 return Vector2D((one.x() + two.x() + three.x()) / 3.0,
112 (one.y() + two.y() + three.y()) / 3.0);
113 }
114 }
115
117 bool operator==(const Vector2D& rhs) const
118 {
119 return x() == rhs.x() and y() == rhs.y();
120 }
121
123
129 bool operator<(const Vector2D& rhs) const
130 {
131 return normSquared() < rhs.normSquared() or
132 (normSquared() == rhs.normSquared() and (phi() < rhs.phi()));
133 }
134
136
138 {
139 return Vector2D(0.0, 0.0);
140 }
141
143 bool isNull() const
144 {
145 return x() == 0.0 and y() == 0.0;
146 }
147
149 bool hasNAN() const
150 {
151 return std::isnan(x()) or std::isnan(y());
152 }
153
155 std::string __str__() const;
156
158 double dot(const Vector2D& rhs) const
159 {
160 return x() * rhs.x() + y() * rhs.y();
161 }
163 double cross(const Vector2D& rhs) const
164 {
165 return x() * rhs.y() - y() * rhs.x();
166 }
167
169 double normSquared() const
170 {
171 return x() * x() + y() * y();
172 }
173
175 double norm() const
176 {
177 return hypot2(x(), y());
178 }
179
187 double cosWith(const Vector2D& rhs) const
188 {
189 return dot(rhs) / (norm() * rhs.norm());
190 }
192 double sinWith(const Vector2D& rhs) const
193 {
194 return cross(rhs) / (norm() * rhs.norm());
195 }
197 double angleWith(const Vector2D& rhs) const
198 {
199 return atan2(cross(rhs), dot(rhs));
200 }
202
204 double distance(const Vector2D& rhs = Vector2D(0.0, 0.0)) const
205 {
206 double deltaX = x() - rhs.x();
207 double deltaY = y() - rhs.y();
208 return hypot2(deltaX, deltaY);
209 }
210
212 Vector2D& scale(const double factor)
213 {
214 m_x *= factor;
215 m_y *= factor;
216 return *this;
217 }
219 Vector2D& operator*=(const double factor)
220 {
221 return scale(factor);
222 }
223
225 Vector2D scaled(const double factor) const
226 {
227 return Vector2D(x() * factor, y() * factor);
228 }
229
231 friend Vector2D operator*(const Vector2D& vec2D, const double factor)
232 {
233 return vec2D.scaled(factor);
234 }
235
237 Vector2D& divide(const double denominator)
238 {
239 m_x /= denominator;
240 m_y /= denominator;
241 return *this;
242 }
243
245 Vector2D& operator/=(const double denominator)
246 {
247 return divide(denominator);
248 }
249
251 Vector2D divided(const double denominator) const
252 {
253 return Vector2D(x() / denominator, y() / denominator);
254 }
256 Vector2D operator/(const double denominator) const
257 {
258 return divided(denominator);
259 }
260
262 Vector2D& add(const Vector2D& rhs)
263 {
264 m_x += rhs.x();
265 m_y += rhs.y();
266 return *this;
267 }
268
271 {
272 return add(rhs);
273 }
274
277 {
278 m_x -= rhs.x();
279 m_y -= rhs.y();
280 return *this;
281 }
284 {
285 return subtract(rhs);
286 }
287
290 {
291 return Vector2D(-y(), x());
292 }
293
295 Vector2D orthogonal(const ERotation ccwInfo) const
296 {
297 return isValid(ccwInfo) ? Vector2D(-static_cast<double>(ccwInfo) * y(), static_cast<double>(ccwInfo) * x()) : Vector2D();
298 }
299
301
303 double normalize()
304 {
305 double originalLength = norm();
306 if (originalLength != 0.0) divide(originalLength);
307 return originalLength;
308 }
309
311
313 double normalizeTo(const double toLength)
314 {
315 double originalLength = norm();
316 if (originalLength != 0.0) scale(toLength / originalLength);
317 return originalLength;
318 }
319
322 {
323 return isNull() ? Vector2D(0.0, 0.0) : divided(norm());
324 }
325
328 {
329 scale(-1.0);
330 return *this;
331 }
332
335 {
336 return scaled(-1.0);
337 }
340 {
341 return reversed();
342 }
343
346 {
347 m_x = -x();
348 }
349
352 {
353 m_y = -y();
354 }
355
359 {
360 return Vector2D(-x(), y());
361 }
362
366 {
367 return Vector2D(x(), -y());
368 }
369
371 Vector2D flippedOver(const Vector2D& reflectionLine) const
372 {
373 return *this - orthogonalVector(reflectionLine) * 2;
374 }
375
377 Vector2D flippedAlong(const Vector2D& flippingDirection) const
378 {
379 return *this - parallelVector(flippingDirection) * 2;
380 }
381
383
386 {
388 }
389
391
394 {
395 return divided(normSquared());
396 }
397
399 Vector2D operator+(const Vector2D& rhs) const
400 {
401 return Vector2D(x() + rhs.x(), y() + rhs.y());
402 }
403
405 Vector2D operator-(const Vector2D& rhs) const
406 {
407 return Vector2D(x() - rhs.x(), y() - rhs.y());
408 }
409
411 double parallelComp(const Vector2D& relativTo) const
412 {
413 return relativTo.dot(*this) / relativTo.norm();
414 }
415
417 Vector2D parallelVector(const Vector2D& relativTo) const
418 {
419 return relativTo.scaled(relativTo.dot(*this) / relativTo.normSquared());
420 }
421
423
425 double unnormalizedParallelComp(const Vector2D& relativTo) const
426 {
427 return relativTo.dot(*this);
428 }
429
431
432 double orthogonalComp(const Vector2D& relativTo) const
433 {
434 return relativTo.cross(*this) / relativTo.norm();
435 }
436
438 Vector2D orthogonalVector(const Vector2D& relativTo) const
439 {
440 return relativTo.scaled(relativTo.cross(*this) / relativTo.normSquared()).orthogonal();
441 }
442
444
446 double unnormalizedOrthogonalComp(const Vector2D& relativTo) const
447 {
448 return relativTo.cross(*this);
449 }
450
454 {
455 return static_cast<ERightLeft>(-sign(unnormalizedOrthogonalComp(rhs)));
456 }
457
459 bool isLeftOf(const Vector2D& rhs) const
460 {
461 return isRightOrLeftOf(rhs) == ERightLeft::c_Left;
462 }
463
465 bool isRightOf(const Vector2D& rhs) const
466 {
467 return isRightOrLeftOf(rhs) == ERightLeft::c_Right;
468 }
469
473 {
474 return static_cast<ERotation>(sign(unnormalizedOrthogonalComp(rhs)));
475 }
476
479 bool isCCWOf(const Vector2D& rhs) const
480 {
481 return isCCWOrCWOf(rhs) == ERotation::c_CounterClockwise;
482 }
483
486 bool isCWOf(const Vector2D& rhs) const
487 {
488 return isCCWOrCWOf(rhs) == ERotation::c_Clockwise;
489 }
490
494 {
495 return static_cast<EForwardBackward>(sign(unnormalizedParallelComp(rhs)));
496 }
497
500 bool isForwardOf(const Vector2D& rhs) const
501 {
502 return isForwardOrBackwardOf(rhs) == EForwardBackward::c_Forward;
503 }
504
507 bool isBackwardOf(const Vector2D& rhs) const
508 {
509 return isForwardOrBackwardOf(rhs) == EForwardBackward::c_Backward;
510 }
511
512 private:
514 static bool sameSign(float n1, float n2, float n3)
515 {
516 return ((n1 > 0 and n2 > 0 and n3 > 0) or (n1 < 0 and n2 < 0 and n3 < 0));
517 }
518
519 public:
525 bool isBetween(const Vector2D& lower, const Vector2D& upper) const
526 {
527 // Set up a linear (nonorthogonal) transformation that maps
528 // lower -> (1, 0)
529 // upper -> (0, 1)
530 // Check whether this transformation is orientation conserving
531 // If yes this vector must lie in the first quadrant to be between lower and upper
532 // If no it must lie in some other quadrant.
533 double det = lower.cross(upper);
534 if (det == 0) {
535 // lower and upper are coaligned
536 return isRightOf(lower) and isLeftOf(upper);
537 } else {
538 bool flipsOrientation = det < 0;
539
540 double transformedX = cross(upper);
541 double transformedY = -cross(lower);
542 bool inFirstQuadrant = sameSign(det, transformedX, transformedY);
543 if (flipsOrientation) {
544 inFirstQuadrant = not inFirstQuadrant;
545 }
546 return inFirstQuadrant;
547 }
548 }
549
552 {
553 std::swap(m_x, m_y);
554 }
555
557 double cylindricalR() const
558 {
559 return hypot2(x(), y());
560 }
561
563 void setCylindricalR(const double cylindricalR)
564 {
566 }
567
569 double phi() const
570 {
571 return isNull() ? NAN : atan2(y(), x());
572 }
573
575 void passiveMoveBy(const Vector2D& by)
576 {
577 subtract(by);
578 }
579
582 {
583 return *this - by;
584 }
585
589 Vector2D passiveRotatedBy(const Vector2D& phiVec) const
590 {
592 }
593
595 double x() const
596 {
597 return m_x;
598 }
600 void setX(const double x)
601 {
602 m_x = x;
603 }
605 double y() const
606 {
607 return m_y;
608 }
610 void setY(const double y)
611 {
612 m_y = y;
613 }
614
616 void setXY(const double x, const double y)
617 {
618 setX(x);
619 setY(y);
620 }
622 void setXY(const Vector2D& xy)
623 {
624 m_x = xy.x();
625 m_y = xy.y();
626 }
627
629 double first() const
630 {
631 return m_x;
632 }
634 void setFirst(const double first)
635 {
636 m_x = first;
637 }
639 double second() const
640 {
641 return m_y;
642 }
644 void setSecond(const double second)
645 {
646 m_y = second;
647 }
648
650 void set(const double first, const double second)
651 {
652 setX(first);
653 setY(second);
654 }
656 void set(const Vector2D& both)
657 {
658 m_x = both.x();
659 m_y = both.y();
660 }
661
662 private:
664 double m_x;
665
667 double m_y;
668 };
669
671 std::ostream& operator<<(std::ostream& output, const Vector2D& vector2D);
672 }
674}
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:32
void setSecond(const double second)
Setter for the second coordinate.
Definition: Vector2D.h:644
double normalize()
Normalizes the vector to unit length.
Definition: Vector2D.h:303
void swapCoordinates()
Swaps the coordinates in place.
Definition: Vector2D.h:551
Vector2D & divide(const double denominator)
Divides all coordinates by a common denominator in place.
Definition: Vector2D.h:237
double parallelComp(const Vector2D &relativTo) const
Calculates the component parallel to the given vector.
Definition: Vector2D.h:411
void flipSecond()
Flips the first coordinate inplace (no difference between active and passive)
Definition: Vector2D.h:351
Vector2D(const Vector2D &coordinateVec, const double parallelCoor, const double orthoCoor)
Constructs a vector from a unit coordinate system vector and the coordinates in that system.
Definition: Vector2D.h:55
std::string __str__() const
Output operator for python.
Definition: Vector2D.cc:22
Vector2D & operator-=(const Vector2D &rhs)
Same as subtract()
Definition: Vector2D.h:283
Vector2D flippedSecond() const
Makes a copy of the vector with the second coordinate flipped (no difference between active and passi...
Definition: Vector2D.h:365
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition: Vector2D.h:313
Vector2D(const double x, const double y)
Constructor from two coordinates.
Definition: Vector2D.h:43
Vector2D & scale(const double factor)
Scales the vector in place by the given factor.
Definition: Vector2D.h:212
static Vector2D compose(const Vector2D &coordinateVec, const double parallelCoor, const double orthoCoor)
Constructs a vector from a unit coordinate system vector and the coordinates in that system.
Definition: Vector2D.h:74
Vector2D operator-(const Vector2D &rhs) const
Returns a new vector as differenc of this and rhs.
Definition: Vector2D.h:405
double dot(const Vector2D &rhs) const
Calculates the two dimensional dot product.
Definition: Vector2D.h:158
static Vector2D average(const Vector2D &one, const Vector2D &two)
Constructs the average of two vectors.
Definition: Vector2D.h:84
Vector2D reversed() const
Returns a vector pointing in the opposite direction.
Definition: Vector2D.h:334
void setXY(const double x, const double y)
Setter for both coordinate.
Definition: Vector2D.h:616
static bool sameSign(float n1, float n2, float n3)
Check if three values have the same sign.
Definition: Vector2D.h:514
Vector2D & add(const Vector2D &rhs)
Adds a vector to this in place.
Definition: Vector2D.h:262
Vector2D scaled(const double factor) const
Returns a scaled copy of the vector.
Definition: Vector2D.h:225
void set(const double first, const double second)
Setter for both coordinate.
Definition: Vector2D.h:650
double distance(const Vector2D &rhs=Vector2D(0.0, 0.0)) const
Calculates the distance of this point to the rhs.
Definition: Vector2D.h:204
double cylindricalR() const
Gives the cylindrical radius of the vector. Same as norm()
Definition: Vector2D.h:557
double unnormalizedParallelComp(const Vector2D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition: Vector2D.h:425
double cosWith(const Vector2D &rhs) const
Definition: Vector2D.h:187
void flipFirst()
Flips the first coordinate inplace (no difference between active and passive)
Definition: Vector2D.h:345
void passiveMoveBy(const Vector2D &by)
Passivelly moves the vector inplace by the given vector.
Definition: Vector2D.h:575
Vector2D orthogonalVector(const Vector2D &relativTo) const
Calculates the part of this vector that is parallel to the given vector.
Definition: Vector2D.h:438
Vector2D operator-() const
Same as reversed()
Definition: Vector2D.h:339
EForwardBackward isForwardOrBackwardOf(const Vector2D &rhs) const
Indicates if the given vector is more coaligned or reverse if you looked in the direction of this vec...
Definition: Vector2D.h:493
ERotation isCCWOrCWOf(const Vector2D &rhs) const
Indicates if the given vector is more counterclockwise or more clockwise if you looked in the directi...
Definition: Vector2D.h:472
void set(const Vector2D &both)
Setter for both coordinate by an other vector.
Definition: Vector2D.h:656
Vector2D flippedFirst() const
Makes a copy of the vector with the first coordinate flipped (no difference between active and passiv...
Definition: Vector2D.h:358
friend Vector2D operator*(const Vector2D &vec2D, const double factor)
Same as scaled()
Definition: Vector2D.h:231
Vector2D operator+(const Vector2D &rhs) const
Returns a new vector as sum of this and rhs.
Definition: Vector2D.h:399
double cross(const Vector2D &rhs) const
Calculated the two dimensional cross product.
Definition: Vector2D.h:163
Vector2D & operator*=(const double factor)
Same as scale()
Definition: Vector2D.h:219
Vector2D & reverse()
Reverses the direction of the vector in place.
Definition: Vector2D.h:327
double m_x
Memory for the first coordinate.
Definition: Vector2D.h:664
Vector2D orthogonal(const ERotation ccwInfo) const
Orthogonal vector to the direction given by the counterclockwise info.
Definition: Vector2D.h:295
Vector2D()
Default constructor for ROOT compatibility.
Definition: Vector2D.h:36
ERightLeft isRightOrLeftOf(const Vector2D &rhs) const
Indicates if the given vector is more left or more right if you looked in the direction of this vecto...
Definition: Vector2D.h:453
Vector2D operator/(const double denominator) const
Same as divided()
Definition: Vector2D.h:256
double x() const
Getter for the x coordinate.
Definition: Vector2D.h:595
double sinWith(const Vector2D &rhs) const
Sine of the angle between this and rhs.
Definition: Vector2D.h:192
Vector2D & subtract(const Vector2D &rhs)
Subtracts a vector from this in place.
Definition: Vector2D.h:276
Vector2D conformalTransformed() const
Returns a copy of the vector transformed in conformal space.
Definition: Vector2D.h:393
Vector2D & operator/=(const double denominator)
Same as divide()
Definition: Vector2D.h:245
bool isRightOf(const Vector2D &rhs) const
Indicates if the given vector is more right if you looked in the direction of this vector.
Definition: Vector2D.h:465
void conformalTransform()
Transforms the vector to conformal space inplace.
Definition: Vector2D.h:385
bool isCCWOf(const Vector2D &rhs) const
Indicates if the given vector is more counterclockwise if you looked in the direction of this vector.
Definition: Vector2D.h:479
double orthogonalComp(const Vector2D &relativTo) const
Calculates the component orthogonal to the given vector.
Definition: Vector2D.h:432
void setCylindricalR(const double cylindricalR)
Set the cylindrical radius while keeping the azimuth angle phi the same.
Definition: Vector2D.h:563
Vector2D & operator+=(const Vector2D &rhs)
Same as add()
Definition: Vector2D.h:270
static Vector2D average(const Vector2D &one, const Vector2D &two, const Vector2D &three)
Constructs the average of three vectors.
Definition: Vector2D.h:101
bool isBetween(const Vector2D &lower, const Vector2D &upper) const
Checks if this vector is between two other vectors Between means here that when rotating the lower ve...
Definition: Vector2D.h:525
double phi() const
Gives the azimuth angle being the angle to the x axes ( range -M_PI to M_PI )
Definition: Vector2D.h:569
bool operator==(const Vector2D &rhs) const
Equality comparison with both coordinates.
Definition: Vector2D.h:117
static Vector2D getLowest()
Getter for the lowest possible vector.
Definition: Vector2D.h:137
Vector2D parallelVector(const Vector2D &relativTo) const
Calculates the part of this vector that is parallel to the given vector.
Definition: Vector2D.h:417
double second() const
Getter for the second coordinate.
Definition: Vector2D.h:639
void setXY(const Vector2D &xy)
Setter for both coordinate by an other vector.
Definition: Vector2D.h:622
double normSquared() const
Calculates .
Definition: Vector2D.h:169
bool isNull() const
Checks if the vector is the null vector.
Definition: Vector2D.h:143
Vector2D orthogonal() const
Orthogonal vector to the counterclockwise direction.
Definition: Vector2D.h:289
bool hasNAN() const
Checks if one of the coordinates is NAN.
Definition: Vector2D.h:149
Vector2D divided(const double denominator) const
Returns a copy where all coordinates got divided by a common denominator.
Definition: Vector2D.h:251
void setY(const double y)
Setter for the y coordinate.
Definition: Vector2D.h:610
double y() const
Getter for the y coordinate.
Definition: Vector2D.h:605
double first() const
Getter for the first coordinate.
Definition: Vector2D.h:629
Vector2D flippedOver(const Vector2D &reflectionLine) const
Reflects this vector over line designated by the given vector.
Definition: Vector2D.h:371
Vector2D flippedAlong(const Vector2D &flippingDirection) const
Reflects this vector along line designated by the given vector.
Definition: Vector2D.h:377
Vector2D unit() const
Returns a unit vector colaligned with this.
Definition: Vector2D.h:321
double angleWith(const Vector2D &rhs) const
The angle between this and rhs.
Definition: Vector2D.h:197
double norm() const
Calculates the length of the vector.
Definition: Vector2D.h:175
Vector2D passiveRotatedBy(const Vector2D &phiVec) const
Returns a transformed vector version rotated by the given vector.
Definition: Vector2D.h:589
double m_y
Memory for the second coordinate.
Definition: Vector2D.h:667
bool isLeftOf(const Vector2D &rhs) const
Indicates if the given vector is more left if you looked in the direction of this vector.
Definition: Vector2D.h:459
void setFirst(const double first)
Setter for the first coordinate.
Definition: Vector2D.h:634
bool isForwardOf(const Vector2D &rhs) const
Indicates if the given vector is more coaligned if you looked in the direction of this vector.
Definition: Vector2D.h:500
void setX(const double x)
Setter for the x coordinate.
Definition: Vector2D.h:600
bool isCWOf(const Vector2D &rhs) const
Indicates if the given vector is more clockwise if you looked in the direction of this vector.
Definition: Vector2D.h:486
bool isBackwardOf(const Vector2D &rhs) const
Indicates if the given vector is more Reverse if you looked in the direction of this vector.
Definition: Vector2D.h:507
double unnormalizedOrthogonalComp(const Vector2D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition: Vector2D.h:446
bool operator<(const Vector2D &rhs) const
Total ordering based on cylindrical radius first and azimuth angle second.
Definition: Vector2D.h:129
static Vector2D Phi(const double phi)
Constucts a unit vector with azimuth angle equal to phi.
Definition: Vector2D.h:62
Vector2D passiveMovedBy(const Vector2D &by) const
Returns a transformed vector passivelly moved by the given vector.
Definition: Vector2D.h:581
EForwardBackward
Enumeration to represent the distinct possibilities of the right left passage information.
ERightLeft
Enumeration to represent the distinct possibilities of the right left passage.
Definition: ERightLeft.h:25
ERotation
Enumeration to represent the distinct possibilities of the right left passage information.
Definition: ERotation.h:25
Abstract base class for different kinds of events.