Geogram Version 1.9.6-rc
A programming library of geometric algorithms
Loading...
Searching...
No Matches
generic_RVD_utils.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_GENERIC_RVD_UTILS
41#define GEOGRAM_VORONOI_GENERIC_RVD_UTILS
42
48#include <stack>
49#include <vector>
50
57namespace GEOGen {
58
59 using GEO::NO_INDEX;
60
70 template <class T>
72 public:
76 void push(const T& x) {
77 rep_.push_back(x);
78 }
79
84 void pop() {
85 rep_.pop_back();
86 }
87
93 const T& top() const {
94 return *rep_.rbegin();
95 }
96
100 bool empty() const {
101 return rep_.size() == 0;
102 }
103
104 private:
105 GEO::vector<T> rep_;
106 };
107
108 /************************************************************************/
109
116 struct FacetSeed {
117
123 FacetSeed(index_t f_in, index_t seed_in) :
124 f(f_in),
125 seed(seed_in) {
126 }
127
133 }
134
140 bool operator< (const FacetSeed& rhs) const {
141 if(f < rhs.f) {
142 return true;
143 }
144 if(f > rhs.f) {
145 return false;
146 }
147 return seed < rhs.seed;
148 }
149
150 index_t f;
151 index_t seed;
152 };
153
161 /************************************************************************/
162
163#ifdef GEO_OS_ANDROID
164 // VectorStack uses AlignedAllocator, that is protected
165 // by a global lock under Android (needed because it
166 // seems that malloc() is not SMP-thread-safe under Android).
167
173
179
185#else
186
191 typedef std::stack<FacetSeed> FacetSeedStack;
192
197 typedef std::stack<TetSeed> TetSeedStack;
198
203 typedef std::stack<index_t> SeedStack;
204#endif
205
206 /************************************************************************/
207
220 public:
225 FacetSeedMarking(index_t /* nb_facets*/, index_t nb_seeds) {
226 set_size(nb_seeds);
227 }
228
232 bool is_marked(index_t facet, index_t seed) const {
233 return (find_index(seed, facet) != NO_INDEX);
234 }
235
239 bool is_marked(const FacetSeed& fs) const {
240 return is_marked(fs.f, fs.seed);
241 }
242
247 index_t get_connected_component(const FacetSeed& fs) const {
248 return find_value(fs.seed, fs.f);
249 }
250
255 void mark(const FacetSeed& fs, index_t conn_comp) {
256 insert(fs.seed, fs.f, conn_comp);
257 }
258
263 for(index_t i = 0; i < nb_arrays(); ++i) {
264 free(keys_[i]);
265 }
266 for(index_t i = 0; i < nb_arrays(); ++i) {
267 free(values_[i]);
268 }
269 }
270
271 protected:
275 index_t nb_arrays() const {
276 return index_t(keys_.size());
277 }
278
283 void set_size(index_t nb_arrays) {
284 keys_.assign(nb_arrays, nullptr);
285 values_.assign(nb_arrays, nullptr);
286 size_.assign(nb_arrays, 0);
287 }
288
294 index_t array_size(index_t array) const {
295 return size_[array];
296 }
297
307 index_t array_capacity(index_t array) const {
308 index_t size = array_size(array);
309 if(size == 0) {
310 return 0;
311 }
312 index_t result = 1;
313 index_t mask = 1;
314 for(index_t i = 0; i < 32; i++) {
315 mask = mask << 1;
316 if((size & mask) != 0) {
317 result = mask;
318 }
319 }
320 // If size is not already a power of two,
321 if(result != size) {
322 result = result << 1;
323 }
324 return result;
325 }
326
333 index_t find_index(index_t array, index_t key) const {
334 index_t* K = keys_[array];
335 for(index_t i = 0; i < array_size(array); ++i) {
336 if(K[i] == key) {
337 return i;
338 }
339 }
340 return NO_INDEX;
341 }
342
351 index_t find_value(index_t array, index_t key) const {
352 index_t i = find_index(array, key);
353 if(i == NO_INDEX) {
354 return NO_INDEX;
355 }
356 return values_[array][i];
357 }
358
365 void insert(index_t array, index_t key, index_t value) {
366 index_t i = find_index(array, key);
367 if(i == NO_INDEX) {
368 // If not found, append at the end of array
369 i = size_[array];
370 if(i == array_capacity(array)) {
371 // If capacity is reached, grow storage
372 index_t new_nb = index_t(2*i);
373 if(new_nb == 0) {
374 new_nb = 1;
375 }
376 keys_[array] = reinterpret_cast<index_t*>(
377 realloc(keys_[array], sizeof(index_t) * new_nb)
378 );
379 values_[array] = reinterpret_cast<index_t*>(
380 realloc(values_[array], sizeof(index_t) * new_nb)
381 );
382 }
383 size_[array] = i + 1;
384 }
385 keys_[array][i] = key;
386 values_[array][i] = value;
387 }
388
389 private:
390 std::vector<index_t*> keys_;
391 std::vector<index_t*> values_;
392 std::vector<index_t> size_;
393 };
394
395 /************************************************************************/
396
402
403 /************************************************************************/
404}
405
406#endif
Stores associations between (facet,seed) pairs and the index of a connected component.
void insert(index_t array, index_t key, index_t value)
Inserts a (key,value) pair into one of the arrays.
index_t array_size(index_t array) const
Gets the size of one of the arrays.
index_t get_connected_component(const FacetSeed &fs) const
Gets the index of the connected component associated with a given FacetSeed.
index_t find_index(index_t array, index_t key) const
Finds the index of one of the keys in one of the arrays.
bool is_marked(const FacetSeed &fs) const
Tests whether a fiven FacetSeed is marked.
FacetSeedMarking(index_t, index_t nb_seeds)
Creates a new FacetSeedMarking.
index_t find_value(index_t array, index_t key) const
Finds the value associated with a key in one of the arrays.
index_t array_capacity(index_t array) const
Gets the capacity of one of the arrays.
void mark(const FacetSeed &fs, index_t conn_comp)
Marks a FacetSeed and sets the associated connected component index.
index_t nb_arrays() const
Gets the number of arrays used internally.
~FacetSeedMarking()
FacetSeedMarking destructor.
bool is_marked(index_t facet, index_t seed) const
Tests whether a given facet,seed couple is marked.
void set_size(index_t nb_arrays)
Sets the number of arrays to be used.
A stack implemented in a GEO::vector.
const T & top() const
Gets the item on the top.
void pop()
Pops the top of the stack.
void push(const T &x)
Pushes a new item onto the stack.
bool empty() const
Tests whether the stack is empty.
Vector with aligned memory allocation.
Definition memory.h:660
Internal representation of polyhedra for GEO::GenericVoronoiDiagram.
Internal representation of polygons for GenericVoronoiDiagram.
std::stack< TetSeed > TetSeedStack
A stack of TetSeed.
std::stack< FacetSeed > FacetSeedStack
A stack of FacetSeed.
std::stack< index_t > SeedStack
A stack of seed indices (index_t).
Types and utilities for manipulating vertices in geometric and symbolic forms in restricted Voronoi d...
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
A (facet,seed) pair.
FacetSeed()
Creates a new uninitialized FacetSeed.
bool operator<(const FacetSeed &rhs) const
Compares two facet seeds using lexicographic order.
FacetSeed(index_t f_in, index_t seed_in)
Creates a new FacetSeed.