Adding spheres on a grid ...

In this post, we shall look at the method of adding more than one sphere. The result is shown below:

Fig. 1.

The code for adding more spheres is shown in the program below. The only thing to notice is that, during the process of adding actors to the renderer, it is not necessary to store the information about the individual objects, such as the aforementioned vtk objects, or actors. You simply add individual polygons to the renderer, one after the other. The renderer remembers all the polygons and their properties that are added to it. This results in significant memory savings if there are a lot of objects to render.


#! /usr/bin/python
from vtk import *

# create a rendering window and renderer
ren = vtkRenderer()
renWin = vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(300,300)
renWin.StereoCapableWindowOn()

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Create a number of spheres
for i in range(0, 160, 20):
for j in range(0, 160, 20):
for k in range(0, 160, 20):
# create the object to be drawn
sphere = vtkSphereSource()
sphere.SetRadius(2)
sphere.SetThetaResolution(30)
sphere.SetPhiResolution(30)
sphere.SetCenter(i, j, k)

# Convert the sphere into polygons
sphereMapper = vtkPolyDataMapper()
sphereMapper.SetInput(sphere.GetOutput())

#Create an actor for the sphere 
sphereActor = vtkActor()
sphereActor.SetMapper(sphereMapper)
(sphereActor.GetProperty()).SetColor(1 - k/160.0, 0  , k/160.0)
(sphereActor.GetProperty()).SetOpacity(k/200.0)

# assign our actor to the renderer
ren.AddActor(sphereActor)

# enable user interface interactor
iren.Initialize()

iren.Start()

0 comments:

Post a Comment