| 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_INTEGRATION_SIMPLEX | ||
| 41 | #define GEOGRAM_VORONOI_INTEGRATION_SIMPLEX | ||
| 42 | |||
| 43 | #include <geogram/basic/common.h> | ||
| 44 | #include <geogram/basic/numeric.h> | ||
| 45 | #include <geogram/basic/counted.h> | ||
| 46 | #include <geogram/basic/smart_pointer.h> | ||
| 47 | #include <geogram/basic/geometry.h> | ||
| 48 | #include <geogram/basic/thread_sync.h> | ||
| 49 | |||
| 50 | /** | ||
| 51 | * \file geogram/voronoi/integration_simplex.h | ||
| 52 | * \brief base classes for computing integrals over | ||
| 53 | * the cells of a restricted Voronoi diagram | ||
| 54 | */ | ||
| 55 | |||
| 56 | namespace GEOGen { | ||
| 57 | class Vertex; | ||
| 58 | } | ||
| 59 | |||
| 60 | namespace GEO { | ||
| 61 | |||
| 62 | class Mesh; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * \brief Computes an objective function and its gradient | ||
| 66 | * over a restricted Voronoi diagram. | ||
| 67 | * \details This function can be used by CentroidalVoronoiTesselation | ||
| 68 | * and its variants to optimize the placement of points by minimizing | ||
| 69 | * an objective function. Sub-classing this class makes it possible | ||
| 70 | * to define new objective functions. | ||
| 71 | */ | ||
| 72 | class GEOGRAM_API IntegrationSimplex : public Counted { | ||
| 73 | |||
| 74 | public: | ||
| 75 | |||
| 76 | /** | ||
| 77 | * \brief IntegrationSimplex destructor. | ||
| 78 | */ | ||
| 79 | ~IntegrationSimplex() override; | ||
| 80 | |||
| 81 | /** | ||
| 82 | * \brief Computes the contribution of a given integration | ||
| 83 | * simplex to the function and its gradient. An integration | ||
| 84 | * simplex is obtained as the intersection between a Voronoi | ||
| 85 | * cell and a triangle or tetrahedron of a background mesh. | ||
| 86 | * \param[in] center_vertex_index index of the first vertex | ||
| 87 | * of the integration simplex, that corresponds to one of | ||
| 88 | * the vertices of the Delaunay triangulation | ||
| 89 | * \param[in] v0 second vertex of the integration simplex, in | ||
| 90 | * both geometric and symbolic forms | ||
| 91 | * \param[in] v1 third vertex of the integration simplex, in | ||
| 92 | * both geometric and symbolic forms | ||
| 93 | * \param[in] v2 fourth vertex of the integration simplex, in | ||
| 94 | * both geometric and symbolic forms | ||
| 95 | * \param[in] t the triangle or tetrahedron of the background mesh | ||
| 96 | * \param[in] t_adj the background mesh tetrahedron adjacent to this | ||
| 97 | * integration simplex accros (\p v0, \p v1, \p v2) or NO_INDEX | ||
| 98 | * if no such tetrahedron exists. | ||
| 99 | * \param[in] v_adj if (\p v0, \p v1, \p v2) is supported by a bisector, | ||
| 100 | * the index of the other extremity of the bisector, else NO_INDEX | ||
| 101 | */ | ||
| 102 | virtual double eval( | ||
| 103 | index_t center_vertex_index, | ||
| 104 | const GEOGen::Vertex& v0, | ||
| 105 | const GEOGen::Vertex& v1, | ||
| 106 | const GEOGen::Vertex& v2, | ||
| 107 | index_t t, | ||
| 108 | index_t t_adj = NO_INDEX, | ||
| 109 | index_t v_adj = NO_INDEX | ||
| 110 | ) = 0; | ||
| 111 | |||
| 112 | |||
| 113 | /** | ||
| 114 | * \brief Sets the input points and the location where | ||
| 115 | * the computed gradient will be stored. | ||
| 116 | * \details This function needs to be called once per evaluation | ||
| 117 | * of the objective function, before evaluating the | ||
| 118 | * contribution of the simplices with eval(). | ||
| 119 | * \param[in] dimension number of coordinates of the points, | ||
| 120 | * or number of doubles between two consecutive points | ||
| 121 | * \param[in] nb_points number of points | ||
| 122 | * \param[in] points a const pointer to the | ||
| 123 | * contiguous array of coordinates of the points | ||
| 124 | * \param[out] g a pointer to the components | ||
| 125 | * of the gradient of the objective function | ||
| 126 | * \param[in] spinlocks a pointer to the spinlocks array to | ||
| 127 | * be used in multithreading mode, or nullptr in single-threaded | ||
| 128 | * mode | ||
| 129 | */ | ||
| 130 | void set_points_and_gradient( | ||
| 131 | coord_index_t dimension, | ||
| 132 | index_t nb_points, | ||
| 133 | const double* points, | ||
| 134 | double* g, | ||
| 135 | Process::SpinLockArray* spinlocks=nullptr | ||
| 136 | ) { | ||
| 137 | ✗ | nb_points_ = nb_points; | |
| 138 | ✗ | points_stride_ = index_t(dimension); | |
| 139 | ✗ | points_ = points; | |
| 140 | ✗ | g_ = g; | |
| 141 | ✗ | spinlocks_ = spinlocks; | |
| 142 | ✗ | } | |
| 143 | |||
| 144 | /** | ||
| 145 | * \brief Tests whether this IntegrationSimplex is volumetric. | ||
| 146 | * \details A volumetric IntegrationSimplex is meant to be computed | ||
| 147 | * over the Voronoi cells restricted to the tetrahedra of the mesh. | ||
| 148 | * A surfacic one is meant to be computed over the Voronoi cells | ||
| 149 | * restricted to the facets of the mesh. | ||
| 150 | * \retval true if this IntegrationSimplex is volumetric | ||
| 151 | * \retval false otherwise (surfacic) | ||
| 152 | */ | ||
| 153 | bool volumetric() const { | ||
| 154 | ✗ | return volumetric_; | |
| 155 | } | ||
| 156 | |||
| 157 | /** | ||
| 158 | * \brief Specifies whether the background | ||
| 159 | * mesh has varying attributes used in the | ||
| 160 | * computation. | ||
| 161 | * \details The RestrictedVoronoiDiagram class | ||
| 162 | * computes the intersection between a Voronoi | ||
| 163 | * diagram and a background mesh. If the triangles | ||
| 164 | * or tetrahedra of this background mesh have a | ||
| 165 | * property that varies on each triangle / tetrahedron, | ||
| 166 | * then the intersection between the Voronoi cells and | ||
| 167 | * each individual triangle / tetrahedron is computed. | ||
| 168 | * Default mode is constant, subclasses may override. | ||
| 169 | * \retval true if the background mesh has varying | ||
| 170 | * attributes | ||
| 171 | * \retval false otherwise | ||
| 172 | */ | ||
| 173 | bool background_mesh_has_varying_attribute() const { | ||
| 174 | ✗ | return varying_background_; | |
| 175 | } | ||
| 176 | |||
| 177 | /** | ||
| 178 | * \brief Before starting computation, resets | ||
| 179 | * thread local storage variables. | ||
| 180 | * \details RestrictedVoronoiDiagram can operate | ||
| 181 | * in multi-threading mode. Some derived classes | ||
| 182 | * may need to reset some thread local storage | ||
| 183 | * variables before starting each thread. | ||
| 184 | */ | ||
| 185 | virtual void reset_thread_local_storage(); | ||
| 186 | |||
| 187 | protected: | ||
| 188 | /** | ||
| 189 | * \brief Constructs a new IntegrationSimplex. | ||
| 190 | * \param[in] mesh the mesh | ||
| 191 | * \param[in] volumetric true if volumetric, false if surfacic | ||
| 192 | * \param[in] nb_frames number of frames, typically number of | ||
| 193 | * elements of the background mesh | ||
| 194 | * \param[in] nb_comp_per_frame number of components per frame, | ||
| 195 | * 3 for 3-axis anisotropy, 1 for vector anisotropy. | ||
| 196 | * \param[in] frames a const pointer to the array of | ||
| 197 | * 3*nb_frames*nb_comp_per_frame of frame coordinates. | ||
| 198 | */ | ||
| 199 | IntegrationSimplex( | ||
| 200 | const Mesh& mesh, | ||
| 201 | bool volumetric, | ||
| 202 | index_t nb_frames, | ||
| 203 | index_t nb_comp_per_frame, | ||
| 204 | const double* frames | ||
| 205 | ); | ||
| 206 | |||
| 207 | |||
| 208 | /** | ||
| 209 | * \brief Gets a point by index. | ||
| 210 | * \param[in] i index of the point | ||
| 211 | * \return a const pointer to the coordinates of the point | ||
| 212 | */ | ||
| 213 | const double* point(index_t i) const { | ||
| 214 | geo_debug_assert(i < nb_points_); | ||
| 215 | return points_ + i * points_stride_; | ||
| 216 | } | ||
| 217 | |||
| 218 | /** | ||
| 219 | * \brief Gets a frame by index. | ||
| 220 | * \param[in] i index of the frame | ||
| 221 | * \return a const pointer to the nb_components_per_frame | ||
| 222 | * coordinates of the frame | ||
| 223 | */ | ||
| 224 | const double* frame(index_t i) const { | ||
| 225 | geo_debug_assert(i < nb_frames_); | ||
| 226 | return frames_ + i * nb_comp_per_frame_; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | protected: | ||
| 231 | const Mesh& mesh_; | ||
| 232 | bool volumetric_; | ||
| 233 | index_t nb_points_; | ||
| 234 | index_t points_stride_; | ||
| 235 | const double* points_; | ||
| 236 | double* g_; | ||
| 237 | index_t nb_frames_; | ||
| 238 | index_t nb_comp_per_frame_; | ||
| 239 | const double* frames_; | ||
| 240 | Process::SpinLockArray* spinlocks_; | ||
| 241 | bool varying_background_; | ||
| 242 | }; | ||
| 243 | |||
| 244 | typedef SmartPointer<IntegrationSimplex> | ||
| 245 | IntegrationSimplex_var; | ||
| 246 | |||
| 247 | } | ||
| 248 | |||
| 249 | #endif | ||
| 250 |