Let's start
We can first declare a new Triangle Mesh and load a ply file:
#include <vclib/meshes.h>
#include <vclib/load_save.h>
A class representing a box in N-dimensional space.
Definition box.h:46
void loadMesh(MeshType &m, const std::string &filename, MeshInfo &loadedInfo, const LoadSettings &settings, LogType &log=nullLogger)
Loads a mesh from a file with the given filename and stores it in the given mesh object....
Definition load_mesh.h:95
Or:
The TriMesh data structure has containers of Vertices and Faces, plus some other info like Bounding Box, Transform Matrix, ...
You can find more info about the TriMesh class and other built-in classes in the Built-in Meshes page and the list of them in the Meshes group.
We can, for example, print some statistics and update the bounding box component of the mesh after that it has been loaded:
#include <vclib/algorithms.h>
std::cout <<
"Min bb: " << myTriMesh.boundingBox().
min() <<
"; "
<<
"Max bb: " << myTriMesh.boundingBox().
max() <<
"\n";
std::cout << "Vertex number: " << myTriMesh.vertexNumber() "; "
<< "Face number: "<< myTriMesh.faceNumber() "\n";
PointT & max()
Returns a reference to the maximum point of the box.
Definition box.h:104
PointT & min()
Returns a reference to the minimum point of the box.
Definition box.h:90
void updateBoundingBox(MeshType &m)
Updates the bounding box of the mesh.
Definition bounding_box.h:41
Access Elements
We can access to the elements (Vertices, Faces, ...) of the mesh by index:
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:55
We can also iterate over Vertices and over Faces of the mesh:
bar.setZero();
for (const vcl::TriMesh::Vertex& v : myTriMesh.vertices()) {
bar += v.position();
}
bar /= myTriMesh.vertexNumber();
for (const vcl::TriMesh::Face& f : myTriMesh.faces()) {
bool up = true;
for (const vcl::TriMesh::Vertex* v : f.vertices()) {
if (v->position().y() < bar.
y())
up = false;
}
if (up) {
std::cout << f.index() << " is above the y barycenter\n";
}
}
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:150
Note that iterating over the vertices of the faces of the Mesh returns the vertices/faces/elements objects by reference, while iterating over the vertices of the face (or, in general, adjacencies of a specific element) returns the pointer to an element that is stored inside the same Mesh. Note also that this pointer may be nullptr
: in this small example we assume that the mesh is consistent meaning that this cannot happen.
Add, set and delete Elements
We can create from scratch a mesh:
mesh.addVertices(2);
mesh.addVertices(p0, p1, p2, p3, p4);
mesh.addFace(0, 1, 2);
uint fid = mesh.addFace();
mesh.face(fid).setVertices(&mesh.vertex(2), &mesh.vertex(1), &mesh.vertex(3));
Point3< double > Point3d
A convenience alias for a 3-dimensional Point with double-precision floating-point components.
Definition point.h:779
Components
Each data that is stored in a mesh or in a element is called "component". For example, the position of a vertex is a component, the normal of a face is a component, the color of a vertex is a component, and so on.
Depending on how the Element is defined, the components can be stored in different ways, and they can be optional: that means that a component can be enabled or disabled at runtime, using the required memory only when it is needed.
For example, in the TriMesh class, the Vertex and Face elements have their Color component optional, and disabled by default. We can enable them by calling:
myTriMesh.enablePerVertexColor();
myTriMesh.enablePerFaceColor();
Accessing to disabled components is not allowed, and will cause undefined behavior.