Is there a method cross reference? (fwd)

Tom G. Smith smitty at kcc.com
Wed Oct 13 13:24:13 EDT 1999


Sorry I took a while to respond.  The actual example I'd been working
with involved piping the output of a perl script to vtk, like this:

	./points.pl | vtk points

points.pl looks like this:

	bash$ cat points.pl
	#!/usr/local/bin/perl
	$degrees=atan2(0,-1)/180; # Conversion factor, degrees to radians.
	for($a=90; $a<=270; $a+=30) {
	        $z=sin($a*$degrees);
	        $radius=-cos($a*$degrees);
	        for($b=0; $b<360; $b+=30) {
	                $x=$radius*cos($b*$degrees);
	                $y=$radius*sin($b*$degrees);
	                print("$x $y $z\n");
	                last if($radius<10**-13);
	        }
	}
	bash$ 

points, which I've appended to this e-mail, in case it's of use to
anyone, is a Tcl script that visualizes spheres at all the coordinate
points on stdin.  It's my first exercise in trying to create something
useful while learning vtk.

The problem of course is if I attempt to invoke vtk first, then
"source points" as you suggest, there's no way to feed it the output
from points.pl.  I tried "./points.pl | vtk" but vtk gobbled up my
coordinates and didn't know what to do with them:

	bash$ ./points.pl | vtk
	invalid command name "-6.12303176911189e-17"
	invalid command name "0.5"
	invalid command name "0.433012701892219"
	...

