About STL Slicing and Extracting the Inner Loops data

Hi there,

I started working with VTK recently.
My objective is to extract the contour points on the cutting plane when sliced through a STL (Standard Tessellation Language) file.

I kind of achieved it by using vtkCutter, but the problem is, I am not getting the points in an ordered way (like, in the order of formation of the polygon).

I even tried using vtkPlaneCutter and the result is the same…

The final output that was expected is to get the contour points in and orderly fashion which makes the polygon… and the second thing is, if the sliced layer has holes(inner loops) inside them, then how to get those contour points seperately… you can view it in the image below…
image
The gray color means solid and I need the contour points of the red rectangle, green circle and the green rectangle separately…

Any advices regarding, the classes and functions that can be useful to approach this problem, would be very much helpful…

Thanks!

1 Like

The vtkStripper filter can order the points in each outline and create a polyline for each of them. Use the JoinContiguousSegmentsOn() option of this filter.

The vtkConnectivityFilter can label/color the outlines or extract them.

Edit: The filter vtkContourLoopExtraction, which I just recently discovered, should be able to do the ordering and extraction of the contours in one step. With this filter, you must supply a normal in order to separate interior loops from outer loops.

The sorting of the contour points has been taken care of… thanks to you!

But the another problem, like I said, “To separate the points based on the contour data…”, this, I am unable to figure out… I tried for stl file… And you can see the picture of code and the output data below…
I used “connectivityFilter->GetNumberOfExtractedRegions()” to give the contour numbers… but am unable to extract the points based on the contour…

CODE:

OUTPUT:

As you can see, the points are again getting repeated for the “contour1” also… Actually the total 16 points has 8 from contour0 and another 8 from contour1… They are getting repeated again…

Is there any function that can separate these contour points based on the contour… or Am I missing something?

If your goal is just to get the points from the contours, rather than to create a new dataset from each contour, take a look at this example, specifically at the code that starts with numberOfLines = stripper->GetOutput()->GetNumberOfLines().

The duplicated contours are probably due to the STL data itself. Sometimes STL data will have duplicated triangles, where two triangles have the same points or nearly the same points, either with the same ordering or opposite ordering. This can cause duplicated lines when cutting. Sometimes vtkCleanPolyData (either before or after cutting) can help.

Let me make my objective clear…

MY OBJECTIVE:
To get the contour points (including the outerContours and InnerContours(if any)) to be stored in a “vector<vector<'Points>>” for further processes…

WHAT I DID TILL NOW:

  • I Read and stored a STL file into a “vtkSmartPointer<'vtkPolyData> inputPart
  • Used “vtkCutter”. By setting a plane and giving the above inputPart as the source I updated it.
  • Then used vtkStripper, connected the vtkCutter to it, and then used the command JoinContiguousSegmentsOn() and then updated it.
  • After that I used,
    stripper->GetOutput()->GetNumberOfLines()
    stripper->GetOutput()->GetNumberOfCells()
    stripper->GetOutput()->GetNumberOfPolys()
  • Among the above one’s Polys is always showing the value of zero no matter what stl I use
  • I was able to get the value of number of Lines present using → GetNumberOfLines()
  • I stored the lines in a cell array as shown:
    vtkCellArray cells = stripper->GetOutput()->GetLines()
  • Then I ran the below shown loop…

  • With this I was able to print the lines in closed ordered way for simple stl files… but, the real problem is I am interested to extract the polygons(closed loop lines)
  • When am giving a complex stl file… its first layer has 2 loops (1 outerloop and 1 innerloop), but it is giving me the lines which do not make a closed polygon…

WHAT I DO REQUIRE:

  • I want the points of the loops… closed polygons extracted from the vtkCutter object.
  • I have to iterate through each of those polygons(outerloop and innerloops(holes)) and extract the points from them.
  • As you suggested to use vtkContourLoopExtraction… I was not knowing how to get the output (ie, the closed polygons from the it).
  • I was unable to find the any examples to know how to use vtkContourLoopExtraction

If you have any suggestions and examples to know about the class more and to achieve my goal, please share them…

Thanks!

For vtkContourLoopExtraction, just try using it the same way that you use vtkStripper. The only major difference is that, by default, vtkContourLoopExtraction will output the contours as Polys rather than Lines. Since this filter was designed specifically for generating contours, it will probably give better results than vtkStripper.

How can I know the number of contours(polys) present in it… and how to iterate through them?

Use the code that you pasted above. With the cell iterator.

Even after using the vtkContourLoopExtraction and vtkStripper the contour generation is happening as shown in the video below…

This video is generated using vtkStripper data!

