Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_104_SurfaceMesh

Constructing a SurfaceMesh (a tetrahedron) from vertices and face connectivity, two ways.

Overview

This is the first surface-mesh tutorial. Where a PointCloud is just a bag of points, a SurfaceMesh also records how the points are connected into faces. We build the simplest closed surface — a tetrahedron with 4 vertices and 4 triangular faces — and print its element counts. The same mesh is built twice, once with each of Easy3D's two construction APIs, so you can compare them. It is console-only; later tutorials (105/106/107) reuse this tetrahedron.

The technique

SurfaceMesh is a halfedge data structure for orientable 2-manifold polygonal meshes. Every edge is split into two oppositely oriented halfedges, and each halfedge stores links to its target vertex, its opposite twin, the next halfedge around the face, and the face it bounds. Those pointers make adjacency queries (neighbors of a vertex, faces around an edge, ...) cheap and local — see Tutorial_105_SurfaceMesh_Connectivity. The structure assumes a manifold: each edge is shared by exactly two faces (a boundary edge pairs with a null face) and the faces around a vertex form a single fan. Because of that assumption there are two ways to build a mesh: (1) call SurfaceMesh::add_vertex()/add_triangle() directly, which is only safe when you already know the input is manifold; or (2) use SurfaceMeshBuilder, which detects and repairs non-manifold configurations as it goes and is the recommended default. Faces must be given a consistent winding (counter-clockwise seen from outside) so the surface stays orientable. A tetrahedron has V=4, F=4 and E=6, satisfying Euler's formula V - E + F = 2 for a closed sphere- like surface.

Easy3D API

Running it

Console-only. With the default option (2) it prints "#face: 4", "#vertex: 4", "#edge: 6". Change the 'option' variable to 1 to build the same mesh through the direct SurfaceMesh API and observe identical counts. The mesh is a local object, so it is freed when main returns.

1
27#include <easy3d/core/surface_mesh.h>
28#include <easy3d/core/surface_mesh_builder.h>
29#include <easy3d/util/initializer.h>
30
79using namespace easy3d;
80
81int main(int argc, char **argv) {
82 // initialize Easy3D.
83 initialize();
84
85 // Easy3D provides two options to construct a surface mesh.
86 // - Option 1: use the add_vertex() and add_[face/triangle/quad]() functions of SurfaceMesh. You can only choose
87 // this option if you are sure that the mesh is manifold.
88 // - Option 2: use the SurfaceMeshBuilder that can resolve non-manifoldness during the construction of a mesh. This
89 // is the default option in Easy3D and client code is highly recommended to use SurfaceMeshBuilder.
90
91 // You can easily change an option.
92 const int option = 2;
93
94 // In this example, we create a surface mesh representing a tetrahedron (i.e., 4 triangle faces, 4 vertices).
95 //
96 // v0
97 // /|\
98 // / | \
99 // / | \
100 // v1 /_ _|_ _\ v2
101 // \ | /
102 // \ | /
103 // \ | /
104 // v3
105 //
106 const std::vector<vec3> points = {vec3(0, 0, 0), vec3(1, 0, 0), vec3(0, 1, 0), vec3(0, 0, 1)};
107
108 // Create a surface mesh
109 SurfaceMesh mesh;
110
111 if (option == 1) { // Option 1: use the built-in functions of SurfaceMesh.
112 // Add vertices
113 SurfaceMesh::Vertex v0 = mesh.add_vertex(points[0]);
114 SurfaceMesh::Vertex v1 = mesh.add_vertex(points[1]);
115 SurfaceMesh::Vertex v2 = mesh.add_vertex(points[2]);
116 SurfaceMesh::Vertex v3 = mesh.add_vertex(points[3]);
117 // Add faces. Each triangle lists its corners in a consistent (outward) winding; the
118 // direct API trusts you that the result is manifold and does no repair.
119 mesh.add_triangle(v0, v1, v3);
120 mesh.add_triangle(v1, v2, v3);
121 mesh.add_triangle(v2, v0, v3);
122 mesh.add_triangle(v0, v2, v1);
123 }
124
125 else if (option == 2) { // Option 2: use SurfaceMeshBuilder.
126 // Add vertices
127 SurfaceMeshBuilder builder(&mesh);
128 builder.begin_surface();
129 SurfaceMesh::Vertex v0 = builder.add_vertex(vec3(0, 0, 0));
130 SurfaceMesh::Vertex v1 = builder.add_vertex(vec3(1, 0, 0));
131 SurfaceMesh::Vertex v2 = builder.add_vertex(vec3(0, 1, 0));
132 SurfaceMesh::Vertex v3 = builder.add_vertex(vec3(0, 0, 1));
133 // Add faces
134 builder.add_triangle(v0, v1, v3);
135 builder.add_triangle(v1, v2, v3);
136 builder.add_triangle(v2, v0, v3);
137 builder.add_triangle(v0, v2, v1);
138 // Finalize the connectivity. The 'false' argument suppresses the log of any non-manifold
139 // issues the builder had to repair (there are none for this clean tetrahedron).
140 builder.end_surface(false);
141 } else
142 std::cerr << "option must be 1 or 2" << std::endl;
143
144 // Report the topology: a tetrahedron has 4 faces, 4 vertices and 6 edges (V - E + F = 2).
145 std::cout << "#face: " << mesh.n_faces() << std::endl;
146 std::cout << "#vertex: " << mesh.n_vertices() << std::endl;
147 std::cout << "#edge: " << mesh.n_edges() << std::endl;
148
149 return EXIT_SUCCESS;
150}
A helper class for constructing manifold surface mesh models.
Definition surface_mesh_builder.h:55
void end_surface(bool log_issues=true)
Finalize surface construction.
Definition surface_mesh_builder.cpp:69
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
void begin_surface()
Begin surface construction.
Definition surface_mesh_builder.cpp:54
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
Definition collider.cpp:182
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135
See also
SurfaceMesh, SurfaceMeshBuilder, Tutorial_105_SurfaceMesh_Connectivity, Tutorial_106_SurfaceMesh_Property