Belle II Software development
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/trackingUtilities/geometry/VectorUtil.h>
11
12#include <tracking/trackingUtilities/numerics/EForwardBackward.h>
13#include <tracking/trackingUtilities/numerics/ERightLeft.h>
14#include <tracking/trackingUtilities/numerics/ESign.h>
15
16#include <Math/Vector2D.h>
17
18#include <cmath>
19
20namespace Belle2 {
25 namespace TrackingUtilities {
26
39 class Line2D {
40
41 public:
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 ROOT::Math::XYVector& 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 * static_cast<double>(orientation), slope * static_cast<double>(orientation), -orientation);
91 }
92
94 static Line2D throughPoints(const ROOT::Math::XYVector& start, const ROOT::Math::XYVector& end)
95 {
96 return Line2D(VectorUtil::Cross(end, start), VectorUtil::Orthogonal(start - end));
97 }
98
99 public:
101 double n0() const
102 {
103 return m_n0;
104 }
105
107 double n1() const
108 {
109 return m_n12.X();
110 }
111
113 double n2() const
114 {
115 return m_n12.Y();
116 }
117
119 const ROOT::Math::XYVector& 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.SetX(n1);
141 }
142
144 void setN2(const double n2)
145 {
146 m_n12.SetY(n2);
147 }
148
150 void setN12(const double n1, const double n2)
151 {
152 m_n12.SetXY(n1, n2);
153 }
154
156 void setN12(const ROOT::Math::XYVector& n12)
157 {
158 m_n12.SetXY(n12.X(), n12.Y());
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
173 {
174 setN(0.0, 0.0, 0.0);
175 }
176
179 void setSlopeIntercept(const double slope, const double intercept)
180 {
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 * static_cast<double>(orientation));
194 setN1(slope * static_cast<double>(orientation));
195 setN2(-orientation);
196 normalize();
197 }
198
201 {
202 if (not isInvalid()) {
203 scaleN(1.0 / normalization());
204 }
205 }
206
208 void reverse()
209 {
210 scaleN(-1.0);
211 }
212
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().Mag2();
231 }
232
234 double normalization() const
235 {
236 return sqrt(normalizationSquared());
237 }
238
239 public:
245 double distance(const ROOT::Math::XYVector& 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 ROOT::Math::XYVector& point) const
271 {
272 return fabs(distance(point));
273 }
274
276 ERightLeft isRightOrLeft(const ROOT::Math::XYVector& point) const
277 {
278 return static_cast<ERightLeft>(sign(distance(point)));
279 }
280
282 bool isLeft(const ROOT::Math::XYVector& rhs) const
283 {
284 return isRightOrLeft(rhs) == ERightLeft::c_Left;
285 }
286
288 bool isRight(const ROOT::Math::XYVector& rhs) const
289 {
290 return isRightOrLeft(rhs) == ERightLeft::c_Right;
291 }
292
294 ROOT::Math::XYVector closest(const ROOT::Math::XYVector& point) const
295 {
296 const double closestParallel = -n0();
297 const double closestOrthgonal = VectorUtil::unnormalizedOrthogonalComp(point, n12());
298 return VectorUtil::compose(n12(), closestParallel, closestOrthgonal);
299 }
300
302 ROOT::Math::XYVector closestToOrigin() const
303 {
304 return n12() * (-n0());
305 }
306
314 double lengthOnCurve(const ROOT::Math::XYVector& from, const ROOT::Math::XYVector& to) const
315 {
316 return VectorUtil::unnormalizedOrthogonalComp(to, n12()) - VectorUtil::unnormalizedOrthogonalComp(from, n12());
317 }
318
320 bool isInvalid() const
321 {
322 return n0() == 0.0 and VectorUtil::isNull(n12());
323 }
324
326 ROOT::Math::XYVector tangential() const
327 {
328 return VectorUtil::Orthogonal(normal());
329 }
330
332 const ROOT::Math::XYVector& normal() const
333 {
334 return n12();
335 }
336
338 const ROOT::Math::XYVector& gradient() const
339 {
340 return n12();
341 }
342
344 ROOT::Math::XYVector support() const
345 {
346 return closestToOrigin();
347 }
348
351 EForwardBackward alignedWithFirst() const
352 {
353 return static_cast<EForwardBackward>(-sign(n2()));
354 }
355
358 EForwardBackward alignedWithSecond() const
359 {
360 return static_cast<EForwardBackward>(sign(n1()));
361 }
362
364 ROOT::Math::XYVector intersection(const Line2D& line) const;
365
369 void moveBy(const ROOT::Math::XYVector& by)
370 {
371 m_n0 -= VectorUtil::unnormalizedParallelComp(by, n12());
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 ROOT::Math::XYVector& by)
400 {
401 m_n0 += VectorUtil::unnormalizedParallelComp(by, n12());
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
430 {
431 m_n12.SetX(-m_n12.X());
432 }
433
436 {
437 m_n12.SetY(-m_n12.Y());
438 }
439
443 {
444 return Line2D(n0(), ROOT::Math::XYVector(-n12().X(), n12().Y()));
445 }
446
450 {
451 return Line2D(n0(), ROOT::Math::XYVector(n12().X(), -n12().Y()));
452 }
453
454
459 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 }
487
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 {
502 m_n12.SetXY(m_n12.Y(), m_n12.X());
503 reverse();
504 }
505
508 {
509 return Line2D(-n0(), -n2(), -n1());
510 }
511
512
513 private:
515 double m_n0;
516
518 ROOT::Math::XYVector m_n12;
519
520 };
521 }
523}
A two dimensional normal line.
Definition Line2D.h:39
Line2D passiveMovedAlongFirst(const double first) const
Return a copy of the line passively moved long the first coordinate.
Definition Line2D.h:417
void flipSecond()
Flips the first coordinate inplace (no difference between active and passive)
Definition Line2D.h:435
Line2D movedAlongFirst(const double first) const
Return a copy of the line actively moved long the first coordinate.
Definition Line2D.h:387
Line2D()
Default constructor for ROOT compatibility.
Definition Line2D.h:43
double n1() const
Getter for the second line parameter.
Definition Line2D.h:107
EForwardBackward alignedWithFirst() const
Returns if the direction of positive advance has a common component aligned or anti aligned with the ...
Definition Line2D.h:351
Line2D flippedSecond() const
Makes a copy of the line with the second coordinate flipped (no difference between active and passive...
Definition Line2D.h:449
bool isRight(const ROOT::Math::XYVector &rhs) const
Return if the point given is right of the line.
Definition Line2D.h:288
double normalization() const
Calculates the normalization. Helper for normalize.
Definition Line2D.h:234
double zero() const
Returns the root of the line.
Definition Line2D.h:477
bool isInvalid() const
Indicates if all circle parameters are zero.
Definition Line2D.h:320
void reverse()
Flips orientation the line in place.
Definition Line2D.h:208
ROOT::Math::XYVector intersection(const Line2D &line) const
Calculates the intersection point of two line. Infinity for parallels.
Definition Line2D.cc:13
Line2D inverted() const
Returns the inverse function line as a copy.
Definition Line2D.h:507
void setN0(const double n0)
Setter for first line parameter This sets the signed distance of the line to the origin.
Definition Line2D.h:131
double slope() const
Returns the slope over the first coordinate.
Definition Line2D.h:459
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:179
double map(const double first) const
Maps the first coordinate to the second.
Definition Line2D.h:483
const ROOT::Math::XYVector & n12() const
Getter for the unit normal vector to the line.
Definition Line2D.h:119
const ROOT::Math::XYVector & normal() const
Getter for the unit normal vector of the line.
Definition Line2D.h:332
void setN2(const double n2)
Setter for the third line parameter. May violate the normalization.
Definition Line2D.h:144
void moveBy(const ROOT::Math::XYVector &by)
Actively moves the line in the direction given in place by the vector given.
Definition Line2D.h:369
bool isLeft(const ROOT::Math::XYVector &rhs) const
Return if the point given is left of the line.
Definition Line2D.h:282
ROOT::Math::XYVector closest(const ROOT::Math::XYVector &point) const
Calculates the point of closest approach on the line to the point.
Definition Line2D.h:294
ROOT::Math::XYVector closestToOrigin() const
Returns the point closest to the origin.
Definition Line2D.h:302
void flipFirst()
Flips the first coordinate inplace (no difference between active and passive)
Definition Line2D.h:429
void setN1(const double n1)
Setter for the second line parameter. May violate the normalization.
Definition Line2D.h:138
double m_n0
Memory for the first line parameter.
Definition Line2D.h:515
double distanceToOrigin() const
Returns the distance to the origin The distance to the origin is equivalent to the first line paramet...
Definition Line2D.h:264
double intercept() const
Returns the intercept over the first coordinate.
Definition Line2D.h:471
void moveAlongFirst(const double first)
Actively moves the line in the direction given in place along the first coordinate.
Definition Line2D.h:375
void setN12(const double n1, const double n2)
Setter for the normal vector by its coordinates.
Definition Line2D.h:150
double inverseSlope() const
Returns the slope over the second coordinate.
Definition Line2D.h:465
double normalizationSquared() const
Calculates the squared normalization. Helper for normalize.
Definition Line2D.h:228
double inverseMap(const double second) const
Maps the second coordinate to the first.
Definition Line2D.h:494
double operator()(const double first) const
Maps the first coordinate to the second.
Definition Line2D.h:488
double distance(const ROOT::Math::XYVector &point) const
Calculates the signed distance of the point to the line.
Definition Line2D.h:245
void invert()
Turns the line function into its inverse function in place.
Definition Line2D.h:500
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:86
void passiveMoveAlongFirst(const double first)
Passively move the coordinate system in place along the first coordinate.
Definition Line2D.h:405
void invalidate()
Sets all line parameters to zero.
Definition Line2D.h:172
ERightLeft isRightOrLeft(const ROOT::Math::XYVector &point) const
Return if the point given is right or left of the line.
Definition Line2D.h:276
void passiveMoveBy(const ROOT::Math::XYVector &by)
Passively move the coordinate system in place by the given vector.
Definition Line2D.h:399
ROOT::Math::XYVector tangential() const
Gives the tangential vector in the direction of positive advance on the line.
Definition Line2D.h:326
EForwardBackward alignedWithSecond() const
Returns if the direction of positive advance has a common component aligned or anti aligned with the ...
Definition Line2D.h:358
Line2D flippedFirst() const
Makes a copy of the line with the first coordinate flipped (no difference between active and passive)
Definition Line2D.h:442
void scaleN(const double factor)
Scales all parameters. Helper for normalize.
Definition Line2D.h:221
Line2D reversed() const
Returns a copy of the line with the reversed orientation.
Definition Line2D.h:214
double n2() const
Getter for the third line parameter.
Definition Line2D.h:113
Line2D movedAlongSecond(const double second) const
Return a copy of the line actively moved long the first coordinate.
Definition Line2D.h:393
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:255
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:189
double absoluteDistance(const ROOT::Math::XYVector &point) const
Returns the absolute value of distance(point)
Definition Line2D.h:270
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:164
ROOT::Math::XYVector support() const
Getter for the support point of the line being the point closest to the origin.
Definition Line2D.h:344
void normalize()
Updates the parameters to obey the normalization condition.
Definition Line2D.h:200
Line2D(const double n0, const double n1, const double n2)
Constructs taking all three line parameters.
Definition Line2D.h:50
void setN12(const ROOT::Math::XYVector &n12)
Setter for the normal vector.
Definition Line2D.h:156
void passiveMoveAlongSecond(const double second)
Passively move the coordinate system in place along the second coordinate.
Definition Line2D.h:411
double n0() const
Getter for the first line parameter.
Definition Line2D.h:101
double lengthOnCurve(const ROOT::Math::XYVector &from, const ROOT::Math::XYVector &to) const
Calculates the length on the curve between two points.
Definition Line2D.h:314
static Line2D throughPoints(const ROOT::Math::XYVector &start, const ROOT::Math::XYVector &end)
Constructs a line through the two given points.
Definition Line2D.h:94
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:72
const ROOT::Math::XYVector & gradient() const
Getter for the gradient of the distance field.
Definition Line2D.h:338
Line2D(const double n0, const ROOT::Math::XYVector &n12)
Constructs taking the distance to the origin ( n0 ) and the normal vector.
Definition Line2D.h:58
void moveAlongSecond(const double second)
Actively moves the line in the direction given in place along the second coordinate.
Definition Line2D.h:381
Line2D passiveMovedAlongSecond(const double second) const
Return a copy of the line passively moved long the first coordinate.
Definition Line2D.h:423
ROOT::Math::XYVector m_n12
Memory for the second line parameter.
Definition Line2D.h:518
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.