Easy3D 2.6.1
Loading...
Searching...
No Matches
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
54
56 public:
60 struct Vertex : std::vector<double> {
67 explicit Vertex(const vec3 &xyz, int idx = 0) : index(idx) { append(xyz); }
68
77 template<typename FT>
78 Vertex(const FT *data, std::size_t size, int idx = 0) : std::vector<double>(data, data + size), index(idx) {}
79
86 explicit Vertex(std::size_t size = 0, int idx = 0) : std::vector<double>(size), index(idx) {}
87
94 Vertex(const Vertex &v, int idx = 0) : std::vector<double>(v.begin(), v.end()), index(idx) {}
95
102 template<typename Vec>
103 void append(const Vec &v) {
104 for (int i = 0; i < v.size(); ++i)
105 push_back(v[i]);
106 }
107
109 int index;
110 };
111
112 public:
116 Tessellator();
120 ~Tessellator();
121
124 WINDING_ODD = 100130,
125 WINDING_NONZERO = 100131,
126 WINDING_POSITIVE = 100132,
127 WINDING_NEGATIVE = 100133,
128 WINDING_ABS_GEQ_TWO = 100134
129 };
130
140 void set_boundary_only(bool b);
141
160 void set_winding_rule(WindingRule rule);
161
175 void begin_polygon(const vec3 &normal);
180 void begin_polygon();
181
185 void begin_contour();
186
191 void add_vertex(const Vertex &data);
199 void add_vertex(const float *data, unsigned int size, int idx = 0);
206 void add_vertex(const vec3 &xyz, int idx = 0);
214 void add_vertex(const vec3 &xyz, const vec2 &t, int idx = 0);
222 void add_vertex(const vec3 &xyz, const vec3 &v1, int idx = 0);
231 void add_vertex(const vec3 &xyz, const vec3 &v1, const vec2 &t, int idx = 0);
240 void add_vertex(const vec3 &xyz, const vec3 &v1, const vec3 &v2, int idx = 0);
250 void add_vertex(const vec3 &xyz, const vec3 &v1, const vec3 &v2, const vec2 &t, int idx = 0);
251
255 void end_contour();
256
260 void end_polygon();
261
263
268 const std::vector<Vertex *> &vertices() const;
269
275 const std::vector<std::vector<unsigned int> > &elements() const { return elements_; }
276
282 unsigned int num_elements_in_polygon() const { return num_elements_in_polygon_; }
283
284
286
293 void reset();
294
295 private:
296 void _add_element(const std::vector<unsigned int> &element);
297
298 // GLU tessellator callbacks
299 static void beginCallback(unsigned int w, void *cbdata);
300 static void endCallback(void *cbdata);
301 static void vertexCallback(void *vertex, void *cbdata);
302 static void combineCallback(const double coords[3], void *vertex_data[4], const float weight[4], void **dataOut, void *cbdata);
303
304 private:
305 void *tess_obj_;
306 void *vertex_manager_;
307
308 // The tessellator decides the most efficient primitive type while performing tessellation.
309 unsigned int primitive_type_; // GL_TRIANGLES, GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP
310
311 // The list of elements (triangle or contour) created over many calls. Each entry is the vertex indices of the
312 // element.
313 std::vector<std::vector<unsigned int> > elements_;
314
315 // The growing number of elements (triangle or contour) in the current polygon.
316 unsigned int num_elements_in_polygon_;
317
318 // The vertex indices (including the original ones and the new vertices) of the current polygon.
319 std::vector<unsigned int> vertex_ids_;
320
321 // The length of the vertex data. Used to handle user provided data in the combine function.
322 unsigned int vertex_data_size_;
323 };
324
325
326 namespace csg {
336 void tessellate(std::vector<Polygon2> &polygons, Tessellator::WindingRule rule);
337
342 void union_of(std::vector<Polygon2> &polygons);
343
350 void intersection_of(const Polygon2& polygon_a, const Polygon2& polygon_b, std::vector<Polygon2> &result);
351
358 void difference_of(const Polygon2& polygon_a, const Polygon2& polygon_b, std::vector<Polygon2> &result);
359 }
360
361}
362
363#endif // EASY3D_ALGO_TESSELLATOR_H
void add_vertex(const Vertex &data)
Add a vertex of a contour to the tessellator.
Definition tessellator.cpp:163
void end_polygon()
Finish the current polygon.
Definition tessellator.cpp:233
void begin_polygon()
Begin the tessellation of a complex polygon.
Definition tessellator.cpp:152
WindingRule
The winding rule (default rule is ODD, modify if needed)
Definition tessellator.h:123
Tessellator()
Constructor.
Definition tessellator.cpp:105
void begin_contour()
Begin a contour of a complex polygon (a polygon may have multiple contours).
Definition tessellator.cpp:158
~Tessellator()
Destructor.
Definition tessellator.cpp:129
unsigned int num_elements_in_polygon() const
The number of elements (triangle or contour) in the last polygon.
Definition tessellator.h:282
const std::vector< Vertex * > & vertices() const
The list of vertices in the result.
Definition tessellator.cpp:238
void reset()
Clear all recorded data (triangle list and vertices) and restart index counter.
Definition tessellator.cpp:382
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:275
void set_winding_rule(WindingRule rule)
Set the winding rule. The new rule will be effective until being changed by calling this function aga...
Definition tessellator.cpp:140
void end_contour()
Finish the current contour of a polygon.
Definition tessellator.cpp:228
void set_boundary_only(bool b)
Set the working mode of the tessellator.
Definition tessellator.cpp:135
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
static size_t size()
Returns the dimension/size of this vector.
Definition vec.h:79
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
GenericPolygon< float > Polygon2
A 2D polygon of float type.
Definition types.h:116
Vec< 2, float > vec2
A 2D point/vector of float type.
Definition types.h:42
Vertex(std::size_t size=0, int idx=0)
Initialize with a known size but memory is allocated without data initialization.
Definition tessellator.h:86
void append(const Vec &v)
Append a property (e.g., color, texture coordinates) to this vertex.
Definition tessellator.h:103
Vertex(const vec3 &xyz, int idx=0)
Initialize with xyz coordinates and an optional index.
Definition tessellator.h:67
int index
The index of this vertex. This can be used to carry and map to the original vertex index.
Definition tessellator.h:109
Vertex(const Vertex &v, int idx=0)
Copy constructor.
Definition tessellator.h:94
Vertex(const FT *data, std::size_t size, int idx=0)
Initialize from a C-style array.
Definition tessellator.h:78