[vtkusers] How do I render a volume from an UnstructuredGrid?

Ruff albin.nilsson at gmail.com
Thu Jul 4 09:21:02 EDT 2013


I've created an unstructured grid and would like to volume render it's scalar
values, but nothing is rendered.
It seems like it is calculating the space I've specified but I cannot see
anything. Like I'm missing some small obvious thing.

I also tried setting a function to the mapper (Not that I know exactly what
it does) but it doesn't seem to do anything.

/I know that my current dataset could easily be used as structuredPoints,
but I am planning to use a more arbitrary structure for the datafiles./

The code is as follows:
#!/usr/bin/env python

import math
from random import*
from vtk import*

#------------------------------------------------------------------#
# Just generating a pretty pattern                                 #
#------------------------------------------------------------------#
cubeSize = 40
atomIndex = 0
coordinates = vtk.vtkPoints()
scalars = vtk.vtkFloatArray()

for z in range(0,cubeSize):
	for y in range(0,cubeSize):
		for x in range(0,cubeSize):
			distX = abs(cubeSize/2 - x)
			distY = abs(cubeSize/2 - y)
			distZ = abs(cubeSize/2 - z)
			dist = math.sqrt(distX*distX + distY*distY + distZ*distZ)
			
			coordinates.InsertPoint(atomIndex, x, y, z)
			scalars.InsertValue(atomIndex, abs(math.cos(dist*.1)*255))
#------------------------------------------------------------------#
# Done generating pretty pattern                                   #
#------------------------------------------------------------------#

#Setting up scalar grid
ScalarGrid = vtk.vtkUnstructuredGrid()
ScalarGrid.SetPoints(coordinates)
ScalarGrid.GetPointData().SetScalars(scalars)

#------------------------------------------------------------------#
# Fixing a bunch of volume properties                              #
#------------------------------------------------------------------#
# Scalars (0-255) mapped to transparency
opacity = vtk.vtkPiecewiseFunction()
opacity.AddPoint(150, 0)
opacity.AddPoint(255, 1)

# Scalars (0-255) mapped to colors
colors = vtk.vtkColorTransferFunction()
colors.AddRGBPoint(0,1,1,1)
colors.AddRGBPoint(230,0,1,0)
colors.AddRGBPoint(250,1,0,0)

# Colors and opacity goes into a property, along with some fancy stuff
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colors)
volumeProperty.SetScalarOpacity(opacity)
volumeProperty.ShadeOn()
volumeProperty.SetInterpolationTypeToLinear()

#------------------------------------------------------------------#
# End of volume properties                                         #
#------------------------------------------------------------------#

# Mapping the grid
volumeMapper = vtkUnstructuredGridVolumeRayCastMapper()
volumeMapper.SetInput(ScalarGrid)
#This seems to do nothing at all
Function = vtk.vtkUnstructuredGridBunykRayCastFunction()
volumeMapper.SetRayCastFunction(Function)

# Volume using the mapper
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)

# Standard renderer, render window and interactor
ren = vtk.vtkRenderer()
ren.AddVolume(volume)
ren.SetBackground(1, 1, 1)

# =======THIS IS WRITTEN ONLY TO SEE THAT SOMETHING IS RENDERED=======
cylinder = vtkCylinderSource()
cylinderMapper = vtkPolyDataMapper()
cylinderMapper.SetInput(cylinder.GetOutput())
cylinderActor = vtkActor()
cylinderActor.SetMapper(cylinderMapper)
ren.AddActor(cylinderActor)
# =====================================================================

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(600, 600)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.Initialize()
iren.Start()




--
View this message in context: http://vtk.1045678.n5.nabble.com/How-do-I-render-a-volume-from-an-UnstructuredGrid-tp5721766.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list