Easy3D 2.6.1
Loading...
Searching...
No Matches
delaunay.h
1/********************************************************************
2 * Copyright (C) 2015-2021 by Liangliang Nan <liangliang.nan@gmail.com>
3 * Copyright (C) 2000-2005 INRIA - Project ALICE
4 *
5 * The code in this file is partly from OGF/Graphite (2.0 alpha-4) with
6 * modifications and enhancement:
7 * https://gforge.inria.fr/forum/forum.php?forum_id=11459
8 * The original code was distributed under the GNU GPL license.
9 ********************************************************************/
10
11#ifndef EASY3D_ALGO_DELAUNAY_H
12#define EASY3D_ALGO_DELAUNAY_H
13
14#include <cassert>
15
16#include <easy3d/core/types.h>
17
18
19namespace easy3d {
20
29 class Delaunay {
30 public:
35 explicit Delaunay(unsigned int dimension);
36
38 virtual ~Delaunay();
39
44 unsigned int dimension() const { return dimension_; }
45
50 unsigned int cell_size() const { return cell_size_; }
51
58 virtual void set_vertices(unsigned int nb_vertices, const float *vertices);
59
64 const float *vertices_ptr() const { return vertices_; }
65
71 const float *vertex_ptr(unsigned int i) const {
72 assert(i < nb_vertices());
73 return vertices_ + dimension() * i;
74 }
75
80 unsigned int nb_vertices() const { return nb_vertices_; }
81
86 unsigned int nb_cells() const { return nb_cells_; }
87
92 const int *cell_to_v() const { return cell_to_v_; }
93
98 const int *cell_to_cell() const { return cell_to_cell_; }
99
105 virtual unsigned int nearest_vertex(const float *p) const;
106
113 int cell_vertex(unsigned int c, unsigned int lv) const {
114 assert(c < nb_cells());
115 assert(lv < cell_size());
116 return cell_to_v_[c * cell_v_stride_ + lv];
117 }
118
125 int cell_adjacent(unsigned int c, unsigned int lf) const {
126 assert(c < nb_cells());
127 assert(lf < cell_size());
128 return cell_to_cell_[c * cell_neigh_stride_ + lf];
129 }
130
136 int vertex_cell(unsigned int v) const {
137 assert(v < nb_vertices());
138 assert(v < v_to_cell_.size());
139 return v_to_cell_[v];
140 }
141
148 unsigned int index(unsigned int c, int v) const {
149 assert(c < nb_cells());
150 assert(v < (int) nb_vertices());
151 for (unsigned int iv = 0; iv < cell_size(); iv++) {
152 if (cell_vertex(c, iv) == v) { return iv; }
153 }
154 DCHECK(false) << "should not have reached here";
155 return cell_size();
156 }
157
164 unsigned int adjacent_index(unsigned int c1, unsigned int c2) const {
165 assert(c1 < nb_cells());
166 assert(c2 < nb_cells());
167 for (unsigned int f = 0; f < cell_size(); f++) {
168 if (cell_adjacent(c1, f) == c2) { return f; }
169 }
170 DCHECK(false) << "should not have reached here";
171 return cell_size();
172 }
173
180 unsigned int next_around_vertex(unsigned int c, unsigned int lv) const {
181 assert(c < nb_cells());
182 assert(lv < cell_size());
183 return cicl_[cell_size() * c + lv];
184 }
185
191 virtual void get_neighbors(unsigned int v, std::vector<unsigned int> &neighbors) const;
192
193
199
200 protected:
201
202 void get_neighbors_internal(unsigned int v, std::vector<unsigned int> &neighbors) const;
203
204 virtual void set_arrays(unsigned int nb_cells, const int *cell_to_v, const int *cell_to_cell);
205
206 void update_v_to_cell();
207
208 void update_cicl();
209
210 void update_neighbors();
211
212 void set_next_around_vertex(unsigned int c1, unsigned int lv, unsigned int c2) {
213 assert(c1 < nb_cells());
214 assert(c2 < nb_cells());
215 assert(lv < cell_size());
216 cicl_[cell_size() * c1 + lv] = static_cast<int>(c2);
217 }
218
219 protected:
220 unsigned int dimension_;
221 unsigned int cell_size_;
222 unsigned int cell_v_stride_;
223 unsigned int cell_neigh_stride_;
224 const float *vertices_;
225 unsigned int nb_vertices_;
226 unsigned int nb_cells_;
227 const int *cell_to_v_;
228 const int *cell_to_cell_;
229 std::vector<int> v_to_cell_;
230 std::vector<int> cicl_;
231 std::vector<std::vector<unsigned int>> neighbors_;
232 bool is_locked_;
233 };
234
235} // namespace easy3d
236
237
238#endif // EASY3D_ALGO_DELAUNAY_H
239
unsigned int index(unsigned int c, int v) const
Returns the local index of vertex v within cell c.
Definition delaunay.h:148
const float * vertices_ptr() const
Returns a pointer to the vertices array.
Definition delaunay.h:64
const int * cell_to_cell() const
Returns a pointer to the cell-to-cell mapping.
Definition delaunay.h:98
unsigned int nb_cells() const
Returns the number of cells.
Definition delaunay.h:86
int vertex_cell(unsigned int v) const
Returns the index of a cell containing the vertex v.
Definition delaunay.h:136
int cell_adjacent(unsigned int c, unsigned int lf) const
Returns the index of the cell adjacent to the lf-th face of the c-th cell.
Definition delaunay.h:125
unsigned int next_around_vertex(unsigned int c, unsigned int lv) const
Returns the next cell around vertex lv in cell c.
Definition delaunay.h:180
unsigned int adjacent_index(unsigned int c1, unsigned int c2) const
Returns the local index of the face shared by cells c1 and c2.
Definition delaunay.h:164
virtual unsigned int nearest_vertex(const float *p) const
Finds the index of the nearest vertex to a given point.
Definition delaunay.cpp:74
unsigned int nb_vertices() const
Returns the number of vertices.
Definition delaunay.h:80
virtual ~Delaunay()
Virtual destructor.
const float * vertex_ptr(unsigned int i) const
Returns a pointer to the vertex of index i.
Definition delaunay.h:71
int cell_vertex(unsigned int c, unsigned int lv) const
Returns the index of the lv-th vertex in the c-th cell.
Definition delaunay.h:113
const int * cell_to_v() const
Returns a pointer to the cell-to-vertex mapping.
Definition delaunay.h:92
Delaunay(unsigned int dimension)
Constructor.
Definition delaunay.cpp:34
bool check_duplicate_vertices()
Checks for duplicate vertices in stored neighbor lists.
Definition delaunay.cpp:221
unsigned int dimension() const
Returns the dimension of the triangulation.
Definition delaunay.h:44
virtual void set_vertices(unsigned int nb_vertices, const float *vertices)
Sets the vertices for the triangulation.
Definition delaunay.cpp:51
virtual void get_neighbors(unsigned int v, std::vector< unsigned int > &neighbors) const
Retrieves the one-ring neighbors of vertex v.
Definition delaunay.cpp:90
unsigned int cell_size() const
Returns the size of a cell.
Definition delaunay.h:50
Definition collider.cpp:182