Making a sphere using vtk


This post is going to explore the method for drawing shapes using vtk. Specifically a sphere. This builds upon the example of the cone by Gobbi. You should see the cone tutorial first, understand what it is doing, and then start on this tutorial. Three things will be introduced here.
  1. The vtkSphereSource class,
  2. Changing the way in which the sphere is created, and
  3. Changing the way in which the sphere is displayed.
Before carrying on, it is proper to introduce visually the different properties that has been changed in each of the different steps. It is summarized in the picture below.
Fig. 1. The different spheres

1. The vtkSphereSource class:
The cone object from Gobii's example has been changed to the sphere with a radius of 8 using the vtkSphereSource class. This is the easy part. This is shown in Fig. 1(a). The code for doing this is shown below. The vtkSphereSource class uses the SetRadius() method to change the size of the sphere.

#! /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 the object to be drawn

sphere = vtkSphereSource()

sphere.SetRadius(8)


# Convert the sphere into polygons

sphereMapper = vtkPolyDataMapper()

sphereMapper.SetInput(sphere.GetOutput())


#Create an actor for the sphere

sphereActor = vtkActor()

sphereActor.SetMapper(sphereMapper)


# assign our actor to the renderer

ren.AddActor(sphereActor)


# enable user interface interactor

iren.Initialize()


iren.Start()


2. Changing the way in which the sphere is created
Notice that the sphere is really approximated by a number of flat surfaces. This is because, the vtkSphereSource class, does not draw the sphere. It just creates a bunch of surfaces. Then, the mapper maps these into openGL objects. To make the sphere appear more like a sphere, and less like a three-dimensional polygon, you would want to increase the number of these surfaces. This is done, from within the vtkSphereSource class by adding the following lines after creating the vtkSphereSource class.

sphere.SetThetaResolution(30)

sphere.SetPhiResolution(30)


These change the number of discrete points along the theta and phi directions (spherical coordinates) to 30 each. This is shown in Fig. 1(b).

3. Changing the way in which the sphere is displayed
The actual rendering is done by the vtkActor class. One of the protected attributes of this class is Property, which is a vtkProperty class, that controls the color, opacity, interpolation, lighting, etc. To set the color to red for example, use the following code:

(sphereActor.GetProperty()).SetColor(1,0,0)


Note that you first need to get a pointer to the Property you need to set, before you can set it. The result is shown in Fig. 1(c).

The final code stands as:

#! /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 the object to be drawn

sphere = vtkSphereSource()

sphere.SetRadius(8)

sphere.SetThetaResolution(30)

sphere.SetPhiResolution(30)


# 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,0,0)


# assign our actor to the renderer

ren.AddActor(sphereActor)


# enable user interface interactor

iren.Initialize()


iren.Start()



0 comments:

Post a Comment