I have a mesh that has vertices, indices and colours. I want to crop the mesh using a cropbox. I can remove the correct points and colours easily, but is there a way to know which indices relate to which vertices so when I remove the vertices I don’t want I can also remove the indices?
Meshes usually are data-vise a collection of vertices and a collection of faces.
The vertices are organised in a flat collection and their order within this collection doesn’t really matter. They are basically three-dimensional points.
Each face within the faces collection is a sub-collection of indices of vertices, forming the face in question usually in a counter-clockwise order.
Now, if you select certain vertices, delete them, and simply delete their indices in all faces that they belong to, you’ll end up with an invalid mesh, where some vertices have a valence smaller than 2, which in a quad mesh is impossible (also in a tri mesh)! All vertices should be connected to at least two edges, and most are to three or even four. A vertex with a valence of greater than four would be considered an ngon vertex.
What instead you want to do, is select faces, delete these, and delete the related vertices, IF they are not part of any other face that is not be deleted. And then you also want to reconstruct or rebuild your mesh, by adapting the individual faces to the new vertex indices. Remember by deleting vertices, you have changed the order and size of the vertices collection!
Maybe this last part is done automatically, if you use
ofMesh.removeVertex(). I’m not sure though!
I like to work with quad meshes, since many concepts are easier to imagine than with tri meshes.
@p1r4t3b0y thanks for the detailed reply, I was hoping there was a way to do this a bit easier, but laziness should not stop me. Quads are definately easier to manage than triangles but I am a bit stuck there already.
I did just see that ofMesh has a method getUniqueFaces(), it is a bit unclear how this works as it says:
**Returns** : the mesh as a vector of unique ofMeshFace_s a list of triangles that do not share vertices or indices
I did not try it yet, but pretty much all of the triangles in my mesh have shared vertices, let’s see what I get.
But I will play with this, and see if the faces maintain colours or texture coordinates and it could be a good way around what I need to do.
If performance is not an issue, you can make triangles with unique vertices and then add them to the mesh. Even if some vertices have the same X Y Z values as other vertices, they are unique vertices. The triangles will not have shared vertices.
Use triangles instead of triangle strip:
Hi @fresla, the of3dPrimitives examples uses .getUniqueFaces() in ofApp::draw() to get a std::vector of ofMeshFace. These triangles are then manipulated, and the meshes of the various primitives are then .setFromTriangles(). After deleting vertices, you’d have to clear the indices and then add them again.
Edit: you’d MAYBE have to clear the indices and then add them again. Sorry.
Ok this is great, I will try it out, one question, if I set the mesh from triangles wont the indices be implicitly in the triangles (as they are connected points)?
I don’t have an easy way of rebuilding the indices once I start deleting points.
I think you’ll have to try it and see what happens with the indices when it draws. Rebuilding them is rough. If they don’t come with the triangles (the ofMeshFace, which they might and hopefully will), you could try keeping track of the indexes of the vertices you delete, and then loop thru the indices and delete the matching values. But, I’m thinking that each ofMeshFace might be its own mini “complete mesh” replica (vertices, indices, texCoords, etc) of the mesh it gets extracted from, and .setFromTriangles() just puts them all back into a single mesh, ordered according to their place in the vector.
I was just working on this kind of thing yesterday and was tempted to try deleting some ofMeshFace to see what happens, but ran out of time and energy. So let us know how it goes!
@TimChi @EdwardLiv @p1r4t3b0y
Thanks everyone for the help, after some testing with ofMesh.getUniqueFaces(), it solves my problem perfectly. The mesh is broken into faces (in my case triangles) that retain all the data they had, so texture coordinates, colours and normals, the indices are inherently part of the triangles so they are maintained.
This lets me start with my original mesh, get the faces and then iterate through each face and check the vertices and remove any that I don’t want (they can even be surrounded by other faces). I did some testing taking out random triangles and saving the new mesh as a ply with colours that were added to the original mesh and it is perfect does not show errors when I check it with meshlab.