[vtkusers] BlankCell for UnstructuredGrid not working
Hui Zhou
meilizhuti at gmail.com
Wed Aug 1 11:04:00 EDT 2012
I am learning VTK and I am adapting some example code of VTK to experiment
with it. A simple tast I am doing is to blank some cells of a
structuredgrid, but somehow could not get it work. I printed the
IsCellVisible flag for those cells I try to blank, and all are zero. So it
really puzzeled me why those cells are always visible on the visualization.
I appreciate your input very much.
Best Regards!
'''
My first vtk code
Manually generate a structured grid
'''
import vtk
from math import sin, cos
from random import random
dims = [30, 20, 11]
nd = dims[0] * dims[1] * dims[2]
sgrid = vtk.vtkStructuredGrid()
sgrid.SetDimensions(dims)
for i in range(40, 50):
sgrid.BlankCell(i)
s = sgrid.IsCellVisible(i)
print s
pts = vtk.vtkPoints()
pts.Allocate(nd)
vectors = vtk.vtkFloatArray()
vectors.SetNumberOfComponents(1)
vectors.SetNumberOfTuples(nd)
dz = 2.0 / (dims[2] - 1)
rmin = 0.5
rmax = 1.0
dr = (rmax - rmin) / (dims[1] - 1)
x = [0, 0, 0]
v = [0, 0, 0]
for k in range(0, dims[2]) :
x[2] = -1.0 + k * dz
kOffset = k * dims[0] * dims[1]
for j in range(0, dims[1]):
r = rmin + j * dr
jOffset = j * dims[0]
for i in range(0, dims[0]):
theta = i * vtk.vtkMath.RadiansFromDegrees(180.0 / (dims[0]-1))
x[0] = r * cos(theta)
x[1] = r * sin(theta)
v[0] = -x[1]
v[1] = x[0]
offset = i + jOffset + kOffset
pts.InsertPoint(offset, x)
#vectors.InsertTuple(offset, v)
#vectors.SetValue(offset, random())
cnt = 0
for k in range(0, dims[2]-1):
for j in range(0, dims[1]-1):
for i in range(0, dims[0]-1):
vectors.SetValue(cnt, cnt)
cnt += 1
sgrid.SetPoints(pts)
#sgrid.GetPointData().SetVectors(vectors)
vectors.SetName("HU")
#sgrid.GetPointData().AddArray(vectors)
sgrid.GetCellData().AddArray(vectors)
for i in range(40, 50):
s = sgrid.IsCellVisible(i)
print s
#hedgehog = vtk.vtkHedgeHog();
#hedgehog.SetInput(sgrid)
#hedgehog.SetScaleFactor(0.1)
sgridOutline = vtk.vtkOutlineFilter()
sgridOutline.SetInput(sgrid)
lut = vtk.vtkLookupTable()
#lut.SetNumberOfColors(256)
#lut.SetHueRange(0, 1)
def CreateSgridActor(imin, imax, jmin, jmax, kmin, kmax):
sgridFilter = vtk.vtkStructuredGridGeometryFilter()
sgridFilter.SetInput(sgrid)
sgridFilter.SetExtent(imin, imax, jmin, jmax, kmin, kmax)
sgridMapper = vtk.vtkPolyDataMapper()
sgridMapper.SetLookupTable(lut)
sgridMapper.SetInput(sgridFilter.GetOutput())
sgridMapper.SetScalarModeToUseCellFieldData()
sgridMapper.SelectColorArray("HU")
sgridMapper.SetScalarRange(0, dims[0]*dims[1]*dims[2])
sgridActor = vtk.vtkActor()
sgridActor.SetMapper(sgridMapper)
return sgridActor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(CreateSgridActor(0, 0, 0, dims[1], 0, dims[2]))
ren.AddActor(CreateSgridActor(0, dims[0], 0, 0, 0, dims[2]))
ren.AddActor(CreateSgridActor(0, dims[0], 0, dims[1], 0, 0))
ren.AddActor(CreateSgridActor(dims[0]-1, dims[0]-1, 0, dims[1], 0,
dims[2]))
ren.AddActor(CreateSgridActor(0, dims[0], dims[1]-1, dims[1]-1, 0,
dims[2]))
ren.AddActor(CreateSgridActor(0, dims[0], 0, dims[1], dims[2]-1,
dims[2]-1))
# colorbar
colorbar = vtk.vtkScalarBarActor()
colorbar.SetLookupTable(lut)
colorbar.SetTitle("HU")
colorbar.SetNumberOfLabels(4)
ren.AddActor2D(colorbar)
#ren.SetBackground(1, 1, 1)
ren.ResetCamera()
ren.GetActiveCamera().Elevation(60)
ren.GetActiveCamera().Azimuth(30)
ren.GetActiveCamera().Zoom(0.8)
renWin.SetSize(500, 500)
iren.Initialize()
renWin.Render()
iren.Start()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120801/97c47181/attachment.htm>
More information about the vtkusers
mailing list