Voronoi.h
Go to the documentation of this file.
1 /**
2  * @file VoronoiDiagram.h
3  *
4  * @brief Implements a VoronoiDiagram for use in clustering
5  *
6  * @author usher@slac.stanford.edu
7  *
8  */
9 #ifndef VoronoiDiagram_h
10 #define VoronoiDiagram_h
11 
12 // std includes
13 #include <queue>
14 
15 // LArSoft includes
19 
20 #include <boost/polygon/voronoi.hpp>
21 
22 // Algorithm includes
24 //------------------------------------------------------------------------------------------------------------------------------------------
25 
26 namespace voronoi2d
27 {
28 /**
29  * @brief VoronoiDiagram class definiton
30  */
32 {
33 public:
34  using PointPair = std::pair<dcel2d::Point,dcel2d::Point>;
35  using MinMaxPointPair = std::pair<PointPair,PointPair>;
36 
37  /**
38  * @brief Constructor
39  *
40  * @param pset
41  */
43 
44  /**
45  * @brief Destructor
46  */
48 
49  /**
50  * @brief Given an input set of 2D points construct a 2D voronoi diagram
51  *
52  * @param PointList The input list of 2D points
53  */
55 
57 
58  /**
59  * @brief Recover the list of faces
60  */
61  const dcel2d::FaceList& getFaceList() const {return fFaceList;}
62 
63  /**
64  * @brief Recover the list of vertices
65  */
66  const dcel2d::VertexList& getVertexList() const {return fVertexList;}
67 
68  /**
69  * @brief recover the area of the convex hull
70  */
71  double getVoronoiDiagramArea() const {return fVoronoiDiagramArea;}
72 
73  /**
74  * @brief recover the point list representing the convex hull
75  */
77 
78  /**
79  * @brief Given an input Point, find the nearest edge
80  */
82 
83  /**
84  * @brief Given an input Point, find the nearest edge
85  */
86  PointPair findNearestEdge(const dcel2d::Point&, double&) const;
87 
88  /**
89  * @brief Given an input point, find the distance to the nearest edge/point
90  */
91  double findNearestDistance(const dcel2d::Point&) const;
92 
93 private:
94 
95  using EventQueue = std::priority_queue<IEvent*, std::vector<IEvent*>, bool(*)(const IEvent*,const IEvent*)>;
96 
97  /**
98  * @brief There are two types of events in the queue, here we handle site events
99  */
100  void handleSiteEvents(BeachLine&, EventQueue&, IEvent*);
101 
102  /**
103  * @brief There are two types of events in the queue, here we handle circle events
104  */
105  void handleCircleEvents(BeachLine&, EventQueue&, IEvent*);
106 
107  void makeLeftCircleEvent(EventQueue&, BSTNode*, double);
108  void makeRightCircleEvent(EventQueue&, BSTNode*, double);
109 
110  /**
111  * @brief There are two types of events in the queue, here we handle circle events
112  */
113  IEvent* makeCircleEvent(BSTNode*, BSTNode*, BSTNode*, double);
114 
115  bool computeCircleCenter(const dcel2d::Coords&, const dcel2d::Coords&, const dcel2d::Coords&, dcel2d::Coords&, double&, double&) const;
116 
117  bool computeCircleCenter2(const dcel2d::Coords&, const dcel2d::Coords&, const dcel2d::Coords&, dcel2d::Coords&, double&, double&) const;
118 
119  bool computeCircleCenter3(const dcel2d::Coords&, const dcel2d::Coords&, const dcel2d::Coords&, dcel2d::Coords&, double&, double&) const;
120 
121  /**
122  * @brief this function recovers the convex hull
123  */
124  void getConvexHull(const BSTNode*);
125 
126  /**
127  * @brief this aims to process remaining elements in the beachline after the event queue has been depleted
128  */
129  void terminateInfiniteEdges(BeachLine&, double);
130 
131  /**
132  * @brief Is a vertex inside the convex hull - meant to be a fast check
133  */
134  bool isInsideConvexHull(const dcel2d::Vertex&) const;
135 
136  /**
137  * @brief is vertex outside the convex hull and if so return some useful information
138  */
140 
141  /**
142  * @brief Find the min/max values in x-y to use as a bounding box
143  */
144  void findBoundingBox(const dcel2d::VertexList&);
145 
146  /**
147  * @brief Translate boost to dcel
148  */
149  using BoostEdgeToEdgeMap = std::map<const boost::polygon::voronoi_edge<double>*, dcel2d::HalfEdge*>;
150  using BoostVertexToVertexMap = std::map<const boost::polygon::voronoi_vertex<double>*, dcel2d::Vertex*>;
151  using BoostCellToFaceMap = std::map<const boost::polygon::voronoi_cell<double>*, dcel2d::Face*>;
152 
154  const boost::polygon::voronoi_edge<double>*,
155  const boost::polygon::voronoi_edge<double>*,
159 
160  /**
161  * @brief merge degenerate vertices (found by zero length edges)
162  */
164 
165  /**
166  * @brief Compute the area of the faces
167  */
168  double ComputeFaceArea();
169 
170  /**
171  * @brief Gets the cross product of line from p0 to p1 and p0 to p2
172  */
173  double crossProduct(const dcel2d::Point& p0, const dcel2d::Point& p1, const dcel2d::Point& p2) const;
174 
175  /**
176  * @brief Computes the area of the enclosed convext hull
177  */
178  double Area() const;
179 
180  /**
181  * @brief Determines whether a point is to the left, right or on line specifiec by p0 and p1
182  */
183  bool isLeft(const dcel2d::Point& p0, const dcel2d::Point& p1, const dcel2d::Point& pCheck) const;
184 
188 
190  SiteEventList fSiteEventList; //< Container for site events
191  CircleEventList fCircleEventList; //< Container for circle events
192  BSTNodeList fCircleNodeList; //< Container for the circle "nodes"
193 
194  dcel2d::PointList fConvexHullList; //< Points representing the convex hull
195  dcel2d::Coords fConvexHullCenter; //< Center of the convex hull
196 
197  double fXMin; //< Bounding box min value x
198  double fXMax; //< Bounding box max value x
199  double fYMin; //< Bounding box min value y
200  double fYMax; //< Bounding box max value y
201  mutable int fNumBadCircles; //< Number bad circles
203 
204  EventUtilities fUtilities; //< Handy functions to operate on arcs
205 
206 };
207 
208 } // namespace lar_cluster3d
209 #endif
std::pair< PointPair, PointPair > MinMaxPointPair
Definition: Voronoi.h:35
double getVoronoiDiagramArea() const
recover the area of the convex hull
Definition: Voronoi.h:71
This defines the actual beach line. The idea is to implement this as a self balancing binary search t...
Definition: BeachLine.h:124
bool computeCircleCenter2(const dcel2d::Coords &, const dcel2d::Coords &, const dcel2d::Coords &, dcel2d::Coords &, double &, double &) const
Definition: Voronoi.cxx:762
std::list< SiteEvent > SiteEventList
Definition: SweepEvent.h:100
dcel2d::HalfEdgeList & fHalfEdgeList
Definition: Voronoi.h:185
std::list< HalfEdge > HalfEdgeList
Definition: DCEL.h:184
std::list< CircleEvent > CircleEventList
Definition: SweepEvent.h:101
Provides some basic functions operating in IEvent class objects.
BSTNode class definiton specifically for use in constructing Voronoi diagrams. We are trying to follo...
Definition: BeachLine.h:32
bool isInsideConvexHull(const dcel2d::Vertex &) const
Is a vertex inside the convex hull - meant to be a fast check.
Definition: Voronoi.cxx:1102
double Area() const
Computes the area of the enclosed convext hull.
Definition: Voronoi.cxx:104
intermediate_table::const_iterator const_iterator
dcel2d::FaceList & fFaceList
Definition: Voronoi.h:187
std::list< BSTNode > BSTNodeList
Definition: BeachLine.h:117
Internal class definitions to facilitate construction of diagram.
Represents the beachline implemented as a self balancing binary search tree.
~VoronoiDiagram()
Destructor.
Definition: Voronoi.cxx:76
void makeLeftCircleEvent(EventQueue &, BSTNode *, double)
Definition: Voronoi.cxx:557
bool computeCircleCenter(const dcel2d::Coords &, const dcel2d::Coords &, const dcel2d::Coords &, dcel2d::Coords &, double &, double &) const
Definition: Voronoi.cxx:690
BSTNodeList fCircleNodeList
Definition: Voronoi.h:192
IEvent * makeCircleEvent(BSTNode *, BSTNode *, BSTNode *, double)
There are two types of events in the queue, here we handle circle events.
Definition: Voronoi.cxx:653
void handleCircleEvents(BeachLine &, EventQueue &, IEvent *)
There are two types of events in the queue, here we handle circle events.
Definition: Voronoi.cxx:472
VoronoiDiagram class definiton.
Definition: Voronoi.h:31
std::list< Face > FaceList
Definition: DCEL.h:183
CircleEventList fCircleEventList
Definition: Voronoi.h:191
SiteEventList fSiteEventList
Definition: Voronoi.h:190
EventUtilities fUtilities
Definition: Voronoi.h:204
double ComputeFaceArea()
Compute the area of the faces.
Definition: Voronoi.cxx:1215
std::priority_queue< IEvent *, std::vector< IEvent * >, bool(*)(const IEvent *, const IEvent *)> EventQueue
Definition: Voronoi.h:95
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:44
void buildVoronoiDiagram(const dcel2d::PointList &)
Given an input set of 2D points construct a 2D voronoi diagram.
Definition: Voronoi.cxx:133
double findNearestDistance(const dcel2d::Point &) const
Given an input point, find the distance to the nearest edge/point.
Definition: Voronoi.cxx:1424
void findBoundingBox(const dcel2d::VertexList &)
Find the min/max values in x-y to use as a bounding box.
Definition: Voronoi.cxx:1346
dcel2d::VertexList & fVertexList
Definition: Voronoi.h:186
void mergeDegenerateVertices()
merge degenerate vertices (found by zero length edges)
Definition: Voronoi.cxx:1187
dcel2d::Coords fConvexHullCenter
Definition: Voronoi.h:195
void makeRightCircleEvent(EventQueue &, BSTNode *, double)
Definition: Voronoi.cxx:606
dcel2d::PointList fPointList
Definition: Voronoi.h:189
double crossProduct(const dcel2d::Point &p0, const dcel2d::Point &p1, const dcel2d::Point &p2) const
Gets the cross product of line from p0 to p1 and p0 to p2.
Definition: Voronoi.cxx:91
PointPair findNearestEdge(const dcel2d::Point &, double &) const
Given an input Point, find the nearest edge.
Definition: Voronoi.cxx:1363
dcel2d::PointList fConvexHullList
Definition: Voronoi.h:194
std::pair< dcel2d::Point, dcel2d::Point > PointPair
Definition: Voronoi.h:34
const dcel2d::PointList & getConvexHull() const
recover the point list representing the convex hull
Definition: Voronoi.h:76
void boostTranslation(const dcel2d::PointList &, const boost::polygon::voronoi_edge< double > *, const boost::polygon::voronoi_edge< double > *, BoostEdgeToEdgeMap &, BoostVertexToVertexMap &, BoostCellToFaceMap &)
Definition: Voronoi.cxx:321
PointPair getExtremePoints() const
Given an input Point, find the nearest edge.
Definition: Voronoi.cxx:1040
std::map< const boost::polygon::voronoi_edge< double > *, dcel2d::HalfEdge * > BoostEdgeToEdgeMap
Translate boost to dcel.
Definition: Voronoi.h:149
std::map< const boost::polygon::voronoi_vertex< double > *, dcel2d::Vertex * > BoostVertexToVertexMap
Definition: Voronoi.h:150
bool computeCircleCenter3(const dcel2d::Coords &, const dcel2d::Coords &, const dcel2d::Coords &, dcel2d::Coords &, double &, double &) const
Definition: Voronoi.cxx:793
void terminateInfiniteEdges(BeachLine &, double)
this aims to process remaining elements in the beachline after the event queue has been depleted ...
Definition: Voronoi.cxx:829
std::list< Point > PointList
Definition: DCEL.h:45
void buildVoronoiDiagramBoost(const dcel2d::PointList &)
Definition: Voronoi.cxx:265
Eigen::Vector3f Coords
Definition: DCEL.h:46
int bool
Definition: qglobal.h:345
const dcel2d::VertexList & getVertexList() const
Recover the list of vertices.
Definition: Voronoi.h:66
const dcel2d::FaceList & getFaceList() const
Recover the list of faces.
Definition: Voronoi.h:61
std::list< Vertex > VertexList
Definition: DCEL.h:182
std::map< const boost::polygon::voronoi_cell< double > *, dcel2d::Face * > BoostCellToFaceMap
Definition: Voronoi.h:151
VoronoiDiagram(dcel2d::HalfEdgeList &, dcel2d::VertexList &, dcel2d::FaceList &)
Constructor.
Definition: Voronoi.cxx:51
bool isOutsideConvexHull(const dcel2d::Vertex &, dcel2d::PointList::const_iterator, dcel2d::Coords &, double &) const
is vertex outside the convex hull and if so return some useful information
Definition: Voronoi.cxx:1131
bool isLeft(const dcel2d::Point &p0, const dcel2d::Point &p1, const dcel2d::Point &pCheck) const
Determines whether a point is to the left, right or on line specifiec by p0 and p1.
Definition: Voronoi.cxx:82
void handleSiteEvents(BeachLine &, EventQueue &, IEvent *)
There are two types of events in the queue, here we handle site events.
Definition: Voronoi.cxx:415