Commit 9f0ed320 authored by Berk Geveci's avatar Berk Geveci
Browse files

New example.

parent d9d605b9
# This example demonstrates the use of vtkCardinalSpline.
# It creates random points and connects them with a spline
#
# First we include the vtktcl package which will make available
# all of the vtk commands to Tcl
#
package require vtktcl
# Load the color definitions
::vtktcl::load_script colors
# This will be used later to get random numbers.
vtkMath math
# Total number of points.
set numberOfInputPoints 10
# One spline for each direction.
vtkCardinalSpline aSplineX
vtkCardinalSpline aSplineY
vtkCardinalSpline aSplineZ
# Generate random (pivot) points and add the corresponding
# coordinates to the splines.
# aSplineX will interpolate the x values of the points
# aSplineY will interpolate the y values of the points
# aSplineZ will interpolate the z values of the points
vtkPoints inputPoints
for {set i 0} {$i < $numberOfInputPoints} {incr i 1} {
set x [math Random 0 1]
set y [math Random 0 1]
set z [math Random 0 1]
aSplineX AddPoint $i $x
aSplineY AddPoint $i $y
aSplineZ AddPoint $i $z
inputPoints InsertPoint $i $x $y $z
}
# The following section will create glyphs for the pivot points
# in order to make the effect of the spline more clear.
# Create a polydata to be glyphed.
vtkPolyData inputData
inputData SetPoints inputPoints
# Use sphere as glyph source.
vtkSphereSource balls
balls SetRadius .01
balls SetPhiResolution 10
balls SetThetaResolution 10
vtkGlyph3D glyphPoints
glyphPoints SetInput inputData
glyphPoints SetSource [balls GetOutput]
vtkPolyDataMapper glyphMapper
glyphMapper SetInput [glyphPoints GetOutput]
vtkActor glyph
glyph SetMapper glyphMapper
eval [glyph GetProperty] SetDiffuseColor $tomato
[glyph GetProperty] SetSpecular .3
[glyph GetProperty] SetSpecularPower 30
# Generate the polyline for the spline.
vtkPoints points
vtkPolyData profileData
# Number of points on the spline
set numberOfOutputPoints 400
# Interpolate x, y and z by using the three spline filters and
# create new points
for {set i 0} {$i< $numberOfOutputPoints} {incr i 1} {
set t [expr ( $numberOfInputPoints - 1.0 ) / ( $numberOfOutputPoints - 1.0 ) * $i]
points InsertPoint $i [aSplineX Evaluate $t] [aSplineY Evaluate $t] [aSplineZ Evaluate $t]
}
# Create the polyline.
vtkCellArray lines
lines InsertNextCell $numberOfOutputPoints
for {set i 0} {$i < $numberOfOutputPoints} {incr i 1} {
lines InsertCellPoint $i
}
profileData SetPoints points
profileData SetLines lines
# Add thickness to the resulting line.
vtkTubeFilter profileTubes
profileTubes SetNumberOfSides 8
profileTubes SetInput profileData
profileTubes SetRadius .005
vtkPolyDataMapper profileMapper
profileMapper SetInput [profileTubes GetOutput]
vtkActor profile
profile SetMapper profileMapper
eval [profile GetProperty] SetDiffuseColor $banana
[profile GetProperty] SetSpecular .3
[profile GetProperty] SetSpecularPower 30
# Now create the RenderWindow, Renderer and Interactor
#
vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin
# Add the actors
ren1 AddActor glyph
ren1 AddActor profile
renWin SetSize 500 500
# render the image
#
iren Initialize
iren SetUserMethod {wm deiconify .vtkInteract}
# prevent the tk window from showing up
wm withdraw .
# KeyFrame.tcl - Keyframes for vtk
#
# These procs provide a simple (yet powerful) keyframing capability
# for vtk tcl applications
# A keyframe contains a time sorted ordering of methods
# and arguments for an object. Key locations, colors, and
# other parameters are stored and can be interpolated
# at intermediate points.
# The protocol for keyframing is illustrated in the following
# example to keyframe the position of the camera camera1:
# the renderer ren1:
# 1) KeyNew CameraPosition camera1 SetPosition
# create a keyframe for camera1 that will use the
# SetPosition method of the camera
# 2) KeyAdd CameraPosition camera1 [camera1 GetPosition]
# adds the current camera position as a key frame
# 3) Repeat 2, changing the camera position for each step
# 4) KeyRun CameraPosition 30
# runs key frame for 30 steps. Steps that lie
# between keyframes are interpolated with Cubic Splines.
# After each step is interpolated, the proc KeyRender
# is invoked. This proc can be redefined by the use to
# do something more sophisticated. The default proc does
# a: renWin Render
#
############################################
# Create a new keyframe for object and method
proc KeyNew {keyFrame object method} {
upvar $keyFrame a
set a(object) $object
set a(method) $method
set a(counter) 0
set a(debug) 0
}
#
# Reset the keyframe count to 0
proc KeyReset {keyFrame} {
upvar $keyFrame a
set a(counter) 0
if { $a(debug) == 1} {puts "Resetting $keyFrame"}
}
#
# Add a new keyframe with contents value
proc KeyAdd {keyFrame value} {
upvar $keyFrame a
set a($a(counter)) $value
incr a(counter)
}
#
# Run a keyframe for "frames" frames
proc KeyRun {keyFrame frames} {
upvar $keyFrame a
set object $a(object)
set method $a(method)
set depends [llength $a(0)]
#
# create splines if they do not exist
#
for {set j 0} {$j < $depends} {incr j} {
set spline $object${method}Spline$j
if {[info commands $spline] == ""} {
vtkKochanekSpline $spline
}
$spline RemoveAllPoints
}
#
# add points to the splines
#
for {set i 0} {$i < $a(counter)} {incr i} {
for {set j 0} {$j < $depends} {incr j} {
$object${method}Spline$j AddPoint $i [lindex $a($i) $j]
}
}
#
# evaluate splines at key frames
#
for {set i 0} {$i < $frames} {incr i} {
set t [expr ( $a(counter) - 1.0 ) / ( $frames - 1) * $i]
KeyGoto a $t
}
}
# Goto keyframe #
proc KeyGoto {keyFrame t} {
upvar $keyFrame a
set object $a(object)
set method $a(method)
set depends [llength $a(0)]
set keyCommand "$object $method "
for {set j 0} {$j < $depends} {incr j} {
lappend keyCommand "[$object${method}Spline$j Evaluate $t]"
}
if {$a(debug) == 1} {puts "$keyCommand"}
eval $keyCommand
KeyRender
}
# Called after keyframe is executed
proc KeyRender {} {
renWin Render
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment