Easy3D 2.5.3
tessellator.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_ALGO_TESSELLATOR_H
28#define EASY3D_ALGO_TESSELLATOR_H
29
30#include <vector>
31#include <easy3d/core/types.h>
32
33namespace easy3d {
34
57 public:
59 struct Vertex : std::vector<double> {
60
66 explicit Vertex(const vec3 &xyz, int idx = 0) : index(idx) { append(xyz); }
67
74 template<typename FT>
75 Vertex(const FT *data, std::size_t size, int idx = 0) : std::vector<double>(data, data + size), index(idx) {}
76
82 explicit Vertex(std::size_t size = 0, int idx = 0) : std::vector<double>(size), index(idx) {}
83
89 Vertex(const Vertex &v, int idx = 0) : std::vector<double>(v.begin(), v.end()), index(idx) {}
90
97 template<typename Vec>
98 inline void append(const Vec &v) {
99 for (int i = 0; i < v.size(); ++i)
100 push_back(v[i]);
101 }
102
103 // this can be used to carry and map to the original vertex index
104 int index;
105 };
106
107 public:
108 Tessellator();
109
110 ~Tessellator();
111
114 WINDING_ODD = 100130,
115 WINDING_NONZERO = 100131,
116 WINDING_POSITIVE = 100132,
117 WINDING_NEGATIVE = 100133,
118 WINDING_ABS_GEQ_TWO = 100134
119 };
120
122
132 void set_boundary_only(bool b);
133
151 void set_winding_rule(WindingRule rule);
152
166 void begin_polygon(const vec3 &normal);
171 void begin_polygon();
172
176 void begin_contour();
177
184 void add_vertex(const Vertex &data);
186 void add_vertex(const float *data, unsigned int size, int idx = 0);
188 void add_vertex(const vec3 &xyz, int idx = 0);
190 void add_vertex(const vec3 &xyz, const vec2 &t, int idx = 0);
192 void add_vertex(const vec3 &xyz, const vec3 &v1, int idx = 0);
194 void add_vertex(const vec3 &xyz, const vec3 &v1, const vec2 &t, int idx = 0);
196 void add_vertex(const vec3 &xyz, const vec3 &v1, const vec3 &v2, int idx = 0);
198 void add_vertex(const vec3 &xyz, const vec3 &v1, const vec3 &v2, const vec2 &t, int idx = 0);
199
203 void end_contour();
204
208 void end_polygon();
209
211
215 const std::vector<Vertex *> &vertices() const;
216
221 const std::vector<std::vector<unsigned int> > &elements() const { return elements_; }
222
227 unsigned int num_elements_in_polygon() const { return num_elements_in_polygon_; }
228
229
231
237 void reset();
238
239 private:
240 void _add_element(const std::vector<unsigned int> &element);
241
242 // GLU tessellator callbacks
243 static void beginCallback(unsigned int w, void *cbdata);
244 static void endCallback(void *cbdata);
245 static void vertexCallback(void *vertex, void *cbdata);
246 static void combineCallback(const double coords[3], void *vertex_data[4], const float weight[4], void **dataOut, void *cbdata);
247
248 private:
249 void *tess_obj_;
250 void *vertex_manager_;
251
252 // The tessellator decides the most efficient primitive type while performing tessellation.
253 unsigned int primitive_type_; // GL_TRIANGLES, GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP
254
255 // The list of elements (triangle or contour) created over many calls. Each entry is the vertex indices of the
256 // element.
257 std::vector<std::vector<unsigned int> > elements_;
258
259 // The growing number of elements (triangle or contour) in the current polygon.
260 unsigned int num_elements_in_polygon_;
261
262 // The vertex indices (including the original ones and the new vertices) of the current polygon.
263 std::vector<unsigned int> vertex_ids_;
264
265 // The length of the vertex data. Used to handle user provided data in the combine function.
266 unsigned int vertex_data_size_;
267 };
268
269
270 namespace csg {
280 void tessellate(std::vector<Polygon2> &polygons, Tessellator::WindingRule rule);
281
286 void union_of(std::vector<Polygon2> &polygons);
287
293 void intersection_of(const Polygon2& polygon_a, const Polygon2& polygon_b, std::vector<Polygon2> &result);
294
301 void difference_of(const Polygon2& polygon_a, const Polygon2& polygon_b, std::vector<Polygon2> &result);
302 }
303
304}
305
306#endif // EASY3D_ALGO_TESSELLATOR_H
Tessellator subdivides concave planar polygons, polygons with holes, or polygons with intersecting ed...
Definition: tessellator.h:56
void add_vertex(const Vertex &data)
Add a vertex of a contour to the tessellator.
Definition: tessellator.cpp:161
void end_polygon()
Finish the current polygon.
Definition: tessellator.cpp:231
void begin_polygon()
Begin the tessellation of a complex polygon.
Definition: tessellator.cpp:150
WindingRule
The winding rule (default rule is ODD, modify if needed)
Definition: tessellator.h:113
void begin_contour()
Begin a contour of a complex polygon (a polygon may have multiple contours).
Definition: tessellator.cpp:156
unsigned int num_elements_in_polygon() const
The number of elements (triangle or contour) in the last polygon.
Definition: tessellator.h:227
const std::vector< Vertex * > & vertices() const
The list of vertices in the result.
Definition: tessellator.cpp:236
void reset()
Clear all recorded data (triangle list and vertices) and restart index counter. This function is usef...
Definition: tessellator.cpp:380
const std::vector< std::vector< unsigned int > > & elements() const
The list of elements (triangle or contour) created over many calls. Each element is represented by it...
Definition: tessellator.h:221
void set_winding_rule(WindingRule rule)
Set the wining rule. The new rule will be effective until being changed by calling this function agai...
Definition: tessellator.cpp:138
void end_contour()
Finish the current contour of a polygon.
Definition: tessellator.cpp:226
void set_boundary_only(bool b)
Set the working mode of the tessellator.
Definition: tessellator.cpp:133
size_t size() const
Returns the dimension/size of this vector.
Definition: vec.h:78
Definition: collider.cpp:182
GenericPolygon< float > Polygon2
A 2D polygon of float type.
Definition: types.h:117
Definition: tessellator.h:59
Vertex(std::size_t size=0, int idx=0)
initialize with a known size but memory is allocated without data initialization.
Definition: tessellator.h:82
void append(const Vec &v)
append a property (e.g., color, texture coordinates) to this vertex.
Definition: tessellator.h:98
Vertex(const vec3 &xyz, int idx=0)
initialize with xyz coordinates and an optional index.
Definition: tessellator.h:66
Vertex(const Vertex &v, int idx=0)
copy constructor.
Definition: tessellator.h:89
Vertex(const FT *data, std::size_t size, int idx=0)
initialize from a C-style array.
Definition: tessellator.h:75