Belle II Software  release-05-01-25
Line2D.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/geometry/Vector2D.h>
13 
14 #include <tracking/trackFindingCDC/numerics/EForwardBackward.h>
15 #include <tracking/trackFindingCDC/numerics/ERightLeft.h>
16 #include <tracking/trackFindingCDC/numerics/ESign.h>
17 
18 #include <cmath>
19 
20 namespace Belle2 {
25  namespace TrackFindingCDC {
26 
39  class Line2D {
40 
41  public:
43  Line2D()
44  : m_n0(0.0)
45  , m_n12(0.0, 0.0)
46  {
47  }
48 
50  Line2D(const double n0, const double n1, const double n2)
51  : m_n0(n0)
52  , m_n12(n1, n2)
53  {
54  normalize();
55  }
56 
58  Line2D(const double n0, const Vector2D& n12)
59  : m_n0(n0)
60  , m_n12(n12)
61  {
62  normalize();
63  }
64 
72  static Line2D fromSlopeIntercept(const double slope, const double intercept)
73  {
74  return Line2D(intercept, slope, -EForwardBackward::c_Forward);
75  }
76 
86  static Line2D fromSlopeIntercept(const double slope,
87  const double intercept,
88  const EForwardBackward orientation)
89  {
90  return Line2D(intercept * orientation, slope * orientation, -orientation);
91  }
92 
94  static Line2D throughPoints(const Vector2D& start, const Vector2D& end)
95  {
96  return Line2D(end.cross(start), (start - end).orthogonal());
97  }
98 
99  public:
101  double n0() const
102  {
103  return m_n0;
104  }
105 
107  double n1() const
108  {
109  return m_n12.first();
110  }
111 
113  double n2() const
114  {
115  return m_n12.second();
116  }
117 
119  const Vector2D& n12() const
120  {
121  return m_n12;
122  }
123 
124  public:
131  void setN0(const double n0)
132  {
133  m_n0 = n0;
134  }
135 
136  private:
138  void setN1(const double n1)
139  {
140  m_n12.setFirst(n1);
141  }
142 
144  void setN2(const double n2)
145  {
147  }
148 
150  void setN12(const double n1, const double n2)
151  {
153  }
154 
156  void setN12(const Vector2D& n12)
157  {
159  }
160 
161  public:
164  void setN(const double n0, const double n1, const double n2)
165  {
166  setN0(n0);
167  setN12(n1, n2);
168  normalize();
169  }
170 
172  void invalidate()
173  {
174  setN(0.0, 0.0, 0.0);
175  }
176 
179  void setSlopeIntercept(const double slope, const double intercept)
180  {
181  setN0(intercept);
182  setN1(slope);
183  setN2(-1.0);
184  normalize();
185  }
186 
189  void setSlopeIntercept(const double slope,
190  const double intercept,
191  const EForwardBackward orientation)
192  {
193  setN0(intercept * orientation);
194  setN1(slope * orientation);
195  setN2(-orientation);
196  normalize();
197  }
198 
200  void normalize()
201  {
202  if (not isInvalid()) {
203  scaleN(1.0 / normalization());
204  }
205  }
206 
208  void reverse()
209  {
210  scaleN(-1.0);
211  }
212 
214  Line2D reversed() const
215  {
216  return Line2D(-n0(), -n1(), -n2());
217  }
218 
219  private:
221  void scaleN(const double factor)
222  {
223  m_n0 *= factor;
224  m_n12 *= factor;
225  }
226 
228  double normalizationSquared() const
229  {
230  return n12().normSquared();
231  }
232 
234  double normalization() const
235  {
236  return sqrt(normalizationSquared());
237  }
238 
239  public:
245  double distance(const Vector2D& point) const
246  {
247  return n0() + point.dot(n12());
248  }
249 
255  double distance(const double first, const double second) const
256  {
257  return n0() + first * n1() + second * n2();
258  }
259 
264  double distanceToOrigin() const
265  {
266  return n0();
267  }
268 
270  double absoluteDistance(const Vector2D& point) const
271  {
272  return fabs(distance(point));
273  }
274 
276  ERightLeft isRightOrLeft(const Vector2D& point) const
277  {
278  return static_cast<ERightLeft>(sign(distance(point)));
279  }
280 
282  bool isLeft(const Vector2D& rhs) const
283  {
284  return isRightOrLeft(rhs) == ERightLeft::c_Left;
285  }
286 
288  bool isRight(const Vector2D& rhs) const
289  {
290  return isRightOrLeft(rhs) == ERightLeft::c_Right;
291  }
292 
294  Vector2D closest(const Vector2D& point) const
295  {
296  const double closestParallel = -n0();
297  const double closestOrthgonal = point.unnormalizedOrthogonalComp(n12());
298  return Vector2D(n12(), closestParallel, closestOrthgonal);
299  }
300 
303  {
304  return n12() * (-n0());
305  }
306 
314  double lengthOnCurve(const Vector2D& from, const Vector2D& to) const
315  {
317  }
318 
320  bool isInvalid() const
321  {
322  return n0() == 0.0 and n12().isNull();
323  }
324 
326  Vector2D tangential() const
327  {
328  return normal().orthogonal();
329  }
330 
332  const Vector2D& normal() const
333  {
334  return n12();
335  }
336 
338  const Vector2D& gradient() const
339  {
340  return n12();
341  }
342 
344  Vector2D support() const
345  {
346  return closestToOrigin();
347  }
348 
352  {
353  return static_cast<EForwardBackward>(-sign(n2()));
354  }
355 
359  {
360  return static_cast<EForwardBackward>(sign(n1()));
361  }
362 
364  Vector2D intersection(const Line2D& line) const;
365 
368  void moveBy(const Vector2D& by)
370  {
372  }
373 
375  void moveAlongFirst(const double first)
376  {
377  m_n0 -= n1() * first;
378  }
379 
381  void moveAlongSecond(const double second)
382  {
383  m_n0 -= n2() * second;
384  }
385 
387  Line2D movedAlongFirst(const double first) const
388  {
389  return Line2D(n0() - n1() * first, n1(), n2());
390  }
391 
393  Line2D movedAlongSecond(const double second) const
394  {
395  return Line2D(n0() - n2() * second, n1(), n2());
396  }
397 
399  void passiveMoveBy(const Vector2D& by)
400  {
402  }
403 
405  void passiveMoveAlongFirst(const double first)
406  {
407  m_n0 += n1() * first;
408  }
409 
411  void passiveMoveAlongSecond(const double second)
412  {
413  m_n0 += n2() * second;
414  }
415 
417  Line2D passiveMovedAlongFirst(const double first) const
418  {
419  return Line2D(n0() + n1() * first, n1(), n2());
420  }
421 
423  Line2D passiveMovedAlongSecond(const double second) const
424  {
425  return Line2D(n0() + n2() * second, n1(), n2());
426  }
427 
429  void flipFirst()
430  {
432  }
433 
435  void flipSecond()
436  {
438  }
439 
442  Line2D flippedFirst() const
443  {
444  return Line2D(n0(), n12().flippedFirst());
445  }
446 
449  Line2D flippedSecond() const
450  {
451  return Line2D(n0(), n12().flippedSecond());
452  }
458  double slope() const
460  {
461  return -n1() / n2();
462  }
463 
465  double inverseSlope() const
466  {
467  return -n2() / n1();
468  }
469 
471  double intercept() const
472  {
473  return -n0() / n2();
474  }
475 
477  double zero() const
478  {
479  return -n0() / n1();
480  }
481 
483  double map(const double first) const
484  {
485  return -(n0() + n1() * first) / n2();
486  }
488  double operator()(const double first) const
489  {
490  return map(first);
491  }
492 
494  double inverseMap(const double second) const
495  {
496  return -(n0() + n2() * second) / n1();
497  }
498 
500  void invert()
501  {
503  reverse();
504  }
505 
507  Line2D inverted() const
508  {
509  return Line2D(-n0(), -n2(), -n1());
510  }
513  private:
515  double m_n0;
516 
518  Vector2D m_n12;
519 
520  };
521  }
523 }
Belle2::TrackFindingCDC::Line2D::slope
double slope() const
Returns the slope over the first coordinate.
Definition: Line2D.h:467
Belle2::TrackFindingCDC::Vector2D::setSecond
void setSecond(const double second)
Setter for the second coordinate.
Definition: Vector2D.h:658
Belle2::TrackFindingCDC::Line2D::m_n0
double m_n0
Memory for the first line parameter.
Definition: Line2D.h:523
Belle2::TrackFindingCDC::Line2D::movedAlongSecond
Line2D movedAlongSecond(const double second) const
Return a copy of the line activally moved long the first coordinate.
Definition: Line2D.h:401
Belle2::TrackFindingCDC::Line2D::n0
double n0() const
Getter for the first line parameter.
Definition: Line2D.h:109
Belle2::TrackFindingCDC::Vector2D::orthogonal
Vector2D orthogonal() const
Orthogonal vector to the counterclockwise direction.
Definition: Vector2D.h:303
Belle2::TrackFindingCDC::Line2D::alignedWithSecond
EForwardBackward alignedWithSecond() const
Returns if the direction of positiv advance has a common component aligned or anti aligned with the s...
Definition: Line2D.h:366
Belle2::TrackFindingCDC::Line2D::setSlopeIntercept
void setSlopeIntercept(const double slope, const double intercept)
Sets the new intercept and slope of the line the direction is set to be forward with the increasing x...
Definition: Line2D.h:187
Belle2::TrackFindingCDC::Line2D::Line2D
Line2D(const double n0, const double n1, const double n2)
Constructs taking all three line parameters.
Definition: Line2D.h:58
Belle2::TrackFindingCDC::Vector2D
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:37
Belle2::TrackFindingCDC::Line2D::moveAlongSecond
void moveAlongSecond(const double second)
Activelly moves the line in the direction given in place along the second coordinate.
Definition: Line2D.h:389
Belle2::TrackFindingCDC::Vector2D::normSquared
double normSquared() const
Calculates .
Definition: Vector2D.h:183
Belle2::TrackFindingCDC::Line2D::operator()
double operator()(const double first) const
Maps the first coordinate to the second.
Definition: Line2D.h:496
Belle2::TrackFindingCDC::Line2D::scaleN
void scaleN(const double factor)
Scales all parameters. Helper for normalize.
Definition: Line2D.h:229
Belle2::TrackFindingCDC::Line2D::n2
double n2() const
Getter for the third line parameter.
Definition: Line2D.h:121
Belle2::TrackFindingCDC::Line2D::isLeft
bool isLeft(const Vector2D &rhs) const
Return if the point given is left of the line.
Definition: Line2D.h:290
Belle2::TrackFindingCDC::Line2D::passiveMovedAlongFirst
Line2D passiveMovedAlongFirst(const double first) const
Return a copy of the line passively moved long the first coordinate.
Definition: Line2D.h:425
Belle2::TrackFindingCDC::Line2D::setN2
void setN2(const double n2)
Setter for the third line parameter. May violate the normlization.
Definition: Line2D.h:152
Belle2::TrackFindingCDC::Vector2D::second
double second() const
Getter for the second coordinate.
Definition: Vector2D.h:653
Belle2::TrackFindingCDC::Line2D::Line2D
Line2D()
Default constructor for ROOT compatibility.
Definition: Line2D.h:51
Belle2::TrackFindingCDC::Vector2D::isNull
bool isNull() const
Checks if the vector is the null vector.
Definition: Vector2D.h:157
Belle2::TrackFindingCDC::Line2D::alignedWithFirst
EForwardBackward alignedWithFirst() const
Returns if the direction of positiv advance has a common component aligned or anti aligned with the f...
Definition: Line2D.h:359
Belle2::TrackFindingCDC::Line2D::intersection
Vector2D intersection(const Line2D &line) const
Calculates the intersection point of two line. Infinity for parallels.
Definition: Line2D.cc:17
Belle2::TrackFindingCDC::Vector2D::flipSecond
void flipSecond()
Flips the first coordinate inplace (no difference between active and passive)
Definition: Vector2D.h:365
Belle2::TrackFindingCDC::NForwardBackward::EForwardBackward
EForwardBackward
Enumeration to represent the distinct possibilities of the right left passage information.
Definition: EForwardBackward.h:35
Belle2::TrackFindingCDC::Line2D::setN1
void setN1(const double n1)
Setter for the second line parameter. May violate the normlization.
Definition: Line2D.h:146
Belle2::TrackFindingCDC::Line2D::zero
double zero() const
Returns the root of the line.
Definition: Line2D.h:485
Belle2::TrackFindingCDC::Line2D::distanceToOrigin
double distanceToOrigin() const
Returns the distance to the origin The distance to the origin is equivalent to the first line paramet...
Definition: Line2D.h:272
Belle2::TrackFindingCDC::Line2D::distance
double distance(const Vector2D &point) const
Calculates the signed distance of the point to the line.
Definition: Line2D.h:253
Belle2::TrackFindingCDC::Line2D::passiveMoveAlongFirst
void passiveMoveAlongFirst(const double first)
Passively move the coordinate system in place along the first coordinate.
Definition: Line2D.h:413
Belle2::TrackFindingCDC::Vector2D::dot
double dot(const Vector2D &rhs) const
Calculates the two dimensional dot product.
Definition: Vector2D.h:172
Belle2::TrackFindingCDC::Vector2D::setFirst
void setFirst(const double first)
Setter for the first coordinate.
Definition: Vector2D.h:648
Belle2::TrackFindingCDC::Line2D::fromSlopeIntercept
static Line2D fromSlopeIntercept(const double slope, const double intercept)
Constructs a line from its slope and intercept over the first coordinate (default forward orientation...
Definition: Line2D.h:80
Belle2::TrackFindingCDC::Line2D::flippedSecond
Line2D flippedSecond() const
Makes a copy of the line with the second coordinate flipped (no difference between active and pasive)
Definition: Line2D.h:457
Belle2::TrackFindingCDC::Vector2D::first
double first() const
Getter for the first coordinate.
Definition: Vector2D.h:643
Belle2::TrackFindingCDC::Line2D::reverse
void reverse()
Flips orientation the line in place.
Definition: Line2D.h:216
Belle2::TrackFindingCDC::Line2D::moveAlongFirst
void moveAlongFirst(const double first)
Activelly moves the line in the direction given in place along the first coordinate.
Definition: Line2D.h:383
Belle2::TrackFindingCDC::Line2D::isInvalid
bool isInvalid() const
Indicates if all circle parameters are zero.
Definition: Line2D.h:328
Belle2::TrackFindingCDC::Line2D::invert
void invert()
Turns the line function into its inverse function in place.
Definition: Line2D.h:508
Belle2::TrackFindingCDC::Line2D::flipSecond
void flipSecond()
Flips the first coordinate inplace (no difference between active and pasive)
Definition: Line2D.h:443
Belle2::TrackFindingCDC::Line2D::lengthOnCurve
double lengthOnCurve(const Vector2D &from, const Vector2D &to) const
Calculates the length on the curve between two points.
Definition: Line2D.h:322
Belle2::TrackFindingCDC::Line2D::isRightOrLeft
ERightLeft isRightOrLeft(const Vector2D &point) const
Return if the point given is right or left of the line.
Definition: Line2D.h:284
Belle2::TrackFindingCDC::Line2D::normalizationSquared
double normalizationSquared() const
Calculates the squared normalization. Helper for normalize.
Definition: Line2D.h:236
Belle2::TrackFindingCDC::Line2D::closest
Vector2D closest(const Vector2D &point) const
Calculates the point of closest approach on the line to the point.
Definition: Line2D.h:302
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::Line2D::normalize
void normalize()
Updates the parameters to obey the normalization condition.
Definition: Line2D.h:208
Belle2::TrackFindingCDC::Line2D::passiveMoveBy
void passiveMoveBy(const Vector2D &by)
Passivelly move the coordinate system in place by the given vector.
Definition: Line2D.h:407
Belle2::TrackFindingCDC::Line2D::n1
double n1() const
Getter for the second line parameter.
Definition: Line2D.h:115
Belle2::TrackFindingCDC::Vector2D::swapCoordinates
void swapCoordinates()
Swaps the coordinates in place.
Definition: Vector2D.h:565
Belle2::TrackFindingCDC::Line2D::inverseSlope
double inverseSlope() const
Returns the slope over the second coordinate.
Definition: Line2D.h:473
Belle2::TrackFindingCDC::Vector2D::unnormalizedParallelComp
double unnormalizedParallelComp(const Vector2D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition: Vector2D.h:439
Belle2::TrackFindingCDC::NRightLeft::ERightLeft
ERightLeft
Enumeration to represent the distinct possibilities of the right left passage.
Definition: ERightLeft.h:35
Belle2::TrackFindingCDC::Line2D::normalization
double normalization() const
Calculates the normalization. Helper for normalize.
Definition: Line2D.h:242
Belle2::TrackFindingCDC::Line2D::moveBy
void moveBy(const Vector2D &by)
Activelly moves the line in the direction given in place by the vector given.
Definition: Line2D.h:377
Belle2::TrackFindingCDC::Line2D::invalidate
void invalidate()
Sets all line parameters to zero.
Definition: Line2D.h:180
Belle2::TrackFindingCDC::Line2D::setN12
void setN12(const double n1, const double n2)
Setter for the normal vector by its coordinates.
Definition: Line2D.h:158
Belle2::TrackFindingCDC::Line2D
A two dimensional normal line.
Definition: Line2D.h:47
Belle2::TrackFindingCDC::Line2D::passiveMovedAlongSecond
Line2D passiveMovedAlongSecond(const double second) const
Return a copy of the line passively moved long the first coordinate.
Definition: Line2D.h:431
Belle2::TrackFindingCDC::Line2D::inverted
Line2D inverted() const
Returns the inverse function line as a copy.
Definition: Line2D.h:515
Belle2::TrackFindingCDC::Line2D::intercept
double intercept() const
Returns the intercept over the first coordinate.
Definition: Line2D.h:479
Belle2::TrackFindingCDC::Line2D::map
double map(const double first) const
Maps the first coordinate to the second.
Definition: Line2D.h:491
Belle2::TrackFindingCDC::Line2D::inverseMap
double inverseMap(const double second) const
Maps the second coordinate to the first.
Definition: Line2D.h:502
Belle2::TrackFindingCDC::Line2D::gradient
const Vector2D & gradient() const
Getter for the gradient of the distance field.
Definition: Line2D.h:346
Belle2::TrackFindingCDC::Line2D::reversed
Line2D reversed() const
Returns a copy of the line with the reversed orientation.
Definition: Line2D.h:222
Belle2::TrackFindingCDC::Line2D::setN
void setN(const double n0, const double n1, const double n2)
Setter the for the line parameters which takes care of the correct normalization of the normal vector...
Definition: Line2D.h:172
Belle2::TrackFindingCDC::Line2D::flipFirst
void flipFirst()
Flips the first coordinate inplace (no difference between active and pasive)
Definition: Line2D.h:437
Belle2::TrackFindingCDC::Line2D::support
Vector2D support() const
Getter for the support point of the line being the point closest to the origin.
Definition: Line2D.h:352
Belle2::TrackFindingCDC::Vector2D::unnormalizedOrthogonalComp
double unnormalizedOrthogonalComp(const Vector2D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition: Vector2D.h:460
Belle2::TrackFindingCDC::Vector2D::set
void set(const double first, const double second)
Setter for both coordinate.
Definition: Vector2D.h:664
Belle2::TrackFindingCDC::Line2D::normal
const Vector2D & normal() const
Getter for the unit normal vector of the line.
Definition: Line2D.h:340
Belle2::TrackFindingCDC::Line2D::passiveMoveAlongSecond
void passiveMoveAlongSecond(const double second)
Passively move the coordinate system in place along the second coordinate.
Definition: Line2D.h:419
Belle2::TrackFindingCDC::Line2D::m_n12
Vector2D m_n12
Memory for the second line parameter.
Definition: Line2D.h:526
Belle2::TrackFindingCDC::Line2D::isRight
bool isRight(const Vector2D &rhs) const
Return if the point given is right of the line.
Definition: Line2D.h:296
Belle2::TrackFindingCDC::Line2D::movedAlongFirst
Line2D movedAlongFirst(const double first) const
Return a copy of the line activally moved long the first coordinate.
Definition: Line2D.h:395
Belle2::TrackFindingCDC::Line2D::n12
const Vector2D & n12() const
Getter for the unit normal vector to the line.
Definition: Line2D.h:127
Belle2::TrackFindingCDC::Line2D::tangential
Vector2D tangential() const
Gives the tangential vector in the direction of positiv advance on the line.
Definition: Line2D.h:334
Belle2::TrackFindingCDC::Line2D::setN0
void setN0(const double n0)
Setter for first line parameter This sets the signed distance of the line to the origin.
Definition: Line2D.h:139
Belle2::TrackFindingCDC::Line2D::flippedFirst
Line2D flippedFirst() const
Makes a copy of the line with the first coordinate flipped (no difference between active and pasive)
Definition: Line2D.h:450
Belle2::TrackFindingCDC::Line2D::closestToOrigin
Vector2D closestToOrigin() const
Returns the point closest to the origin.
Definition: Line2D.h:310
Belle2::TrackFindingCDC::Line2D::absoluteDistance
double absoluteDistance(const Vector2D &point) const
Returns the absolute value of distance(point)
Definition: Line2D.h:278
Belle2::TrackFindingCDC::Line2D::throughPoints
static Line2D throughPoints(const Vector2D &start, const Vector2D &end)
Constructs a line through the two given points.
Definition: Line2D.h:102
Belle2::TrackFindingCDC::Vector2D::flipFirst
void flipFirst()
Flips the first coordinate inplace (no difference between active and passive)
Definition: Vector2D.h:359