GCC Code Coverage Report


Directory: ./
File: delaunay/delaunay.cpp
Date: 2026-11-30 06:25:12
Exec Total Coverage
Lines: 65 203 32.0%
Functions: 9 25 36.0%
Branches: 54 284 19.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #include <geogram/delaunay/delaunay.h>
41 #include <geogram/delaunay/delaunay_nn.h>
42 #include <geogram/delaunay/delaunay_3d.h>
43 #include <geogram/delaunay/delaunay_2d.h>
44
45 #ifdef GEOGRAM_WITH_PDEL
46 #include <geogram/delaunay/parallel_delaunay_3d.h>
47 #endif
48
49 #ifdef GEOGRAM_WITH_TETGEN
50 #include <geogram/delaunay/delaunay_tetgen.h>
51 #endif
52
53 #ifdef GEOGRAM_WITH_TRIANGLE
54 #include <geogram/delaunay/delaunay_triangle.h>
55 #endif
56
57 #include <geogram/basic/logger.h>
58 #include <geogram/basic/command_line.h>
59 #include <geogram/basic/process.h>
60 #include <geogram/basic/geometry_nd.h>
61 #include <geogram/basic/algorithm.h>
62
63 #include <fstream>
64 #include <sstream>
65
66 namespace {
67
68 using namespace GEO;
69
70 /**
71 * \brief Builds the invalid dimension error message
72 * \param[in] dimension the specified dimension
73 * \param[in] name the name of the Delaunay implementation
74 * \param[in] expected the expected dimension
75 * \return a string that contains the error message
76 */
77 ✗ std::string invalid_dimension_error(
78 coord_index_t dimension,
79 const char* name,
80 const char* expected
81 ) {
82 ✗ std::ostringstream out;
83 ✗ out << "Invalid dimension: dimension " << index_t(dimension)
84 << " is not supported by the " << name
85 ✗ << " algorithm. Supported dimension(s): " << expected;
86 ✗ return out.str();
87 ✗ }
88 }
89
90 namespace GEO {
91
92 ✗ Delaunay::InvalidDimension::InvalidDimension(
93 coord_index_t dimension,
94 const char* name,
95 const char* expected
96 ✗ ) :
97 ✗ std::logic_error(invalid_dimension_error(dimension, name, expected)) {
98 ✗ }
99
100 ✗ const char* Delaunay::InvalidDimension::what() const GEO_NOEXCEPT {
101 ✗ return std::logic_error::what();
102 }
103
104
105 ✗ Delaunay::InvalidInput::InvalidInput(int code) :
106 logic_error("Invalid input for Delaunay"),
107 ✗ error_code(code) {
108 ✗ }
109
110 ✗ Delaunay::InvalidInput::InvalidInput(
111 const InvalidInput& rhs
112 ✗ ) :
113 std::logic_error(rhs),
114 ✗ error_code(rhs.error_code),
115 ✗ invalid_facets(rhs.invalid_facets) {
116 ✗ }
117
118 ✗ Delaunay::InvalidInput::~InvalidInput() GEO_NOEXCEPT {
119 ✗ }
120
121 ✗ const char* Delaunay::InvalidInput::what() const GEO_NOEXCEPT {
122 ✗ return std::logic_error::what();
123 }
124
125 /************************************************************************/
126
127 258 void Delaunay::initialize() {
128
129 #ifdef GEOGRAM_WITH_TETGEN
130
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(DelaunayTetgen, "tetgen");
131 #endif
132
133 #ifdef GEOGRAM_WITH_TRIANGLE
134
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(DelaunayTriangle, "triangle");
135 #endif
136
137
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(Delaunay3d, "BDEL");
138
139 #ifdef GEOGRAM_WITH_PDEL
140
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(ParallelDelaunay3d, "PDEL");
141 #endif
142
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(RegularWeightedDelaunay3d, "BPOW");
143
144
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(Delaunay2d, "BDEL2d");
145
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(RegularWeightedDelaunay2d, "BPOW2d");
146
147 #ifndef GEOGRAM_PSM
148
4/10
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 258 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 258 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 258 times.
✗ Branch 10 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
774 geo_register_Delaunay_creator(Delaunay_NearestNeighbors, "NN");
149 #endif
150 258 }
151
152 80 Delaunay* Delaunay::create(coord_index_t dim, const std::string& name_in) {
153
154
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 std::string name = name_in;
155
3/4
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
✓ Branch 4 taken 4 times.
80 if(name == "default") {
156
2/4
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
152 name = CmdLine::get_arg("algo:delaunay");
157 }
158
159 try {
160
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 Delaunay* d = DelaunayFactory::create_object(name, dim);
161
1/2
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
80 if(d != nullptr) {
162 80 return d;
163 }
164
165 ✗ Logger::warn("Delaunay")
166 ✗ << "Could not create Delaunay triangulation: " << name
167 ✗ << std::endl;
168 }
169 ✗ catch(InvalidDimension& ex) {
170 ✗ Logger::warn("Delaunay") << ex.what() << std::endl;
171 ✗ }
172
173 #ifdef GEOGRAM_PSM
174 Logger::err("Delaunay")
175 << "Could not create Delaunay triangulation"
176 << std::endl;
177 return nullptr;
178 #else
179 ✗ Logger::warn("Delaunay")
180 ✗ << "Falling back to NN mode"
181 ✗ << std::endl;
182
183 ✗ return new Delaunay_NearestNeighbors(dim);
184 #endif
185 80 }
186
187
1/2
✓ Branch 4 taken 82 times.
✗ Branch 5 not taken.
82 Delaunay::Delaunay(coord_index_t dimension) {
188 82 set_dimension(dimension);
189 82 vertices_ = nullptr;
190 82 nb_vertices_ = 0;
191 82 nb_cells_ = 0;
192 82 cell_to_v_ = nullptr;
193 82 cell_to_cell_ = nullptr;
194 82 is_locked_ = false;
195 82 store_neighbors_ = false;
196 82 default_nb_neighbors_ = 30;
197 82 constraints_ = nullptr;
198 82 do_reorder_ = true;
199 82 refine_ = false;
200 82 quality_ = 2.0;
201 82 store_cicl_ = false;
202 82 keep_infinite_ = false;
203 82 nb_finite_cells_ = 0;
204 82 keep_regions_ = false;
205 #ifdef GEO_DETERMINISTIC
206 // Reset the global RNG so the randomized spatial sort used when
207 // reordering vertices is reproducible across operations.
208 Numeric::random_reset();
209 #endif
210 82 }
211
212 164 Delaunay::~Delaunay() {
213 164 }
214
215 519 void Delaunay::set_vertices(index_t nb_vertices, const double* vertices) {
216 519 nb_vertices_ = nb_vertices;
217 519 vertices_ = vertices;
218 519 }
219
220 ✗ void Delaunay::set_BRIO_levels(const vector<index_t>& levels) {
221 ✗ geo_argused(levels);
222 // Default implementation does nothing
223 ✗ }
224
225 17 void Delaunay::set_arrays(
226 index_t nb_cells,
227 const index_t* cell_to_v, const index_t* cell_to_cell
228 ) {
229 17 nb_cells_ = nb_cells;
230 17 cell_to_v_ = cell_to_v;
231 17 cell_to_cell_ = cell_to_cell;
232
233
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if(cell_to_cell != nullptr) {
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if(store_cicl_) {
235 ✗ update_v_to_cell();
236 ✗ update_cicl();
237 }
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if(store_neighbors_) {
239 ✗ update_neighbors();
240 }
241 }
242 17 }
243
244 ✗ bool Delaunay::supports_constraints() const {
245 ✗ return false;
246 }
247
248 ✗ index_t Delaunay::nearest_vertex(const double* p) const {
249 // Unefficient implementation (but at least it works).
250 // Derived classes are supposed to overload.
251 ✗ geo_assert(nb_vertices() > 0);
252 ✗ index_t result = 0;
253 ✗ double d = Geom::distance2(vertex_ptr(0), p, dimension());
254 ✗ for(index_t i = 1; i < nb_vertices(); i++) {
255 ✗ double cur_d = Geom::distance2(vertex_ptr(i), p, dimension());
256 ✗ if(cur_d < d) {
257 ✗ d = cur_d;
258 ✗ result = i;
259 }
260 }
261 ✗ return result;
262 }
263
264 505 void Delaunay::update_neighbors() {
265
2/2
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 438 times.
505 if(nb_vertices() != neighbors_.nb_arrays()) {
266 67 neighbors_.init(
267 nb_vertices(),
268 default_nb_neighbors_
269 );
270
2/2
✓ Branch 1 taken 99013 times.
✓ Branch 2 taken 67 times.
99080 for(index_t i = 0; i < nb_vertices(); i++) {
271 99013 neighbors_.resize_array(i, default_nb_neighbors_, false);
272 }
273 }
274
1/2
✓ Branch 2 taken 505 times.
✗ Branch 3 not taken.
505 parallel_for(
275 0, nb_vertices(),
276 1655923 [this](index_t i) { store_neighbors_CB(i); },
277 1, true
278 );
279 505 }
280
281 ✗ void Delaunay::get_neighbors_internal(
282 index_t v, vector<index_t>& neighbors
283 ) const {
284 // Step 1: traverse the incident cells list, and insert
285 // all neighbors (may be duplicated)
286 ✗ neighbors.resize(0);
287 ✗ index_t vt = v_to_cell_[v];
288 ✗ if(vt != NO_INDEX) { // Happens when there are duplicated vertices.
289 ✗ index_t t = vt;
290 do {
291 ✗ index_t lvit = index(t, v);
292 // In the current cell, test all edges incident
293 // to current vertex 'it'
294 ✗ for(index_t lv = 0; lv < cell_size(); lv++) {
295 ✗ if(lvit != lv) {
296 ✗ index_t neigh = cell_vertex(t, lv);
297 ✗ geo_debug_assert(neigh != NO_INDEX);
298 ✗ neighbors.push_back(neigh);
299 }
300 }
301 ✗ t = next_around_vertex(t, index(t, v));
302 ✗ } while(t != vt);
303 }
304
305 // Step 2: Sort the neighbors and remove all duplicates
306 ✗ sort_unique(neighbors);
307 ✗ }
308
309 ✗ void Delaunay::store_neighbors_CB(index_t i) {
310 // TODO: this one is not multithread-friendly
311 // since it does dynamic memory allocation
312 // (but not really a problem, since the one
313 // that is used is in Delaunay_ANN).
314 ✗ vector<index_t> neighbors;
315 ✗ get_neighbors_internal(i, neighbors);
316 ✗ neighbors_.set_array(i, neighbors);
317 ✗ }
318
319 ✗ void Delaunay::update_v_to_cell() {
320 ✗ geo_assert(!is_locked_); // Not thread-safe
321 ✗ is_locked_ = true;
322
323 // Note: if keeps_infinite is set, then infinite vertex
324 // tet chaining is at t2v_[nb_vertices].
325
326 ✗ if(keeps_infinite()) {
327 ✗ v_to_cell_.assign(nb_vertices()+1, NO_INDEX);
328 ✗ for(index_t c = 0; c < nb_cells(); c++) {
329 ✗ for(index_t lv = 0; lv < cell_size(); lv++) {
330 ✗ index_t v = cell_vertex(c, lv);
331 ✗ if(v == NO_INDEX) {
332 ✗ v = nb_vertices();
333 }
334 ✗ v_to_cell_[v] = c;
335 }
336 }
337 } else {
338 ✗ v_to_cell_.assign(nb_vertices(), NO_INDEX);
339 ✗ for(index_t c = 0; c < nb_cells(); c++) {
340 ✗ for(index_t lv = 0; lv < cell_size(); lv++) {
341 ✗ v_to_cell_[cell_vertex(c, lv)] = c;
342 }
343 }
344 }
345 ✗ is_locked_ = false;
346 ✗ }
347
348 ✗ void Delaunay::update_cicl() {
349 ✗ geo_assert(!is_locked_); // Not thread-safe
350 ✗ is_locked_ = true;
351 ✗ cicl_.resize(cell_size() * nb_cells());
352
353 ✗ for(index_t v = 0; v < nb_vertices(); ++v) {
354 ✗ index_t t = v_to_cell_[v];
355 ✗ if(t != NO_INDEX) {
356 ✗ index_t lv = index(t, v);
357 ✗ set_next_around_vertex(t, lv, t);
358 }
359 }
360
361 ✗ if(keeps_infinite()) {
362
363 {
364 // Process the infinite vertex at index nb_vertices().
365 ✗ index_t t = v_to_cell_[nb_vertices()];
366 ✗ if(t != NO_INDEX) {
367 ✗ index_t lv = index(t, NO_INDEX);
368 ✗ set_next_around_vertex(t, lv, t);
369 }
370 }
371
372 ✗ for(index_t t = 0; t < nb_cells(); ++t) {
373 ✗ for(index_t lv = 0; lv < cell_size(); ++lv) {
374 ✗ index_t v = cell_vertex(t, lv);
375 ✗ index_t vv = (v == NO_INDEX) ? nb_vertices() : v;
376 ✗ if(v_to_cell_[vv] != t) {
377 ✗ index_t t1 = v_to_cell_[vv];
378 ✗ index_t lv1 = index(t1, v);
379 ✗ index_t t2 = next_around_vertex(t1, lv1);
380 ✗ set_next_around_vertex(t1, lv1, t);
381 ✗ set_next_around_vertex(t, lv, t2);
382 }
383 }
384 }
385
386
387 } else {
388 ✗ for(index_t t = 0; t < nb_cells(); ++t) {
389 ✗ for(index_t lv = 0; lv < cell_size(); ++lv) {
390 ✗ index_t v = cell_vertex(t, lv);
391 ✗ if(v_to_cell_[v] != t) {
392 ✗ index_t t1 = v_to_cell_[v];
393 ✗ index_t lv1 = index(t1, v);
394 ✗ index_t t2 = next_around_vertex(t1, lv1);
395 ✗ set_next_around_vertex(t1, lv1, t);
396 ✗ set_next_around_vertex(t, lv, t2);
397 }
398 }
399 }
400 }
401
402 ✗ is_locked_ = false;
403 ✗ }
404
405 ✗ void Delaunay::save_histogram(std::ostream& out) const {
406 ✗ vector<index_t> histogram;
407 ✗ for(index_t v = 0; v < nb_vertices(); v++) {
408 ✗ index_t N = neighbors_.array_size(v);
409 ✗ if(histogram.size() < N) {
410 ✗ histogram.resize(N + 1);
411 }
412 ✗ histogram[N]++;
413 }
414 ✗ for(index_t i = 0; i < histogram.size(); i++) {
415 ✗ out << i << " " << histogram[i] << std::endl;
416 }
417 ✗ }
418
419 1145 bool Delaunay::cell_is_infinite(index_t c) const {
420
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1145 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1145 geo_debug_assert(c < nb_cells());
421
2/2
✓ Branch 1 taken 4191 times.
✓ Branch 2 taken 779 times.
4970 for(index_t lv=0; lv < cell_size(); ++lv) {
422
2/2
✓ Branch 1 taken 366 times.
✓ Branch 2 taken 3825 times.
4191 if(cell_vertex(c,lv) == NO_INDEX) {
423 366 return true;
424 }
425 }
426 779 return false;
427 }
428
429 ✗ index_t Delaunay::region(index_t t) const {
430 ✗ geo_argused(t);
431 ✗ geo_debug_assert(t < nb_cells());
432 ✗ return NO_INDEX;
433 }
434 }
435