-
Notifications
You must be signed in to change notification settings - Fork 1
/
Voronoi.cpp
162 lines (134 loc) · 3.17 KB
/
Voronoi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "Voronoi.h"
Voronoi::Voronoi()
{
state = VoronoiState::INIT;
beachLine = new BeachLineBST();
eventPQ = new EventPQ();
}
VoronoiOutput Voronoi::generateVoronoi(std::vector<position2D>& sitePositions, float minX, float maxX, float minY, float maxY)
{
float dX = maxX - minX;
float dY = maxY - minY;
maxEdgeLength = std::sqrt(dX * dX + dY * dY);
VoronoiOutput out;
for (auto pos : sitePositions)
{
out.sites.push_back(new Vertex(pos, true));
}
for (auto site : out.sites)
{
//Create new site Event and push it to pq
eventPQ->push(new Event(site));
}
//Handle the rest of events
while (!eventPQ->isEmpty())
{
Event* event = eventPQ->top();
eventPQ->pop();
if (event->type == EventType::SITE)
{
//Site Event
beachLine->InsertArc(event->point, eventPQ);
}
else
{
//Circle Event
beachLine->RemoveArc(event, eventPQ, out.edges, out.vertices);
}
delete event;
}
finishRemainingEdges(beachLine->root, out.edges);
return out;
}
void Voronoi::VoronoiInteractive(std::vector<position2D>& sitePositions, float minX, float maxX, float minY, float maxY, float sweepLineY, VoronoiOutput &out)
{
switch (state)
{
case VoronoiState::INIT:
{
float dX = maxX - minX;
float dY = maxY - minY;
maxEdgeLength = std::sqrt(dX * dX + dY * dY);
for (auto pos : sitePositions)
{
out.sites.push_back(new Vertex(pos, true));
}
for (auto site : out.sites)
{
//Create new site Event and push it to pq
eventPQ->push(new Event(site));
}
state = VoronoiState::PROCESSING_EVENT_PQ;
}
case VoronoiState::PROCESSING_EVENT_PQ:
{
//Handle the rest of events
while (!eventPQ->isEmpty())
{
Event* event = eventPQ->top();
if (event->point->pos.y < minY)
break;
if (sweepLineY > event->point->pos.y)
return;
eventPQ->pop();
if (event->type == EventType::SITE)
{
//Site Event
beachLine->InsertArc(event->point, eventPQ);
}
else
{
//Circle Event
beachLine->RemoveArc(event, eventPQ, out.edges, out.vertices);
}
delete event;
}
state = VoronoiState::COMPLETE_REMAINING_EDGES;
}
case VoronoiState::COMPLETE_REMAINING_EDGES:
{
//If EventPQ is not empty
while (!eventPQ->isEmpty())
{
Event* event = eventPQ->top();
eventPQ->pop();
delete event;
}
finishRemainingEdges(beachLine->root, out.edges);
beachLine->root = nullptr;
state = VoronoiState::FINISHED;
}
default: return;
}
}
void Voronoi::finishRemainingEdges(BeachLineBSTNode* curr, std::vector<Edge*> &edges)
{
if (curr == nullptr)
return;
if (!curr->isLeaf)
{
finishRemainingEdges(curr->GetLeftChild(), edges);
//curr is a breakpoint
HalfEdge ray = curr->ray;
Edge *e = new Edge();
position2D edgeEnd;
edgeEnd.x = ray.orig.x + maxEdgeLength * ray.dir.x;
edgeEnd.y = ray.orig.y + maxEdgeLength * ray.dir.y;
e->start = new Vertex(ray.orig);
e->end = new Vertex(edgeEnd);
edges.push_back(e);
TempVertices.push_back(e->start);
TempVertices.push_back(e->end);
finishRemainingEdges(curr->GetRightChild(), edges);
}
delete curr;
}
Voronoi::~Voronoi()
{
delete beachLine;
delete eventPQ;
for (auto &v : TempVertices)
{
delete v;
}
}