GCC Code Coverage Report


Directory: ./
File: voronoi/CVT.h
Date: 2026-11-30 05:48:47
Exec Total Coverage
Lines: 9 21 42.9%
Functions: 0 2 0.0%
Branches: 11 51 21.6%

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 #ifndef GEOGRAM_VORONOI_CVT
41 #define GEOGRAM_VORONOI_CVT
42
43 #include <geogram/basic/common.h>
44 #include <geogram/voronoi/RVD.h>
45 #include <geogram/voronoi/integration_simplex.h>
46 #include <geogram/mesh/mesh.h>
47 #include <geogram/delaunay/delaunay.h>
48
49
50 /**
51 * \file geogram/voronoi/CVT.h
52 * \brief Main class for computing centroidal Voronoi tesselations.
53 */
54
55 namespace GEO {
56
57 class RestrictedVoronoiDiagram;
58 class ProgressTask;
59
60 /**
61 * \brief CentroidalVoronoiTesselation is the main component
62 * of the remeshing algorithm.
63 *
64 * \details It evenly distributes points over a surface embedded in Rn,
65 * where n can be of arbitrary dimension. The geometrical
66 * computations are done by RestrictedVoronoiDiagram, and
67 * the numerical optimization by Optimizer.
68 */
69 class GEOGRAM_API CentroidalVoronoiTesselation {
70
71 /** \brief This class type */
72 typedef CentroidalVoronoiTesselation thisclass;
73
74 public:
75 /**
76 * \brief Constructs a new CentroidalVoronoiTesselation.
77 * \details This constructor should be used when the
78 * first three coordinates of the mesh are x,y,z.
79 * \param[in] mesh a pointer to the input mesh
80 * \param[in] dimension If set, uses only the dimension first
81 * coordinates in mesh, else dimension is determined
82 * by mesh->dimension().
83 * \param[in] delaunay factory name of the implementation of
84 * Delaunay triangulation. Default uses ANN and radius
85 * of security.
86 */
87 CentroidalVoronoiTesselation(
88 Mesh* mesh,
89 coord_index_t dimension = 0,
90
3/7
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
20 const std::string& delaunay = "default"
91 );
92
93 /**
94 * \brief Constructs a new CentroidalVoronoiTesselation.
95 * \details This constructor should be used when the coordinates of
96 * the mesh are not related with R3.
97 * \param[in] mesh a pointer to the input mesh
98 * \param[in] R3_embedding (dimension = mesh->nb_vertices()):
99 * coordinates of the mesh vertices in R3. Ignored
100 * if size is zero.
101 * \param[in] dimension If set, uses only the dimension first
102 * coordinates in mesh, else dimension is determined
103 * by mesh->dimension().
104 * \param[in] delaunay factory name of the implementation of
105 * Delaunay triangulation. delaunay="default" uses
106 * ANN and radius of security.
107 */
108 CentroidalVoronoiTesselation(
109 Mesh* mesh,
110 const vector<vec3>& R3_embedding, coord_index_t dimension = 0,
111 const std::string& delaunay = "default"
112 );
113
114 /**
115 * \brief Destructor
116 */
117 virtual ~CentroidalVoronoiTesselation();
118
119 /**
120 * \brief Computes a random initial sampling of the surface in nD.
121 *
122 * \details This initial sampling (of low quality/regularity) needs to
123 * be further optimized (using Lloyd_iterations() and
124 * Newton_iterations()).
125 *
126 * \param[in] nb_samples number of points to generate in the sampling
127 * \param[in] verbose if set, display message
128 */
129 bool compute_initial_sampling(index_t nb_samples, bool verbose=false);
130
131 /**
132 * \brief Initializes the points with a user-specified vector.
133 *
134 * \param[in] nb_points number of points in \p points
135 * \param[in] points (size = dimension()*nb_points):
136 * user-defined initialization. It is copied into
137 * an internal vector
138 */
139 void set_points(index_t nb_points, const double* points);
140
141 /**
142 * \brief Changes the number of points.
143 * \param[in] nb_points new number of points
144 * \details Resizes the internal vector used to store the points
145 */
146 void resize_points(index_t nb_points);
147
148 /**
149 * \brief Relaxes the points with Lloyd's algorithm.
150 * \details It is in general less efficient than Newton, but more
151 * resistant to heterogeneous point distribution. Therefore a
152 * small number of Lloyd iterations may be used right after
153 * a call to compute_initial_sampling() to regularize
154 * the point set before calling Newton_iterations().
155 * \param[in] nb_iter number of iterations
156 * \param[in] safe_mode if set, enables the "radius of security"
157 * check during Lloyd relaxation. This avoids potential errors of the
158 * algorithm when a point has many neighbors. When unset, the potential
159 * errors are typically minor (and thus still produce a suitable input
160 * for Newton_iterations()), but they depend on the number of threads
161 * used (and thus may pose issues for reproducibility across different
162 * machines); they may also be more significant in extreme cases.
163 */
164 virtual void Lloyd_iterations(index_t nb_iter, bool safe_mode = false);
165
166 /**
167 * \brief Relaxes the points with Newton-Lloyd's algorithm.
168 * \param[in] nb_iter number of iterations
169 * \param[in] m number of evaluations used for Hessian approximation
170 */
171 virtual void Newton_iterations(index_t nb_iter, index_t m = 7);
172
173 /**
174 * \brief Computes the surfacic mesh (using the current points).
175 * \param[out] mesh the computed surface
176 * \param[in] multinerve If set, does topology control (uses
177 * the dual of the connected components of the RVD).
178 */
179 void compute_surface(Mesh* mesh, bool multinerve = true);
180
181 /**
182 * \brief Computes the volumetric mesh (using the current points).
183 * \param[out] mesh the computed volumetric mesh
184 * \pre volumetric()
185 */
186 void compute_volume(Mesh* mesh);
187
188 /**
189 * \brief Specifies whether a progress bar should be used.
190 * \param[in] x If set, shows iterations using a "progress bar".
191 */
192 void set_show_iterations(bool x) {
193 show_iterations_ = x;
194 }
195
196 /**
197 * \brief Specifies whether centroids of Voronoi cells should be used.
198 * \param[in] x If set (default = true), compute_surface() replaces
199 * the vertices with the centroids of the
200 * connected components of the restricted Voronoi cells.
201 */
202 void set_use_RVC_centroids(bool x) {
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 use_RVC_centroids_ = x;
204 }
205
206 /**
207 * \brief Specifies whether constrained mode should be used.
208 * \param[in] x If set (default = false), compute_surface() projects
209 * the vertices onto the input surface.
210 */
211 void set_constrained_cvt(bool x) {
212 constrained_cvt_ = x;
213 }
214
215 /**
216 * Returns the input mesh.
217 */
218 Mesh* mesh() {
219 return mesh_;
220 }
221
222 /**
223 * Returns the Delaunay triangulation.
224 */
225 Delaunay* delaunay() {
226 return delaunay_;
227 }
228
229 /**
230 * Returns the RestrictedVoronoiDiagram.
231 */
232 RestrictedVoronoiDiagram* RVD() {
233 return RVD_;
234 }
235
236 /**
237 * \brief Restricts computation to a part of the input mesh.
238 * \details The part of the input mesh should be specified as
239 * a contiguous range of facet indices.
240 * \param[in] facets_begin first facet in the range
241 * \param[in] facets_end one past last facet in the range
242 */
243 void set_facets_range(index_t facets_begin, index_t facets_end) {
244 RVD_->set_facets_range(facets_begin, facets_end);
245 }
246
247 /**
248 * \brief Makes this CentroidalVoronoiTesselation the current one.
249 * \details The Optimizer uses global variables, therefore there can
250 * be only one CentroidalVoronoiTesselation simultaneously active.
251 * This function can be used to change the currently active
252 * CentroidalVoronoiTesselation.
253 * \note Most users will not need to use this function.
254 * \pre There is no current CentroidalVoronoiTesselation.
255 */
256 void make_current() {
257 geo_assert(instance_ == nullptr);
258 instance_ = this;
259 }
260
261 /**
262 * \brief Resets the current CentroidalVoronoiTesselation to nullptr.
263 * \details The Optimizer uses global variables, therefore there can
264 * be only one CentroidalVoronoiTesselation simultaneously active.
265 * This function can be used to change the currently active
266 * CentroidalVoronoiTesselation.
267 * \note Most users will not need to use this function.
268 * \pre This CentroidalVoronoiTesselation is the current one.
269 */
270 void done_current() {
271 geo_assert(instance_ == this);
272 instance_ = nullptr;
273 }
274
275 public:
276 /**
277 * \brief Callback for the numerical solver.
278 * \details Evaluates the objective function and its gradient.
279 * \param[in] n number of variables
280 * \param[in] x current value of the variables
281 * \param[out] f current value of the objective function
282 * \param[out] g gradient of the objective function
283 */
284 static void funcgrad_CB(
285 index_t n, double* x, double& f, double* g
286 );
287
288 /**
289 * \brief Callback for the numerical solver.
290 * \details Updates the progress bar.
291 * \param[in] n number of variables
292 * \param[in] x current value of the variables
293 * \param[in] f current value of the objective function
294 * \param[in] g gradient of the objective function
295 * \param[in] gnorm norm of the gradient of the objective function
296 */
297 static void newiteration_CB(
298 index_t n, const double* x, double f, const double* g, double gnorm
299 );
300
301 /**
302 * \brief Sets a client for the progress bars.
303 * \param[in] progress the ProgressTask.
304 */
305 void set_progress_logger(ProgressTask* progress) {
306
2/10
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
22 progress_ = progress;
307 4 }
308
309 /**
310 * \brief Gets the dimension of the points.
311 * \details Can be smaller than the dimension of the mesh.
312 */
313 coord_index_t dimension() const {
314 ✗ return dimension_;
315 }
316
317 /**
318 * \brief Gets the number of points to be optimized.
319 */
320 index_t nb_points() const {
321
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1 return index_t(points_.size() / dimension_);
322 }
323
324 /**
325 * \brief Gets the representation of a point in R3.
326 * \param[in] p index of the point
327 * \return a const reference to the 3d version of the point
328 * \pre p < nb_points()
329 */
330 const vec3& R3_embedding(index_t p) const {
331 return RVD_->R3_embedding(p);
332 }
333
334 /**
335 * \brief Returns the representation of a point in embedding space.
336 * \param[in] p index of the point
337 * \return a pointer to the coordinates of the point
338 * \pre p < nb_points()
339 */
340 double* embedding(index_t p) {
341 geo_debug_assert(p < nb_points());
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 return &(points_[0]) + dimension_ * p;
343 }
344
345 /**
346 * \brief Tests whether volumetric mode is used.
347 */
348 bool volumetric() const {
349 ✗ return RVD_->volumetric();
350 }
351
352 /**
353 * \brief Sets volumetric mode.
354 * \param[in] x if true, volumetric mode is used, otherwise
355 * surfacic mode is used.
356 */
357 void set_volumetric(bool x) {
358
2/8
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
4 RVD_->set_volumetric(x);
359 4 }
360
361 /**
362 * \brief Tests whether a point is locked.
363 * \details A locked point is constrained to stay at the same position
364 * during the optimization.
365 * \param[in] i index of the point
366 * \pre i < nb_points()
367 */
368 bool point_is_locked(index_t i) const {
369 geo_debug_assert(
370 point_is_locked_.size() == 0 || i < point_is_locked_.size()
371 );
372
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 230877 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
230877 return point_is_locked_.size() != 0 && point_is_locked_[i];
373 }
374
375 /**
376 * \brief Locks a point.
377 * \details A locked point is constrained to stay at the same position
378 * during the optimization.
379 * \param[in] i index of the point
380 * \pre i < nb_points()
381 */
382 ✗ void lock_point(index_t i) {
383 geo_debug_assert(i < nb_points());
384 ✗ if(point_is_locked_.size() != nb_points()) {
385 ✗ point_is_locked_.resize(nb_points(), false);
386 }
387 ✗ point_is_locked_[i] = true;
388 ✗ }
389
390 /**
391 * \brief Unlocks a point.
392 * \details A locked point is constrained to stay at the same position
393 * during the optimization.
394 * \param[in] i index of the point
395 * \pre i < nb_points()
396 */
397 ✗ void unlock_point(index_t i) {
398 geo_debug_assert(i < nb_points());
399 ✗ if(
400 point_is_locked_.size() != nb_points()
401 ) {
402 ✗ point_is_locked_.resize(nb_points(), false);
403 }
404 ✗ point_is_locked_[i] = false;
405 ✗ }
406
407 /**
408 * \brief Unlocks all the points.
409 * \details A locked point is constrained to stay at the same position
410 * during the optimization.
411 */
412 void unlock_all_points() {
413 point_is_locked_.clear();
414 }
415
416 protected:
417 /**
418 * \brief Callback for the numerical solver.
419 * \details Updates the progress bar.
420 */
421 virtual void newiteration();
422
423 /**
424 * \brief Computes the objective function and its gradient.
425 * \param[in] n number of variables
426 * \param[in] x current value of the variables
427 * \param[out] f current value of the objective function
428 * \param[out] g gradient of the objective function
429 */
430 virtual void funcgrad(index_t n, double* x, double& f, double* g);
431
432 /**
433 * \brief Constrains the locked points.
434 * \details Zeroes the gradient relative to the components
435 * of locked points.
436 * \param[in,out] g gradient of the objective function
437 */
438 void constrain_points(double* g) const;
439
440 /**
441 * \brief Computes the 3d representation of the Nd points.
442 * \details It projects the points onto the Nd surface, then recovers
443 * the 3d coordinates by barycentric interpolation.
444 */
445 void compute_R3_embedding();
446
447 static CentroidalVoronoiTesselation* instance_;
448 bool show_iterations_;
449 coord_index_t dimension_;
450 Delaunay_var delaunay_;
451 RestrictedVoronoiDiagram_var RVD_;
452 Mesh* mesh_;
453
454 vector<double> points_;
455 vector<vec3> points_R3_;
456 vector<bool> point_is_locked_;
457
458 ProgressTask* progress_;
459 index_t cur_iter_;
460 index_t nb_iter_;
461
462 bool is_projection_; /**< the Nd -> 3d transform is a projection */
463 bool constrained_cvt_;
464 bool use_RVC_centroids_;
465
466 IntegrationSimplex_var simplex_func_;
467 /**< \brief Integration simplex used by custom codes, e.g. LpCVT */
468
469 private:
470 /** \brief Forbids construction by copy. */
471 CentroidalVoronoiTesselation(const thisclass& rhs);
472
473 /** \brief Forbids assignment. */
474 thisclass& operator= (const thisclass& rhs);
475 };
476 }
477
478 #endif
479