GCC Code Coverage Report


Directory: ./
File: voronoi/CVT.cpp
Date: 2026-11-30 05:48:47
Exec Total Coverage
Lines: 109 177 61.6%
Functions: 11 16 68.8%
Branches: 76 244 31.1%

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/voronoi/CVT.h>
41 #include <geogram/voronoi/RVD.h>
42 #include <geogram/mesh/mesh_repair.h>
43 #include <geogram/mesh/mesh_geometry.h>
44 #include <geogram/numerics/optimizer.h>
45 #include <geogram/basic/progress.h>
46 #include <geogram/basic/argused.h>
47 #include <geogram/bibliography/bibliography.h>
48
49 /****************************************************************************/
50
51 namespace GEO {
52
53 CentroidalVoronoiTesselation*
54 CentroidalVoronoiTesselation::instance_ = nullptr;
55
56 12 CentroidalVoronoiTesselation::CentroidalVoronoiTesselation(
57 Mesh* mesh, coord_index_t dim, const std::string& delaunay
58
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
12 ) {
59 12 use_RVC_centroids_ = true;
60 12 show_iterations_ = false;
61 12 constrained_cvt_ = false;
62 12 dimension_ =
63
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
19 (dim != 0) ? dim : coord_index_t(mesh->vertices.dimension());
64
2/10
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
24 geo_assert(index_t(dimension_) <= mesh->vertices.dimension());
65 12 is_projection_ = true;
66
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 delaunay_ = Delaunay::create(dimension_, delaunay);
67
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 RVD_ = RestrictedVoronoiDiagram::create(delaunay_, mesh);
68 12 mesh_ = mesh;
69
1/10
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
12 geo_assert(instance_ == nullptr);
70 12 instance_ = this;
71 12 progress_ = nullptr;
72
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 geo_cite("Lloyd82leastsquares");
73
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 geo_cite("Du:1999:CVT:340312.340319");
74
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 geo_cite("DBLP:journals/tog/LiuWLSYLY09");
75 12 }
76
77 ✗ CentroidalVoronoiTesselation::CentroidalVoronoiTesselation(
78 Mesh* mesh, const vector<vec3>& R3_embedding, coord_index_t dim,
79 const std::string& delaunay
80 ✗ ) {
81 ✗ use_RVC_centroids_ = true;
82 ✗ show_iterations_ = false;
83 ✗ constrained_cvt_ = false;
84 ✗ dimension_ =
85 ✗ (dim != 0) ? dim : coord_index_t(mesh->vertices.dimension());
86 ✗ geo_assert(index_t(dimension_) <= mesh->vertices.dimension());
87 ✗ is_projection_ = (R3_embedding.size() == 0);
88 ✗ delaunay_ = Delaunay::create(dimension_, delaunay);
89 ✗ if(is_projection_) {
90 ✗ RVD_ = RestrictedVoronoiDiagram::create(delaunay_, mesh);
91 } else {
92 RVD_ = RestrictedVoronoiDiagram::create(
93 delaunay_, mesh, R3_embedding
94 ✗ );
95 }
96 ✗ mesh_ = mesh;
97 ✗ geo_assert(instance_ == nullptr);
98 ✗ instance_ = this;
99 ✗ progress_ = nullptr;
100 ✗ geo_cite("Lloyd82leastsquares");
101 ✗ geo_cite("Du:1999:CVT:340312.340319");
102 ✗ geo_cite("DBLP:journals/tog/LiuWLSYLY09");
103 ✗ }
104
105 24 CentroidalVoronoiTesselation::~CentroidalVoronoiTesselation() {
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
24 instance_ = nullptr;
107 24 }
108
109 12 bool CentroidalVoronoiTesselation::compute_initial_sampling(
110 index_t nb_samples, bool verbose
111 ) {
112 12 points_.resize(dimension_ * nb_samples);
113 12 return RVD_->compute_initial_sampling(
114 points_.data(), nb_samples, verbose
115 12 );
116 }
117
118 ✗ void CentroidalVoronoiTesselation::set_points(
119 index_t nb_points, const double* points
120 ) {
121 ✗ points_.resize(dimension_ * nb_points);
122 ✗ for(index_t i = 0; i < points_.size(); i++) {
123 ✗ points_[i] = points[i];
124 }
125 ✗ }
126
127 ✗ void CentroidalVoronoiTesselation::resize_points(
128 index_t nb_points
129 ) {
130 ✗ points_.resize(dimension_ * nb_points);
131 ✗ }
132
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 void CentroidalVoronoiTesselation::Lloyd_iterations(
134 index_t nb_iter, bool safe_mode
135 ) {
136
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 index_t nb_points = index_t(points_.size() / dimension_);
137
138 vector<double> mg;
139 vector<double> m;
140
141
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 RVD_->set_check_SR(safe_mode);
142
143
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 if(progress_ != nullptr) {
144
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 progress_->reset(nb_iter);
145 }
146
147 12 cur_iter_ = 0;
148 12 nb_iter_ = nb_iter;
149
150
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 12 times.
72 for(index_t i = 0; i < nb_iter; i++) {
151
1/2
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
60 mg.assign(nb_points * dimension_, 0.0);
152
1/4
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
60 m.assign(nb_points, 0.0);
153
2/4
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
60 delaunay_->set_vertices(nb_points, points_.data());
154
1/2
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
60 RVD_->compute_centroids(mg.data(), m.data());
155 index_t cur = 0;
156
2/2
✓ Branch 0 taken 231000 times.
✓ Branch 1 taken 60 times.
231060 for(index_t j = 0; j < nb_points; j++) {
157
2/2
✓ Branch 0 taken 230877 times.
✓ Branch 1 taken 123 times.
231000 if(m[j] > 1e-30 && !point_is_locked(j)) {
158 230877 double s = 1.0 / m[j];
159
2/2
✓ Branch 0 taken 1080762 times.
✓ Branch 1 taken 230877 times.
1311639 for(index_t coord = 0; coord < dimension_; coord++) {
160 1080762 points_[cur + coord] = s * mg[cur + coord];
161 }
162 }
163 231000 cur += dimension_;
164 }
165
1/2
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
60 newiteration();
166 }
167
168
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 progress_ = nullptr;
169 12 }
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 void CentroidalVoronoiTesselation::compute_surface(
172 Mesh* mesh, bool multinerve
173 ) {
174 7 index_t nb_points = index_t(points_.size() / dimension_);
175 7 delaunay_->set_vertices(nb_points, points_.data());
176
177 vector<index_t> triangles;
178 vector<double> vertices;
179 vector<double> vertices_R3;
180
181 RestrictedVoronoiDiagram::RDTMode mode =
182 RestrictedVoronoiDiagram::RDTMode(0);
183
184
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if(multinerve) {
185 mode = RestrictedVoronoiDiagram::RDTMode(
186 mode | RestrictedVoronoiDiagram::RDT_MULTINERVE
187 );
188 }
189
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if(use_RVC_centroids_) {
190 mode = RestrictedVoronoiDiagram::RDTMode(
191 mode | RestrictedVoronoiDiagram::RDT_RVC_CENTROIDS
192 | RestrictedVoronoiDiagram::RDT_PREFER_SEEDS
193 );
194 }
195
196
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 RVD_->set_check_SR(true);
197
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 RVD_->compute_RDT(
198
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 triangles, vertices, mode, point_is_locked_
199 );
200
201 // TODO: projection is not good when the embedding is not one-to-one,
202 // for instance with Gauss map. We should use barycentric coordinates
203 // instead.
204 7 index_t nb_vertices = index_t(vertices.size() / dimension_);
205
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 vertices_R3.resize(nb_vertices * 3);
206
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if(is_projection_) {
207 double* cur = vertices.data();
208
2/2
✓ Branch 0 taken 35000 times.
✓ Branch 1 taken 7 times.
35007 for(index_t v = 0; v < nb_vertices; v++) {
209 35000 vertices_R3[3 * v] = cur[0];
210 35000 vertices_R3[3 * v + 1] = cur[1];
211 35000 vertices_R3[3 * v + 2] = cur[2];
212 35000 cur += dimension_;
213 }
214 } else {
215 ✗ RVD_->project_points_on_surface(
216 nb_vertices, vertices.data(), vertices_R3
217 );
218 }
219
220
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 mesh->clear();
221
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 mesh->facets.assign_triangle_mesh(3, vertices_R3, triangles, true);
222
223
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if(multinerve) {
224
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 mesh_postprocess_RDT(*mesh);
225 } else {
226 // The 'repair' phase is needed to reconstruct the
227 // facet-facet links, that are not initialized by
228 // Mesh::assign_triangle_mesh()
229 ✗ double radius = bbox_diagonal(*mesh);
230 ✗ mesh_repair(*mesh, MESH_REPAIR_DEFAULT, 1e-6 * radius);
231 // TODO: check: is it really good to have some tolerance here,
232 // not sure, may cause some Moebius configs sometimes.
233 }
234 7 }
235
236 ✗ void CentroidalVoronoiTesselation::compute_volume(
237 Mesh* mesh
238 ) {
239 ✗ geo_assert(volumetric());
240 ✗ index_t nb_points = index_t(points_.size() / dimension_);
241 ✗ delaunay_->set_vertices(nb_points, points_.data());
242
243 vector<index_t> tets;
244 vector<double> vertices;
245 vector<double> vertices_R3;
246
247 ✗ RVD_->set_check_SR(true);
248 ✗ RVD_->compute_RDT(
249 tets, vertices
250 );
251
252 // TODO: projection is not good when the embedding is not one-to-one,
253 // for instance with Gauss map. We should use barycentric coordinates
254 // instead.
255 ✗ index_t nb_vertices = index_t(vertices.size() / dimension_);
256 ✗ vertices_R3.resize(nb_vertices * 3);
257 ✗ if(is_projection_) {
258 double* cur = vertices.data();
259 ✗ for(index_t v = 0; v < nb_vertices; v++) {
260 ✗ vertices_R3[3 * v] = cur[0];
261 ✗ vertices_R3[3 * v + 1] = cur[1];
262 ✗ vertices_R3[3 * v + 2] = cur[2];
263 ✗ cur += dimension_;
264 }
265 } else {
266 // TODO: map vertices from embedding space to 3D space
267 // when we are not in projection mode
268 ✗ geo_assert_not_reached;
269 }
270 ✗ mesh->clear();
271 ✗ mesh->cells.assign_tet_mesh(3, vertices_R3, tets, true);
272 ✗ }
273
274 12 void CentroidalVoronoiTesselation::Newton_iterations(
275 index_t nb_iter, index_t m
276 ) {
277
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 Optimizer_var optimizer = Optimizer::create("HLBFGS");
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if(optimizer.is_null()) {
279 ✗ Logger::warn("CVT") << "This geogram was not compiled with HLBFGS"
280 << " (falling back to Lloyd iterations)"
281 << std::endl;
282 ✗ Lloyd_iterations(nb_iter);
283 return;
284 }
285
286 index_t n = index_t(points_.size());
287
288
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 RVD_->set_check_SR(true);
289
290
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 if(progress_ != nullptr) {
291
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 progress_->reset(nb_iter);
292 }
293
294 12 cur_iter_ = 0;
295 12 nb_iter_ = nb_iter;
296
297
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_epsg(0.0);
298
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_epsf(0.0);
299
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_epsx(0.0);
300
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_newiteration_callback(newiteration_CB);
301
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_funcgrad_callback(funcgrad_CB);
302
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_N(n);
303
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_M(m);
304
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 optimizer->set_max_iter(nb_iter);
305
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 optimizer->optimize(points_.data());
306
307 simplex_func_.reset();
308
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 progress_ = nullptr;
309 }
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 void CentroidalVoronoiTesselation::constrain_points(double* g) const {
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 if(point_is_locked_.size() != 0) {
313 double* cur_g = g;
314 ✗ for(index_t i = 0; i < nb_points(); ++i) {
315 ✗ if(point_is_locked_[i]) {
316 ✗ for(index_t c = 0; c < dimension_; c++) {
317 ✗ cur_g[c] = 0.0;
318 }
319 }
320 ✗ cur_g += dimension_;
321 }
322 }
323 383 }
324
325 383 void CentroidalVoronoiTesselation::funcgrad(
326 index_t n, double* x, double& f, double* g
327 ) {
328 383 index_t nb_points = n / dimension_;
329 383 delaunay_->set_vertices(nb_points, x);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 Memory::clear(g, n * sizeof(double));
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 f = 0.0;
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 if(!simplex_func_.is_null()) {
333 ✗ RVD_->compute_integration_simplex_func_grad(
334 f,g,simplex_func_
335 );
336 } else {
337 383 RVD_->compute_CVT_func_grad(f, g);
338 }
339 383 constrain_points(g);
340 383 }
341
342 412 void CentroidalVoronoiTesselation::newiteration() {
343
2/2
✓ Branch 0 taken 396 times.
✓ Branch 1 taken 16 times.
412 if(progress_ != nullptr) {
344 396 progress_->next();
345 }
346 412 cur_iter_++;
347 412 }
348
349 383 void CentroidalVoronoiTesselation::funcgrad_CB(
350 index_t n, double* x, double& f, double* g
351 ) {
352 383 instance_->funcgrad(n, x, f, g);
353 383 }
354
355 352 void CentroidalVoronoiTesselation::newiteration_CB(
356 index_t n, const double* x, double f, const double* g, double gnorm
357 ) {
358 geo_argused(n);
359 geo_argused(x);
360 geo_argused(f);
361 geo_argused(g);
362 geo_argused(gnorm);
363 352 instance_->newiteration();
364 352 }
365
366 ✗ void CentroidalVoronoiTesselation::compute_R3_embedding() {
367 ✗ index_t nb_points = index_t(points_.size() / dimension_);
368 ✗ points_R3_.resize(nb_points);
369 ✗ if(is_projection_ && !constrained_cvt_) {
370 double* cur = points_.data();
371 ✗ for(index_t p = 0; p < nb_points; p++) {
372 ✗ points_R3_[p] = vec3(cur[0], cur[1], cur[2]);
373 ✗ cur += dimension_;
374 }
375 } else {
376 ✗ RVD_->project_points_on_surface(
377 ✗ nb_points, points_.data(), points_R3_, constrained_cvt_
378 );
379 }
380 ✗ }
381 }
382