<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">Here is an example of how to clip a surface with vtkClipClosedSurface based on input from David Gobbi. I wonder if there is anything that can be done to make this run faster? Part of the problem is also the large amount of triangles generated by the clipper. Is there any way to reduce the number of triangles in the cap?<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">Thanks!<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">Dov<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">
</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><pre class=""><span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">An example of how to create a slice from the Stanford Bunny stl:</span>
<span style="color:rgb(178,34,34)">#</span>
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)"><a href="http://www.thingiverse.com/download:15654">http://www.thingiverse.com/download:15654</a></span>
<span style="color:rgb(178,34,34)">#</span>
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">Dov Grobgeld</span>
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">2014-02-11 Tue</span>
<span style="color:rgb(178,34,34)">#</span>
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">This file is in the public domain.</span>
<span style="color:rgb(160,32,240)">import</span> vtk
sr = vtk.vtkSTLReader()
Filename = <span style="color:rgb(0,139,0)">'bunny-flatfoot.stl'</span>
sr.SetFileName(Filename)
sr.Update()
BBox = sr.GetOutput().GetBounds()
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">Cut the bunny straight at z=0.6 * z-height</span>
SlicePos = 0.6*(BBox[4]+BBox[5])
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">Define the cutting plane</span>
plane=vtk.vtkPlane()
plane.SetOrigin(0,0,SlicePos)
plane.SetNormal(0,0,-1)
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">Need a plane collection for clipping</span>
planeCollection = vtk.vtkPlaneCollection()
planeCollection.AddItem(plane)
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">The clipper generates a clipped polygonial model</span>
clipper = vtk.vtkClipClosedSurface()
clipper.SetClippingPlanes(planeCollection)
clipper.SetInputConnection(sr.GetOutputPort())
clipper.SetGenerateFaces(1)
clipper.SetScalarModeToLabels()
clipper.Update()
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">Threshold the polygonial model to extract only the end caps.</span>
threshold = vtk.vtkThreshold()
threshold.SetInputConnection(clipper.GetOutputPort())
threshold.ThresholdByUpper(1)
threshold.SetInputArrayToProcess(0,0,0,vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS_THEN_CELLS,<span style="color:rgb(0,139,0)">'Labels'</span>)
threshold.Update()
<span style="color:rgb(178,34,34)"># </span><span style="color:rgb(178,34,34)">Output the number of triangles. Outputs 607</span>
<span style="color:rgb(160,32,240)">print</span> <span style="color:rgb(0,139,0)">"Got"</span>, threshold.GetOutput().GetNumberOfCells(), <span style="color:rgb(0,139,0)">"triangles"</span></pre></div>
</div>