[vtkusers] polyhedron in python

Meehan, Bernard MEEHANBT at nv.doe.gov
Thu Sep 11 12:08:58 EDT 2014


I thought I could fix it for you - but there is a bug in my program. You
need to write out a vtkUnstructuredGrid rather than a vtkPolyData if you
wanted to be able to select the polyhedron as a cell. I hadn't worked with
those before, so I figured that I would give it a shot. I think that the
face stream is wrong ... which is why no faces show up in ParaVew. You can
see the points with a glyph filter, but the facets don't show up. Anyone
out there have an idea of where I went wrong? (modified version of old
code below)

# I am using VTK 6.1.0
import vtk
# This is a Rhombic Dodecahedron.

# First, you need to store the vertex locations.
vertex_locations = vtk.vtkPoints()
vertex_locations.SetNumberOfPoints(14)
vertex_locations.SetPoint( 0, (-0.816497, -0.816497,  0.00000))
vertex_locations.SetPoint( 1, (-0.816497,  0.000000, -0.57735))
vertex_locations.SetPoint( 2, (-0.816497,  0.000000,  0.57735))
vertex_locations.SetPoint( 3, (-0.816497,  0.816497,  0.00000))
vertex_locations.SetPoint( 4, ( 0.000000, -0.816497, -0.57735))
vertex_locations.SetPoint( 5, ( 0.000000, -0.816497,  0.57735))
vertex_locations.SetPoint( 6, ( 0.000000,  0.000000, -1.15470))
vertex_locations.SetPoint( 7, ( 0.000000,  0.000000,  1.15470))
vertex_locations.SetPoint( 8, ( 0.000000,  0.816497, -0.57735))
vertex_locations.SetPoint( 9, ( 0.000000,  0.816497,  0.57735))
vertex_locations.SetPoint(10, ( 0.816497, -0.816497,  0.00000))
vertex_locations.SetPoint(11, ( 0.816497,  0.000000, -0.57735))
vertex_locations.SetPoint(12, ( 0.816497,  0.000000,  0.57735))
vertex_locations.SetPoint(13, ( 0.816497,  0.816497,  0.00000))

# Next, you describe the polygons that represent the faces using the vertex
# indices in the vtkPoints that stores the vertex locations. There are a
number
# of ways to do this that you can find in examples on the Wiki.

polygon_faces = vtk.vtkCellArray()

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  7)
q.GetPointIds().SetId(1, 12)
q.GetPointIds().SetId(2, 10)
q.GetPointIds().SetId(3,  5)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  7)
q.GetPointIds().SetId(1, 12)
q.GetPointIds().SetId(2, 13)
q.GetPointIds().SetId(3,  9)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  7)
q.GetPointIds().SetId(1,  9)
q.GetPointIds().SetId(2,  3)
q.GetPointIds().SetId(3,  2)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  7)
q.GetPointIds().SetId(1,  2)
q.GetPointIds().SetId(2,  0)
q.GetPointIds().SetId(3,  5)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  6)
q.GetPointIds().SetId(1, 11)
q.GetPointIds().SetId(2, 10)
q.GetPointIds().SetId(3,  4)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  6)
q.GetPointIds().SetId(1,  4)
q.GetPointIds().SetId(2,  0)
q.GetPointIds().SetId(3,  1)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  6)
q.GetPointIds().SetId(1,  1)
q.GetPointIds().SetId(2,  3)
q.GetPointIds().SetId(3,  8)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  6)
q.GetPointIds().SetId(1,  8)
q.GetPointIds().SetId(2, 13)
q.GetPointIds().SetId(3, 11)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0, 10)
q.GetPointIds().SetId(1, 11)
q.GetPointIds().SetId(2, 13)
q.GetPointIds().SetId(3, 12)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0, 13)
q.GetPointIds().SetId(1,  8)
q.GetPointIds().SetId(2,  3)
q.GetPointIds().SetId(3,  9)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  3)
q.GetPointIds().SetId(1,  1)
q.GetPointIds().SetId(2,  0)
q.GetPointIds().SetId(3,  2)
polygon_faces.InsertNextCell(q)

q = vtk.vtkQuad()
q.GetPointIds().SetId(0,  0)
q.GetPointIds().SetId(1,  4)
q.GetPointIds().SetId(2, 10)
q.GetPointIds().SetId(3,  5)
polygon_faces.InsertNextCell(q)

# Next you create a vtkPolyData to store your face and vertex information
that
# represents your polyhedron.
pd = vtk.vtkPolyData()
pd.SetPoints(vertex_locations)
pd.SetPolys(polygon_faces)

# If you wanted to be able to load in the saved file and select the entire
# polyhedron, you would need to save it as a vtkUnstructuredGrid, and you
would
# need to put the data into a vtkPolyhedron. This is a bit more involved
than
# the vtkPolyData that I used above. For a more in-depth discussion, see:
# http://www.vtk.org/Wiki/VTK/Polyhedron_Support

# Based on the link above, I need to construct a face stream:
face_stream = vtk.vtkIdList()
face_stream.InsertNextId(polygon_faces.GetNumberOfCells())
vertex_list = vtk.vtkIdList()

polygon_faces.InitTraversal()
while polygon_faces.GetNextCell(vertex_list) == 1:

  face_stream.InsertNextId(vertex_list.GetNumberOfIds())

  for j in range(vertex_list.GetNumberOfIds()):
    face_stream.InsertNextId(vertex_list.GetId(j))

ug = vtk.vtkUnstructuredGrid()
ug.SetPoints(vertex_locations)
ug.InsertNextCell(vtk.VTK_POLYHEDRON, face_stream)

#--------------#
# output stuff #
#--------------#
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName("rhombic_dodecahedron.vtk")
writer.SetInputData(ug)
writer.Write()

#---------------------#
# visualization stuff #
#---------------------#
# mapper = vtk.vtkPolyDataMapper()
# mapper.SetInputData(pd)
mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(ug)

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)

renw = vtk.vtkRenderWindow()
renw.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renw)

ren.ResetCamera()
renw.Render()
iren.Start()


On 9/10/14 2:34 PM, "George" <george.gerber at gmail.com> wrote:

>Hi,
>The example of Meehan works quite well.
>However, I see that each face of the polyhedron is listed as a cell. Is it
>possible to hide these face 'cells', so that when you select the
>polyhedron
>in paraview it lists only 1 cell (the polyhedron itself)?
>
>Best regards,
>George
>
>
>
>--
>View this message in context:
>http://vtk.1045678.n5.nabble.com/polyhedron-in-python-tp5728643p5728664.ht
>ml
>Sent from the VTK - Users mailing list archive at Nabble.com.
>_______________________________________________
>Powered by www.kitware.com
>
>Visit other Kitware open-source projects at
>http://www.kitware.com/opensource/opensource.html
>
>Please keep messages on-topic and check the VTK FAQ at:
>http://www.vtk.org/Wiki/VTK_FAQ
>
>Follow this link to subscribe/unsubscribe:
>http://public.kitware.com/mailman/listinfo/vtkusers
>




More information about the vtkusers mailing list