Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
CVT.h
Go to the documentation of this file.
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>
46 #include <geogram/mesh/mesh.h>
48 
49 
55 namespace GEO {
56 
57  class RestrictedVoronoiDiagram;
58  class ProgressTask;
59 
69  class GEOGRAM_API CentroidalVoronoiTesselation {
70 
73 
74  public:
88  Mesh* mesh,
89  coord_index_t dimension = 0,
90  const std::string& delaunay = "default"
91  );
92 
109  Mesh* mesh,
110  const vector<vec3>& R3_embedding, coord_index_t dimension = 0,
111  const std::string& delaunay = "default"
112  );
113 
118 
129  bool compute_initial_sampling(index_t nb_samples, bool verbose=false);
130 
139  void set_points(index_t nb_points, const double* points);
140 
146  void resize_points(index_t nb_points);
147 
157  virtual void Lloyd_iterations(index_t nb_iter);
158 
164  virtual void Newton_iterations(
165  index_t nb_iter, index_t m = 7
166  );
167 
174  void compute_surface(Mesh* mesh, bool multinerve = true);
175 
181  void compute_volume(Mesh* mesh);
182 
187  void set_show_iterations(bool x) {
188  show_iterations_ = x;
189  }
190 
197  void set_use_RVC_centroids(bool x) {
198  use_RVC_centroids_ = x;
199  }
200 
206  void set_constrained_cvt(bool x) {
207  constrained_cvt_ = x;
208  }
209 
213  Mesh* mesh() {
214  return mesh_;
215  }
216 
221  return delaunay_;
222  }
223 
228  return RVD_;
229  }
230 
238  void set_facets_range(index_t facets_begin, index_t facets_end) {
239  RVD_->set_facets_range(facets_begin, facets_end);
240  }
241 
251  void make_current() {
252  geo_assert(instance_ == nullptr);
253  instance_ = this;
254  }
255 
265  void done_current() {
266  geo_assert(instance_ == this);
267  instance_ = nullptr;
268  }
269 
270  public:
279  static void funcgrad_CB(
280  index_t n, double* x, double& f, double* g
281  );
282 
292  static void newiteration_CB(
293  index_t n, const double* x, double f, const double* g, double gnorm
294  );
295 
301  progress_ = progress;
302  }
303 
309  return dimension_;
310  }
311 
315  index_t nb_points() const {
316  return index_t(points_.size() / dimension_);
317  }
318 
325  const vec3& R3_embedding(index_t p) const {
326  return RVD_->R3_embedding(p);
327  }
328 
335  double* embedding(index_t p) {
336  geo_debug_assert(p < nb_points());
337  return &(points_[0]) + dimension_ * p;
338  }
339 
343  bool volumetric() const {
344  return RVD_->volumetric();
345  }
346 
352  void set_volumetric(bool x) {
353  RVD_->set_volumetric(x);
354  }
355 
363  bool point_is_locked(index_t i) const {
365  point_is_locked_.size() == 0 || i < point_is_locked_.size()
366  );
367  return point_is_locked_.size() != 0 && point_is_locked_[i];
368  }
369 
377  void lock_point(index_t i) {
378  geo_debug_assert(i < nb_points());
379  if(point_is_locked_.size() != nb_points()) {
380  point_is_locked_.resize(nb_points(), false);
381  }
382  point_is_locked_[i] = true;
383  }
384 
393  geo_debug_assert(i < nb_points());
394  if(
395  point_is_locked_.size() != nb_points()
396  ) {
397  point_is_locked_.resize(nb_points(), false);
398  }
399  point_is_locked_[i] = false;
400  }
401 
408  point_is_locked_.clear();
409  }
410 
411  protected:
416  virtual void newiteration();
417 
425  virtual void funcgrad(index_t n, double* x, double& f, double* g);
426 
433  void constrain_points(double* g) const;
434 
441 
442  static CentroidalVoronoiTesselation* instance_;
443  bool show_iterations_;
444  coord_index_t dimension_;
445  Delaunay_var delaunay_;
447  Mesh* mesh_;
448 
449  vector<double> points_;
450  vector<vec3> points_R3_;
451  vector<bool> point_is_locked_;
452 
453  ProgressTask* progress_;
454  index_t cur_iter_;
455  index_t nb_iter_;
456 
458  bool constrained_cvt_;
459  bool use_RVC_centroids_;
460 
464  private:
467 
469  thisclass& operator= (const thisclass& rhs);
470  };
471 }
472 
473 #endif
Class and functions to compute restricted Voronoi diagrams and extract information from them.
#define geo_assert(x)
Verifies that a condition is met.
Definition: assert.h:149
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
CentroidalVoronoiTesselation is the main component of the remeshing algorithm.
Definition: CVT.h:69
virtual ~CentroidalVoronoiTesselation()
Destructor.
void done_current()
Resets the current CentroidalVoronoiTesselation to nullptr.
Definition: CVT.h:265
virtual void funcgrad(index_t n, double *x, double &f, double *g)
Computes the objective function and its gradient.
void set_volumetric(bool x)
Sets volumetric mode.
Definition: CVT.h:352
void resize_points(index_t nb_points)
Changes the number of points.
void unlock_point(index_t i)
Unlocks a point.
Definition: CVT.h:392
CentroidalVoronoiTesselation(Mesh *mesh, const vector< vec3 > &R3_embedding, coord_index_t dimension=0, const std::string &delaunay="default")
Constructs a new CentroidalVoronoiTesselation.
void constrain_points(double *g) const
Constrains the locked points.
bool compute_initial_sampling(index_t nb_samples, bool verbose=false)
Computes a random initial sampling of the surface in nD.
void set_use_RVC_centroids(bool x)
Specifies whether centroids of Voronoi cells should be used.
Definition: CVT.h:197
void compute_volume(Mesh *mesh)
Computes the volumetric mesh (using the current points).
CentroidalVoronoiTesselation(Mesh *mesh, coord_index_t dimension=0, const std::string &delaunay="default")
Constructs a new CentroidalVoronoiTesselation.
void make_current()
Makes this CentroidalVoronoiTesselation the current one.
Definition: CVT.h:251
void compute_R3_embedding()
Computes the 3d representation of the Nd points.
virtual void newiteration()
Callback for the numerical solver.
static void funcgrad_CB(index_t n, double *x, double &f, double *g)
Callback for the numerical solver.
coord_index_t dimension() const
Gets the dimension of the points.
Definition: CVT.h:308
virtual void Newton_iterations(index_t nb_iter, index_t m=7)
Relaxes the points with Newton-Lloyd's algorithm.
virtual void Lloyd_iterations(index_t nb_iter)
Relaxes the points with Lloyd's algorithm.
bool point_is_locked(index_t i) const
Tests whether a point is locked.
Definition: CVT.h:363
IntegrationSimplex_var simplex_func_
Integration simplex used by custom codes, e.g. LpCVT.
Definition: CVT.h:461
void set_points(index_t nb_points, const double *points)
Initializes the points with a user-specified vector.
index_t nb_points() const
Gets the number of points to be optimized.
Definition: CVT.h:315
void compute_surface(Mesh *mesh, bool multinerve=true)
Computes the surfacic mesh (using the current points).
void lock_point(index_t i)
Locks a point.
Definition: CVT.h:377
static void newiteration_CB(index_t n, const double *x, double f, const double *g, double gnorm)
Callback for the numerical solver.
void unlock_all_points()
Unlocks all the points.
Definition: CVT.h:407
void set_constrained_cvt(bool x)
Specifies whether constrained mode should be used.
Definition: CVT.h:206
bool volumetric() const
Tests whether volumetric mode is used.
Definition: CVT.h:343
void set_show_iterations(bool x)
Specifies whether a progress bar should be used.
Definition: CVT.h:187
double * embedding(index_t p)
Returns the representation of a point in embedding space.
Definition: CVT.h:335
void set_progress_logger(ProgressTask *progress)
Sets a client for the progress bars.
Definition: CVT.h:300
void set_facets_range(index_t facets_begin, index_t facets_end)
Restricts computation to a part of the input mesh.
Definition: CVT.h:238
RestrictedVoronoiDiagram * RVD()
Definition: CVT.h:227
const vec3 & R3_embedding(index_t p) const
Gets the representation of a point in R3.
Definition: CVT.h:325
Abstract interface for Delaunay triangulation in Nd.
Definition: delaunay.h:71
Represents a mesh.
Definition: mesh.h:2693
Tracks the progress of a task.
Definition: progress.h:240
Computes a Restricted Voronoi Diagram (RVD).
Definition: RVD.h:89
Specialization of vector for elements of type bool.
Definition: memory.h:795
Abstract interface for Delaunay.
Common include file, providing basic definitions. Should be included before anything else by all head...
base classes for computing integrals over the cells of a restricted Voronoi diagram
The class that represents a mesh.
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition: numeric.h:363