2 proc BindTkRenderWidget {widget} {
3 bind $widget <Any-ButtonPress> {StartMotionOrWindowLevelInteraction %W %x %y}
4 bind $widget <Any-ButtonRelease> {EndMotionOrWindowLevelInteraction %W %x %y}
5 bind $widget <B1-Motion> {PanOrRotate %W %x %y}
6 bind $widget <B2-Motion> {ZoomOrWinLevel %W %x %y}
7 bind $widget <B3-Motion> {ZoomOrWinLevel %W %x %y}
8 bind $widget <KeyPress-r> {Reset %W %x %y}
9 bind $widget <KeyPress-u> {wm deiconify .vtkInteract}
10 # bind $widget <Enter> {Enter %W %x %y}
11 # bind $widget <Leave> {focus $oldFocus}
12 bind $widget <Expose> {Expose %W}
15 proc BindTkRenderWidget2D {widget} {
16 bind $widget <Any-ButtonPress> {StartMotionOrWindowLevelInteraction %W %x %y}
17 bind $widget <Any-ButtonRelease> {EndMotionOrWindowLevelInteraction %W %x %y}
18 bind $widget <B1-Motion> {PanOrRedim %W %x %y}
19 bind $widget <B2-Motion> {ZoomOrWinLevel %W %x %y}
20 bind $widget <B3-Motion> {ZoomOrWinLevel %W %x %y}
21 bind $widget <KeyPress-r> {Reset %W %x %y}
22 bind $widget <KeyPress-u> {wm deiconify .vtkInteract}
23 # bind $widget <Enter> {Enter %W %x %y}
24 # bind $widget <Leave> {focus $oldFocus}
25 bind $widget <Expose> {Expose %W}
28 # Global variable keeps track of whether active renderer was found
31 # a litle more complex than just "bind $widget <Expose> {%W Render}"
32 # we have to handle all pending expose events otherwise they que up.
33 proc Expose {widget} {
34 if {[GetWidgetVariableValue $widget InExpose] == 1} {
37 SetWidgetVariableValue $widget InExpose 1
39 [$widget GetRenderWindow] Render
40 SetWidgetVariableValue $widget InExpose 0
43 # Create event bindings
46 global CurrentCamera CurrentLight CurrentRenderWindow
48 eval $CurrentLight SetPosition [$CurrentCamera GetPosition]
49 eval $CurrentLight SetFocalPoint [$CurrentCamera GetFocalPoint]
51 $CurrentRenderWindow Render
54 proc UpdateRenderer {widget x y} {
55 global CurrentCamera CurrentLight
56 global CurrentRenderWindow CurrentRenderer
57 global RendererFound LastX LastY
58 global WindowCenterX WindowCenterY
60 # Get the renderer window dimensions
61 set WindowX [lindex [$widget configure -width] 4]
62 set WindowY [lindex [$widget configure -height] 4]
64 # Find which renderer event has occurred in
65 set CurrentRenderWindow [$widget GetRenderWindow]
66 set renderers [$CurrentRenderWindow GetRenderers]
67 set numRenderers [$renderers GetNumberOfItems]
69 $renderers InitTraversal; set RendererFound 0
70 for {set i 0} {$i < $numRenderers} {incr i} {
71 set CurrentRenderer [$renderers GetNextItem]
72 set vx [expr double($x) / $WindowX]
73 set vy [expr ($WindowY - double($y)) / $WindowY]
74 set viewport [$CurrentRenderer GetViewport]
75 set vpxmin [lindex $viewport 0]
76 set vpymin [lindex $viewport 1]
77 set vpxmax [lindex $viewport 2]
78 set vpymax [lindex $viewport 3]
79 if { $vx >= $vpxmin && $vx <= $vpxmax && \
80 $vy >= $vpymin && $vy <= $vpymax} {
82 set WindowCenterX [expr double($WindowX)*(($vpxmax - $vpxmin)/2.0\
84 set WindowCenterY [expr double($WindowY)*(($vpymax - $vpymin)/2.0\
90 set CurrentCamera [$CurrentRenderer GetActiveCamera]
91 set lights [$CurrentRenderer GetLights]
92 $lights InitTraversal; set CurrentLight [$lights GetNextItem]
98 proc Enter {widget x y} {
103 UpdateRenderer $widget $x $y
106 proc StartMotionOrWindowLevelInteraction {widget x y} {
107 global CurrentCamera CurrentLight
108 global CurrentRenderWindow CurrentRenderer
113 UpdateRenderer $widget $x $y
114 if { ! $RendererFound } { return }
118 $CurrentRenderWindow SetDesiredUpdateRate 3.0
119 } elseif {$vFnMPR(winlevel) == 1} {
120 puts "Modificar WindowLevel"
121 # save the starting mouse position and the corresponding window/level
122 SetWidgetVariableValue $widget X $x
123 SetWidgetVariableValue $widget Y $y
124 SetWidgetVariableValue $widget Window [vtkFnMPR(ColorLookupTable) GetWindow]
125 SetWidgetVariableValue $widget Level [vtkFnMPR(ColorLookupTable) GetLevel]
126 UpdateWindowLevelInteraction $widget $x $y
130 proc EndMotionOrWindowLevelInteraction {widget x y} {
131 global CurrentRenderWindow
135 if { ! $RendererFound } {return}
137 if {$vFnMPR(zoom) == 1} {
138 $CurrentRenderWindow SetDesiredUpdateRate 0.01
140 } elseif {$vFnMPR(winlevel) == 1} {
141 $CurrentRenderWindow SetDesiredUpdateRate 0.01
146 proc PanOrRotate {widget x y} {
149 global CurrentRenderer CurrentCamera
150 global WindowCenterX WindowCenterY LastX LastY
153 if { ! $RendererFound } { return }
155 if {$vFnMPR(rotate) == 1} {
156 $CurrentCamera Azimuth [expr (($LastX - $x)/3.0)]
157 $CurrentCamera Elevation [expr (($y - $LastY)/3.0)]
158 $CurrentCamera OrthogonalizeViewUp
164 } elseif {$vFnMPR(pan) == 1} {
166 if { ! $RendererFound } { return }
168 set FPoint [$CurrentCamera GetFocalPoint]
169 set FPoint0 [lindex $FPoint 0]
170 set FPoint1 [lindex $FPoint 1]
171 set FPoint2 [lindex $FPoint 2]
173 set PPoint [$CurrentCamera GetPosition]
174 set PPoint0 [lindex $PPoint 0]
175 set PPoint1 [lindex $PPoint 1]
176 set PPoint2 [lindex $PPoint 2]
178 $CurrentRenderer SetWorldPoint $FPoint0 $FPoint1 $FPoint2 1.0
179 $CurrentRenderer WorldToDisplay
180 set DPoint [$CurrentRenderer GetDisplayPoint]
181 set focalDepth [lindex $DPoint 2]
183 set APoint0 [expr $WindowCenterX + ($x - $LastX)]
184 set APoint1 [expr $WindowCenterY - ($y - $LastY)]
186 $CurrentRenderer SetDisplayPoint $APoint0 $APoint1 $focalDepth
187 $CurrentRenderer DisplayToWorld
188 set RPoint [$CurrentRenderer GetWorldPoint]
189 set RPoint0 [lindex $RPoint 0]
190 set RPoint1 [lindex $RPoint 1]
191 set RPoint2 [lindex $RPoint 2]
192 set RPoint3 [lindex $RPoint 3]
193 if { $RPoint3 != 0.0 } {
194 set RPoint0 [expr $RPoint0 / $RPoint3]
195 set RPoint1 [expr $RPoint1 / $RPoint3]
196 set RPoint2 [expr $RPoint2 / $RPoint3]
199 $CurrentCamera SetFocalPoint \
200 [expr ($FPoint0 - $RPoint0)/2.0 + $FPoint0] \
201 [expr ($FPoint1 - $RPoint1)/2.0 + $FPoint1] \
202 [expr ($FPoint2 - $RPoint2)/2.0 + $FPoint2]
204 $CurrentCamera SetPosition \
205 [expr ($FPoint0 - $RPoint0)/2.0 + $PPoint0] \
206 [expr ($FPoint1 - $RPoint1)/2.0 + $PPoint1] \
207 [expr ($FPoint2 - $RPoint2)/2.0 + $PPoint2]
216 proc PanOrRedim {widget x y} {
219 global CurrentRenderer CurrentCamera
220 global WindowCenterX WindowCenterY LastX LastY
223 if { ! $RendererFound } { return }
225 set FPoint [$CurrentCamera GetFocalPoint]
226 set FPoint0 [lindex $FPoint 0]
227 set FPoint1 [lindex $FPoint 1]
228 set FPoint2 [lindex $FPoint 2]
230 set PPoint [$CurrentCamera GetPosition]
231 set PPoint0 [lindex $PPoint 0]
232 set PPoint1 [lindex $PPoint 1]
233 set PPoint2 [lindex $PPoint 2]
235 $CurrentRenderer SetWorldPoint $FPoint0 $FPoint1 $FPoint2 1.0
236 $CurrentRenderer WorldToDisplay
237 set DPoint [$CurrentRenderer GetDisplayPoint]
238 set focalDepth [lindex $DPoint 2]
240 set APoint0 [expr $WindowCenterX + ($x - $LastX)]
241 set APoint1 [expr $WindowCenterY - ($y - $LastY)]
243 $CurrentRenderer SetDisplayPoint $APoint0 $APoint1 $focalDepth
244 $CurrentRenderer DisplayToWorld
245 set RPoint [$CurrentRenderer GetWorldPoint]
246 set RPoint0 [lindex $RPoint 0]
247 set RPoint1 [lindex $RPoint 1]
248 set RPoint2 [lindex $RPoint 2]
249 set RPoint3 [lindex $RPoint 3]
250 if { $RPoint3 != 0.0 } {
251 set RPoint0 [expr $RPoint0 / $RPoint3]
252 set RPoint1 [expr $RPoint1 / $RPoint3]
253 set RPoint2 [expr $RPoint2 / $RPoint3]
256 $CurrentCamera SetFocalPoint \
257 [expr ($FPoint0 - $RPoint0)/2.0 + $FPoint0] \
258 [expr ($FPoint1 - $RPoint1)/2.0 + $FPoint1] \
259 [expr ($FPoint2 - $RPoint2)/2.0 + $FPoint2]
261 $CurrentCamera SetPosition \
262 [expr ($FPoint0 - $RPoint0)/2.0 + $PPoint0] \
263 [expr ($FPoint1 - $RPoint1)/2.0 + $PPoint1] \
264 [expr ($FPoint2 - $RPoint2)/2.0 + $PPoint2]
273 proc ZoomOrWinLevel {widget x y} {
279 if { ! $RendererFound } { return }
281 if {$vFnMPR(zoom) == 1} {
282 set zoomFactor [expr pow(1.01,($y - $LastY))]
283 set clippingRange [$CurrentCamera GetClippingRange]
284 set minRange [lindex $clippingRange 0]
285 set maxRange [lindex $clippingRange 1]
286 $CurrentCamera SetClippingRange [expr $minRange / $zoomFactor] \
287 [expr $maxRange / $zoomFactor]
288 $CurrentCamera Dolly $zoomFactor
294 } elseif {$vFnMPR(winlevel) == 1} {
295 UpdateWindowLevelInteraction $widget $x $y
299 proc Reset {widget x y} {
300 global CurrentRenderWindow
302 global CurrentRenderer
305 # Get the renderer window dimensions
306 set WindowX [lindex [$widget configure -width] 4]
307 set WindowY [lindex [$widget configure -height] 4]
309 # Find which renderer event has occurred in
310 set CurrentRenderWindow [$widget GetRenderWindow]
311 set renderers [$CurrentRenderWindow GetRenderers]
312 set numRenderers [$renderers GetNumberOfItems]
314 $renderers InitTraversal; set RendererFound 0
315 for {set i 0} {$i < $numRenderers} {incr i} {
316 set CurrentRenderer [$renderers GetNextItem]
317 set vx [expr double($x) / $WindowX]
318 set vy [expr ($WindowY - double($y)) / $WindowY]
320 set viewport [$CurrentRenderer GetViewport]
321 set vpxmin [lindex $viewport 0]
322 set vpymin [lindex $viewport 1]
323 set vpxmax [lindex $viewport 2]
324 set vpymax [lindex $viewport 3]
325 if { $vx >= $vpxmin && $vx <= $vpxmax && \
326 $vy >= $vpymin && $vy <= $vpymax} {
332 if { $RendererFound } {
333 [$CurrentRenderer GetActiveCamera] SetPosition 32.5 32.5 1000
334 [$CurrentRenderer GetActiveCamera] SetViewUp 0 1 0
335 [$CurrentRenderer GetActiveCamera] ComputeViewPlaneNormal
336 $CurrentRenderer ResetCamera
339 #vtkFnMPR(ColorLookupTable) SetWindow 255
340 #vtkFnMPR(ColorLookupTable) SetLevel 128
342 vtkFnMPR(ColorLookupTable) SetWindow $vFnMPR(intmax)
343 vtkFnMPR(ColorLookupTable) SetLevel [expr $vFnMPR(intmax) / 4]
349 global CurrentRenderer
351 set actors [$CurrentRenderer GetActors]
353 $actors InitTraversal
354 set actor [$actors GetNextItem]
355 while { $actor != "" } {
356 [$actor GetProperty] SetRepresentationToWireframe
357 set actor [$actors GetNextItem]
364 global CurrentRenderer
366 set actors [$CurrentRenderer GetActors]
368 $actors InitTraversal
369 set actor [$actors GetNextItem]
370 while { $actor != "" } {
371 [$actor GetProperty] SetRepresentationToSurface
372 set actor [$actors GetNextItem]
379 # clicking on the window sets up sliders with current value at mouse,
380 # and scaled so that the whole window represents x4 change.
381 proc UpdateWindowLevelInteraction {widget x y} {
384 # get the widgets dimensions
385 set width [lindex [$widget configure -width] 4]
386 set height [lindex [$widget configure -height] 4]
388 # get the old window level values
389 set window [GetWidgetVariableValue $widget Window]
390 set level [GetWidgetVariableValue $widget Level]
392 # get starting x, y and window/level values to compute delta
393 set start_x [GetWidgetVariableValue $widget X]
394 set start_y [GetWidgetVariableValue $widget Y]
396 # compute normalized delta
397 set dx [expr 4.0 * ($x - $start_x) / $width]
398 set dy [expr 4.0 * ($start_y - $y) / $height]
400 # scale by current values
401 set dx [expr $dx * $window]
402 set dy [expr $dy * $level]
404 #puts " update: ($x, $y), dx = $dx, dy = $dy"
406 # abs so that direction does not flip
407 if {$window < 0.0} {set dx [expr -$dx]}
408 if {$level < 0.0} {set dy [expr -$dy]}
410 # compute new window level
411 set new_window [expr $dx + $window]
412 if {$new_window < 0.0} {
413 set new_level [expr $dy + $level]
415 set new_level [expr $level - $dy]
418 # zero window or level can trap the value.
419 # put a limit of 1 / 100 value
422 # if window is negative, then delta level should flip (down is dark).
423 if {$new_window < 0.0} {set dy [expr -$dy]}
425 vtkFnMPR(ColorLookupTable) SetWindow $new_window
426 vtkFnMPR(ColorLookupTable) SetLevel $new_level
428 .frText.t insert end "Window : $new_window - Level : $new_level\n"