[vtkusers] Slicing with vtkClipClosedSurface example

Dov Grobgeld dov.grobgeld at gmail.com
Tue Feb 11 09:58:57 EST 2014


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?

Thanks!
Dov
​

# An example of how to create a slice from the Stanford Bunny stl:##
 http://www.thingiverse.com/download:15654## Dov Grobgeld# 2014-02-11
Tue## This file is in the public domain.
import vtk

sr = vtk.vtkSTLReader()
Filename = 'bunny-flatfoot.stl'
sr.SetFileName(Filename)
sr.Update()
BBox = sr.GetOutput().GetBounds()
# Cut the bunny straight at z=0.6 * z-height
SlicePos = 0.6*(BBox[4]+BBox[5])
# Define the cutting plane
plane=vtk.vtkPlane()
plane.SetOrigin(0,0,SlicePos)
plane.SetNormal(0,0,-1)
# Need a plane collection for clipping
planeCollection = vtk.vtkPlaneCollection()
planeCollection.AddItem(plane)
# The clipper generates a clipped polygonial model
clipper = vtk.vtkClipClosedSurface()
clipper.SetClippingPlanes(planeCollection)
clipper.SetInputConnection(sr.GetOutputPort())
clipper.SetGenerateFaces(1)
clipper.SetScalarModeToLabels()
clipper.Update()
# Threshold the polygonial model to extract only the end caps.
threshold = vtk.vtkThreshold()
threshold.SetInputConnection(clipper.GetOutputPort())
threshold.ThresholdByUpper(1)
threshold.SetInputArrayToProcess(0,0,0,vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS_THEN_CELLS,'Labels')
threshold.Update()
# Output the number of triangles. Outputs 607print "Got",
threshold.GetOutput().GetNumberOfCells(), "triangles"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140211/8bc01c00/attachment.html>


More information about the vtkusers mailing list