Belle II Software  release-05-01-25
Line2D.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Kirill Chilikin *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 /* Own header. */
12 #include <klm/eklm/geometry/Line2D.h>
13 
14 /* KLM headers. */
15 #include <klm/eklm/geometry/Circle2D.h>
16 
17 using namespace Belle2;
18 
19 EKLM::Line2D::Line2D(double x, double y, double vecx, double vecy) :
20  m_Point(x, y, 0),
21  m_Vector(vecx, vecy, 0)
22 {
23 }
24 
26 {
27 }
28 
31  HepGeom::Point3D<double>* intersection) const
32 {
33  double t[2];
34  return findIntersection(line, intersection, t);
35 }
36 
38 findIntersection(const Circle2D& circle,
39  HepGeom::Point3D<double> intersections[2]) const
40 {
41  double t[2], angles[2];
42  return findIntersection(circle, intersections, t, angles);
43 }
44 
47  HepGeom::Point3D<double> intersections[2]) const
48 {
49  int i, n;
50  double t[2], angles[2];
51  bool condition[2];
52  n = findIntersection(arc, intersections, t, angles);
53  for (i = 0; i < n; i++)
54  condition[i] = arc.angleWithinRange(angles[i]);
55  return selectIntersections(intersections, condition, n);
56 }
57 
58 /*
59  * System of equations:
60  * m_Point.x() + m_Vector.x() * t[0] =
61  * line.getInitialPoint().x() + line.getVector().x() * t[1],
62  * m_Point.y() + m_Vector.y() * t[0] =
63  * line.getInitialPoint().y() + line.getVector().y() * t[1].
64  * Other form:
65  * a1 * t[0] + b1 * t[1] + c1 = 0, a2 * t[0] + b2 * t[1] + c2 = 0.
66  */
69  HepGeom::Point3D<double>* intersection, double t[2]) const
70 {
71  double a1, a2, b1, b2, c1, c2, d, dt[2];
72  a1 = m_Vector.x();
73  a2 = m_Vector.y();
74  b1 = -line.getVector().x();
75  b2 = -line.getVector().y();
76  c1 = m_Point.x() - line.getInitialPoint().x();
77  c2 = m_Point.y() - line.getInitialPoint().y();
78  d = a1 * b2 - a2 * b1;
79  /* Same line (d == 0, dt[i] == 0) considered as no intersection! */
80  if (d == 0)
81  return 0;
82  dt[0] = c1 * b2 - c2 * b1;
83  dt[1] = a1 * c2 - a2 * c1;
84  t[0] = -dt[0] / d;
85  t[1] = -dt[1] / d;
86  intersection->setX(m_Point.x() + m_Vector.x() * t[0]);
87  intersection->setY(m_Point.y() + m_Vector.y() * t[0]);
88  intersection->setZ(0);
89  return 1;
90 }
91 
92 /*
93  * Equation: (m_Point.x() + m_Vector.x() * t - circle.getCenter().x())^2 +
94  * (m_Point.y() + m_Vector.y() * t - circle.getCenter().y())^2 =
95  * circle.getRadius()^2
96  */
98 findIntersection(const Circle2D& circle,
99  HepGeom::Point3D<double> intersections[2],
100  double t[2], double angles[2]) const
101 {
102  int i;
103  double a, b, c, d, x0, y0;
104  const HepGeom::Point3D<double>& circleCenter = circle.getCenter();
105  x0 = m_Point.x() - circleCenter.x();
106  y0 = m_Point.y() - circleCenter.y();
107  a = m_Vector.x() * m_Vector.x() + m_Vector.y() * m_Vector.y();
108  b = 2.0 * (m_Vector.x() * x0 + m_Vector.y() * y0);
109  c = x0 * x0 + y0 * y0 - circle.getRadius() * circle.getRadius();
110  d = b * b - 4 * a * c;
111  if (d < 0)
112  return 0;
113  if (d == 0) {
114  t[0] = -b / (2.0 * a);
115  intersections[0].setX(m_Point.x() + m_Vector.x() * t[0]);
116  intersections[0].setY(m_Point.y() + m_Vector.y() * t[0]);
117  intersections[0].setZ(0);
118  angles[0] = atan2(intersections[0].y() - circleCenter.y(),
119  intersections[0].x() - circleCenter.x());
120  return 1;
121  }
122  t[0] = (-b - sqrt(d)) / (2.0 * a);
123  t[1] = (-b + sqrt(d)) / (2.0 * a);
124  for (i = 0; i < 2; i++) {
125  intersections[i].setX(m_Point.x() + m_Vector.x() * t[i]);
126  intersections[i].setY(m_Point.y() + m_Vector.y() * t[i]);
127  intersections[i].setZ(0);
128  angles[i] = atan2(intersections[i].y() - circleCenter.y(),
129  intersections[i].x() - circleCenter.x());
130  }
131  return 2;
132 }
133 
135  bool* condition, int n) const
136 {
137  int i, j;
138  j = 0;
139  for (i = 0; i < n; i++) {
140  if (condition[i]) {
141  if (i != j)
142  intersections[j] = intersections[i];
143  j++;
144  }
145  }
146  return j;
147 }
148 
Belle2::EKLM::Circle2D::getRadius
double getRadius() const
Get radius.
Definition: Circle2D.h:63
Belle2::EKLM::Arc2D
2D arc.
Definition: Arc2D.h:35
Belle2::EKLM::Line2D
2D line.
Definition: Line2D.h:40
Belle2::EKLM::Circle2D
2D circle.
Definition: Circle2D.h:35
Belle2::EKLM::Line2D::~Line2D
~Line2D()
Destructor.
Definition: Line2D.cc:25
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::EKLM::Arc2D::angleWithinRange
bool angleWithinRange(double angle) const
Check if angle is within the arc.
Definition: Arc2D.cc:28
Belle2::EKLM::Line2D::Line2D
Line2D(double x, double y, double vecx, double vecy)
Constructor.
Definition: Line2D.cc:19
Belle2::EKLM::Line2D::selectIntersections
int selectIntersections(HepGeom::Point3D< double > *intersections, bool *condition, int n) const
Select intersections.
Definition: Line2D.cc:134
Belle2::EKLM::Line2D::findIntersection
int findIntersection(const Line2D &line, HepGeom::Point3D< double > *intersection) const
Find intersection with a line.
Definition: Line2D.cc:30
Belle2::EKLM::Circle2D::getCenter
const HepGeom::Point3D< double > & getCenter() const
Get center.
Definition: Circle2D.h:55
HepGeom::Point3D< double >