GCC Code Coverage Report


Directory: ./
File: voronoi/CVT.cpp
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 95 208 45.7%
Functions: 7 16 43.8%
Branches: 73 268 27.2%

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 12 ) {
59 12 use_RVC_centroids_ = true;
60 12 show_iterations_ = false;
61 12 constrained_cvt_ = false;
62 12 dimension_ =
63
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
12 (dim != 0) ? dim : coord_index_t(mesh->vertices.dimension());
64
2/8
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
12 geo_assert(index_t(dimension_) <= mesh->vertices.dimension());
65 12 is_projection_ = true;
66
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 delaunay_ = Delaunay::create(dimension_, delaunay);
67
2/4
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
12 RVD_ = RestrictedVoronoiDiagram::create(delaunay_, mesh);
68 12 mesh_ = mesh;
69
1/6
✗ 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.
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 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 17 void CentroidalVoronoiTesselation::Lloyd_iterations(
134 index_t nb_iter, bool safe_mode
135 ) {
136 17 index_t nb_points = index_t(points_.size() / dimension_);
137
138 17 vector<double> mg;
139 17 vector<double> m;
140
141
2/4
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
17 RVD_->set_check_SR(safe_mode);
142
143
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 if(progress_ != nullptr) {
144
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 progress_->reset(nb_iter);
145 }
146
147 17 cur_iter_ = 0;
148 17 nb_iter_ = nb_iter;
149
150
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 17 times.
472 for(index_t i = 0; i < nb_iter; i++) {
151
1/2
✓ Branch 1 taken 455 times.
✗ Branch 2 not taken.
455 mg.assign(nb_points * dimension_, 0.0);
152
1/2
✓ Branch 1 taken 455 times.
✗ Branch 2 not taken.
455 m.assign(nb_points, 0.0);
153
2/4
✓ Branch 1 taken 455 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 455 times.
✗ Branch 6 not taken.
455 delaunay_->set_vertices(nb_points, points_.data());
154
2/4
✓ Branch 1 taken 455 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 455 times.
✗ Branch 7 not taken.
455 RVD_->compute_centroids(mg.data(), m.data());
155 455 index_t cur = 0;
156
2/2
✓ Branch 0 taken 1598000 times.
✓ Branch 1 taken 455 times.
1598455 for(index_t j = 0; j < nb_points; j++) {
157
7/10
✓ Branch 1 taken 1598000 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1597920 times.
✓ Branch 4 taken 80 times.
✓ Branch 6 taken 1597920 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1597920 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1597920 times.
✓ Branch 11 taken 80 times.
1598000 if(m[j] > 1e-30 && !point_is_locked(j)) {
158
1/2
✓ Branch 1 taken 1597920 times.
✗ Branch 2 not taken.
1597920 double s = 1.0 / m[j];
159
2/2
✓ Branch 0 taken 7901520 times.
✓ Branch 1 taken 1597920 times.
9499440 for(index_t coord = 0; coord < dimension_; coord++) {
160
2/4
✓ Branch 1 taken 7901520 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7901520 times.
✗ Branch 5 not taken.
7901520 points_[cur + coord] = s * mg[cur + coord];
161 }
162 }
163 1598000 cur += dimension_;
164 }
165
1/2
✓ Branch 1 taken 455 times.
✗ Branch 2 not taken.
455 newiteration();
166 }
167
168 17 progress_ = nullptr;
169 17 }
170
171 7 void CentroidalVoronoiTesselation::compute_surface(
172 Mesh* mesh, bool multinerve
173 ) {
174 7 index_t nb_points = index_t(points_.size() / dimension_);
175
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
7 delaunay_->set_vertices(nb_points, points_.data());
176
177 7 vector<index_t> triangles;
178 7 vector<double> vertices;
179 7 vector<double> vertices_R3;
180
181 7 RestrictedVoronoiDiagram::RDTMode mode =
182 RestrictedVoronoiDiagram::RDTMode(0);
183
184
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if(multinerve) {
185 7 mode = RestrictedVoronoiDiagram::RDTMode(
186 7 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 5 mode = RestrictedVoronoiDiagram::RDTMode(
191 5 mode | RestrictedVoronoiDiagram::RDT_RVC_CENTROIDS
192 5 | 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 7 double* cur = vertices.data();
208
2/2
✓ Branch 0 taken 34999 times.
✓ Branch 1 taken 7 times.
35006 for(index_t v = 0; v < nb_vertices; v++) {
209
1/2
✓ Branch 1 taken 34999 times.
✗ Branch 2 not taken.
34999 vertices_R3[3 * v] = cur[0];
210
1/2
✓ Branch 1 taken 34999 times.
✗ Branch 2 not taken.
34999 vertices_R3[3 * v + 1] = cur[1];
211
1/2
✓ Branch 1 taken 34999 times.
✗ Branch 2 not taken.
34999 vertices_R3[3 * v + 2] = cur[2];
212 34999 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 5 void CentroidalVoronoiTesselation::Newton_iterations(
275 index_t nb_iter, index_t m
276 ) {
277
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 Optimizer_var optimizer = Optimizer::create("HLBFGS");
278
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if(optimizer.is_null()) {
279
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
10 Logger::warn("CVT") << "This geogram was not compiled with HLBFGS"
280
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 << " (falling back to Lloyd iterations)"
281
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 << std::endl;
282
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 Lloyd_iterations(nb_iter);
283 5 return;
284 }
285
286 ✗ index_t n = index_t(points_.size());
287
288 ✗ RVD_->set_check_SR(true);
289
290 ✗ if(progress_ != nullptr) {
291 ✗ progress_->reset(nb_iter);
292 }
293
294 ✗ cur_iter_ = 0;
295 ✗ nb_iter_ = nb_iter;
296
297 ✗ optimizer->set_epsg(0.0);
298 ✗ optimizer->set_epsf(0.0);
299 ✗ optimizer->set_epsx(0.0);
300 ✗ optimizer->set_newiteration_callback(newiteration_CB);
301 ✗ optimizer->set_funcgrad_callback(funcgrad_CB);
302 ✗ optimizer->set_N(n);
303 ✗ optimizer->set_M(m);
304 ✗ optimizer->set_max_iter(nb_iter);
305 ✗ optimizer->optimize(points_.data());
306
307 ✗ simplex_func_.reset();
308 ✗ progress_ = nullptr;
309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 }
310
311 ✗ void CentroidalVoronoiTesselation::constrain_points(double* g) const {
312 ✗ 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 ✗ }
324
325 ✗ void CentroidalVoronoiTesselation::funcgrad(
326 index_t n, double* x, double& f, double* g
327 ) {
328 ✗ index_t nb_points = n / dimension_;
329 ✗ delaunay_->set_vertices(nb_points, x);
330 ✗ Memory::clear(g, n * sizeof(double));
331 ✗ f = 0.0;
332 ✗ if(!simplex_func_.is_null()) {
333 ✗ RVD_->compute_integration_simplex_func_grad(
334 f,g,simplex_func_
335 );
336 } else {
337 ✗ RVD_->compute_CVT_func_grad(f, g);
338 }
339 ✗ constrain_points(g);
340 ✗ }
341
342 455 void CentroidalVoronoiTesselation::newiteration() {
343
2/2
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 15 times.
455 if(progress_ != nullptr) {
344 440 progress_->next();
345 }
346 455 cur_iter_++;
347 455 }
348
349 ✗ void CentroidalVoronoiTesselation::funcgrad_CB(
350 index_t n, double* x, double& f, double* g
351 ) {
352 ✗ instance_->funcgrad(n, x, f, g);
353 ✗ }
354
355 ✗ 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 ✗ instance_->newiteration();
364 ✗ }
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