Belle II Software development
LineSegment2D Class Reference

2D line segment. More...

#include <LineSegment2D.h>

Inheritance diagram for LineSegment2D:
Line2D

Public Member Functions

 LineSegment2D (const HepGeom::Point3D< double > &point1, const HepGeom::Point3D< double > &point2)
 Constructor.
 
 ~LineSegment2D ()
 Destructor.
 
int findIntersection (const Line2D &line, HepGeom::Point3D< double > *intersection) const
 Find intersection with a line.
 
int findIntersection (const LineSegment2D &lineSegment, HepGeom::Point3D< double > *intersection) const
 Find intersection with a line segment.
 
int findIntersection (const Circle2D &circle, HepGeom::Point3D< double > intersections[2]) const
 Find intersections with a circle.
 
int findIntersection (const Arc2D &arc, HepGeom::Point3D< double > intersections[2]) const
 Find intersections with an arc.
 
bool tWithinRange (double t) const
 Check if t is within the line segment (0 <= t <= 1).
 
const HepGeom::Point3D< double > & getInitialPoint () const
 Get initial point.
 
const HepGeom::Vector3D< double > & getVector () const
 Get vector.
 
int findIntersection (const Line2D &line, HepGeom::Point3D< double > *intersection, double t[2]) const
 Find intersection with a line.
 

Protected Member Functions

int findIntersection (const Circle2D &circle, HepGeom::Point3D< double > intersections[2], double t[2], double angles[2]) const
 Find intersections with a circle.
 
int selectIntersections (HepGeom::Point3D< double > *intersections, bool *condition, int n) const
 Select intersections.
 

Protected Attributes

HepGeom::Point3D< double > m_Point
 Initial point.
 
HepGeom::Vector3D< double > m_Vector
 Vector.
 

Detailed Description

2D line segment.

Equation: m_Point + m_Vector * t, t = [0, 1].

Definition at line 28 of file LineSegment2D.h.

Constructor & Destructor Documentation

◆ LineSegment2D()

LineSegment2D ( const HepGeom::Point3D< double > &  point1,
const HepGeom::Point3D< double > &  point2 
)

Constructor.

Parameters
[in]point1First point.
[in]point2Second point.

Definition at line 14 of file LineSegment2D.cc.

15 :
16 Line2D(point1.x(), point1.y(),
17 point2.x() - point1.x(), point2.y() - point1.y())
18{
19}
Line2D(double x, double y, double vecx, double vecy)
Constructor.
Definition: Line2D.cc:17

◆ ~LineSegment2D()

Destructor.

Definition at line 21 of file LineSegment2D.cc.

22{
23}

Member Function Documentation

◆ findIntersection() [1/6]

int findIntersection ( const Arc2D arc,
HepGeom::Point3D< double >  intersections[2] 
) const

Find intersections with an arc.

Parameters
[in]arcArc.
[out]intersectionsIntersections.
Returns
Number of intersections (0, 1 or 2).

Definition at line 67 of file LineSegment2D.cc.

70{
71 int i, n;
72 double t[2], angles[2];
73 bool condition[2];
74 n = Line2D::findIntersection(arc, intersections, t, angles);
75 for (i = 0; i < n; i++)
76 condition[i] = tWithinRange(t[i]) && arc.angleWithinRange(angles[i]);
77 return selectIntersections(intersections, condition, n);
78}
int findIntersection(const Line2D &line, HepGeom::Point3D< double > *intersection) const
Find intersection with a line.
Definition: Line2D.cc:28
int selectIntersections(HepGeom::Point3D< double > *intersections, bool *condition, int n) const
Select intersections.
Definition: Line2D.cc:132
bool tWithinRange(double t) const
Check if t is within the line segment (0 <= t <= 1).

◆ findIntersection() [2/6]

int findIntersection ( const Circle2D circle,
HepGeom::Point3D< double >  intersections[2] 
) const

Find intersections with a circle.

Parameters
[in]circleCircle.
[out]intersectionsIntersections.
Returns
Number of intersections (0, 1 or 2).

Definition at line 54 of file LineSegment2D.cc.

57{
58 int i, n;
59 double t[2], angles[2];
60 bool condition[2];
61 n = Line2D::findIntersection(circle, intersections, t, angles);
62 for (i = 0; i < n; i++)
63 condition[i] = tWithinRange(t[i]);
64 return selectIntersections(intersections, condition, n);
65}

◆ findIntersection() [3/6]

int findIntersection ( const Circle2D circle,
HepGeom::Point3D< double >  intersections[2],
double  t[2],
double  angles[2] 
) const
protectedinherited

Find intersections with a circle.

Parameters
[in]circleCircle.
[out]intersectionsIntersections.
[out]tValues of t for intersection points.
[out]anglesValues of angles for intersection points.
Returns
Number of intersections (0, 1 or 2).

Definition at line 95 of file Line2D.cc.

