Easy3D 2.6.1
Loading...
Searching...
No Matches
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
56 public:
60
61 public:
66 explicit SurfaceMeshBuilder(SurfaceMesh *mesh);
71
72 // -------------------------------------------------------------------------------------------------------------
73
80 void begin_surface();
81
87 Vertex add_vertex(const vec3 &p);
88
95 Face add_face(const std::vector<Vertex> &vertices);
96
106
116 Face add_quad(Vertex v1, Vertex v2, Vertex v3, Vertex v4);
117
125 void end_surface(bool log_issues = true);
126
127 // -------------------------------------------------------------------------------------------------------------
128
137 const std::vector<Vertex> &face_vertices() const { return face_vertices_; }
138
139 private:
140
141 // A face (without duplicating a vertex) cannot be added to a SurfaceMesh if
142 // - it has less than 3 vertices, or
143 // - it has self duplicated vertices, or
144 // - one of the vertex is out-of-range.
145 bool vertices_valid(const std::vector<Vertex> &vertices);
146
147 // Copy a vertex v and its attributes.
148 // Return the new vertex.
149 Vertex copy_vertex(Vertex v);
150
151 // A vertex might have been copied a few times. If copies occurred before, the original vertex will never work.
152 // To avoid unnecessary duplication, we reuse one of its copy that is not on a closed disk. We test each copy in
153 // the order the copies were made. If no valid copy can be found, we make a new copy.
154 // If no copy exists and v is on a closed disk, we simply copy it.
155 Vertex get(Vertex v);
156
157 // Resolve all non-manifold vertices of a mesh.
158 // Return the number of non-manifold vertices.
159 std::size_t resolve_non_manifold_vertices(SurfaceMesh *mesh);
160
161 // Vertices might be copied, for two reasons:
162 // - resolve non-manifoldness. In two phases: during the construction of the mesh by call to 'add_face()' and
163 // in 'resolve_non_manifold_vertices()'.
164 // - ensure boundary consistency. All happen during the construction of the mesh by call to 'add_face()'.
165 //
166 // The copied vertices: vertices in 'second' were copied from 'first'.
167 // Usually only a small number of vertices will be copied, so no need to use vertex property.
168 typedef std::unordered_map<Vertex, std::vector<Vertex>, Vertex::Hash> CopyRecord;
169
170 // Resolve the non-manifoldness of a vertex that is denoted by an incoming halfedge.
171 // @param h The halfedge pointing to the non-manifold vertex.
172 // Return the number of vertex copies.
173 std::size_t resolve_non_manifold_vertex(Halfedge h, SurfaceMesh *mesh, CopyRecord &copy_record);
174
175 private:
176 SurfaceMesh *mesh_;
177
178 // faces with less than three vertices
179 std::size_t num_faces_less_three_vertices_;
180
181 // faces with duplicate vertices
182 std::size_t num_faces_duplicate_vertices;
183
184 // faces with out-of-range vertex indices
185 std::size_t num_faces_out_of_range_vertices_;
186
187 // faces with unknown topology
188 std::size_t num_faces_unknown_topology_;
189
190 // record for linking a face to the mesh
191 CopyRecord copied_vertices_for_linking_;
192 // all copy record
193 CopyRecord copied_vertices_;
194
195 // The actual vertices after the face was successfully added to the mesh.
196 std::vector<Vertex> face_vertices_;
197
198 // A vertex property to record the original vertex of each vertex.
199 SurfaceMesh::VertexProperty <Vertex> original_vertex_;
200
201 // The record of all halfedges (each associated with a valid face) originating from a vertex.
202 // This is used for fast query of duplicate edges. All vertices are their original indices.
203 // - first: the index of a vertex
204 // - second: the indices of the target vertices
205 std::unordered_map<int, std::vector<int> > outgoing_halfedges_;
206 };
207
208}
209
210#endif // EASY3D_CORE_SURFACE_MESH_BUILDER_H
~SurfaceMeshBuilder()
Destructor.
Definition surface_mesh_builder.cpp:49
SurfaceMesh::Vertex Vertex
Vertex type.
Definition surface_mesh_builder.h:57
Face add_face(const std::vector< Vertex > &vertices)
Add a face to the mesh.
Definition surface_mesh_builder.cpp:272
SurfaceMesh::Face Face
Face type.
Definition surface_mesh_builder.h:59
void end_surface(bool log_issues=true)
Finalize surface construction.
Definition surface_mesh_builder.cpp:69
const std::vector< Vertex > & face_vertices() const
Get the actual vertices of the previously added face.
Definition surface_mesh_builder.h:137
SurfaceMesh::Halfedge Halfedge
Halfedge type.
Definition surface_mesh_builder.h:58
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:359
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:354
Vertex add_vertex(const vec3 &p)
Add a vertex to the mesh.
Definition surface_mesh_builder.cpp:228
SurfaceMeshBuilder(SurfaceMesh *mesh)
Constructor that initializes the builder with a given surface mesh.
Definition surface_mesh_builder.cpp:44
void begin_surface()
Begin surface construction.
Definition surface_mesh_builder.cpp:54
A halfedge data structure for polygonal meshes of 2-manifold.
Definition surface_mesh.h:51
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
Definition surface_mesh.h:191
This type represents a halfedge (internally it is basically an index).
Definition surface_mesh.h:155
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135