Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_112_PolyMesh

Constructing a PolyMesh (a single tetrahedron) — a volumetric mesh made of 3D cells.

Overview

This console tutorial builds the simplest polyhedral mesh: one tetrahedral cell from four vertices. It then prints the element counts, the position of every vertex, and the corner indices of the cell. It is the entry point to the PolyMesh series (113 connectivity, 114 properties, 115 I/O) and the 3D analogue of Tutorial_104_SurfaceMesh.

The technique

PolyMesh is a half-face data structure for polyhedral (volumetric) meshes. It lifts the halfedge idea of a SurfaceMesh one dimension up: the elements are vertices, edges, halffaces, faces and cells. A cell is a solid volume (a tetrahedron, hexahedron, or general polyhedron) bounded by polygonal faces; each face is represented by two oppositely oriented halffaces, one per adjacent cell, with normals pointing outward from their cell. A face on the boundary of the volume has a halfface whose opposite is invalid (the "outside"). This is the key difference from a SurfaceMesh, whose faces are 2D patches living in 3D space: a PolyMesh fills the interior with 3D cells, which is what volumetric simulation (FEM), tetrahedralization and solid modeling need. A single tetrahedron has 4 vertices, 6 edges, 4 triangular faces and 1 cell. add_tetra(v0,v1,v2,v3) creates that cell together with its faces and halffaces in one call.

Easy3D API

Running it

Console-only. It prints "#cell: 1", "#face: 4", "#vertex: 4", "#edge: 6", then a line with each vertex's coordinates and a line listing the cell's vertex indices. The mesh is a local object and is freed when main returns.

1
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/util/initializer.h>
29
73using namespace easy3d;
74
75int main(int argc, char **argv) {
76 // initialize Easy3D.
77 initialize();
78
79 // Create an empty polyhedral mesh. Unlike a SurfaceMesh it will hold 3D cells (volumes).
80 PolyMesh mesh;
81
82 // Add four vertices
83 auto v0 = mesh.add_vertex(vec3(-1.0, 0.0, 0.0));
84 auto v1 = mesh.add_vertex(vec3(0.0, 0.0, 1.0));
85 auto v2 = mesh.add_vertex(vec3(1.0, 0.0, 0.0));
86 auto v3 = mesh.add_vertex(vec3(0.0, 0.0, -1.0));
87
88 // Add the only tetrahedron. add_tetra() builds the cell plus its 4 triangular faces and the
89 // outward-oriented halffaces in one call, returning a Cell handle.
90 mesh.add_tetra(v0, v1, v2, v3);
91
92 // Report the topology: one tetrahedral cell has 4 faces, 4 vertices and 6 edges.
93 std::cout << "#cell: " << mesh.n_cells() << std::endl;
94 std::cout << "#face: " << mesh.n_faces() << std::endl;
95 std::cout << "#vertex: " << mesh.n_vertices() << std::endl;
96 std::cout << "#edge: " << mesh.n_edges() << std::endl;
97
98 // Print positions of vertices to std out
99 for (auto v : mesh.vertices())
100 std::cout << "coordinates of " << v << ": " << mesh.position(v) << std::endl;
101
102 // For each cell, list the indices of its corner vertices (a tetrahedron has four).
103 for (auto c : mesh.cells()) {
104 std::cout << "vertex indices of " << c << ": ";
105 for (auto v : mesh.vertices(c))
106 std::cout << v.idx() << " ";
107 std::cout << std::endl;
108 }
109
110 return EXIT_SUCCESS;
111}
Data structure representing a polyhedral mesh.
Definition poly_mesh.h:50
Definition collider.cpp:182
See also
PolyMesh, Tutorial_113_PolyMesh_Connectivity, Tutorial_114_PolyMesh_Property, Tutorial_104_SurfaceMesh