Belle II Software development
Line2D.cc
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
9/* Own header. */
10#include <klm/eklm/geometry/Line2D.h>
11
12/* KLM headers. */
13#include <klm/eklm/geometry/Circle2D.h>
14
15using namespace Belle2;
16
17EKLM::Line2D::Line2D(double x, double y, double vecx, double vecy) :
18 m_Point(x, y, 0),
19 m_Vector(vecx, vecy, 0)
20{
21}
22
24{
25}
26
28findIntersection(const Line2D& line,
29 HepGeom::Point3D<double>* intersection) const
30{
31 double t[2];
32 return findIntersection(line, intersection, t);
33}
34
36findIntersection(const Circle2D& circle,
37 HepGeom::Point3D<double> intersections[2]) const
38{
39 double t[2], angles[2];
40 return findIntersection(circle, intersections, t, angles);
41}
42
44findIntersection(const Arc2D& arc,
45 HepGeom::Point3D<double> intersections[2]) const
46{
47 int i, n;
48 double t[2], angles[2];
49 bool condition[2];
50 n = findIntersection(arc, intersections, t, angles);
51 for (i = 0; i < n; i++)
52 condition[i] = arc.angleWithinRange(angles[i]);
53 return selectIntersections(intersections, condition, n);
54}
55
56/*
57 * System of equations:
58 * m_Point.x() + m_Vector.x() * t[0] =
59 * line.getInitialPoint().x() + line.getVector().x() * t[1],
60 * m_Point.y() + m_Vector.y() * t[0] =
61 * line.getInitialPoint().y() + line.getVector().y() * t[1].
62 * Other form:
63 * a1 * t[0] + b1 * t[1] + c1 = 0, a2 * t[0] + b2 * t[1] + c2 = 0.
64 */
66findIntersection(const Line2D& line,
67 HepGeom::Point3D<double>* intersection, double t[2]) const
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}
89
90/*
91 * Equation: (m_Point.x() + m_Vector.x() * t - circle.getCenter().x())^2 +
92 * (m_Point.y() + m_Vector.y() * t - circle.getCenter().y())^2 =
93 * circle.getRadius()^2
94 */
96findIntersection(const Circle2D& circle,
97 HepGeom::Point3D<double> intersections[2],
98 double t[2], double angles[2]) const
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}
131
133 bool* condition, int n) const
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}
146
bool angleWithinRange(double angle) const
Check if angle is within the arc.
Definition: Arc2D.cc:26
const HepGeom::Point3D< double > & getCenter() const
Get center.
Definition: Circle2D.h:45
double getRadius() const
Get radius.
Definition: Circle2D.h:53
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
Line2D(double x, double y, double vecx, double vecy)
Constructor.
Definition: Line2D.cc:17
~Line2D()
Destructor.
Definition: Line2D.cc:23
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Abstract base class for different kinds of events.