As a another learning exercise, I decided to try converting Cube.cxx
to a Tcl script (which I've also attached), which I could then try
"sourcing" as you suggested, since it would not require any input
on stdin.  This worked, but I see no advantage to it over doing "vtk
cube" then entering e.g.  "cells Print" in the Interactor window.

I can just as easily cut-and-paste from that window as I can from the
xterm window.  In fact it's actually a little easier to cut-and-paste
from the Interactor window, since I can copy multiple pages in one
operation, and I can't in the xterm window.

The conclusion I'm rapidly approaching is, one simply can't put
output statements such as "cells ListMethods" or "cells Print" in
a Tcl script to be run under vtk.  It wasn't designed to support
the standard model of command pipes using stdin, stderr and stdout.
Such commands can only be entered manually.  This is unfortunate and
a design flaw in my opinion, but then, at this point, my opinion is
pretty uninformed, having only begun the learning curve.

> 
> "Tom G. Smith" wrote:
> 
> > Thanks for the explanation of ListMethod.  I've now penciled
> > "ListMethods, page 461" in the margins of the index to "The Visualization
> > Toolkit."  Writing a good index is the hardest part of finishing any
> > book, a discipline in its own right.
> >
> > I hope I'm not being a pest, but I still have a question about output.
> > In Tcl, is there a way of directing the output of statements 2 and 3
> > in the following code snippet to stdout?
> >
> > 1  vtkPolyVertex cells ;# Declare cells to be instance of PolyVertex.
> > 2  cells ListMethods
> > 3  cells Print
> >
> > The only way I can see the output of such statements is to invoke
> > vtk with e.g. "vtk sample.tcl," press "u" in the rendered window and
> > manually enter them on the interactor command line.  Then the output
> > only shows in the interactor window.
> >
> >
> 
> Tom,
> 
> you can start the vtk shell all by itself, then type "source sample.tcl"  You will
> 
> have access to interactive mode through the command line of the shell once
> the tcl script has finished loading.  Then you can use the listmethods function,
> and either copy the output from your xterm with highlight, cut and paste, or
> you may even be able to redirect the output, because the tcl shell generally
> supports
> csh commands.  I haven't tried redirect though, I usually cut and paste.
> 
> Kerrie
> 
> 


-- 
points:
..........................................................................
#!/usr/bin/tcl
#-----------------------------------------------------------------------
# Description:  This Tcl script, when run under vtk, visualizes a set
# of points in 3D space, given their (x,y,z) coordinates on stdin.
# The coordinates for one point should appear on each line of stdin,
# blank-delimited.  A sample call sequence would be -
#	echo 1 2 3 | vtk points
# which causes the visualization of a single point, x=1, y=2, z=3.
#-----------------------------------------------------------------------
# Changelog:
# 990928 Smitty created.
#-----------------------------------------------------------------------

proc dospheres {} {
#-----------------------------------------------------------------------
# Called to activate vtk objects to put spheres at each input point.
#-----------------------------------------------------------------------
	global geometry radius
	vtkSphereSource geometry
	# vtkSphereSource creates a sphere (represented by polygons)
	# of specified radius centered at the origin. The resolution
	# (polygonal discretization) in both the latitude (phi) and
	# longitude (theta) directions can be specified. It also is
	# possible to create partial spheres by specifying maximum phi
	# and theta angles.

	# vtkSphereSource is of the superclass vtkPolyDataSource, which
	# is if the superclass vtkSource.
	#
	# Other vtkPolyDataSource types are -
	#
	#	vtkPolyDataReader, vtkAxes, vtkBYUReader, vtkConeSource
	#	vtkCubeSource, vtkCursor3D, vtkCyberReader, vtkCylinderSource
	#	vtkDiskSource, vtkLineSource, vtkMCubesReader, vtkOutlineSource
	#	vtkPlaneSource, vtkPointSource, vtkSTLReader, vtkSphereSource
	#	vtkTextSource, vtkUGFacetReader, vtkVectorText
	geometry SetRadius $radius
} ;# End dospheres.

# ---------------------------------------------------------------
# Mainline starts here.
source startup ;# Initialize vtk.
source getpoints ;# Get the getpoints routine.
getpoints ;# Read 3d point coordinates from stdin.
dospheres ;# Display spheres at each point.

vtkGlyph3D glyphs
	# vtkGlyph3D is a filter that copies a geometric representation
	# (called a glyph) to every point in the input dataset. The
	# glyph is defined with polygonal data from a source filter
	# input. The glyph may be oriented along the input vectors or
	# normals, and it may be scaled according to scalar data or
	# vector magnitude. More than one glyph may be used by creating
	# a table of source objects, each defining a different glyph. If
	# a table of glyphs is defined, then the table can be indexed
	# into by using either scalar value or vector magnitude. To
	# use this object you'll have to provide an input dataset and
	# a source to define the glyph. Then decide whether you want
	# to scale the glyph and how to scale the glyph (using scalar
	# value or vector magnitude). Next decide whether you want to
	# orient the glyph, and whether to use the vector data or normal
	# data to orient it. Finally, decide whether to use a table of
	# glyphs, or just a single glyph. If you use a table of glyphs,
	# you'll have to decide whether to index into it with scalar
	# value or with vector magnitude.
  glyphs SetScaleModeToDataScalingOff
	# Without SetScaleModeToDataScalingOff, you get just one big sphere.
  glyphs SetInput ugrid
	# Tell glyphs to get the locations from ugrid where each
	# sphere is to be placed.
  glyphs SetSource [geometry GetOutput]
	# Tell glyphs that geometry is the source for the geometric object
	# to be drawn at each point described by input ugrid.

vtkDataSetMapper glyphsMapper
  glyphsMapper SetInput [glyphs GetOutput]
vtkActor glyphsActor
  glyphsActor SetMapper glyphsMapper
ren1 AddActor glyphsActor ;# assign our actor to the renderer

# enable user interface interactor
iren SetUserMethod {wm deiconify .vtkInteract}
# render the image
iren Initialize
# prevent the tk window from showing up then start the event loop
wm withdraw .


getpoints:
..........................................................................
#!/usr/bin/tcl
#-----------------------------------------------------------------------
# Description:  When sourced in a Tcl script run under vtk, enables
# subroutine getpoints that reads the (x,y,z) coordinates on stdin
# for a set of points in 3D space. The coordinates for each point
# should appear on one line of stdin, blank-delimited.
#-----------------------------------------------------------------------
# Changelog:
# 991008 Smitty created.
#-----------------------------------------------------------------------

proc getpoints {} {
#-----------------------------------------------------------------------
# Called to read the (x,y,z) point coordinates from stdin, storing
# them in global array vtkPoints object points.  These global
# variables are set:
# cells = vtkPolyVertex object.  Each cell contains the id of a point
#	in object points, e.g. cell 0 has point 0, cell 1 has point 1,
#	etc.
# points = vtkPoints object containing (x,y,z) coordinates.
# pointcount = the number of points read, i.e. the count of items in
#	each of the arrays x, y, and z.
# radius = the desired radius of the spheres that will represent each
#	point in the scene.  The value is the magnitude of the range
#	of coordinates, divided by 10.
# ugrid = vtkUnstructuredGrid object.
#-----------------------------------------------------------------------
	set names {x y z}
	foreach n $names {
		set $n {} ;# Initialize list to empty.
	}
	while {[gets stdin line] >= 0} {
		set i 0
		foreach n $names {
			set v [lindex $line $i] ;# Get value from input line.
			lappend $n $v ;# Append value to list.
			if {![info exists max] || $v > $max } {
				set max $v
			}
			if {![info exists min] || $v < $min } {
				set min $v
			}
			incr i
		}
	}
	global pointcount
	set pointcount [llength $x]
	global radius
	set radius [expr ($max-$min)/10.0] ;# radius of sphere at each point.
	# Put the points in vtkpoints object.
	global points
	vtkPoints points
		# vtkPoints represents 3D points. The data model for vtkPoints
		# is an array of vx-vy-vz triplets accessible by (point or
		# cell) id.
	points SetNumberOfPoints $pointcount
		# Subroutine getpoints (see above) set global $pointcount to
		# the number of (x,y,z) point coordinate sets it read from
		# stdin and stored in global arrays x, y, and z.  The following
		# loop uses method InsertPoint to add all of the coordinate
		# sets read from stdin to the vtkpoints object named points.
	set n 0
	while {$n < $pointcount} {
		points InsertPoint $n \
			[lindex $x $n] [lindex $y $n] [lindex $z $n]
		incr n
	}

	global cells
	vtkPolyVertex cells
		# vtkPolyVertex is a member of the superclass vtkPointSet,
		# which is a member of the abstract class vtkDataSet, which
		# is a member of the superclass vtkDataObject, and is
		# a concrete implementation of vtkCell to represent a set of
		# 3D vertices. 
	
		# vtkCell is an abstract class that specifies the
		# interfaces for data cells. Data cells are simple
		# topological elements like points, lines, polygons,
		# and tetrahedra of which visualization datasets are
		# composed. In some cases visualization datasets may
		# explicitly represent cells (e.g., vtkPolyData,
		# vtkUnstructuredGrid), and in some cases, the
		# datasets are implicitly composed of cells (e.g.,
		# vtkStructuredPoints).

	[cells GetPointIds] SetNumberOfIds $pointcount
		# You have to know all the class relationships
		# because method GetPointIds is associated
		# with class vtkCell, and on the html pages at
		# http://www.kitware.com/vtkhtml/vtkdata/manhtml/, #
		# the only source for online documentation on vtk that
		# most closely resembles traditional man pages, you
		# find the description for GetPointIds under vtkCell,
		# and there is no reference to it under vtkPolyVertex,
		# the first place you'd most likely look for it.
 
		# Further, there is no cross-reference for methods,
		# nor is there a search capability at kitware.com.
		# You have to scan up the hierarchy of classes in
		# order to find documentation on any inherited method.
 
		# According to the man page, GetPointIds "For cell
		# point i, return the actual point id,"  but its C
		# declaration indicates it returns a pointer to an
		# object of type vtkIdList.  Apparently in this Tcl #
		# context, the method GetpointIds of the vtkPolyVertex
		# object named cells returns a reference to an object
		# of type vtkIdList, and its method SetNumberOfIds
		# is then used to set the number of Ids in the list.

		# From the context, it wouldn't make sense that the
		# object of type vtkIdList is temporary

	set n 0
	while {$n < $pointcount} {
		[cells GetPointIds] SetId $n $n
		incr n
	}

	global ugrid
	vtkUnstructuredGrid ugrid
		# vtkUnstructuredGrid is a data object that is a concrete
		# implementation of vtkDataSet. vtkUnstructuredGrid represents
		# any combinations of any cell types. This includes 0D (e.g.,
		# points), 1D (e.g., lines, polylines), 2D (e.g., triangles,
		# polygons), and 3D (e.g., hexahedron, tetrahedron).

		# Dataset objects are the fundamental visualization data
		# types.  They are input and output from sources, filters,
		# and mappers.

		# vtkDataSet is an abstract class that specifies an interface
		# for dataset objects. vtkDataSet also provides methods to
		# provide information about the data, such as center, bounding
		# box, and representative length. In vtk a dataset consists of
		# a structure (geometry and topology) and attribute data. The
		# structure is defined implicitly or explicitly as a collection
		# of cells. The geometry of the structure is contained in
		# point coordinates plus the cell interpolation functions. The
		# topology of the dataset structure is defined by cell types
		# and how the cells share their defining points. Attribute data
		# in vtk is either point data (data at points) or cell data
		# (data at cells). Typically filters operate on point data,
		# but some may operate on cell data, both cell and point data,
		# either one, or none.

	ugrid Allocate 1 1
		# 1st arg is size of connectivity list.
		# 2nd arg is amount to extend storage (if necessary).
	ugrid InsertNextCell [cells GetCellType] [cells GetPointIds]
	ugrid SetPoints points
} ;# End getpoints.


startup:
..........................................................................
#!/usr/bin/tcl
#-----------------------------------------------------------------------
# Description:  This Tcl script can be sourced to get the typical
# vtk initialization.
#-----------------------------------------------------------------------
# Changelog:
# 991007 Smitty created.
#-----------------------------------------------------------------------
catch {load vtktcl} ;# Load the shared vtk libraries.
source /opt/vtk/examplesTcl/vtkInt.tcl ;# Get the interactor ui.
vtkRenderer ren1
	# vtkRenderer provides an abstract specification for renderers. A
	# renderer is an object that controls the rendering process for
	# objects. Rendering is the process of converting geometry,
	# a specification for lights, and a camera view into an
	# image. vtkRenderer also performs coordinate transformation
	# between world coordinates, view coordinates (the computer
	# graphics rendering coordinate system), and display coordinates
	# (the actual screen coordinates on the display device). 
vtkRenderWindow renWin
	# vtkRenderWindow is an abstract object to specify the behavior
	# of a rendering window. A rendering window is a window
	# in a graphical user interface where renderers draw their
	# images. Methods are provided to synchronize the rendering
	# process, set window size, and control double buffering.
  renWin AddRenderer ren1
	# Associate renderer ren1 with renWin.
vtkRenderWindowInteractor iren
	# vtkRenderWindowInteractor is a convenience object that provides
	# event bindings to common graphics functions.  Here's a brief
	# summary.  See http://www.kitware.com/vtkhtml/vtkdata/manhtml/
	# vtkRenderWindowInteractor.html for more detail:
	# Mouse bindings:
	# 	camera: Button 1 - rotate
	# 		Button 2 - pan
	# 		Button 3 - zoom
	# 		ctrl-Button 1 - spin
	# 	actor:  Button 1 - rotate
	# 		Button 2 - pan
	# 		Button 3 - uniform scale
	# 		ctrl-Button 1 - spin
	# 		ctrl-Button 2 - dolly.
	# Keyboard bindings (upper or lower case):
	# j - joystick like mouse interactions
	# t - trackball like mouse interactions
	# o - object/actor interaction
	# c - camera interaction
	# r - reset camera view
	# w - turn all actors wireframe
	# s - turn all actors surface
	# u - execute user defined function
	# p - pick actor under mouse pointer (if pickable)
	# 3 - toggle in/out of 3D mode (if supported by renderer)
	# e - exit
	# q - exit
  iren SetRenderWindow renWin
	# Associate renderwindow renWin with interactor iren.



cube
..........................................................................
# ---------------------------------------------------------------
# Mainline starts here.
source startup ;# Initialize vtk.

vtkPolyData cube
vtkPoints points
vtkCellArray polys
vtkScalars scalars

set x {{0 0 0}  {1 0 0}  {1 1 0}  {0 1 0} {0 0 1}  {1 0 1}  {1 1 1}  {0 1 1}}
set i 0
while {$i < 8} {
	set t [lindex $x $i]
	points InsertPoint $i [lindex $t 0] [lindex $t 1] [lindex $t 2]
	incr i
}
set pts {{0 1 2 3}  {4 5 6 7}  {0 1 5 4} {1 2 6 5}  {2 3 7 6}  {3 0 4 7}}
set i 0
while {$i < 6} {
	set t [lindex $pts $i]
	polys InsertNextCell 4 
	set j 0
	while {$j < 4} {
		polys InsertCellPoint [lindex $t $j]
		incr j
	}
	incr i
}
set i 0
while {$i < 8} {
	scalars InsertScalar $i $i
	incr i
}

cube SetPoints points
points Delete 
cube SetPolys polys
polys Delete 
[cube GetPointData] SetScalars scalars
scalars Delete 

vtkPolyDataMapper cubeMapper
  cubeMapper SetInput cube
  cubeMapper SetScalarRange 0 7
vtkActor cubeActor
  cubeActor SetMapper cubeMapper

vtkCamera camera
  camera SetPosition 1 1 1
  camera SetFocalPoint 0 0 0
  camera ComputeViewPlaneNormal 
ren1 AddActor cubeActor ;# assign our actor to the renderer
ren1 SetActiveCamera camera
ren1 ResetCamera 
ren1 SetBackground 1 1 1
  
renWin SetSize 300 300

iren SetUserMethod {wm deiconify .vtkInteract}; # Enable user interactor.
cube ListMethods
cube Print
iren Initialize; # render the image
wm withdraw .;  # prevent tk window from showing up, then start the event loop.



-----------------------------------------------------------------------------
This is the private VTK discussion list.  Please keep messages on-topic.
Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at gsao.med.ge.com>.  For help, send message body containing
"info vtkusers" to the same address.     Live long and prosper.
-----------------------------------------------------------------------------




More information about the vtkusers mailing list