Belle II Software development
GeneralizedCircle.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/Line2D.h>
11#include <tracking/trackingUtilities/geometry/VectorUtil.h>
12
13#include <tracking/trackingUtilities/numerics/EForwardBackward.h>
14#include <tracking/trackingUtilities/numerics/ERightLeft.h>
15#include <tracking/trackingUtilities/numerics/ERotation.h>
16#include <tracking/trackingUtilities/numerics/ESign.h>
17
18#include <Math/Vector2D.h>
19
20#include <utility>
21#include <iosfwd>
22#include <cmath>
23
24namespace Belle2 {
29
30 namespace TrackingUtilities {
31 class Circle2D;
32
54
55 public:
57 GeneralizedCircle() = default;
58
60 GeneralizedCircle(double n0, double n1, double n2, double n3 = 0);
61
63 GeneralizedCircle(double n0, const ROOT::Math::XYVector& n12, double n3 = 0);
64
66 explicit GeneralizedCircle(const Line2D& n012);
67
69 explicit GeneralizedCircle(const Circle2D& circle);
70
80 fromCenterAndRadius(const ROOT::Math::XYVector& center,
81 double absRadius,
82 ERotation orientation = ERotation::c_CounterClockwise);
83
89 fromPerigeeParameters(double curvature, const ROOT::Math::XYVector& tangential, double impact);
90
97
98 protected:
104 void setN0(const double n0)
105 {
106 m_n0 = n0;
107 }
108
114 void setN1(const double n1)
115 {
116 m_n12.SetX(n1);
117 }
118
124 void setN2(const double n2)
125 {
126 m_n12.SetY(n2);
127 }
128
134 void setN12(const double n1, const double n2)
135 {
136 m_n12.SetXY(n1, n2);
137 }
138
144 void setN12(const ROOT::Math::XYVector& n12)
145 {
146 m_n12.SetXY(n12.X(), n12.Y());
147 }
148
154 void setN3(const double n3)
155 {
156 m_n3 = n3;
157 }
158
159 public:
161 void setCenterAndRadius(const ROOT::Math::XYVector& center,
162 double absRadius,
163 ERotation orientation = ERotation::c_CounterClockwise);
164
166 void setPerigeeParameters(double curvature, const ROOT::Math::XYVector& tangential, double impact);
167
169 void
170 setPerigeeParameters(const double curvature, const double tangentialPhi, const double impact)
171 {
173 }
174
180 void setN(const double n0, const double n1, const double n2, const double n3 = 0.0)
181 {
182 setN0(n0);
183 setN12(n1, n2);
184 setN3(n3);
185 normalize();
186 }
187
193 void setN(const double n0, const ROOT::Math::XYVector& n12, const double n3 = 0.0)
194 {
195 setN0(n0);
196 setN12(n12);
197 setN3(n3);
198 normalize();
199 }
200
202 void setN(const Line2D& n012)
203 {
204 setN(n012.n0(), n012.n12());
205 }
206
208 void setN(const GeneralizedCircle& n0123)
209 {
210 setN(n0123.n0(), n0123.n12(), n0123.n3());
211 }
212
215 {
216 setN(0.0, 0.0, 0.0, 0.0);
217 }
218
220 void reverse()
221 {
222 scaleN(-1);
223 }
224
232 {
233 std::swap(m_n0, m_n3);
234 reverse(); // Correct orientation
235 }
236
241 void passiveMoveBy(const ROOT::Math::XYVector& by)
242 {
243 setN(fastDistance(by), gradient(by), n3());
244 }
245
246 protected:
255 {
256 double normalization_squared = normalizationSquared();
257 if (normalization_squared > 0) scaleN(1.0 / std::sqrt(normalization_squared));
258 }
259
260 private:
262 void scaleN(const double factor)
263 {
264 m_n0 *= factor;
265 m_n12 *= factor;
266 m_n3 *= factor;
267 }
268
269 public:
271 double n0() const
272 {
273 return m_n0;
274 }
275
277 double n1() const
278 {
279 return m_n12.x();
280 }
281
283 double n2() const
284 {
285 return m_n12.y();
286 }
287
289 const ROOT::Math::XYVector& n12() const
290 {
291 return m_n12;
292 }
293
295 double n3() const
296 {
297 return m_n3;
298 }
299
300 public:
302 bool isInvalid() const
303 {
304 return n0() == 0 and VectorUtil::isNull(n12()) and n3() == 0;
305 }
306
308 bool isValid() const
309 {
310 return not isInvalid();
311 }
312
317 double normalizationSquared() const
318 {
319 return n12().Mag2() - 4 * n0() * n3();
320 }
321
322 public:
325 {
326 return GeneralizedCircle(-n0(), -n12(), -n3());
327 }
328
329 public:
338 {
339 return GeneralizedCircle(-n3(), -n12(), -n0());
340 }
341
342 public:
349 ROOT::Math::XYVector gradient(const ROOT::Math::XYVector& point) const
350 {
351 return point * (2.0 * n3()) + n12();
352 }
353
363 ROOT::Math::XYVector normal(const ROOT::Math::XYVector& point) const
364 {
365 return VectorUtil::unit(gradient(point));
366 }
367
376 ROOT::Math::XYVector tangential(const ROOT::Math::XYVector& point) const
377 {
378 return VectorUtil::Orthogonal(normal(point));
379 }
380
387 ROOT::Math::XYVector closest(const ROOT::Math::XYVector& point) const;
388
392 ROOT::Math::XYVector perigee() const;
393
398 ROOT::Math::XYVector apogee() const;
399
410 EForwardBackward isForwardOrBackwardOf(const ROOT::Math::XYVector& from, const ROOT::Math::XYVector& to) const
411 {
412 ROOT::Math::XYVector difference = to - from;
413 ROOT::Math::XYVector tangentialAtFrom = tangential(from);
414 return VectorUtil::isForwardOrBackwardOf(tangentialAtFrom, difference);
415 }
416
431 ROOT::Math::XYVector
432 chooseNextForwardOf(const ROOT::Math::XYVector& start, const ROOT::Math::XYVector& end1, const ROOT::Math::XYVector& end2) const;
433
435 std::pair<ROOT::Math::XYVector, ROOT::Math::XYVector>
436 atCylindricalR(double cylindricalR) const;
437
451 ROOT::Math::XYVector atCylindricalRForwardOf(const ROOT::Math::XYVector& startPoint, double cylindricalR) const;
452
461 double fastDistance(const ROOT::Math::XYVector& point) const
462 {
463 return n0() + point.Dot(n12()) + point.Mag2() * n3();
464 }
465
467 double fastImpact() const
468 {
469 return n0();
470 }
471
476 double distance(const ROOT::Math::XYVector& point) const;
477
483 double distance(double fastDistance) const;
484
489 double fastDistance(const double distance) const
490 {
491 return (distance * n3() + 1.0) * distance;
492 }
493
495 double impact() const
496 {
497 return distance(fastImpact());
498 }
499
501 double d0() const
502 {
503 return -impact();
504 }
505
507 ROOT::Math::XYVector tangential() const
508 {
509 return VectorUtil::unit(tangential(ROOT::Math::XYVector(0.0, 0.0)));
510 }
511
513 double tangentialPhi() const
514 {
515 return tangential().Phi();
516 }
517
519 double minimalCylindricalR() const
520 {
521 return std::fabs(impact());
522 }
523
525 double maximalCylindricalR() const
526 {
527 return std::fabs(impact() + 2 * radius());
528 }
529
531 double absDistance(const ROOT::Math::XYVector& point) const
532 {
533 return fabs(distance(point));
534 }
535
540 ERightLeft isRightOrLeft(const ROOT::Math::XYVector& point) const
541 {
542 return static_cast<ERightLeft>(sign(fastDistance(point)));
543 }
544
546 bool isLeft(const ROOT::Math::XYVector& rhs) const
547 {
548 return isRightOrLeft(rhs) == ERightLeft::c_Left;
549 }
550
552 bool isRight(const ROOT::Math::XYVector& rhs) const
553 {
554 return isRightOrLeft(rhs) == ERightLeft::c_Right;
555 }
556
558 bool isLine() const
559 {
560 return n3() == 0.0;
561 }
562
564 bool isCircle() const
565 {
566 return n3() != 0.0;
567 }
568
570 double radius() const
571 {
572 return 1 / curvature();
573 }
574
576 double absRadius() const
577 {
578 return fabs(radius());
579 }
580
582 double curvature() const
583 {
584 return 2 * n3();
585 }
586
591 double omega() const
592 {
593 return -curvature();
594 }
595
597 ROOT::Math::XYVector center() const
598 {
599 ROOT::Math::XYVector tmp = n12();
600 return tmp / (-2. * n3());
601 }
602
604 double perimeter() const
605 {
606 return 2 * M_PI * radius();
607 }
608
615 ERotation orientation() const
616 {
617 return static_cast<ERotation>(sign(n3()));
618 }
619
621 double arcLengthPeriod() const
622 {
623 return std::fabs(perimeter());
624 }
625
635 double arcLengthBetween(const ROOT::Math::XYVector& from, const ROOT::Math::XYVector& to) const;
636
638 double arcLengthTo(const ROOT::Math::XYVector& to) const;
639
646 double arcLengthToCylindricalR(double cylindricalR) const;
647
656 double arcLengthFactor(const double directDistance) const
657 {
658 return arcLengthFactor(directDistance, curvature());
659 }
660
669 static double arcLengthFactor(double directDistance, double curvature);
670
676 std::pair<ROOT::Math::XYVector, ROOT::Math::XYVector> intersections(const GeneralizedCircle& generalizedCircle) const;
677
682 ROOT::Math::XYVector atArcLength(double arcLength) const;
683
684 private:
685 // Order of this parameters make them easier to initialize
686
688 double m_n3 = 0.0;
689
691 ROOT::Math::XYVector m_n12;
692
694 double m_n0 = 0.0;
695 };
696
698 std::ostream& operator<<(std::ostream& output, const GeneralizedCircle& circle);
699 }
701}
A two dimensional circle in its natural representation using center and radius as parameters.
Definition Circle2D.h:32
GeneralizedCircle()=default
Default constructor for ROOT compatibility.
ROOT::Math::XYVector tangential(const ROOT::Math::XYVector &point) const
Tangential vector to the circle near the given position.
static GeneralizedCircle fromPerigeeParameters(double curvature, const ROOT::Math::XYVector &tangential, double impact)
Constructor of a generalized circle from perigee parameters.
double fastImpact() const
Approximate distance to the origin.
double m_n3
Memory for the fourth parameter.
double n1() const
Getter for the second circle parameter.
ROOT::Math::XYVector gradient(const ROOT::Math::XYVector &point) const
Gradient of the distance field Gives the gradient of the approximated distance field for the given po...
bool isLine() const
Indicates if the generalized circle is actually a line.
void setN(const double n0, const ROOT::Math::XYVector &n12, const double n3=0.0)
Setter for all four circle parameters.
ROOT::Math::XYVector apogee() const
Calculates the point on the circle that is furthest away from the origin.
bool isRight(const ROOT::Math::XYVector &rhs) const
Return if the point given is right of the line.
GeneralizedCircle reversed() const
Returns a copy of the circle with opposite orientation.
double fastDistance(const ROOT::Math::XYVector &point) const
Approximate distance.
bool isInvalid() const
Indicates if all circle parameters are zero.
double minimalCylindricalR() const
Gives the minimal cylindrical radius the circle reaches (unsigned)
void reverse()
Flips the orientation of the circle in place.
void setN0(const double n0)
Setter for first circle parameter.
double arcLengthBetween(const ROOT::Math::XYVector &from, const ROOT::Math::XYVector &to) const
Calculates the arc length between two points of closest approach on the circle.
double radius() const
Gives the signed radius of the circle. If it was a line this will be infinity.
void setN(const Line2D &n012)
Setter for all four circle parameters from another circle.
static GeneralizedCircle fromCenterAndRadius(const ROOT::Math::XYVector &center, double absRadius, ERotation orientation=ERotation::c_CounterClockwise)
Constructor from center, radius and a optional orientation.
void setCenterAndRadius(const ROOT::Math::XYVector &center, double absRadius, ERotation orientation=ERotation::c_CounterClockwise)
Setter for the circle center and radius.
const ROOT::Math::XYVector & n12() const
Getter for the second and third circle parameter which natuarally from a vector.
void setN2(const double n2)
Setter for third circle parameter.
bool isLeft(const ROOT::Math::XYVector &rhs) const
Return if the point given is left of the line.
ROOT::Math::XYVector closest(const ROOT::Math::XYVector &point) const
Closest approach on the circle to the point.
double omega() const
Gives the omega parameter as used by the framework helix.
void setN3(const double n3)
Setter for fourth circle parameter.
void setN1(const double n1)
Setter for second circle parameter.
double m_n0
Memory for the first parameter.
bool isValid() const
Indicates if the combination of the circle parameters makes up a valid circle.
void setN(const GeneralizedCircle &n0123)
Setter for all four circle parameters from another circle.
EForwardBackward isForwardOrBackwardOf(const ROOT::Math::XYVector &from, const ROOT::Math::XYVector &to) const
Calculates if the to vector is closer to the from vector following the along orientation of the circl...
ROOT::Math::XYVector normal(const ROOT::Math::XYVector &point) const
Normal vector to the circle near the given position.
double impact() const
Gives the signed distance of the origin to the circle.
std::pair< ROOT::Math::XYVector, ROOT::Math::XYVector > intersections(const GeneralizedCircle &generalizedCircle) const
Calculates the two points common to both circles.
void setN12(const double n1, const double n2)
Setter for second and third circle parameter.
double normalizationSquared() const
Calculates the generalized circle specific squared norm.
double absDistance(const ROOT::Math::XYVector &point) const
Gives the proper absolute distance of the point to the circle line.
double maximalCylindricalR() const
Gives the maximal cylindrical radius the circle reaches.
double distance(const ROOT::Math::XYVector &point) const
Gives the proper distance of the point to the circle line retaining the sign of the fast distance.
double arcLengthTo(const ROOT::Math::XYVector &to) const
Calculates the arc length between the perigee and the given point.
double n3() const
Getter for the fourth circle parameter.
void invalidate()
Sets all circle parameters to zero.
ERightLeft isRightOrLeft(const ROOT::Math::XYVector &point) const
Indicates if the point is on the right or left side of the circle.
ROOT::Math::XYVector perigee() const
Calculates the closest approach to the two dimensional origin.
void passiveMoveBy(const ROOT::Math::XYVector &by)
Moves the coordinate system by the given vector.
ROOT::Math::XYVector tangential() const
Gives the tangential vector at the closest approach to the origin / at the perigee.
void conformalTransform()
Transforms the generalized circle to conformal space inplace Applies the conformal map in the self-in...
void scaleN(const double factor)
Scales the circle parameters by a common factor.
double perimeter() const
Gives the perimeter of the circle.
double n2() const
Getter for the third circle parameter.
bool isCircle() const
Indicates if the generalized circle is actually a circle.
void setPerigeeParameters(const double curvature, const double tangentialPhi, const double impact)
Setter for the perigee parameters.
ROOT::Math::XYVector chooseNextForwardOf(const ROOT::Math::XYVector &start, const ROOT::Math::XYVector &end1, const ROOT::Math::XYVector &end2) const
Returns the end point which is first reached if one follows the forward direction of the circle start...
ROOT::Math::XYVector atArcLength(double arcLength) const
Calculates the point, which lies at the give perpendicular travel distance (counted from the perigee)
double curvature() const
Gives the signed curvature of the generalized circle.
double d0() const
Getter for the absolute distance to the z axes at the support point.
double absRadius() const
Gives the signed radius of the circle. If it was a line this will be infinity.
void setPerigeeParameters(double curvature, const ROOT::Math::XYVector &tangential, double impact)
Setter for the perigee parameters.
std::pair< ROOT::Math::XYVector, ROOT::Math::XYVector > atCylindricalR(double cylindricalR) const
Calculates the two points with the given cylindrical radius on the generalised circle.
double arcLengthFactor(const double directDistance) const
Helper function the calculate the factor between the length of a secant line and the length on the ar...
ROOT::Math::XYVector atCylindricalRForwardOf(const ROOT::Math::XYVector &startPoint, double cylindricalR) const
Approach on the circle with the given cylindrical radius that lies in the forward direction of a star...
void normalize()
Normalizes the circle parameters.
void setN12(const ROOT::Math::XYVector &n12)
Setter for second and third circle parameter.
double tangentialPhi() const
Gives to azimuth angle phi of the direction of flight at the perigee.
ROOT::Math::XYVector center() const
Gives the center of the circle. If it was a line both components will be infinity.
double fastDistance(const double distance) const
Helper function to translate the proper distance to the linearized distance measure of the generalize...
double n0() const
Getter for the first circle parameter.
void setN(const double n0, const double n1, const double n2, const double n3=0.0)
Setter for all four circle parameters.
ERotation orientation() const
Gives the orientation of the circle.
double arcLengthPeriod() const
Getter for the arc length for a full round of the circle.
double arcLengthToCylindricalR(double cylindricalR) const
Calculates the two dimensional arc length till the cylindrical radius is reached If the radius can no...
GeneralizedCircle conformalTransformed() const
Returns a copy of the circle in conformal space.
ROOT::Math::XYVector m_n12
Memory for the second and third parameter.
A two dimensional normal line.
Definition Line2D.h:39
const ROOT::Math::XYVector & n12() const
Getter for the unit normal vector to the line.
Definition Line2D.h:119
double n0() const
Getter for the first line parameter.
Definition Line2D.h:101
Abstract base class for different kinds of events.