$darkmode
To create a simple single triangle mesh or to add elements to an existing mesh you should use the AddVertices and AddFaces functions, elements are added at the end of the mesh. These functions returns a pointer to the first allocated element.
Adding element to a vector can cause reallocation, and therefore invalidation of any pointer that points to the mesh elements. These fucntion manage safely re-allocation and updating of pointers for all the pointers stored internally in the mesh (e.g. if you add some vertices and that causes a reallocation of the vertex vector, the pointers from faces to vertices will be automatically updated by the Allocator functions. You should never directly reallocate or resize the vertex or face vectors.
look to platonic.h for more examples.
Alternatively you can add single vertex and faces in a more compact way as follows:
If you keep internally some pointers to the mesh elements adding elements can invalidate them. In that case you should pass to the vcg::tri::Allocator functions a PointerUpdater to be used to update your private pointers.
The library adopts a Lazy Deletion Strategy i.e. the elements in the vector that are deleted are only flagged as deleted, but they are still there.
Note that the basic deletion functions are very low level ones. They simply mark as deleted the corresponding entries without affecting the rest of the structures. So for example if you delete a vertex with these structures without checking that all the faces incident on it have been removed you could create a non consistent situation. Similarly, but less dangerously, when you delete a face its vertices are left around so at the end you can have unreferenced floating vertices. Again you should never, ever try to delete vertexes by yourself by directly flagging deleted, but you must call the Allocator utility function. The following snippet of code shows how to delete a few faces from an icosahedron.
After such a deletion the vector of the faces still contains 20 elements (number of faces of an icosahedron) but the m.FN()
function correctly reports 18 faces. Therefore if your algorithm performs deletions it happens that the size of a container could be different from the number of valid element of your meshes:
Therefore when you scan the containers of vertices and faces you could encounter deleted elements so you should take care with a simple !IsD() check:
In some situations, particularly when you have to loop many many times over the element of the mesh without deleting/creating anything, it can be practical and convenient to get rid of deleted elements by explicitly calling the two garbage collecting functions:
After calling these function it is safe to not check the IsD() state of every element and always holds that:
FN()
or VN()
as end guards in the for; the following code snipped is WRONG: Given the intricate nature of the mesh itself it is severely forbidden any attempt of copying meshes as simple object. To copy a mesh you have to use the Append utility class:
In the vcg::tri::Append utility class there are also functions for copying only a subset of the second mesh (the selected one) or to append the second mesh to the first one.