|
From: Gordon K. <kin...@us...> - 2004-05-10 10:27:18
|
Update of /cvsroot/teem/teem-vtk/camAnim In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14485 Added Files: camAnim.tcl Removed Files: isocam.tcl Log Message: rename --- isocam.tcl DELETED --- --- NEW FILE: camAnim.tcl --- # # teem: Gordon Kindlmann's research software # Copyright (C) 2003, 2001, 2000, 1999, 1998 University of Utah # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # ### isocam.tcl ### ### The purpose of this program is to generate camera information ### usable in teem's miter and mrender commands, as guided by an ### isosurface of a scalar field. The field is centered inside the ### same bi-unit cube that is used by hoover. Note that VTK assumes ### node-centered samples. ### if {$argc < 1} { puts "usage: isocam.tcl <vtk volume file>" exit 1 } set frameShown 0 set cubeWidth 0.04 set cursorX 0 set cursorY 0 set cursorZ 0 ### ### this is set to 1 only after everything has been set up ### set going 0 ### v3Sub, v3Add, v3Scale ### ### 3-vector utility functions ### proc v3Dot {a b} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] set bx [lindex $b 0]; set by [lindex $b 1]; set bz [lindex $b 2] return "[expr $ax*$bx + $ay*$by + $az*$bz]" } proc v3Sub {a b} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] set bx [lindex $b 0]; set by [lindex $b 1]; set bz [lindex $b 2] return "[expr $ax - $bx] [expr $ay - $by] [expr $az - $bz]" } proc v3Add {a b} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] set bx [lindex $b 0]; set by [lindex $b 1]; set bz [lindex $b 2] return "[expr $ax + $bx] [expr $ay + $by] [expr $az + $bz]" } proc v3Scale {s a} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] return "[expr $s*$ax] [expr $s*$ay] [expr $s*$az]" } proc v3Len {a} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] return [expr sqrt($ax*$ax + $ay*$ay + $az*$az)] } proc v3Norm {a} { return [v3Scale [expr 1.0/[v3Len $a]] $a] } proc v3Cross {a b} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] set bx [lindex $b 0]; set by [lindex $b 1]; set bz [lindex $b 2] return "[expr $ay*$bz - $az*$by] \ [expr $az*$bx - $ax*$bz] \ [expr $ax*$by - $ay*$bx]" } proc short {s} { return [expr round($s*1000.0)/1000.0] } proc v3Short {a} { set ax [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] return "[short $ax] [short $ay] [short $az]" } proc v3Max {a} { set m [lindex $a 0]; set ay [lindex $a 1]; set az [lindex $a 2] if {$m < $ay} {set m $ay} if {$m < $az} {set m $az} return $m } proc v3Angle {a b} { if {[v3Dot $a $b] < 0.0} { set ret [expr 3.1415926 - 2*asin([v3Len [v3Add $a $b]]/2)] } else { set ret [expr 2*asin([v3Len [v3Sub $a $b]]/2)] } return [short [expr 180*$ret/3.1415926]] } proc m3Transpose {m} { set c0 [list [lindex [lindex $m 0] 0] \ [lindex [lindex $m 1] 0] \ [lindex [lindex $m 2] 0]] set c1 [list [lindex [lindex $m 0] 1] \ [lindex [lindex $m 1] 1] \ [lindex [lindex $m 2] 1]] set c2 [list [lindex [lindex $m 0] 2] \ [lindex [lindex $m 1] 2] \ [lindex [lindex $m 2] 2]] return [list $c0 $c1 $c2] } ### ### All event-driven callbacks ### set oldView "" set oldUp "" set buttonPressed 0 proc buttonDownCB {} { global oldView global oldUp global buttonPressed global camera set buttonPressed 1 set oldView [v3Short [v3Norm [v3Sub [$camera GetPosition] \ [$camera GetFocalPoint]]]] set oldUp [v3Short [$camera GetViewUp]] } proc buttonUpCB {} { global oldView global oldUp global axisEnt angleEnt global buttonPressed global camera global cursor cursorX cursorY cursorZ puts "buttonUpCB: hello" set buttonPressed 0 set newView [v3Norm [v3Sub [$camera GetPosition] \ [$camera GetFocalPoint]]] set newUp [$camera GetViewUp] set axis [v3Cross $newView $oldView] if {0 == [v3Len $axis]} { set axis [v3Cross $newUp $oldUp] } if {0 == [v3Len $axis]} { set axisEnt "(none)" set angleEnt "(none)" return } set axisEnt [v3Short [v3Norm $axis]] set angleEnt [v3Angle $newView $oldView] render return } proc configureCB {} { global imgSize set imgSize [iren GetSize] } proc moveCB {} { global buttonPressed global lockUp global upVector global camera global cursor cursorX cursorY cursorZ if {!$buttonPressed} { return } if ($lockUp) { eval $camera SetViewUp $upVector } else { set upVector [v3Short [$camera GetViewUp]] } set cursor [v3Short [$camera GetFocalPoint]] set cursorX [lindex $cursor 0] set cursorY [lindex $cursor 1] set cursorZ [lindex $cursor 2] eval cube SetBounds [getCubeBounds] } ### ### renderer, window, interactor, etc ### vtkRenderer ren vtkRenderWindow renWin renWin AddRenderer ren vtkWindowToImageFilter win2png win2png SetInput renWin vtkPNGWriter pngWriter pngWriter SetInput [win2png GetOutput] vtkInteractorStyleTrackballCamera style vtkRenderWindowInteractor iren iren SetRenderWindow renWin iren SetInteractorStyle style iren AddObserver LeftButtonPressEvent buttonDownCB iren AddObserver MiddleButtonPressEvent buttonDownCB iren AddObserver RightButtonPressEvent buttonDownCB iren AddObserver LeftButtonReleaseEvent buttonUpCB iren AddObserver MiddleButtonReleaseEvent buttonUpCB iren AddObserver RightButtonReleaseEvent buttonUpCB iren AddObserver MouseMoveEvent moveCB iren AddObserver ConfigureEvent configureCB ### ### volume reader ### vtkStructuredPointsReader reader reader SetFileName [lindex $argv 0] reader Update ### ### compute the bi-unit bounding box information from volume ### ### ext{X,Y,Z} : original volume extents in X,Y,Z ### hvs{X,Y,Z} : half bi-unit volume size in X,Y,Z axes ### set ext [[reader GetOutput] GetExtent] set spc [[reader GetOutput] GetSpacing] set extX [expr 1.0*[lindex $ext 1]*[lindex $spc 0]] set extY [expr 1.0*[lindex $ext 3]*[lindex $spc 1]] set extZ [expr 1.0*[lindex $ext 5]*[lindex $spc 2]] set maxext [v3Max {$extX $extY $extZ}] set hvsX [expr $extX/$maxext] set hvsY [expr $extY/$maxext] set hvsZ [expr $extZ/$maxext] ### ### set up transform to map volume into bi-unit cube ### vtkTransform tx tx PostMultiply tx Identity tx Scale [expr 2.0*$hvsX/$extX] \ [expr 2.0*$hvsY/$extY] [expr 2.0*$hvsZ/$extZ] tx Translate [expr -$hvsX] [expr -$hvsY] [expr -$hvsZ] ### ### The spheres mark the corners of the bi-unit cube ### set radius 0.04 for {set i 0} {$i <= 1} {incr i} { for {set j 0} {$j <= 1} {incr j} { for {set k 0} {$k <= 1} {incr k} { vtkSphereSource sphere$i$j$k sphere$i$j$k SetCenter [expr 2*$i - 1] [expr 2*$j - 1] [expr 2*$k - 1] sphere$i$j$k SetRadius $radius vtkPolyDataMapper sphereMapper$i$j$k sphereMapper$i$j$k SetInput [sphere$i$j$k GetOutput] vtkActor sphereActor$i$j$k sphereActor$i$j$k SetMapper sphereMapper$i$j$k [sphereActor$i$j$k GetProperty] SetColor \ [expr 0.4 + 0.6*$i] [expr 0.4 + 0.6*$j] [expr 0.4 + 0.6*$k] } } } set rgb {{1.0 0.4 0.4} {0.4 1.0 0.4} {0.4 0.4 1.0}} eval [sphereActor100 GetProperty] SetColor [lindex $rgb 0] eval [sphereActor010 GetProperty] SetColor [lindex $rgb 1] eval [sphereActor001 GetProperty] SetColor [lindex $rgb 2] ### ### red, green, and blue cylinders for the X, Y, Z axes ### for {set i 0} {$i <= 2} {incr i} { vtkCylinderSource cyl$i cyl$i SetResolution 10 cyl$i SetRadius $radius cyl$i SetHeight 1.0 cyl$i SetCenter 0.0 0.5 0.0 vtkTransform cylT$i cylT$i Identity cylT$i Translate -1 -1 -1 vtkTransformFilter cylTF$i cylTF$i SetTransform cylT$i cylTF$i SetInput [cyl$i GetOutput] vtkPolyDataMapper cylMapper$i cylMapper$i SetInput [cylTF$i GetOutput] vtkActor cylActor$i cylActor$i SetMapper cylMapper$i eval [cylActor$i GetProperty] SetColor [lindex $rgb $i] } cylT0 RotateZ -90 cylT2 RotateX 90 ### ### cube for at-point cursor ### proc getCubeBounds {} { global cubeWidth cursorX cursorY cursorZ puts "getCubeBounds: $cursorX $cursorY $cursorZ" return [list [expr $cursorX - $cubeWidth] \ [expr $cursorX + $cubeWidth] \ [expr $cursorY - $cubeWidth] \ [expr $cursorY + $cubeWidth] \ [expr $cursorZ - $cubeWidth] \ [expr $cursorZ + $cubeWidth] ] } vtkOutlineSource cube eval cube SetBounds [getCubeBounds] vtkPolyDataMapper cubeMapper cubeMapper SetInput [cube GetOutput] vtkActor cubeActor cubeActor SetMapper cubeMapper [cubeActor GetProperty] SetColor 1 0 0 ren AddActor cubeActor ### ### rest of vtk pipeline ### vtkContourFilter iso iso SetInput [reader GetOutput] vtkTransformFilter txf txf SetTransform tx txf SetInput [iso GetOutput] vtkPolyDataMapper isoMapper isoMapper SetInput [txf GetOutput] isoMapper ScalarVisibilityOff vtkActor isoActor isoActor SetMapper isoMapper [isoActor GetProperty] SetColor 1 1 1 [isoActor GetProperty] SetOpacity 1.0 ren AddActor isoActor renWin SetSize 512 512 ren SetBackground 0.5 0.5 0.5 ### ### commands invoked via GUI ### set camCount 0 proc saveCam {} { global camCount global camera set is [iren GetSize] set fr [$camera GetPosition] set at [$camera GetFocalPoint] set up [$camera GetViewUp] set di [$camera GetDistance] set dn [lindex [$camera GetClippingRange] 0] set df [lindex [$camera GetClippingRange] 1] set theta [expr [$camera GetViewAngle]/2.0] set vm [expr tan((3.14159/180.0)*$theta)*$di] set um [expr $vm*[lindex $is 0]/[lindex $is 1]] set fname [format %03d.cam.txt $camCount] set file [open $fname "w"] puts $file "-fr $fr" puts $file "-at $at" puts $file "-up $up -rh" puts $file "-dn $dn -di $di -df $df" puts $file "-vr [expr -$vm] $vm -ur [expr -$um] $um" puts $file "-is $is" close $file incr camCount set fname [format %03d.cam.txt $camCount] .top.buttCam config -text "Save $fname" } proc newIsoValue {} { global isoValue #puts "newIsoValue; going=$going" iso SetValue 0 $isoValue render } proc newViewAngle {blah} { global viewAngle global camera set oldva [$camera GetViewAngle] set fact [expr tan((3.14159/180.0)*$oldva/2)/ \ tan((3.14159/180.0)*$viewAngle/2)] set nd [expr $fact*[$camera GetDistance]] $camera SetViewAngle $viewAngle set at [$camera GetFocalPoint] set norm [$camera GetViewPlaneNormal] eval $camera SetPosition [v3Add $at [v3Scale $nd $norm]] render } proc newUpVector {} { global camera upVector eval $camera SetViewUp $upVector render } proc lockUpCmd {} { global lockUp global upVector global camera if ($lockUp) { eval $camera SetViewUp $upVector } else { $camera OrthogonalizeViewUp set upVector [v3Short [$camera GetViewUp]] } } proc newImgSize {} { global imgSize if {2 != [llength $imgSize]} { configureCB return } set nx [expr round([lindex $imgSize 0])] set ny [expr round([lindex $imgSize 1])] if {!($nx > 0 && $ny > 0)} { configureCB return } set oldsize [iren GetSize] set ox [lindex $oldsize 0] set oy [lindex $oldsize 1] if {$nx != $ox || $ny != $oy} { iren UpdateSize $nx $ny render } return } proc savePng {} { global pngCount set fname [format %03d.png $pngCount] win2png Modified pngWriter SetFileName $fname pngWriter Write incr pngCount set fname [format %03d.png $pngCount] .top.buttPng config -text "Save $fname" } proc toggleFrame {} { global frameShown for {set i 0} {$i <= 1} {incr i} { for {set j 0} {$j <= 1} {incr j} { for {set k 0} {$k <= 1} {incr k} { if {$frameShown} { ren RemoveActor sphereActor$i$j$k } else { ren AddActor sphereActor$i$j$k } } } } for {set i 0} {$i <= 2} {incr i} { if {$frameShown} { ren RemoveActor cylActor$i } else { ren AddActor cylActor$i } } if {$frameShown} { .top.buttFrame config -text "Show Frame" } else { .top.buttFrame config -text "Hide Frame" } render set frameShown [expr 1 - $frameShown] } proc render {} { global going if {$going} { ren ResetCameraClippingRange renWin Render puts "--- Render ---" } } ### ### Tk GUI ### toplevel .top wm title .top "Isocam: isosurface-based camera placement widget" set row 0 label .top.isoText -text "Isovalue" set isoRange [[[[reader GetOutput] GetPointData] GetScalars] GetRange] set isoValue [lindex $isoRange 1] set hi [lindex $isoRange 1] set lo [lindex $isoRange 0] set len 400 set res [expr (($hi - $lo)/($len -1))/100.0] scale .top.isoValue -len $len -from $lo -to $hi -res $res -ori h -var isoValue bind .top.isoValue <ButtonRelease> newIsoValue grid .top.isoText -s e -row $row -column 0 grid .top.isoValue -s w -row $row -column 1 -columnspan 3 incr row set viewAngle 15 label .top.vatext -text "View Angle" scale .top.vavalue -len $len -from 1 -to 60 -res 0.01 -ori h \ -var viewAngle -command newViewAngle grid .top.vatext -s e -row $row -column 0 grid .top.vavalue -s w -row $row -column 1 -columnspan 3 incr row label .top.axlab -text "Rotation Axis" grid .top.axlab -s e -row $row -column 0 set axisEnt "(none)" entry .top.axent -width 20 -textvariable axisEnt grid .top.axent -s w -row $row -column 1 label .top.nglab -text "Rotation Angle" grid .top.nglab -s e -row $row -column 2 set angleEnt "(none)" entry .top.ngent -width 10 -textvariable angleEnt grid .top.ngent -s w -row $row -column 3 incr row set lockUp 0 checkbutton .top.upcheck -text "Lock Up" -variable lockUp -command lockUpCmd grid .top.upcheck -s e -row $row -column 0 set upVector "" entry .top.upent -width 20 -textvariable upVector bind .top.upent <KeyRelease-Return> newUpVector grid .top.upent -s w -row $row -column 1 label .top.imgSizeLab -text "Image Size" grid .top.imgSizeLab -s e -row $row -column 2 set imgSize "" entry .top.imgSizeEnt -width 10 -textvariable imgSize grid .top.imgSizeEnt -s w -row $row -column 3 bind .top.imgSizeEnt <KeyRelease-Return> newImgSize grid rowconfigure .top $row -minsize 40 proc newCursorPos {blah} { global camera cursor cursorX cursorY cursorZ puts "newCursorPos: hello" set cursor [v3Short [list $cursorX $cursorY $cursorZ]] eval cube SetBounds [getCubeBounds] $camera SetFocalPoint $cursorX $cursorY $cursorZ render puts "newCursorPos: bye" } proc newCursor {} { global cursor cursorX cursorY cursorZ set cursorX [lindex $cursor 0] set cursorY [lindex $cursor 1] set cursorZ [lindex $cursor 2] newCursorPos blah } incr row label .top.clab -text "At Point" frame .top.c label .top.c.x -text X label .top.c.y -text Y label .top.c.z -text Z scale .top.c.xpos -len [expr $len/1.5] -from -1.4 -to 1.4 -res 0.001 -ori h \ -var cursorX -command newCursorPos -showvalue 0 scale .top.c.ypos -len [expr $len/1.5] -from -1.4 -to 1.4 -res 0.001 -ori h \ -var cursorY -command newCursorPos -showvalue 0 scale .top.c.zpos -len [expr $len/1.5] -from -1.4 -to 1.4 -res 0.001 -ori h \ -var cursorZ -command newCursorPos -showvalue 0 set cursor [v3Short [list $cursorX $cursorY $cursorZ]] entry .top.c.ent -width 20 -textvariable cursor bind .top.c.ent <KeyPress-Return> newCursor button .top.c.but -text "Hide Cursor" -command toggleCursor grid .top.c.ent -row 1 -col 0 grid .top.c.but -row 2 -col 0 grid .top.c.x -row 0 -col 1 grid .top.c.y -row 1 -col 1 grid .top.c.z -row 2 -col 1 grid .top.c.xpos -row 0 -col 2 grid .top.c.ypos -row 1 -col 2 grid .top.c.zpos -row 2 -col 2 grid .top.clab -s e -row $row -col 0 grid .top.c -s w -row $row -col 1 -columnspan 3 incr row set pngCount 0 set camCount 0 button .top.buttFrame -text "Hide Frame" -command toggleFrame grid .top.buttFrame -s w -row $row -column 1 button .top.buttCam -text "Save [format %03d $camCount].cam.txt" \ -command saveCam grid .top.buttCam -row $row -column 2 button .top.buttPng -text "Save [format %03d $pngCount].png" -command savePng grid .top.buttPng -s e -row $row -column 3 grid rowconfigure .top $row -minsize 40 incr row button .top.buttDie -text "Quit" -command exit grid .top.buttDie -s sw -row $row -column 0 grid rowconfigure .top $row -minsize 40 ### ### here we go ### toggleFrame newIsoValue configureCB wm withdraw . set camera [ren GetActiveCamera] #$camera SetViewUp 0 0 -1 #$camera SetPosition 2 7 -2 set upVector [$camera GetViewUp] newViewAngle blah ren ResetCameraClippingRange source anim.tcl set going 1 renWin Render newImgSize ren ResetCameraClippingRange |