Belle II Software  release-08-01-10
Polygon2D.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/Polygon2D.h>
11 
12 /* Basf2 headers. */
13 #include <framework/core/RandomNumbers.h>
14 
15 using namespace Belle2;
16 
18  m_nPoints(n)
19 {
20  int i;
21  m_LineSegments = new LineSegment2D*[n];
22  for (i = 0; i < n; i++) {
23  if (i < n - 1)
24  m_LineSegments[i] = new LineSegment2D(points[i], points[i + 1]);
25  else
26  m_LineSegments[i] = new LineSegment2D(points[i], points[0]);
27  }
28 }
29 
31 {
32  int i;
33  for (i = 0; i < m_nPoints; i++)
34  delete m_LineSegments[i];
35  delete[] m_LineSegments;
36 }
37 
39 {
40  int i, n, nIntersections;
41  double angle, t[2];
42  HepGeom::Point3D<double> intersection;
43 new_line: {
44  nIntersections = 0;
45  angle = gRandom->Uniform(0., 2.0 * M_PI);
46  Line2D line(point.x(), point.y(), cos(angle), sin(angle));
47  for (i = 0; i < m_nPoints; i++) {
48  n = m_LineSegments[i]->Line2D::findIntersection(line, &intersection, t);
49  if (n == 0)
50  continue;
51  /* Line goes through vertex, need to generate another line. */
52  if (t[0] == 0 || t[0] == 1)
53  goto new_line;
54  if (t[0] > 0 && t[0] < 1 && t[1] > 0)
55  nIntersections++;
56  }
57  }
58  if (nIntersections % 2 == 1)
59  return true;
60  return false;
61 }
62 
63 bool EKLM::Polygon2D::hasIntersection(const LineSegment2D& lineSegment) const
64 {
65  int i;
66  HepGeom::Point3D<double> intersection;
67  for (i = 0; i < m_nPoints; i++) {
68  if (m_LineSegments[i]->findIntersection(lineSegment, &intersection) > 0)
69  return true;
70  }
71  if (pointInside(lineSegment.getInitialPoint()))
72  return true;
73  return false;
74 }
75 
77 {
78  int i;
79  HepGeom::Point3D<double> intersections[2];
80  for (i = 0; i < m_nPoints; i++) {
81  if (m_LineSegments[i]->findIntersection(arc, intersections) > 0)
82  return true;
83  }
84  if (pointInside(arc.getInitialPoint()))
85  return true;
86  return false;
87 }
88 
89 bool EKLM::Polygon2D::hasIntersection(const Polygon2D& polygon) const
90 {
91  int i;
92  for (i = 0; i < m_nPoints; i++) {
93  if (polygon.hasIntersection(*m_LineSegments[i]))
94  return true;
95  }
96  if (pointInside(polygon.getLineSegments()[0]->getInitialPoint()) ||
97  polygon.pointInside(m_LineSegments[0]->getInitialPoint()))
98  return true;
99  return false;
100 }
HepGeom::Point3D< double > getInitialPoint() const
Get initial point.
Definition: Arc2D.cc:33
const HepGeom::Point3D< double > & getInitialPoint() const
Get initial point.
Definition: Line2D.h:51
Polygon2D(const HepGeom::Point3D< double > *points, int n)
Constructor.
Definition: Polygon2D.cc:17
LineSegment2D ** getLineSegments() const
Get line segments.
Definition: Polygon2D.h:84
bool pointInside(const HepGeom::Point3D< double > &point) const
Check if point is inside the polygon.
Definition: Polygon2D.cc:38
bool hasIntersection(const LineSegment2D &lineSegment) const
Check whether polygon has an intersection with a line segment or this line segment is fully inside th...
Definition: Polygon2D.cc:63
LineSegment2D ** m_LineSegments
Line segments.
Definition: Polygon2D.h:95
~Polygon2D()
Destructor.
Definition: Polygon2D.cc:30
Abstract base class for different kinds of events.