99{
100 int i;
101 double a, b, c, d, x0, y0;
102 const HepGeom::Point3D<double>& circleCenter = circle.getCenter();
103 x0 = m_Point.x() - circleCenter.x();
104 y0 = m_Point.y() - circleCenter.y();
105 a = m_Vector.x() * m_Vector.x() + m_Vector.y() * m_Vector.y();
106 b = 2.0 * (m_Vector.x() * x0 + m_Vector.y() * y0);
107 c = x0 * x0 + y0 * y0 - circle.getRadius() * circle.getRadius();
108 d = b * b - 4 * a * c;
109 if (d < 0)
110 return 0;
111 if (d == 0) {
112 t[0] = -b / (2.0 * a);
113 intersections[0].setX(m_Point.x() + m_Vector.x() * t[0]);
114 intersections[0].setY(m_Point.y() + m_Vector.y() * t[0]);
115 intersections[0].setZ(0);
116 angles[0] = atan2(intersections[0].y() - circleCenter.y(),
117 intersections[0].x() - circleCenter.x());
118 return 1;
119 }
120 t[0] = (-b - sqrt(d)) / (2.0 * a);
121 t[1] = (-b + sqrt(d)) / (2.0 * a);
122 for (i = 0; i < 2; i++) {
123 intersections[i].setX(m_Point.x() + m_Vector.x() * t[i]);
124 intersections[i].setY(m_Point.y() + m_Vector.y() * t[i]);
125 intersections[i].setZ(0);
126 angles[i] = atan2(intersections[i].y() - circleCenter.y(),
127 intersections[i].x() - circleCenter.x());
128 }
129 return 2;
130}
HepGeom::Point3D< double > m_Point
Initial point.
Definition: Line2D.h:130
HepGeom::Vector3D< double > m_Vector
Vector.
Definition: Line2D.h:133
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28

◆ findIntersection() [4/6]

int findIntersection ( const Line2D line,
HepGeom::Point3D< double > *  intersection 
) const

Find intersection with a line.

Parameters
[in]lineLine.
[out]intersectionIntersection.
Returns
Number of intersections (0 or 1).

Definition at line 25 of file LineSegment2D.cc.

28{
29 int n;
30 double t[2];
31 bool condition;
32 n = Line2D::findIntersection(line, intersection, t);
33 if (n == 0)
34 return 0;
35 condition = tWithinRange(t[0]);
36 return selectIntersections(intersection, &condition, 1);
37}

◆ findIntersection() [5/6]

int findIntersection ( const Line2D line,
HepGeom::Point3D< double > *  intersection,
double  t[2] 
) const
inherited

Find intersection with a line.

Parameters
[in]lineLine.
[out]intersectionIntersection.
[out]tValues of t for intersection point (t[0] - this line, t[1] - line from argument).
Returns
Number of intersections (0 or 1).

If (*this) and line are the same line then 0 is returned.

Definition at line 65 of file Line2D.cc.

68{
69 double a1, a2, b1, b2, c1, c2, d, dt[2];
70 a1 = m_Vector.x();
71 a2 = m_Vector.y();
72 b1 = -line.getVector().x();
73 b2 = -line.getVector().y();
74 c1 = m_Point.x() - line.getInitialPoint().x();
75 c2 = m_Point.y() - line.getInitialPoint().y();
76 d = a1 * b2 - a2 * b1;
77 /* Same line (d == 0, dt[i] == 0) considered as no intersection! */
78 if (d == 0)
79 return 0;
80 dt[0] = c1 * b2 - c2 * b1;
81 dt[1] = a1 * c2 - a2 * c1;
82 t[0] = -dt[0] / d;
83 t[1] = -dt[1] / d;
84 intersection->setX(m_Point.x() + m_Vector.x() * t[0]);
85 intersection->setY(m_Point.y() + m_Vector.y() * t[0]);
86 intersection->setZ(0);
87 return 1;
88}

◆ findIntersection() [6/6]

int findIntersection ( const LineSegment2D lineSegment,
HepGeom::Point3D< double > *  intersection 
) const

Find intersection with a line segment.

Parameters
[in]lineSegmentLine segment.
[out]intersectionIntersection.
Returns
Number of intersections (0 or 1).

Definition at line 40 of file LineSegment2D.cc.

43{
44 int n;
45 double t[2];
46 bool condition;
47 n = Line2D::findIntersection(lineSegment, intersection, t);
48 if (n == 0)
49 return 0;
50 condition = tWithinRange(t[0]) && lineSegment.tWithinRange(t[1]);
51 return selectIntersections(intersection, &condition, 1);
52}

◆ getInitialPoint()

const HepGeom::Point3D< double > & getInitialPoint ( ) const
inlineinherited

Get initial point.

Definition at line 51 of file Line2D.h.

52 {
53 return m_Point;
54 }

◆ getVector()

const HepGeom::Vector3D< double > & getVector ( ) const
inlineinherited

Get vector.

Definition at line 59 of file Line2D.h.

60 {
61 return m_Vector;
62 }

◆ selectIntersections()

int selectIntersections ( HepGeom::Point3D< double > *  intersections,
bool *  condition,
int  n 
) const
protectedinherited

Select intersections.

Parameters
[in,out]intersectionsIntersections.
[in]conditionSelection condition.
[in]nNumber of intersections.
Returns
Number of selected intersections.

Definition at line 132 of file Line2D.cc.

134{
135 int i, j;
136 j = 0;
137 for (i = 0; i < n; i++) {
138 if (condition[i]) {
139 if (i != j)
140 intersections[j] = intersections[i];
141 j++;
142 }
143 }
144 return j;
145}

◆ tWithinRange()

bool tWithinRange ( double  t) const

Check if t is within the line segment (0 <= t <= 1).

Parameters
[in]tLine parameter.

Definition at line 80 of file LineSegment2D.cc.

81{
82 return (t >= 0 && t <= 1);
83}

Member Data Documentation

◆ m_Point

HepGeom::Point3D<double> m_Point
protectedinherited

Initial point.

Definition at line 130 of file Line2D.h.

◆ m_Vector

HepGeom::Vector3D<double> m_Vector
protectedinherited

Vector.

Definition at line 133 of file Line2D.h.


The documentation for this class was generated from the following files: