-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.h
151 lines (119 loc) · 5.44 KB
/
map.h
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
#pragma once
#include <vector>
#include <array>
#include<string>
/*Point generation*/
#include"thinks/poisson_disk_sampling/poisson_disk_sampling.h"
/*Delaunay trianglulation*/
#include<delaunator-cpp/delaunator.hpp>
class Map {
public:
std::vector<std::array<float, 2>> points;
std::vector<double> vertices;
size_t numRegions;
size_t numTriangles;
size_t numEdges;
std::vector<std::size_t> halfedges;
std::vector<std::size_t> triangles;
std::vector<std::pair<double, double>> circumcenters;
std::vector<double> voronoi_edges;
std::vector<double> construct_voronoi_edges(const delaunator::Delaunator& delaunay);
std::vector<std::pair<double, double>> calculateCircumcenters(delaunator::Delaunator d);
std::vector<std::array<float, 2>> generate_points();
std::vector<double> flatten_vertices(std::vector<std::array<float, 2>> points);
Map(){
std::vector<std::array<float, 2>> points = generate_points();
std::vector<double> vertices = flatten_vertices(points);
delaunator::Delaunator delaunay(vertices);
size_t numRegions = points.size();
size_t numTriangles = delaunay.halfedges.size() / 3;
size_t numEdges = delaunay.halfedges.size();
std::vector<std::size_t> halfedges = delaunay.halfedges;
std::vector<std::size_t> triangles = delaunay.triangles;
std::vector<std::pair<double, double>> circumcenters = calculateCircumcenters(delaunay);
std::vector<double> voronoi_edges = construct_voronoi_edges(delaunay);
}
};
std::vector<double> Map::construct_voronoi_edges(const delaunator::Delaunator& delaunay) {
/*
Calculates the circumcenters of each of delaunay the triangles at the beginning and end of a half edge and
returns all the lines between them in a vector = [x1,y1,x1,y2.....] and so on
*/
std::vector<double> line_edges;
for (size_t e = 0; e < delaunay.triangles.size(); e += 3) {
if (e < delaunay.halfedges[e/3]) {
const auto p = delaunator::circumcenter(delaunay.coords[2 * delaunay.triangles[e]], delaunay.coords[2 * delaunay.triangles[e] + 1],
delaunay.coords[2 * delaunay.triangles[e + 1]], delaunay.coords[2 * delaunay.triangles[e + 1] + 1], 2 * delaunay.coords[delaunay.triangles[e + 2]], delaunay.coords[2 * delaunay.triangles[e + 2] + 1]);
const auto q = delaunator::circumcenter(delaunay.coords[2 * delaunay.triangles[delaunay.halfedges[e]]], delaunay.coords[2 * delaunay.triangles[delaunay.halfedges[e/3]] + 1], delaunay.coords[2 * delaunay.triangles[delaunay.halfedges[e/3 + 1]]], delaunay.coords[2 * delaunay.triangles[delaunay.halfedges[e/3 + 1]] + 1], delaunay.coords[2 * delaunay.triangles[delaunay.halfedges[e/3 + 2]]], delaunay.coords[2 * delaunay.triangles[delaunay.halfedges[e/3 + 2]] + 1]);
line_edges.push_back(p.first);
line_edges.push_back(p.second);
line_edges.push_back(q.first);
line_edges.push_back(q.second);
}
}
unsigned int index = 0;
int count = 0;
std::cout << "Beep Boop, Printing....." << '\n';
for (auto i : line_edges)
{
index++;
if ((index % 4) != 0) {
std::cout << i << ',';
}
else
{
std::cout << i << ',' << '\n';
};
count++;
}
std::cout << count/4 << " voronoi edges created from:" << '\n';
std::cout << delaunay.triangles.size() << " triangles" << '\n';
return line_edges;
}
std::vector<std::pair<double,double>> Map::calculateCircumcenters(delaunator::Delaunator d) {
std::vector<std::pair<double, double>> c_centers;
for (std::size_t i = 0; i < d.triangles.size(); i += 3) {
c_centers.push_back(delaunator::circumcenter(
d.coords[2 * d.triangles[i]], //ax
d.coords[2 * d.triangles[i] + 1], //ay
d.coords[2 * d.triangles[i + 1]], //bx
d.coords[2 * d.triangles[i + 1] + 1],//by
d.coords[2 * d.triangles[i + 2]],//cx
d.coords[2 * d.triangles[i + 2] + 1]));//cy;
}
return c_centers;
}
std::vector<std::array<float, 2>> Map::generate_points()
{
// Input parameters.
constexpr float max_val = 0.900f;
constexpr float kRadius = 0.088f; //0.008 seems good
constexpr auto kXMin = std::array<float, 2>{ {-max_val, -max_val}};
constexpr auto kXMax = std::array<float, 2>{ {max_val, max_val}};
const std::uint32_t max_sample_attempts = 30;
std::vector<std::array<float, 2>> point_data = thinks::PoissonDiskSampling(kRadius, kXMin, kXMax, max_sample_attempts);
// Samples returned as std::vector<std::array<float, 2>>.
// Default seed and max sample attempts.
return point_data;
}
std::vector<double> Map::flatten_vertices(std::vector<std::array<float, 2>> points) {
std::vector<double> coords;
for (auto point : points) {
for (auto i : point) {
coords.push_back((double)i);
//std::cout << i << '\n';
}
}
return coords;
}
// formatting reminder
//for (std::size_t i = 0; i < d.triangles.size(); i += 3) {
// printf(
// "Triangle points: [[%f, %f], [%f, %f], [%f, %f]]\n",
// d.coords[2 * d.triangles[i]], //tx0
// d.coords[2 * d.triangles[i] + 1], //ty0
// d.coords[2 * d.triangles[i + 1]], //tx1
// d.coords[2 * d.triangles[i + 1] + 1],//ty1
// d.coords[2 * d.triangles[i + 2]], //tx2
// d.coords[2 * d.triangles[i + 2] + 1] //ty2
// );