[vtkusers] polyhedron in python

George Gerber george.gerber at gmail.com
Sun Nov 2 23:09:03 EST 2014


Good day,
Thank you very much. It works!
Below the code for the latest vtk.
Best regards,
George

import vtk

# Create polyhedron (cube)

pointIds = vtk.vtkIdList()

pointIds.SetNumberOfIds(8)

for i in range(8):

pointIds.InsertNextId(i)

points = vtk.vtkPoints()

points.InsertNextPoint(-1.0,-1.0,-1.0)

points.InsertNextPoint( 1.0,-1.0,-1.0)

points.InsertNextPoint( 1.0, 1.0,-1.0)

points.InsertNextPoint(-1.0, 1.0,-1.0)

points.InsertNextPoint(-1.0,-1.0, 1.0)

points.InsertNextPoint( 1.0,-1.0, 1.0)

points.InsertNextPoint( 1.0, 1.0, 1.0)

points.InsertNextPoint(-1.0, 1.0, 1.0)


# List of pointIds that make up the face

faceList = [[0, 3, 2, 1],[0, 4, 7, 3],[4, 5, 6, 7],[5, 1, 2, 6],[0, 1, 5,
4],[2, 3, 7, 6]]

faceId = vtk.vtkIdList()

faceId.InsertNextId(6) # Number faces that make up the cell.

for face in faceList: # Loop over all the faces

faceId.InsertNextId(len(face)) # Number of points in face

[faceId.InsertNextId(i) for i in face] # Insert the pointIds for the face


ugrid = vtk.vtkUnstructuredGrid()

ugrid.SetPoints(points)

ugrid.InsertNextCell(vtk.VTK_POLYHEDRON, faceId)


writer = vtk.vtkXMLUnstructuredGridWriter()

writer.SetInputData(ugrid)

writer.SetFileName("polyhedron.vtu")

writer.SetDataModeToAscii()

writer.Update()


On 3 November 2014 01:48, Guðni Karl Rosenkjær <grosenkj at eos.ubc.ca> wrote:

> Hi George,
>
> In your example you are only defining a single face and I am pretty sure
> that a polyhedron requires a closed / "watertight" volume by definition.
>
> I am using vtk 5.10.1 with python 2.7 and the following example works for
> me (translation of C++ example Bill posted above).
>
> Code:
>   import vtk
>   # Create polyhedron (cube)
>   pointIds = vtk.vtkIdList()
>   pointIds.SetNumberOfIds(8)
>   for i in range(8):
>     pointIds.InsertNextId(i)
>   points = vtk.vtkPoints()
>   points.InsertNextPoint(-1.0,-1.0,-1.0)
>   points.InsertNextPoint( 1.0,-1.0,-1.0)
>   points.InsertNextPoint( 1.0, 1.0,-1.0)
>   points.InsertNextPoint(-1.0, 1.0,-1.0)
>   points.InsertNextPoint(-1.0,-1.0, 1.0)
>   points.InsertNextPoint( 1.0,-1.0, 1.0)
>   points.InsertNextPoint( 1.0, 1.0, 1.0)
>   points.InsertNextPoint(-1.0, 1.0, 1.0)
>
>   # List of pointIds that make up the face
>   faceList = [[0, 3, 2, 1],[0, 4, 7, 3],[4, 5, 6, 7],[5, 1, 2, 6],[0, 1,
> 5, 4],[2, 3, 7, 6]]
>   faceId = vtk.vtkIdList()
>   faceId.InsertNextId(6) # Number faces that make up the cell.
>   for face in faceList: # Loop over all the faces
>     faceId.InsertNextId(len(face)) # Number of points in face
>     [faceId.InsertNextId(i) for i in face] # Insert the pointIds for the
> face
>
>
>   ugrid = vtk.vtkUnstructuredGrid()
>   ugrid.SetPoints(points)
>   ugrid.InsertNextCell(vtk.VTK_POLYHEDRON, faceId)
>
>   writer = vtk.vtkXMLUnstructuredGridWriter()
>   writer.SetInput(ugrid)
>   writer.SetFileName("polyhedron.vtu")
>   writer.SetDataModeToAscii()
>   writer.Update()
> Code ends
>
> The main difference is that the ugrid.InsertNextCells takes 2 arguments:
> type of cell and the vtkIdList of point Ids that make up the cell. For
> polyhedrons, the IdList has to contain information about the number faces
> that make up the cell and then the point Ids of the the faces.
>
> I hope this helps,
> Gudni
>
> On 2 November 2014 13:33, George <george.gerber at gmail.com> wrote:
>
>> Good day,
>>
>> I have translated the example of Bill into the best Python I know. I get
>> the
>> following error:
>> Traceback (most recent call last):
>>   File "C:\Users\george limited\Desktop\unstructuredgrid.py", line 19, in
>> <module>
>>     ug.InsertNextCell(vtk.VTK_POLYHEDRON,4,pointids,1,faces)
>> TypeError: InsertNextCell argument 5: expected a sequence of 0 values, got
>> vtkobject
>>
>> Any advice will be very much appreciated.
>> Best regards,
>> George
>>
>> The code is:
>> import vtk
>>
>> points = vtk.vtkPoints()
>> pointids = [0,1,2,3]
>>
>> points.InsertNextPoint(0,0,0)
>> points.InsertNextPoint(1,0,0)
>> points.InsertNextPoint(1,1,0)
>> points.InsertNextPoint(0,1,0)
>>
>> faces = vtk.vtkCellArray()
>>
>> face0 = [0,1,2,3]
>>
>> faces.InsertNextCell(4,face0)
>>
>> ug = vtk.vtkUnstructuredGrid()
>> ug.SetPoints(points)
>> ug.InsertNextCell(vtk.VTK_POLYHEDRON,4,pointids,1,faces.GetPointer())
>>
>> writer = vtk.vtkUnstructuredGridWriter()
>> writer.SetFileName("polyhedron.vtu")
>> writer.SetInputData(ug)
>> writer.Write()
>>
>>
>>
>> --
>> View this message in context:
>> http://vtk.1045678.n5.nabble.com/polyhedron-in-python-tp5728643p5729319.html
>> 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
>>
>
>
>
> --
> Gudni Karl Rosenkjaer,
> PhD student at Geophysical Inversion Facility
> Department of Earth, Ocean and Atmospheric Sciences,
> University of British Columbia
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20141103/dfe6e596/attachment.html>


More information about the vtkusers mailing list