Geogram Version 1.9.6-rc
A programming library of geometric algorithms
Loading...
Searching...
No Matches
id_map.h
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 H_HEXDOM_ALGO_ID_MAP_H
41#define H_HEXDOM_ALGO_ID_MAP_H
42
44#include <exploragram/hexdom/basic.h>
46
47namespace IdMap {
48
49 using namespace GEO;
50
51 inline void make_identity(vector<index_t>& ind, int size = -1){
52 if (size >= 0) ind.resize(index_t(size));
53 for (index_t i = 0; i < ind.size(); i++)
54 ind[i] = i;
55 }
56
57 inline void connect_to_root(vector<index_t>& ind, index_t i){
58 while (ind[i] != ind[ind[i]]) ind[i] = ind[ind[i]];
59 }
60
61 inline void merge(vector<index_t>& ind, index_t a, index_t b){
62 connect_to_root(ind, a);
63 connect_to_root(ind, b);
64 ind[ind[a]] = ind[b];
65 }
66
67 inline void tree_to_map(vector<index_t>& ind){
68 for (index_t i = 0; i < ind.size(); i++)
69 connect_to_root(ind, i);
70 }
71
72 inline void compress_id2grp(vector<index_t>& ind, index_t & nb_groups){
73 vector<index_t> old_grp_2_new_grp(ind.size(), index_t(-1));
74 nb_groups = 0;
75 for (index_t i = 0; i < ind.size(); i++)
76 geo_assert(ind[i] == ind[ind[i]]);
77
78 for (index_t i = 0; i < ind.size(); i++)if (i == ind[i]) {
79 old_grp_2_new_grp[i] = nb_groups;
80 nb_groups++;
81 }
82 for (index_t i = 0; i < ind.size(); i++)
83 ind[i] = old_grp_2_new_grp[ind[i]];
84 }
85}
86
87namespace IdMapSigned {
88
89 using namespace GEO;
90
91 inline void make_identity(vector<index_t>& ind, vector<bool>& sign, int size = -1){
92 if (size >= 0) {
93 ind.resize(index_t(size));
94 sign.resize(index_t(size));
95 }
96 geo_assert(sign.size() == ind.size());
97 for (index_t i = 0; i < ind.size(); i++){
98 ind[i] = i;
99 sign[i] = true;
100 }
101 }
102
103 inline void connect_to_root(vector<index_t>& ind, vector<bool>& sign, index_t i){
104 while (ind[i] != ind[ind[i]]) {
105 sign[i] = (sign[i] == sign[ind[i]]);
106 ind[i] = ind[ind[i]];
107 }
108 }
109
110 inline void merge(vector<index_t>& ind, vector<bool>& sign, index_t a, index_t b, bool ab_link_sign){
111 connect_to_root(ind, sign, a);
112 connect_to_root(ind, sign, b);
113 sign[ind[a]] = ((sign[a] == sign[b]) == ab_link_sign);
114 ind[ind[a]] = ind[b];
115 }
116
117 inline void tree_to_map(vector<index_t>& ind, vector<bool>& sign){
118 for (index_t i = 0; i < ind.size(); i++)
119 connect_to_root(ind, sign, i);
120 }
121
122 inline void compress_id2grp(vector<index_t>& ind, index_t & nb_groups){
123 vector<index_t> old_grp_2_new_grp(ind.size(), index_t(-1));
124 nb_groups = 0;
125 FOR(i,ind.size()) geo_assert(ind[i] == ind[ind[i]]);
126
127 FOR(i,ind.size())if (i == ind[i]) {
128 old_grp_2_new_grp[i] = nb_groups;
129 nb_groups++;
130 }
131 FOR(i, ind.size()) ind[i] = old_grp_2_new_grp[ind[i]];
132 }
133}
134
135#endif
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:149
Vector with aligned memory allocation.
Definition memory.h:660
index_t size() const
Gets the number of elements.
Definition memory.h:706
Included by all headers in exploragram.
Types and functions for memory manipulation.
Global Vorpaline namespace.
Definition basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:329