[vtkusers] Clipping two vtkPolyData

do ba db001465 at gmail.com
Fri Aug 31 10:57:43 EDT 2018


Hello

I want to find all parts of a polydata (in my case a surface), which lies
inside a second polydate (in my case a volume).  In the following example i
tried to write a simple example. However, something with vtkClipPolyData
does not work. Do i make some mistake by creating the implicit  data set or
by using the vtkClipPolyData function?

Regards
D



import vtk

# Create volume
def mkVtkIdList(it):
      vil = vtk.vtkIdList()
      for i in it:
          vil.InsertNextId(int(i))
      return vil

ptsCoord= []
ptsCoord.append( ( 1.0,-1.0,-1.0) )
ptsCoord.append( ( 1.0, 1.0,-1.0) )
ptsCoord.append( (-1.0, 1.0,-1.0) )
ptsCoord.append( (-1.0,-1.0,-1.0) )
ptsCoord.append( ( 1.0,-1.0, 1.0) )
ptsCoord.append( ( 1.0, 1.0, 1.0) )
ptsCoord.append( (-1.0, 1.0, 1.0) )
ptsCoord.append( (-1.0,-1.0, 1.0) )
ptsIdxFaces = []
ptsIdxFaces.append( (0,1,2,3) )   # bottom face
ptsIdxFaces.append( (0,1,5,4) )   # side 1
ptsIdxFaces.append( (1,2,6,5) )   # side 2
ptsIdxFaces.append( (2,3,7,6) )   # side 3
ptsIdxFaces.append( (3,0,4,7) )   # side 4
ptsIdxFaces.append( (4,5,6,7) )   # top face
volume    = vtk.vtkPolyData()
points  = vtk.vtkPoints()
polys   = vtk.vtkCellArray()
scalars = vtk.vtkFloatArray()
for i in range(len(ptsCoord)):
    points.InsertPoint(i, ptsCoord[i])
for i in range(len(ptsIdxFaces)):
    polys.InsertNextCell( mkVtkIdList(ptsIdxFaces[i]) )
for i in range(len(ptsCoord)):
    scalars.InsertTuple1(i,i)
volume.SetPoints(points)
volume.SetPolys(polys)
volume.GetPointData().SetScalars(scalars)
writer = vtk.vtkXMLPolyDataWriter();
writer.SetFileName("R:/volume.vtp" );
if vtk.VTK_MAJOR_VERSION <= 5:
    writer.SetInput(volume)
else:
    writer.SetInputData(volume)
writer.Write()


# Create Surface
points = vtk.vtkPoints()
triangle = vtk.vtkTriangle()
triangles = vtk.vtkCellArray()

points.InsertNextPoint( 0.0,-1.5, 0.0 )
points.InsertNextPoint( 1.5, 0.0, 1.0 )
points.InsertNextPoint( 0.0, 1.5, 0.0 )
points.InsertNextPoint(-1.5, 0.0, 1.0 )
triangle.GetPointIds().SetId(0,0)
triangle.GetPointIds().SetId(1,1)
triangle.GetPointIds().SetId(2,2)
triangles.InsertNextCell(triangle)

triangle.GetPointIds().SetId(0,0)
triangle.GetPointIds().SetId(1,2)
triangle.GetPointIds().SetId(2,3)
triangles.InsertNextCell(triangle)
surface = vtk.vtkPolyData()
surface.SetPoints( points )
surface.SetPolys( triangles )
writer = vtk.vtkXMLPolyDataWriter();
writer.SetFileName("R:/surface.vtp" );
if vtk.VTK_MAJOR_VERSION <= 5:
    writer.SetInput(surface)
else:
    writer.SetInputData(surface)
writer.Write()


# Now, clip!
tri = vtk.vtkDelaunay3D()
tri.SetInputData(volume)
tri.BoundingTriangulationOff()

# vtkImplicitDataSet needs some scalars to interpolate to find
inside/outside
elev = vtk.vtkElevationFilter()
elev.SetInputConnection(tri.GetOutputPort())

implicit = vtk.vtkImplicitDataSet()
implicit.SetDataSet(elev.GetOutput())

clipper = vtk.vtkClipPolyData()
clipper.SetClipFunction(implicit)
clipper.SetInputData(surface)
clipper.InsideOutOn()
clipper.Update()

writer = vtk.vtkXMLPolyDataWriter();
writer.SetFileName("R:/clipper.vtp" );
if vtk.VTK_MAJOR_VERSION <= 5:
    writer.SetInput(clipper)
else:
    writer.SetInputData(clipper)
writer.Write()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180831/e78b624c/attachment.html>


More information about the vtkusers mailing list