-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (143 loc) · 5.62 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (143 loc) · 5.62 KB
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
163
164
/* FortuneAlgorithm
* Copyright (C) 2018 Pierre Vigier
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// STL
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
// SFML
#include <SFML/Graphics.hpp>
// My includes
#include "FortuneAlgorithm.h"
constexpr float WINDOW_WIDTH = 600.0f;
constexpr float WINDOW_HEIGHT = 600.0f;
constexpr float POINT_RADIUS = 0.005f;
constexpr float OFFSET = 1.0f;
std::vector<Vector2> generatePoints(int nbPoints)
{
uint64_t seed = std::chrono::system_clock::now().time_since_epoch().count();
std::cout << "seed: " << seed << '\n';
std::default_random_engine generator(seed);
std::uniform_real_distribution<double> distribution (0.0, 1.0);
std::vector<Vector2> points;
for (int i = 0; i < nbPoints; ++i)
points.push_back(Vector2{distribution(generator), distribution(generator)});
return points;
}
void drawPoint(sf::RenderWindow& window, Vector2 point, sf::Color color)
{
sf::CircleShape shape(POINT_RADIUS);
shape.setPosition(sf::Vector2f(point.x - POINT_RADIUS, 1 - point.y - POINT_RADIUS));
shape.setFillColor(color);
window.draw(shape);
}
void drawEdge(sf::RenderWindow& window, Vector2 origin, Vector2 destination, sf::Color color)
{
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(origin.x, 1.0f - origin.y), color),
sf::Vertex(sf::Vector2f(destination.x, 1.0f - destination.y), color)
};
window.draw(line, 2, sf::Lines);
}
void drawPoints(sf::RenderWindow& window, VoronoiDiagram& diagram)
{
for (std::size_t i = 0; i < diagram.getNbSites(); ++i)
drawPoint(window, diagram.getSite(i)->point, sf::Color(100, 250, 50));
}
void drawDiagram(sf::RenderWindow& window, VoronoiDiagram& diagram)
{
for (std::size_t i = 0; i < diagram.getNbSites(); ++i)
{
const VoronoiDiagram::Site* site = diagram.getSite(i);
Vector2 center = site->point;
VoronoiDiagram::Face* face = site->face;
VoronoiDiagram::HalfEdge* halfEdge = face->outerComponent;
if (halfEdge == nullptr)
continue;
while (halfEdge->prev != nullptr)
{
halfEdge = halfEdge->prev;
if (halfEdge == face->outerComponent)
break;
}
VoronoiDiagram::HalfEdge* start = halfEdge;
while (halfEdge != nullptr)
{
if (halfEdge->origin != nullptr && halfEdge->destination != nullptr)
{
Vector2 origin = (halfEdge->origin->point - center) * OFFSET + center;
Vector2 destination = (halfEdge->destination->point - center) * OFFSET + center;
drawEdge(window, origin, destination, sf::Color::Red);
}
halfEdge = halfEdge->next;
if (halfEdge == start)
break;
}
}
}
VoronoiDiagram generateRandomDiagram(std::size_t nbPoints)
{
// Generate points
std::vector<Vector2> points = generatePoints(nbPoints);
// Construct diagram
FortuneAlgorithm algorithm(points);
auto start = std::chrono::steady_clock::now();
algorithm.construct();
auto duration = std::chrono::steady_clock::now() - start;
std::cout << "construction: " << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << "ms" << '\n';
// Bound the diagram
start = std::chrono::steady_clock::now();
algorithm.bound(Box{-0.05, -0.05, 1.05, 1.05}); // Take the bounding box slightly bigger than the intersection box
duration = std::chrono::steady_clock::now() - start;
std::cout << "bounding: " << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << "ms" << '\n';
VoronoiDiagram diagram = algorithm.getDiagram();
// Intersect the diagram with a box
start = std::chrono::steady_clock::now();
bool valid = diagram.intersect(Box{0.0, 0.0, 1.0, 1.0});
duration = std::chrono::steady_clock::now() - start;
std::cout << "intersection: " << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << "ms" << '\n';
if (!valid)
throw std::runtime_error("An error occured in the box intersection algorithm");
return diagram;
}
int main()
{
std::size_t nbPoints = 100;
VoronoiDiagram diagram = generateRandomDiagram(nbPoints);
// Display the diagram
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Fortune's algorithm", sf::Style::Default, settings);
window.setView(sf::View(sf::FloatRect(-0.1f, -0.1f, 1.2f, 1.2f)));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Key::N)
diagram = generateRandomDiagram(nbPoints);
}
window.clear(sf::Color::Black);
drawDiagram(window, diagram);
drawPoints(window, diagram);
window.display();
}
return 0;
}