This example shows how to partition a polygon (free of self-intersection) with an arbitrary number of holes into a set of convex polygons using the PolygonPartition class of Easy3D.
Note: For complex unknown structures that have self-intersection, you need to use the CSG operators provided in easy3d/algo/tessellator.h to obtain simply polygons (free of self-intersection) first.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/renderer/camera.h>
29#include <easy3d/core/surface_mesh.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/drawable_lines.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/algo/polygon_partition.h>
34#include <easy3d/util/initializer.h>
35
45
47
48
49int main(int argc, char** argv) {
50
52
53
54
55 Viewer viewer(EXAMPLE_TITLE);
56 viewer.camera()->setUpVector(
vec3(0, 1, 0));
57 viewer.camera()->setViewDirection(
vec3(0, 0, -1));
58
59
60 std::vector<vec2> points = {
73 };
74
75
76 std::vector<PolygonPartition::Polygon> polygons = {
77 {0, 1, 2, 3}
78 };
79
80
81 std::vector<PolygonPartition::Polygon> holes = {
82 {7, 6, 5, 4},
83 {11, 10, 9, 8},
84 };
85
86
87 std::vector<PolygonPartition::Polygon> parts;
89 return EXIT_FAILURE;
90
91
92
93
94
96 for (const auto& p : points)
97 mesh->add_vertex({p.x, p.y, 0});
98 for (const auto& poly : parts) {
99 std::vector<SurfaceMesh::Vertex> vts;
100 for (const auto& id : poly)
102 mesh->add_face(vts);
103 }
104
105 viewer.add_model(std::shared_ptr<SurfaceMesh>(mesh));
106
107
108 mesh->renderer()->get_points_drawable("vertices")->set_visible(true);
110 mesh->renderer()->get_points_drawable("vertices")->set_point_size(12);
111
112 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
113
114 mesh->renderer()->get_lines_drawable("borders")->set_visible(true);
115
116
117 return viewer.run();
118}
@ SPHERE
The points will be drawn as spheres.
Definition drawable_points.h:62
static bool apply(const std::vector< vec2 > &points, const std::vector< Polygon > &polys, const std::vector< Polygon > &holes, std::vector< Polygon > &parts)
Convex partition of a general polygon with an arbitrary number of non-hole and hole contours.
Definition polygon_partition.cpp:92
A halfedge data structure for polygonal meshes of 2-manifold.
Definition surface_mesh.h:51
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
Vec< 2, float > vec2
A 2D point/vector of float type.
Definition types.h:42
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135