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