Complete Image (After Complete Formation):

  • There is 1 curved hole, as shown… and 1 large closed loop and 4 small closed loops… These are to be iterated…

  • When I used vtkContourLoopExtractor()->GetOutput()->GetPolys(), I got around 25 polys… and when I used stripperLines, I got around 19 lines.

  • But the vtkContourLoopExtraction has more duplicates and unwanted polyLines… Moreover the polys are not closing even when I used the ContourLoopExtraction->SetLoopClosureToAll() .

  • The vtkStripper is giving the loops closed but as shown in the video it is generating the polylines. (some of them are open and some are closed)

With reference to the above image, all I am expecting to get are 6 polygons, which when iterated gives the individual polygons in a connected way and a way to identify the inner polygon separately.

How to achieve this… I am getting to understand that my solution comes from any of vtkStripper or vtkContourLoopExtraction, but I think am missing something…

Help me out in finding the suitable function or class or iterator or anything which can accomplish my task…!

Thanks!!!

Try using vtkCleanPolyData on the STL data. With vtkCleanPolyData, try a few different values for SetTolerance(), e.g. 1e-7, 1e-5, 0.001, 0.1. You can also try vtkCleanPolyData before vtkContourLoopExtraction.

To find the outer vs. inner contours, use SetNormal() with vtkContourLoopExtraction. Use the same normal as your cut plane. Then, after you get the loop polygons, check to see if their normals point in the same direction as this normal, or if they point in the opposite direction. The direction of the normal should indicate whether it is an inner loop or an outer loop, if vtkContourLoopExtraction extracted the loops correctly.

1 Like

Thanks for your help… Now the polygons are getting extracted perfectly!

But,

  1. The time taken to read the stl files is increasing as the file’s size keeps on increasing… Is there a way to speed up this process!

  2. And moreover, the vtkCleanPolyData class is consuming more time. vtkCleanPolyData is used after the reading of stl file, and the stlReader output is connected to vtkCleanPolyData… Is there a way to increase the speed of this process too?

As I recommended above, you can try running vtkCleanPolyData just before vtkContourLoopExtraction, instead of running it on the output of the STL reader. Did you try this? It might be faster.

Also try vtkStaticCleanPolyData - it’s much faster if you are running on a multithreaded machine

Thanks for your suggestions…

  • David, I am using vtkStripper, and that stipper is in a loop, where multiThreaded operation takes place… and when ever I place vtkCleanPolyData before the vtkStripper, it is throwing an error and stopping the process. So I kept it out of the threaded loop, next to the vtkSTLReader.

Now the cleaning process is fast… thanks to vtkStaticCleanPolyData

But,
I have a loop which runs around thousands of times. In that loop the following process takes place:

  1. Setting of Slice Plane (vtkPlane)

  2. Creating Cutter (vtkCutter)

  3. Creating Stripper and connecting PlaneCutter to it (vtkStripper)

  4. Storing the lines from the PlaneCutter->Output() into a vtkCellArray

  5. Iterating through them to extract the points (vtkCellArrayIterator)

The above process happens for every iteration of the loop… I used multi-threading concept and used 8 threads for this process, even though this loop is running very slow when compared to what I am expecting…

  • What classes or functions can help me increase the speed of this loop?
  • Is there any vtk Class/Function in the loop, hindering the speed of the process…?

If you just need to cut the same STL many times, you can do this with just one vtkPlane and one vtkCutter in one single step. Use the SetValue() method of vtkCutter to set all your desired cut locations (relative to the location of your vtkPlane). It will do all the cuts at once. Be sure to read the vtkCutter docs carefully to properly understand how SetValue() works.

The Non-Manifold Edges are the one’s that are leading to false closed polygons. How to remove those non-manifold edges from the stl file…
I already tried using the vtkFeatureEgdes… I connected in the following order:

  1. vtkSTLReader (for reading stl)

  2. vtkStaticCleaner (for cleaning stl, with tolerance of 1e-7)

  3. vtkFeatureEdges (with NonManifoldEdgesOff())

  4. vtkPlaneCutter (to do the slicing)

  5. vtkStripper (to get the closed lines using JoinContiguousSegmentsOn())

  6. Iterated through those lines using an vtkCellArrayIterator()

I am getting the lines throught vtkFeatureEdges->Output()->GetNumberOfLines(),
But, at vtkStripper->GetOutput()->GetNumberOfLines() is always giving zero… and the outcome is not iterating. How to overcome this…

Are there any other ways of Fixing an STL file using vtk which is efficient and less time consuming in removing the Non-Manifold Edges?

How to fix STLs having self Intersections and non manifold edges?