Get triangles from solid shape


I want to load a solid from a step file, mesh/triangulate the shape and then I want to have the points of every triangle of this (surface) mesh.

I searched in the forum how to do this. I saw hints to use BRepMesh_IncrementalMesh or BRepMesh_FastDiscret, but it is not clear how to use it properly.

What I tried (with the help of OCCutils): I load the step file, I call BRepMesh_IncrementalMesh, I read all face and then all vertices of the single face

auto shape = reader.Shape(1);
BRepMesh_IncrementalMesh mesher(shape, 0.2);

for (auto& face : occutils::shape_components::AllFacesWithin(shape))
auto vertices = occutils::shape_components::AllVertexCoordinatesWithin(face);

If the mesher would have triangulated the soild, then every face in it should have 3 vertices. But the above small examploe doesn't work. I got much more vertices. So my thoughts seems to be wrong, obviously.

I also read about BRep_Tool::Triangulation. but I don't understand why I should use it. If BRepMesh_IncrementalMesh does the job, the solid should be triangulated anyway?!

So my question: how can I reach my goal? I need the triangles of the tesselated solid. The documentation of BRepMesh_IncrementalMesh is not really helpful. It doesn't really describe how it works.

Thanks and best regards


Mikhail Sazonov's picture

BRepMesh_IncrementalMesh makes a new representation of each TopoDS_Face. After that, you have triangulation for each face in your shape. In order to get this representation, you need to get the result of BRep_Tool::Triangulation() of each face. I don't know what is the function occutils. It is not the part of OCCT.

Marco's picture

OCCutils is a small 3rd party library to simplify some standard works in OCC ( Quite handy for some tasks.

The documentation of BRepMesh_IncrementalMesh states "Builds the mesh of a shape with respect of their correctly triangulated parts.". But it doesn't triangulate the shape at all, as I understood you.
So what exactly is BRepMesh_IncrementalMesh doing when the result isn't the triangulated mesh?

Mikhail Sazonov's picture

It does indeed :)
TopoDS_Face is a complex object that can have several representations, as a surface (Geom_Surface) and as a triangulation (Poly_Triangulation). That triangulation contains all nodes and triangles built by BRepMesh_IncrementalMesh. And the method BRep_Tool::Triangulation() returns that triangulated representation. So, you need to explore your solid on faces, and for each face get triangulation, and get triangles from there. Triangle itself is not a TopoDS_Face, it is just a triple of nodes indices that are stored in the triangulation. I hope it became clearer for you now ;)

Marco's picture

Yes! Thank you very much for the explanation!