Handling STL-files en Poly_Triangulation

Dear developers and users,

I need to have the ability of importing STL-files in OCCT. The files can be of large magnitude. The 'StlAPI' and 'read_stl_file' are both too slow. RWStl does the job, but gives back a poly_triangulation. I've tried to convert each poly_triangle into a face, but the creation of that many faces causes poor results. 

Next thing I've tried is creating a face from all the triangles lying in the same plane. This works well on rectangle shaped objects, but very poor on 'curved' (stl is never really curved) objects.

I'm now trying to make a face from triangles that lie approximately in the same direction with the help of BRep_Builder. Result is now that the faces are created relatively easy, but when trying operations on the shape, the program crashes. Reason for this is because only the triangles are known, not the wire that defines the shape.

My question now, is there, in OCCT, a method to rapidly go from an stl-file to a TopoDS_Shape? The triangulation is not important to remember, what counts is the shape.

Thanks in advance,
Thieme

Francois Lauzon's picture

Hello Thieme,

stl object is the same as a Mesh/Triangulation, that's why you have a Poly_Triangulation as a result.  So you should either work with the triangulation directly, of use some of the OCC algos to build a surface from the mesh (which my not be that easy, depending on the kind of mesh/stl shape you have).

Have a look at GeomPlate_MakeApprox/GeomPlate_Surface (there is an example of how to use it in OCC MFC Samples)

If you want to go in that direction (there is also a more powerfull OCC Component https://www.opencascade.com/components/surfaces-from-scattered-points-co...).

Best regards,

François.

Kostadin Vrantzaliev's picture

Dear Thieme, you are trying in general to move from thousands of triangles shapes into surfaces. It is fundamental problem. 

Imagine that you have a picture (bitmap) and you want to use it as a vector file format in adobe or similar (SVG etc). So it is the same problem. 

This is what is called reverse engineering - converting from discrete representation of the world (STL) into vector (BRep). We have been working on solutions for many years, but I doubt there is a straight forward solution to your problem - especially if you bring thousands and millions of triangles. So you have to approximate and interpolate the input information.

I hope this info makes some sense for you.

Kostadin

Thieme Vandeput's picture

Dear Francois and Kostadin,

First of all, I'm very thankfull for the quick responses! I am aware of the problem, converting millions of triangles takes time and resources. The solution I'm working on has to be very accurate, approximating might not be the way to go here (unless you can approximate very closely to the original shape).

In the BRep_Builder, there are the functions:

void  MakeFace (TopoDS_Face &F, const HandlePoly_Triangulation > &T) const
void  MakeEdge (TopoDS_Edge &E, const HandlePoly_PolygonOnTriangulation > &N, const HandlePoly_Triangulation > &T) const

The 'MakeFace' function can visualize the stl meshes instantly, but it seems there is no information of the structure itself. Isn't there a way to distract information (like outerwire for example) from the faces built with the 'MakeFace' function? Then you could divide the polytriangles according to the direction and bundle them together in one face. The most amount of time is going to the creation of faces, so less faces result in faster converting time. One traingle would not result in one face, one face would be build with many poly_triangles. This is done really fast with the function 'MakeFace'. So isn't there a way to get the information from this faces you need? I'm assuming the information has to be found somewhere because they can be displayed and are displayed instantly.

Kind regards,
Thieme

Kirill Gavrilov's picture

Indeed, TopoDS_Face created from Poly_Triangulation is what STL, OBJ and glTF readers in OCCT do provide by default. This could be structured information (object is split into multiple Poly_Triangulation) or not (single Poly_Triangulation for entire Solid) depending on how reader from specific format is implemented and what information is stored in file itself. In this context, BRep_Builder::MakeFace() is just a low-level utility creating a TopoDS_Face object - it is not supposed to perform any algorithms or reconstruct face boundaries, for example.

Defining TopoDS_Wire and TopoDS_Edge boundaries in form of Poly_PolygonOnTriangulation will make definition of TopoDS_Shape more complete, but will require extra steps. Tools like Poly_Connect may help to find free edges on triangulation, but this is not the same as reconstructing boundaries.

While triangulation-only TopoDS_Shape geometry is supported by OCCT, there are fundamental gaps in this support - many algorithms handle only analytical surfaces / curves, making them useless for triangulation-only geometry. There is some progress in this direction which you can see in this issue on Bugtracker - visualization, geometry properties and some other algorithms now handle such geometry; but still there is a lot of algorithms that don't.

Thieme Vandeput's picture

I will write my own algorithms then and use OCCT for visualizing purposes only. Thanks for your answers!

Thieme