Belle II Software  release-08-01-10
Line2D.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/geometry/Vector2D.h>
11 
12 #include <tracking/trackFindingCDC/numerics/EForwardBackward.h>
13 #include <tracking/trackFindingCDC/numerics/ERightLeft.h>
14 #include <tracking/trackFindingCDC/numerics/ESign.h>
15 
16 #include <cmath>
17 
18 namespace Belle2 {
23  namespace TrackFindingCDC {
24 
37  class Line2D {
38 
39  public:
42  : m_n0(0.0)
43  , m_n12(0.0, 0.0)
44  {
45  }
46 
48  Line2D(const double n0, const double n1, const double n2)
49  : m_n0(n0)
50  , m_n12(n1, n2)
51  {
52  normalize();
53  }
54 
56  Line2D(const double n0, const Vector2D& n12)
57  : m_n0(n0)
58  , m_n12(n12)
59  {
60  normalize();
61  }
62 
70  static Line2D fromSlopeIntercept(const double slope, const double intercept)
71  {
72  return Line2D(intercept, slope, -EForwardBackward::c_Forward);
73  }
74 
84  static Line2D fromSlopeIntercept(const double slope,
85  const double intercept,
86  const EForwardBackward orientation)
87  {
88  return Line2D(intercept * static_cast<double>(orientation), slope * static_cast<double>(orientation), -orientation);
89  }
90 
92  static Line2D throughPoints(const Vector2D& start, const Vector2D& end)
93  {
94  return Line2D(end.cross(start), (start - end).orthogonal());
95  }
96 
97  public:
99  double n0() const
100  {
101  return m_n0;
102  }
103 
105  double n1() const
106  {
107  return m_n12.first();
108  }
109 
111  double n2() const
112  {
113  return m_n12.second();
114  }
115 
117  const Vector2D& n12() const
118  {
119  return m_n12;
120  }
121 
122  public:
129  void setN0(const double n0)
130  {
131  m_n0 = n0;
132  }
133 
134  private:
136  void setN1(const double n1)
137  {
138  m_n12.setFirst(n1);
139  }
140 
142  void setN2(const double n2)
143  {
144  m_n12.setSecond(n2);
145  }
146 
148  void setN12(const double n1, const double n2)
149  {
150  m_n12.set(n1, n2);
151  }
152 
154  void setN12(const Vector2D& n12)
155  {
156  m_n12.set(n12);
157  }
158 
159  public:
162  void setN(const double n0, const double n1, const double n2)
163  {
164  setN0(n0);
165  setN12(n1, n2);
166  normalize();
167  }
168 
170  void invalidate()
171  {
172  setN(0.0, 0.0, 0.0);
173  }
174 
177  void setSlopeIntercept(const double slope, const double intercept)
178  {
179  setN0(intercept);
180  setN1(slope);
181  setN2(-1.0);
182  normalize();
183  }
184 
187  void setSlopeIntercept(const double slope,
188  const double intercept,
189  const EForwardBackward orientation)
190  {
191  setN0(intercept * static_cast<double>(orientation));
192  setN1(slope * static_cast<double>(orientation));
193  setN2(-orientation);
194  normalize();
195  }
196 
198  void normalize()
199  {
200  if (not isInvalid()) {
201  scaleN(1.0 / normalization());
202  }
203  }
204 
206  void reverse()
207  {
208  scaleN(-1.0);
209  }
210 
212  Line2D reversed() const
213  {
214  return Line2D(-n0(), -n1(), -n2());
215  }
216 
217  private:
219  void scaleN(const double factor)
220  {
221  m_n0 *= factor;
222  m_n12 *= factor;
223  }
224 
226  double normalizationSquared() const
227  {
228  return n12().normSquared();
229  }
230 
232  double normalization() const
233  {
234  return sqrt(normalizationSquared());
235  }
236 
237  public:
243  double distance(const Vector2D& point) const
244  {
245  return n0() + point.dot(n12());
246  }
247 
253  double distance(const double first, const double second) const
254  {
255  return n0() + first * n1() + second * n2();
256  }
257 
262  double distanceToOrigin() const
263  {
264  return n0();
265  }
266 
268  double absoluteDistance(const Vector2D& point) const
269  {
270  return fabs(distance(point));
271  }
272 
274  ERightLeft isRightOrLeft(const Vector2D& point) const
275  {
276  return static_cast<ERightLeft>(sign(distance(point)));
277  }
278 
280  bool isLeft(const Vector2D& rhs) const
281  {
282  return isRightOrLeft(rhs) == ERightLeft::c_Left;
283  }
284 
286  bool isRight(const Vector2D& rhs) const
287  {
288  return isRightOrLeft(rhs) == ERightLeft::c_Right;
289  }
290 
292  Vector2D closest(const Vector2D& point) const
293  {
294  const double closestParallel = -n0();
295  const double closestOrthgonal = point.unnormalizedOrthogonalComp(n12());
296  return Vector2D(n12(), closestParallel, closestOrthgonal);
297  }
298 
301  {
302  return n12() * (-n0());
303  }
304 
312  double lengthOnCurve(const Vector2D& from, const Vector2D& to) const
313  {
315  }
316 
318  bool isInvalid() const
319  {
320  return n0() == 0.0 and n12().isNull();
321  }
322 
325  {
326  return normal().orthogonal();
327  }
328 
330  const Vector2D& normal() const
331  {
332  return n12();
333  }
334 
336  const Vector2D& gradient() const
337  {
338  return n12();
339  }
340 
343  {
344  return closestToOrigin();
345  }
346 
350  {
351  return static_cast<EForwardBackward>(-sign(n2()));
352  }
353 
357  {
358  return static_cast<EForwardBackward>(sign(n1()));
359  }
360 
362  Vector2D intersection(const Line2D& line) const;
363 
367  void moveBy(const Vector2D& by)
368  {
370  }
371 
373  void moveAlongFirst(const double first)
374  {
375  m_n0 -= n1() * first;
376  }
377 
379  void moveAlongSecond(const double second)
380  {
381  m_n0 -= n2() * second;
382  }
383 
385  Line2D movedAlongFirst(const double first) const
386  {
387  return Line2D(n0() - n1() * first, n1(), n2());
388  }
389 
391  Line2D movedAlongSecond(const double second) const
392  {
393  return Line2D(n0() - n2() * second, n1(), n2());
394  }
395 
397  void passiveMoveBy(const Vector2D& by)
398  {
400  }
401 
403  void passiveMoveAlongFirst(const double first)
404  {
405  m_n0 += n1() * first;
406  }
407 
409  void passiveMoveAlongSecond(const double second)
410  {
411  m_n0 += n2() * second;
412  }
413 
415  Line2D passiveMovedAlongFirst(const double first) const
416  {
417  return Line2D(n0() + n1() * first, n1(), n2());
418  }
419 
421  Line2D passiveMovedAlongSecond(const double second) const
422  {
423  return Line2D(n0() + n2() * second, n1(), n2());
424  }
425 
427  void flipFirst()
428  {
429  m_n12.flipFirst();
430  }
431 
433  void flipSecond()
434  {
435  m_n12.flipSecond();
436  }
437 
441  {
442  return Line2D(n0(), n12().flippedFirst());
443  }
444 
448  {
449  return Line2D(n0(), n12().flippedSecond());
450  }
457  double slope() const
458  {
459  return -n1() / n2();
460  }
461 
463  double inverseSlope() const
464  {
465  return -n2() / n1();
466  }
467 
469  double intercept() const
470  {
471  return -n0() / n2();
472  }
473 
475  double zero() const
476  {
477  return -n0() / n1();
478  }
479 
481  double map(const double first) const
482  {
483  return -(n0() + n1() * first) / n2();
484  }
486  double operator()(const double first) const
487  {
488  return map(first);
489  }
490 
492  double inverseMap(const double second) const
493  {
494  return -(n0() + n2() * second) / n1();
495  }
496 
498  void invert()
499  {
501  reverse();
502  }
503 
505  Line2D inverted() const
506  {
507  return Line2D(-n0(), -n2(), -n1());
508  }
511  private:
513  double m_n0;
514 
517 
518  };
519  }
521 }
A two dimensional normal line.
Definition: Line2D.h:37
Line2D passiveMovedAlongFirst(const double first) const
Return a copy of the line passively moved long the first coordinate.
Definition: Line2D.h:415
const Vector2D & n12() const
Getter for the unit normal vector to the line.
Definition: Line2D.h:117
void flipSecond()
Flips the first coordinate inplace (no difference between active and pasive)
Definition: Line2D.h:433
Line2D movedAlongFirst(const double first) const
Return a copy of the line activally moved long the first coordinate.
Definition: Line2D.h:385
Vector2D tangential() const
Gives the tangential vector in the direction of positiv advance on the line.
Definition: Line2D.h:324
Line2D()
Default constructor for ROOT compatibility.
Definition: Line2D.h:41
double n1() const
Getter for the second line parameter.
Definition: Line2D.h:105
EForwardBackward alignedWithFirst() const
Returns if the direction of positiv advance has a common component aligned or anti aligned with the f...
Definition: Line2D.h:349
Line2D(const double n0, const Vector2D &n12)
Constructs taking the distance to the origin ( n0 ) and the normal vector.
Definition: Line2D.h:56
Line2D flippedSecond() const
Makes a copy of the line with the second coordinate flipped (no difference between active and pasive)
Definition: Line2D.h:447
double absoluteDistance(const Vector2D &point) const
Returns the absolute value of distance(point)
Definition: Line2D.h:268
double normalization() const
Calculates the normalization. Helper for normalize.
Definition: Line2D.h:232
double zero() const
Returns the root of the line.
Definition: Line2D.h:475
bool isInvalid() const
Indicates if all circle parameters are zero.
Definition: Line2D.h:318
void reverse()
Flips orientation the line in place.
Definition: Line2D.h:206
Line2D inverted() const
Returns the inverse function line as a copy.
Definition: Line2D.h:505
void setN0(const double n0)
Setter for first line parameter This sets the signed distance of the line to the origin.
Definition: Line2D.h:129
double slope() const
Returns the slope over the first coordinate.
Definition: Line2D.h:457
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:177
double map(const double first) const
Maps the first coordinate to the second.
Definition: Line2D.h:481
double distance(const Vector2D &point) const
Calculates the signed distance of the point to the line.
Definition: Line2D.h:243
void setN2(const double n2)
Setter for the third line parameter. May violate the normlization.
Definition: Line2D.h:142
void flipFirst()
Flips the first coordinate inplace (no difference between active and pasive)
Definition: Line2D.h:427
Vector2D closestToOrigin() const
Returns the point closest to the origin.
Definition: Line2D.h:300
void setN1(const double n1)
Setter for the second line parameter. May violate the normlization.
Definition: Line2D.h:136
double m_n0
Memory for the first line parameter.
Definition: Line2D.h:513
double distanceToOrigin() const
Returns the distance to the origin The distance to the origin is equivalent to the first line paramet...
Definition: Line2D.h:262
void passiveMoveBy(const Vector2D &by)
Passivelly move the coordinate system in place by the given vector.
Definition: Line2D.h:397
double intercept() const
Returns the intercept over the first coordinate.
Definition: Line2D.h:469
Vector2D support() const
Getter for the support point of the line being the point closest to the origin.
Definition: Line2D.h:342
bool isLeft(const Vector2D &rhs) const
Return if the point given is left of the line.
Definition: Line2D.h:280
bool isRight(const Vector2D &rhs) const
Return if the point given is right of the line.
Definition: Line2D.h:286
static Line2D throughPoints(const Vector2D &start, const Vector2D &end)
Constructs a line through the two given points.
Definition: Line2D.h:92
void moveAlongFirst(const double first)
Activelly moves the line in the direction given in place along the first coordinate.
Definition: Line2D.h:373
void setN12(const double n1, const double n2)
Setter for the normal vector by its coordinates.
Definition: Line2D.h:148
double inverseSlope() const
Returns the slope over the second coordinate.
Definition: Line2D.h:463
double normalizationSquared() const
Calculates the squared normalization. Helper for normalize.
Definition: Line2D.h:226
double inverseMap(const double second) const
Maps the second coordinate to the first.
Definition: Line2D.h:492
const Vector2D & gradient() const
Getter for the gradient of the distance field.
Definition: Line2D.h:336
double operator()(const double first) const
Maps the first coordinate to the second.
Definition: Line2D.h:486
const Vector2D & normal() const
Getter for the unit normal vector of the line.
Definition: Line2D.h:330
void invert()
Turns the line function into its inverse function in place.
Definition: Line2D.h:498
static Line2D fromSlopeIntercept(const double slope, const double intercept, const EForwardBackward orientation)
Constructs a line from its slope and intercept over the first coordinate with the given orientation.
Definition: Line2D.h:84
void passiveMoveAlongFirst(const double first)
Passively move the coordinate system in place along the first coordinate.
Definition: Line2D.h:403
void invalidate()
Sets all line parameters to zero.
Definition: Line2D.h:170
EForwardBackward alignedWithSecond() const
Returns if the direction of positiv advance has a common component aligned or anti aligned with the s...
Definition: Line2D.h:356
Vector2D intersection(const Line2D &line) const
Calculates the intersection point of two line. Infinity for parallels.
Definition: Line2D.cc:15
Line2D flippedFirst() const
Makes a copy of the line with the first coordinate flipped (no difference between active and pasive)
Definition: Line2D.h:440
void scaleN(const double factor)
Scales all parameters. Helper for normalize.
Definition: Line2D.h:219
void moveBy(const Vector2D &by)
Activelly moves the line in the direction given in place by the vector given.
Definition: Line2D.h:367
Line2D reversed() const
Returns a copy of the line with the reversed orientation.
Definition: Line2D.h:212
double n2() const
Getter for the third line parameter.
Definition: Line2D.h:111
ERightLeft isRightOrLeft(const Vector2D &point) const
Return if the point given is right or left of the line.
Definition: Line2D.h:274
void setN12(const Vector2D &n12)
Setter for the normal vector.
Definition: Line2D.h:154
Line2D movedAlongSecond(const double second) const
Return a copy of the line activally moved long the first coordinate.
Definition: Line2D.h:391
double distance(const double first, const double second) const
Calculates the signed distance of the point given by its to coordinates to the line.
Definition: Line2D.h:253
void setSlopeIntercept(const double slope, const double intercept, const EForwardBackward orientation)
Sets the new intercept and slope of the line the direction is set to be forward with the increasing x...
Definition: Line2D.h:187
Vector2D closest(const Vector2D &point) const
Calculates the point of closest approach on the line to the point.
Definition: Line2D.h:292
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:162
void normalize()
Updates the parameters to obey the normalization condition.
Definition: Line2D.h:198
Line2D(const double n0, const double n1, const double n2)
Constructs taking all three line parameters.
Definition: Line2D.h:48
Vector2D m_n12
Memory for the second line parameter.
Definition: Line2D.h:516
void passiveMoveAlongSecond(const double second)
Passively move the coordinate system in place along the second coordinate.
Definition: Line2D.h:409
double n0() const
Getter for the first line parameter.
Definition: Line2D.h:99
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:70
double lengthOnCurve(const Vector2D &from, const Vector2D &to) const
Calculates the length on the curve between two points.
Definition: Line2D.h:312
void moveAlongSecond(const double second)
Activelly moves the line in the direction given in place along the second coordinate.
Definition: Line2D.h:379
Line2D passiveMovedAlongSecond(const double second) const
Return a copy of the line passively moved long the first coordinate.
Definition: Line2D.h:421
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:35
void setSecond(const double second)
Setter for the second coordinate.
Definition: Vector2D.h:656
void swapCoordinates()
Swaps the coordinates in place.
Definition: Vector2D.h:563
void flipSecond()
Flips the first coordinate inplace (no difference between active and passive)
Definition: Vector2D.h:363
double dot(const Vector2D &rhs) const
Calculates the two dimensional dot product.
Definition: Vector2D.h:170
void set(const double first, const double second)
Setter for both coordinate.
Definition: Vector2D.h:662
double unnormalizedParallelComp(const Vector2D &relativTo) const
Same as parallelComp() but assumes the given vector to be of unit length.
Definition: Vector2D.h:437
void flipFirst()
Flips the first coordinate inplace (no difference between active and passive)
Definition: Vector2D.h:357
double second() const
Getter for the second coordinate.
Definition: Vector2D.h:651
double normSquared() const
Calculates .
Definition: Vector2D.h:181
bool isNull() const
Checks if the vector is the null vector.
Definition: Vector2D.h:155
Vector2D orthogonal() const
Orthogonal vector to the counterclockwise direction.
Definition: Vector2D.h:301
double first() const
Getter for the first coordinate.
Definition: Vector2D.h:641
void setFirst(const double first)
Setter for the first coordinate.
Definition: Vector2D.h:646
double unnormalizedOrthogonalComp(const Vector2D &relativTo) const
Same as orthogonalComp() but assumes the given vector to be of unit length.
Definition: Vector2D.h:458
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
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
Abstract base class for different kinds of events.