animating large amount of cylindrical vectors

Jody Winston josephwinston at mac.com
Fri May 12 20:58:22 EDT 2000


James C. Moore writes:
 > Hey Jody,
 > 
 > Sure there is... if your data set is structured.   You can use the SetExtent
 > method for your structured grid or structure points set (via the
 > StructuredGridGeometryFilter) and just move through the domain in steps...
 > I've used it myself.
 > 

Well, I'm having problems with extracting extents.  When I extract
some data using vtkStructuredGridGeometryFilter() and then try to
display it, I only have points.  So, I thought to myself, why I need
to add a vtkCellArray to the vtkPolyData, I tried that and I can't
seem to to display anything but points.  Here is a stand alone
example:

#!/usr/bin/env python

from VTK import *
from math import *

def calcTables(shots):
    
    angleIncrement = 360. / shots * pi / 180.

    cosTable = []
    sinTable = []
    for i in range(shots):
        angle = angleIncrement * i
        c = cos(angle)
        s = sin(angle)
        cosTable.append(c)
        sinTable.append(s)
        
    return cosTable, sinTable

def buildCachedArray(cosTable, sinTable, todo, zLoc):
    result = []

    for i in todo:
        v = [1 * cosTable[i],
             1 * sinTable[i],
             zLoc]
        result.append(v)

    return result

def createStructuredGrid(shots, inputDims):
    
    X = 0
    Y = 1
    Z = 2
    EXT = 1000
    
    sgrid = vtkStructuredGrid()
    dims = [0, 0, 0]
    dims[X] = inputDims[X] + 1
    dims[Y] = inputDims[Y] + 1
    dims[Z] = inputDims[Z]
    print "createStructuredGrid", dims
    
    sgrid.SetDimensions(dims[X], dims[Y], dims[Z]) 

    points = vtkPoints()
    points.Allocate(dims[X]*dims[Y]*dims[Z], EXT)

    vectors = vtkVectors()
    vectors.Allocate(dims[X]*dims[Y]*dims[Z], EXT)

    scalars = vtkScalars()
    scalars.Allocate(dims[X]*dims[Y]*dims[Z], EXT)

    cosTable, sinTable = calcTables(shots)

    todo = range(shots)
    todo.append(0)
    for k in range(dims[Z]):
        buildCache = 1
        for j in todo:
            if buildCache:
                cache = buildCachedArray(cosTable, sinTable, todo, k)
                buildCache = 0
            for i in todo:
                v = cache[i]
##                print "(%d, %d, %d) = (%f, %f, %f)" % (i, j, k, v[X], v[Y], v[Z])
                points.InsertNextPoint(v[X], v[Y], v[Z])
                vectors.InsertNextVector(v[X], v[Y], 0)
                scalars.InsertNextScalar(k)

    sgrid.SetPoints(points)
    sgrid.GetPointData().SetVectors(vectors)
    sgrid.GetPointData().SetScalars(scalars)
    return sgrid

def insertPts(polygons, shots, depths):
    pts = [0, 0, 0, 0]
    offset = 0
    for j in range(depths - 1):
        for i in range(shots):
            pts[0] = i + offset
            pts[1] = pts[0] + 1
            pts[2] = pts[1] + shots + 1
            pts[3] = pts[2] - 1
#            print pts

        for i in range(4):
            polygons.InsertCellPoint(pts[i])            
        offset = offset + shots + 1
        
    return

def displayStructuredGrid(sgrid, shots):

    minRadius, maxRadius = sgrid.GetScalarRange()
    print "sgrid.GetExtent() =", sgrid.GetExtent()

    polyData = vtkStructuredGridGeometryFilter()
    polyData.SetInput(sgrid)
    polyData.SetExtent(0, shots,
                     0, shots,
                     0, 4)

    mapper = vtkPolyDataMapper()

    tryNormals = 0
    if tryNormals:
        polys = vtkCellArray()
        polys.Allocate(polys.EstimateSize(shots * 5, shots), 1000)
        insertPts(polys, shots, 5)
        polys.Squeeze()                     # since we've estimated size; reclaim some space
        polyData.GetOutput().SetPolys(polys)
        
        normals = vtkPolyDataNormals()
        normals.SetInput(polyData.GetOutput())
        mapper.SetInput(normals.GetOutput())
    else:
        mapper.SetInput(polyData.GetOutput())
        
    mapper.SetScalarRange(minRadius, maxRadius)
    
    actor = vtkActor()
    actor.SetMapper(mapper)

    renderer = vtkRenderer()
    
    renderWindow = vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    
    windowInteractor = vtkRenderWindowInteractor()
    windowInteractor.SetRenderWindow(renderWindow)

    renderer.AddActor(actor)
    renderer.SetBackground(1,1,1)

    #
    # axis (Must be in this order)
    #

    axis = vtkCubeAxesActor2D()
    axis.SetProp(actor)
    axis.SetCamera(renderer.GetActiveCamera())
##    axis.SetNumberOfLabels(10)
    axis.SetLabelFormat("%8.2f")
    axis.SetZLabel("Depth")
    axis.SetXAxisVisibility(0)
    axis.SetYAxisVisibility(0)
##    axis.ShadowOn()
##    axis.SetFlyModeToClosestTriad()
##    axis.SetFlyModeToOuterEdges()
##    axis.SetFontFactor(0.8)
##    axis.GetProperty().SetColor(1, 1, 1)

    renderer.AddProp(axis)

    # Create a scalar bar
    scalarBar = vtkScalarBarActor()
    scalarBar.SetLookupTable(mapper.GetLookupTable())
    scalarBar.SetTitle("Radius")
    scalarBar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
    scalarBar.GetPositionCoordinate().SetValue(0.1,0.01)
    scalarBar.SetOrientationToHorizontal()
    scalarBar.SetWidth(0.8)
    scalarBar.SetHeight(0.17)
    scalarBar.SetNumberOfLabels(8)

    renderer.AddActor2D(scalarBar)
    
    renderWindow.SetSize(300,300)
    renderWindow.Render()
    
    windowInteractor.Start()

    return

def main(argv):

    shots = 24
    dims = [shots, shots, 16]

    sgrid = createStructuredGrid(shots, dims)
    displayStructuredGrid(sgrid, shots)

    return

if __name__ == "__main__":
    import sys
    sys.exit(main(sys.argv) or 0)

    
    

--------------------------------------------------------------------
This is the private VTK discussion list. Please keep messages on-topic.
Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at public.kitware.com>. For help, send message body containing
"info vtkusers" to the same address.
--------------------------------------------------------------------



More information about the vtkusers mailing list