Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_602_ConvexPartition/main.cpp

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 * 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#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
46using namespace easy3d;
47
48
49int main(int argc, char** argv) {
50 // initialize Easy3D.
51 initialize();
52
53 // create the default Easy3D viewer.
54 // (a viewer must be created before creating any drawables).
55 Viewer viewer(EXAMPLE_TITLE);
56 viewer.camera()->setUpVector(vec3(0, 1, 0));
57 viewer.camera()->setViewDirection(vec3(0, 0, -1));
58
59 // the vertices of the polygon (including the holes).
60 std::vector<vec2> points = {
61 vec2( 0, 0), // 0: start of outer contour, in counterclockwise order
62 vec2(500, 0), // 1
63 vec2(500, 700), // 2
64 vec2( 0, 700), // 3
65 vec2(100, 100), // 4: start of 1st hole, also in counterclockwise order
66 vec2(400, 100), // 5
67 vec2(400, 300), // 6
68 vec2(100, 300), // 7
69 vec2(100, 400), // 8: start of 2nd hole, also in counterclockwise order
70 vec2(400, 400), // 9
71 vec2(400, 600), // 10
72 vec2(100, 600), // 11
73 };
74
75 // the outer contour represented by the vertex indices. Note: must be in counterclockwise order.
76 std::vector<PolygonPartition::Polygon> polygons = {
77 {0, 1, 2, 3} // outer contour
78 };
79
80 // the interior contours representing holes. Note: hole vertices must be in clockwise order.
81 std::vector<PolygonPartition::Polygon> holes = {
82 {7, 6, 5, 4}, // {4, 5, 6, 7} won't work
83 {11, 10, 9, 8}, // {8, 9, 10, 11} won't work
84 };
85
86 // convex partition
87 std::vector<PolygonPartition::Polygon> parts;
88 if (!PolygonPartition::apply(points, polygons, holes, parts))
89 return EXIT_FAILURE;
90
91 //--------------------------------------------------------
92
93 // Visualizing the result.
94 // we create a mesh to store the resulted convex polygons.
95 SurfaceMesh* mesh = new SurfaceMesh;
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)
101 vts.push_back(SurfaceMesh::Vertex(static_cast<int>(id)));
102 mesh->add_face(vts);
103 }
104 // add the mesh to the viewer.
105 viewer.add_model(std::shared_ptr<SurfaceMesh>(mesh));
106
107 // show the vertices
108 mesh->renderer()->get_points_drawable("vertices")->set_visible(true);
109 mesh->renderer()->get_points_drawable("vertices")->set_impostor_type(easy3d::PointsDrawable::SPHERE);
110 mesh->renderer()->get_points_drawable("vertices")->set_point_size(12);
111 // show the edges
112 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
113 // also show the borders
114 mesh->renderer()->get_lines_drawable("borders")->set_visible(true);
115
116 // run the viewer
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