[vtkusers] numpy_to_vtk and vtkPolyData

Philip Winston pwinston at gmail.com
Thu Nov 3 07:51:10 EDT 2011


Replying to my own message here is something that seems to work to populate
a vtkPolyData from numpy arrays, comments welcome:

    def createPolyData(self, verts, tris):
        """Create and return a vtkPolyData.

        verts is a (N, 3) numpy array of float vertices

        tris is a (N, 1) numpy array of int64 representing the triangles
        (cells) we create from the verts above.  The array contains
        groups of 4 integers of the form: 3 A B C
        Where 3 is the number of points in the cell and A B C are indexes
        into the verts array.
        """

        # save, we share memory with the numpy arrays
        # so they can't get deleted
        self.verts = verts
        self.tris = tris

        poly = vtk.vtkPolyData()

        points = vtk.vtkPoints()
        points.SetData(numpy_to_vtk(verts))
        poly.SetPoints(points)

        cells = vtk.vtkCellArray()
        cells.SetCells(len(tris) / 4, numpy_to_vtkIdTypeArray(tris))
        poly.SetPolys(cells)

        return poly



-Philip



On Sat, Oct 29, 2011 at 4:48 PM, Philip Winston <pwinston at gmail.com> wrote:

> Are there any examples showing how to populate a vtkPolyData with
> verts/triangles from numpy arrays?
>
> I have a solution right now where I loop through by hand adding points and
> triangles, but it is slow.
>
> I see there is a numpy_to_vtk utility function, but I'm not clear how to
> setup my numpy arrays and what vtkPolyData functions I need to call.  I'm
> imagining I want one Nx3 array with a vert per row (X Y Z).  And a second
> Nx3 array of vert indexes with one triangle per row (v1 v2 v3)?  But I can
> create whatever numpy arrays are needed, I just need help slurping them
> into a vtkPolyData.
>
> Thanks!
>
> -Philip
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20111103/fa53758f/attachment.htm>


More information about the vtkusers mailing list