Repository navigation
Expand file tree
/
Copy pathcdt-geogram.cpp
More file actions
210 lines (186 loc) · 7.19 KB
/
Copy pathcdt-geogram.cpp
File metadata and controls
210 lines (186 loc) · 7.19 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// cdt-geogram: Geogram ExactCDT2d or floating-point CDT2d.
#include "../common/corpus.hpp"
#include "../common/csv.hpp"
#include "../common/meta.hpp"
#include "../common/pslg.hpp"
#include "../common/timer.hpp"
#include <geogram/basic/common.h>
#include <geogram/basic/logger.h>
#include <geogram/delaunay/CDT_2d.h>
#include <geogram/numerics/exact_geometry.h>
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <exception>
#include <limits>
#include <string>
#include <vector>
namespace {
#ifndef BENCH_GEOGRAM_VERSION
#define BENCH_GEOGRAM_VERSION "unknown"
#endif
#ifndef GEOGRAM_EXACT_INTERSECTIONS
#define GEOGRAM_EXACT_INTERSECTIONS 1
#endif
#ifndef GEOGRAM_LIB_SLUG
#define GEOGRAM_LIB_SLUG "geogram"
#endif
constexpr const char *kOperation = "cdt";
constexpr const char *kLibrary = GEOGRAM_LIB_SLUG;
constexpr unsigned kCapabilities =
bench::cap_constraints | bench::cap_crossings;
constexpr GEO::index_t kEnclosingVertexCount = 4;
volatile std::size_t g_sink = 0;
#if GEOGRAM_EXACT_INTERSECTIONS
using triangulation_type = GEO::ExactCDT2d;
#else
using triangulation_type = GEO::CDT2d;
#endif
struct bounds_2d {
double min_x = std::numeric_limits<double>::max();
double min_y = std::numeric_limits<double>::max();
double max_x = std::numeric_limits<double>::lowest();
double max_y = std::numeric_limits<double>::lowest();
};
bounds_2d enclosing_bounds(const std::vector<std::array<double, 2>> &points) {
bounds_2d bounds;
for (const auto &point : points) {
bounds.min_x = std::min(bounds.min_x, point[0]);
bounds.min_y = std::min(bounds.min_y, point[1]);
bounds.max_x = std::max(bounds.max_x, point[0]);
bounds.max_y = std::max(bounds.max_y, point[1]);
}
const double span =
std::max({bounds.max_x - bounds.min_x, bounds.max_y - bounds.min_y, 1.0});
const double padding = span * 1048576.0;
bounds.min_x -= padding;
bounds.min_y -= padding;
bounds.max_x += padding;
bounds.max_y += padding;
return bounds;
}
void insert_points(GEO::CDT2d &triangulation,
const std::vector<std::array<double, 2>> &points,
std::vector<GEO::index_t> &indices) {
std::vector<double> coordinates;
coordinates.reserve(points.size() * 2);
for (const auto &point : points) {
coordinates.push_back(point[0]);
coordinates.push_back(point[1]);
}
triangulation.insert(GEO::index_t(points.size()), coordinates.data(),
indices.data());
}
void insert_points(GEO::ExactCDT2d &triangulation,
const std::vector<std::array<double, 2>> &points,
std::vector<GEO::index_t> &indices) {
for (std::size_t i = 0; i < points.size(); ++i) {
const auto &point = points[i];
indices[i] = triangulation.insert(
GEO::ExactCDT2d::ExactPoint(GEO::vec2(point[0], point[1])));
}
}
std::array<double, 2> output_point(const GEO::CDT2d &triangulation,
GEO::index_t vertex) {
const auto point = triangulation.point(vertex);
return {point.x, point.y};
}
std::array<double, 2> output_point(const GEO::ExactCDT2d &triangulation,
GEO::index_t vertex) {
const auto point = GEO::PCK::approximate(triangulation.point(vertex));
return {point.x, point.y};
}
void build_triangulation(const bench::pslg_data &input,
std::vector<std::array<double, 2>> &out_points,
std::vector<std::array<std::int32_t, 3>> &out_faces) {
const bounds_2d bounds = enclosing_bounds(input.points);
triangulation_type triangulation;
triangulation.set_delaunay(true);
triangulation.create_enclosing_rectangle(bounds.min_x, bounds.min_y,
bounds.max_x, bounds.max_y);
std::vector<GEO::index_t> indices(input.points.size());
insert_points(triangulation, input.points, indices);
for (const auto &segment : input.segments)
triangulation.insert_constraint(indices[std::size_t(segment[0])],
indices[std::size_t(segment[1])]);
std::vector<std::int32_t> output_indices(triangulation.nv(), -1);
out_points.clear();
out_points.reserve(std::size_t(triangulation.nv() - kEnclosingVertexCount));
for (GEO::index_t vertex = kEnclosingVertexCount; vertex < triangulation.nv();
++vertex) {
output_indices[vertex] = std::int32_t(out_points.size());
out_points.push_back(output_point(triangulation, vertex));
}
out_faces.clear();
out_faces.reserve(std::size_t(triangulation.nT()));
for (GEO::index_t triangle = 0; triangle < triangulation.nT(); ++triangle) {
const GEO::index_t a = triangulation.Tv(triangle, 0);
const GEO::index_t b = triangulation.Tv(triangle, 1);
const GEO::index_t c = triangulation.Tv(triangle, 2);
if (a < kEnclosingVertexCount || b < kEnclosingVertexCount ||
c < kEnclosingVertexCount)
continue;
out_faces.push_back(
{output_indices[a], output_indices[b], output_indices[c]});
}
}
} // namespace
int main(int argc, char **argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s <operand_list> [out_root]\n", argv[0]);
return 2;
}
const std::string list_path = argv[1];
const std::string out_root = argc > 2 ? argv[2] : "results/raw";
GEO::initialize(GEO::GEOGRAM_INSTALL_NONE);
GEO::Logger::instance()->set_quiet(true);
const auto operands = bench::load_operand_list(list_path);
bench::csv_writer csv(
bench::results_path(out_root, kOperation, kLibrary, "csv"),
{"out_points", "out_faces", "crossings_resolved", "recovery_checked"});
for (const auto &operand : operands) {
const auto input = bench::load_pslg(operand.path);
const std::string parameter = std::to_string(input.expected_crossings);
if (input.n_points() < 3) {
csv.row(operand.case_id, input.n_points(), parameter, 0.0, 0,
"error:too_few_points", {"0", "0", "-1", "0"});
continue;
}
if (input.has_crossings() && !(kCapabilities & bench::cap_crossings)) {
csv.row(operand.case_id, input.n_points(), parameter, 0.0, 0,
bench::note::rejected_no_crossings, {"0", "0", "-1", "0"});
continue;
}
double time_ms = 0.0;
bench::cdt_check check;
std::vector<std::array<double, 2>> out_points;
std::vector<std::array<std::int32_t, 3>> out_faces;
try {
time_ms = bench::time_median_ms(
[&] {
build_triangulation(input, out_points, out_faces);
g_sink += out_faces.size();
},
5);
check = bench::validate_cdt(input, out_points, out_faces);
} catch (const std::exception &error) {
check.valid = 0;
check.note = std::string("error:") + error.what();
}
csv.row(operand.case_id, input.n_points(), parameter, time_ms, check.valid,
check.note,
{std::to_string(check.out_points), std::to_string(check.out_faces),
std::to_string(check.crossings_resolved),
check.recovery_checked ? "1" : "0"});
}
bench::meta_info meta;
meta.operation = kOperation;
meta.library = kLibrary;
meta.library_version = BENCH_GEOGRAM_VERSION;
meta.threads = 1;
bench::write_meta(
bench::results_path(out_root, kOperation, kLibrary, "meta.json"), meta);
(void)g_sink;
return 0;
}