Easy3D 2.5.3
surface_mesh_builder.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_CORE_SURFACE_MESH_BUILDER_H
28#define EASY3D_CORE_SURFACE_MESH_BUILDER_H
29
30
31#include <unordered_map>
32#include <easy3d/core/surface_mesh.h>
33
34
35namespace easy3d {
36
57 public:
60 typedef SurfaceMesh::Face Face;
61
62 public:
63 explicit SurfaceMeshBuilder(SurfaceMesh *mesh);
64
66
67 // -------------------------------------------------------------------------------------------------------------
68
74 void begin_surface();
75
81 Vertex add_vertex(const vec3 &p);
82
89 Face add_face(const std::vector<Vertex> &vertices);
90
97
103 Face add_quad(Vertex v1, Vertex v2, Vertex v3, Vertex v4);
104
111 void end_surface(bool log_issues = true);
112
113 // -------------------------------------------------------------------------------------------------------------
114
121 const std::vector<Vertex> &face_vertices() const { return face_vertices_; }
122
123 private:
124
125 // A face (without duplicating a vertex) cannot be added to a SurfaceMesh if
126 // - it has less than 3 vertices, or
127 // - it has self duplicated vertices, or
128 // - one of the vertex is out-of-range.
129 bool vertices_valid(const std::vector<Vertex> &vertices);
130
131 // Copy a vertex v and its attributes.
132 // Return the new vertex.
133 Vertex copy_vertex(Vertex v);
134
135 // A vertex might have been copied a few times. If copies occurred before, the original vertex will never work.
136 // To avoid unnecessary duplication, we reuse one of its copy that is not on a closed disk. We test each copy in
137 // the order the copies were made. If no valid copy can be found, we make a new copy.
138 // If no copy exists and v is on a closed disk, we simply copy it.
139 Vertex get(Vertex v);
140
141 // Resolve all non-manifold vertices of a mesh.
142 // Return the number of non-manifold vertices.
143 std::size_t resolve_non_manifold_vertices(SurfaceMesh *mesh);
144
145 // Vertices might be copied, for two reasons:
146 // - resolve non-manifoldness. In two phases: during the construction of the mesh by call to 'add_face()' and
147 // in 'resolve_non_manifold_vertices()'.
148 // - ensure boundary consistency. All happen during the construction of the mesh by call to 'add_face()'.
149 //
150 // The copied vertices: vertices in 'second' were copied from 'first'.
151 // Usually only a small number of vertices will be copied, so no need to use vertex property.
152 typedef std::unordered_map<Vertex, std::vector<Vertex>, Vertex::Hash> CopyRecord;
153
154 // Resolve the non-manifoldness of a vertex that is denoted by an incoming halfedge.
155 // @param h The halfedge pointing to the non-manifold vertex.
156 // Return the number of vertex copies.
157 std::size_t resolve_non_manifold_vertex(Halfedge h, SurfaceMesh *mesh, CopyRecord &copy_record);
158
159 private:
160 SurfaceMesh *mesh_;
161
162 // faces with less than three vertices
163 std::size_t num_faces_less_three_vertices_;
164
165 // faces with duplicate vertices
166 std::size_t num_faces_duplicate_vertices;
167
168 // faces with out-of-range vertex indices
169 std::size_t num_faces_out_of_range_vertices_;
170
171 // faces with unknown topology
172 std::size_t num_faces_unknown_topology_;
173
174 // record for linking a face to the mesh
175 CopyRecord copied_vertices_for_linking_;
176 // all copy record
177 CopyRecord copied_vertices_;
178
179 // The actual vertices after the face was successfully added to the mesh.
180 std::vector<Vertex> face_vertices_;
181
182 // A vertex property to record the original vertex of each vertex.
184
185 // The record of all halfedges (each associated with a valid face) originating from a vertex.
186 // This is used for fast query of duplicate edges. All vertices are their original indices.
187 // - first: the index of a vertex
188 // - second: the indices of the target vertices
189 std::unordered_map<int, std::vector<int> > outgoing_halfedges_;
190 };
191
192}
193
194#endif // EASY3D_CORE_SURFACE_MESH_BUILDER_H
Definition: surface_mesh.h:185
A helper class for constructing manifold surface mesh models.
Definition: surface_mesh_builder.h:56
Face add_face(const std::vector< Vertex > &vertices)
Add a face to the mesh.
Definition: surface_mesh_builder.cpp:273
void end_surface(bool log_issues=true)
Finalize surface construction. Must be called at the end of the surface construction and used in pair...
Definition: surface_mesh_builder.cpp:69
const std::vector< Vertex > & face_vertices() const
The actual vertices of the previously added face. The order of the vertices are the same as those pro...
Definition: surface_mesh_builder.h:121
Face add_quad(Vertex v1, Vertex v2, Vertex v3, Vertex v4)
Add a new quad face connecting vertices v1, v2, v3, and v4.
Definition: surface_mesh_builder.cpp:360
Face add_triangle(Vertex v1, Vertex v2, Vertex v3)
Add a new triangle face connecting vertices v1, v2, and v3.
Definition: surface_mesh_builder.cpp:355
Vertex add_vertex(const vec3 &p)
Add a vertex to the mesh.
Definition: surface_mesh_builder.cpp:228
void begin_surface()
Begin surface construction. Must be called at the beginning of the surface construction and used in p...
Definition: surface_mesh_builder.cpp:54
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
Definition: collider.cpp:182
Definition: surface_mesh.h:134
Definition: surface_mesh.h:114
Definition: surface_mesh.h:104