[vtkusers] SIGGRAPH '00 Head Scans in VTK

Randall Hopper aa8vb at yahoo.com
Fri Aug 4 12:37:17 EDT 2000


    For those that got your head scanned at SIGGRAPH '00 last month, here's
a quick VTK viewer in Python.

    It displays the OBJ, 3DS, and STL mesh files.  If you had them do a
.OBJ, you can display the texture by specifying the TIF texture file as a
second argument.
       
    Note: the TIF files display very dark in VTK (not sure why).  To
circumvent, convert to PPM and feed that in as the second argument.

Example:

    show-face randalh1.obj randalh1.tif
or:
    show-face randalh1.obj randalh1.ppm

-- 
Randall Hopper
aa8vb at yahoo.com
-------------- next part --------------
#!/usr/bin/env python
#
#  show-face <mesh-file> [<texture-file>]
#
#  Viewer for texture-mapped head scans from SIGGRAPH '00.
#
#  Display mesh (STL, OBJ, and 3DS) file, optionally with texture map
#    (TIF, PPM) if specified for OBJ mesh files.
#

import os, sys

from libVTKCommonPython import *
from libVTKGraphicsPython import *

try:
  meshname = sys.argv[1]
except IndexError:
  print "%s <mesh-file> [<texture-file>]" % sys.argv[0]
  print "Specify path to mesh file (stl, obj, or 3ds), and optionally"
  print "  texture file (ppm)."
  sys.exit(1);

try:
  texturename = sys.argv[2]
except IndexError:
  texturename = None

if texturename:
  print "Loading %s with texture %s" % (meshname, texturename)
else:
  print "Loading %s" % meshname

#
# Create the RenderWindow and Renderer
#
ren = vtkRenderer()
ren.TwoSidedLightingOff()          # Don't texture the inside of the head
renWin = vtkRenderWindow()
renWin.AddRenderer( ren )

#
# Create the mesh reader
#
importer = reader = shape = None  

ext = os.path.splitext( meshname )[1]
if ext == '.stl':
  reader = vtkSTLReader()
  reader.SetFileName( meshname )
  reader.Update()
elif ext == '.obj':
  reader = vtkOBJReader()
  reader.SetFileName( meshname )
  reader.Update()
elif ext == '.3ds':
  importer = vtk3DSImporter()
  importer.SetFileName( meshname )
else:
  print "Can't handle mesh format for '%s'." % meshname
  sys.exit(1)

#print reader.GetOutput()

#
# Create the texture reader
#
texture = None
if texturename:

  # Read image
  ext = os.path.splitext( texturename )[1]
  if ext == '.tif':
    img_reader = vtkTIFFReader()
  elif ext == '.ppm':
    img_reader = vtkPNMReader()
  else:
    print "Can't handle image format for '%s'." % texturename
    sys.exit(1)
  img_reader.SetFileName( texturename )
  img_reader.Update()

  texture = vtkTexture()
  texture.SetInput( img_reader.GetOutput() )
  
if reader:
  #
  # Reader object - create mapper, actor, and add actor to renderer
  #
  mapper = vtkDataSetMapper()
  mapper.SetInput(reader.GetOutput())

  actor = vtkLODActor()
  actor.SetMapper(mapper)

  if texture: actor.SetTexture( texture )

  ren.AddActor(actor)
else:
  #ren = importer.GetRenderer()
  #renWin = importer.GetRenderWindow()

  #
  # Importer object - it created the renderer
  #
  importer.SetRenderWindow( renWin )
  importer.ComputeNormalsOn()
  importer.Read()

  if texture:
    print "Don't know how to texture 3DS yet."
    sys.exit(1)

  renCollection = renWin.GetRenderers()
  renCollection.InitTraversal()
  ren = renCollection.GetNextItem()

# change view up to +z
camera = ren.GetActiveCamera()
camera.SetPosition( 0, -1, 0 )
camera.SetFocalPoint( 0, 0, 0 )
camera.ComputeViewPlaneNormal()
camera.SetViewUp( 0, 0, 1 )

ren.ResetCamera()
#camera.Dolly( 1.4 )

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.LightFollowCameraOn()

ren.SetBackground(0.1,0.2,0.4)
renWin.SetSize(500,500)

# render the image
#
cam1=ren.GetActiveCamera()
cam1.Zoom(1.4)
iren.Initialize()

iren.Start()


More information about the vtkusers mailing list