]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/tcl/to_change/volTkInteractor.tcl
creaMaracasVisu Library
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / tcl / to_change / volTkInteractor.tcl
1
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}
13 }
14
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}
26 }
27
28 # Global variable keeps track of whether active renderer was found
29 set RendererFound 0
30
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} {
35       return
36    }
37    SetWidgetVariableValue $widget InExpose 1
38    update
39    [$widget GetRenderWindow] Render
40    SetWidgetVariableValue $widget InExpose 0
41 }
42
43 # Create event bindings
44 #
45 proc Render {} {
46     global CurrentCamera CurrentLight CurrentRenderWindow
47
48     eval $CurrentLight SetPosition [$CurrentCamera GetPosition]
49     eval $CurrentLight SetFocalPoint [$CurrentCamera GetFocalPoint]
50
51     $CurrentRenderWindow Render
52 }
53
54 proc UpdateRenderer {widget x y} {
55     global CurrentCamera CurrentLight 
56     global CurrentRenderWindow CurrentRenderer
57     global RendererFound LastX LastY
58     global WindowCenterX WindowCenterY
59
60     # Get the renderer window dimensions
61     set WindowX [lindex [$widget configure -width] 4]
62     set WindowY [lindex [$widget configure -height] 4]
63
64     # Find which renderer event has occurred in
65     set CurrentRenderWindow [$widget GetRenderWindow]
66     set renderers [$CurrentRenderWindow GetRenderers]
67     set numRenderers [$renderers GetNumberOfItems]
68
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} {
81             set RendererFound 1
82             set WindowCenterX [expr double($WindowX)*(($vpxmax - $vpxmin)/2.0\
83                                 + $vpxmin)]
84             set WindowCenterY [expr double($WindowY)*(($vpymax - $vpymin)/2.0\
85                                 + $vpymin)]
86             break
87         }
88     }
89     
90     set CurrentCamera [$CurrentRenderer GetActiveCamera]
91     set lights [$CurrentRenderer GetLights]
92     $lights InitTraversal; set CurrentLight [$lights GetNextItem]
93
94     set LastX $x
95     set LastY $y
96 }
97
98 proc Enter {widget x y} {
99     global oldFocus
100
101     set oldFocus [focus]
102     focus $widget
103     UpdateRenderer $widget $x $y
104 }
105
106 proc StartMotionOrWindowLevelInteraction {widget x y} {
107     global CurrentCamera CurrentLight 
108     global CurrentRenderWindow CurrentRenderer
109     global LastX LastY
110     global RendererFound
111     global vFnMPR
112
113   UpdateRenderer $widget $x $y
114   if { ! $RendererFound } { return }
115
116   if { 1 == 1} {
117
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
127   }
128 }
129
130 proc EndMotionOrWindowLevelInteraction {widget x y} {
131     global CurrentRenderWindow
132     global RendererFound
133     global vFnMPR
134
135     if { ! $RendererFound } {return}
136
137     if {$vFnMPR(zoom) == 1} {
138       $CurrentRenderWindow SetDesiredUpdateRate 0.01
139       Render
140   } elseif {$vFnMPR(winlevel) == 1} {
141       $CurrentRenderWindow SetDesiredUpdateRate 0.01
142       Render
143   }
144 }
145
146 proc PanOrRotate {widget x y} {
147     global LastX LastY
148     global RendererFound
149     global CurrentRenderer CurrentCamera
150     global WindowCenterX WindowCenterY LastX LastY
151     global vFnMPR
152
153   if { ! $RendererFound } { return }
154
155   if {$vFnMPR(rotate) == 1} {
156     $CurrentCamera Azimuth [expr (($LastX - $x)/3.0)]
157     $CurrentCamera Elevation [expr (($y - $LastY)/3.0)]
158     $CurrentCamera OrthogonalizeViewUp
159
160     set LastX $x
161     set LastY $y
162
163     Render
164   } elseif {$vFnMPR(pan) == 1} {
165
166     if { ! $RendererFound } { return }
167
168     set FPoint [$CurrentCamera GetFocalPoint]
169         set FPoint0 [lindex $FPoint 0]
170         set FPoint1 [lindex $FPoint 1]
171         set FPoint2 [lindex $FPoint 2]
172
173     set PPoint [$CurrentCamera GetPosition]
174         set PPoint0 [lindex $PPoint 0]
175         set PPoint1 [lindex $PPoint 1]
176         set PPoint2 [lindex $PPoint 2]
177
178     $CurrentRenderer SetWorldPoint $FPoint0 $FPoint1 $FPoint2 1.0
179     $CurrentRenderer WorldToDisplay
180     set DPoint [$CurrentRenderer GetDisplayPoint]
181     set focalDepth [lindex $DPoint 2]
182
183     set APoint0 [expr $WindowCenterX + ($x - $LastX)]
184     set APoint1 [expr $WindowCenterY - ($y - $LastY)]
185
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]
197     }
198
199     $CurrentCamera SetFocalPoint \
200       [expr ($FPoint0 - $RPoint0)/2.0 + $FPoint0] \
201       [expr ($FPoint1 - $RPoint1)/2.0 + $FPoint1] \
202       [expr ($FPoint2 - $RPoint2)/2.0 + $FPoint2]
203
204     $CurrentCamera SetPosition \
205       [expr ($FPoint0 - $RPoint0)/2.0 + $PPoint0] \
206       [expr ($FPoint1 - $RPoint1)/2.0 + $PPoint1] \
207       [expr ($FPoint2 - $RPoint2)/2.0 + $PPoint2]
208
209     set LastX $x
210     set LastY $y
211
212     Render
213   }
214 }
215
216 proc PanOrRedim {widget x y} {
217     global LastX LastY
218     global RendererFound
219     global CurrentRenderer CurrentCamera
220     global WindowCenterX WindowCenterY LastX LastY
221     global vFnMPR
222
223     if { ! $RendererFound } { return }
224
225     set FPoint [$CurrentCamera GetFocalPoint]
226         set FPoint0 [lindex $FPoint 0]
227         set FPoint1 [lindex $FPoint 1]
228         set FPoint2 [lindex $FPoint 2]
229
230     set PPoint [$CurrentCamera GetPosition]
231         set PPoint0 [lindex $PPoint 0]
232         set PPoint1 [lindex $PPoint 1]
233         set PPoint2 [lindex $PPoint 2]
234
235     $CurrentRenderer SetWorldPoint $FPoint0 $FPoint1 $FPoint2 1.0
236     $CurrentRenderer WorldToDisplay
237     set DPoint [$CurrentRenderer GetDisplayPoint]
238     set focalDepth [lindex $DPoint 2]
239
240     set APoint0 [expr $WindowCenterX + ($x - $LastX)]
241     set APoint1 [expr $WindowCenterY - ($y - $LastY)]
242
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]
254     }
255
256     $CurrentCamera SetFocalPoint \
257       [expr ($FPoint0 - $RPoint0)/2.0 + $FPoint0] \
258       [expr ($FPoint1 - $RPoint1)/2.0 + $FPoint1] \
259       [expr ($FPoint2 - $RPoint2)/2.0 + $FPoint2]
260
261     $CurrentCamera SetPosition \
262       [expr ($FPoint0 - $RPoint0)/2.0 + $PPoint0] \
263       [expr ($FPoint1 - $RPoint1)/2.0 + $PPoint1] \
264       [expr ($FPoint2 - $RPoint2)/2.0 + $PPoint2]
265
266     set LastX $x
267     set LastY $y
268
269     Render
270 }
271
272
273 proc ZoomOrWinLevel {widget x y} {
274     global CurrentCamera
275     global LastX LastY
276     global RendererFound
277     global vFnMPR
278
279   if { ! $RendererFound } { return }
280
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
289
290     set LastX $x
291     set LastY $y
292
293     Render
294   } elseif {$vFnMPR(winlevel) == 1} {
295       UpdateWindowLevelInteraction $widget $x $y
296   }
297 }
298
299 proc Reset {widget x y} {
300     global CurrentRenderWindow
301     global RendererFound
302     global CurrentRenderer
303     global vFnMPR
304
305     # Get the renderer window dimensions
306     set WindowX [lindex [$widget configure -width] 4]
307     set WindowY [lindex [$widget configure -height] 4]
308
309     # Find which renderer event has occurred in
310     set CurrentRenderWindow [$widget GetRenderWindow]
311     set renderers [$CurrentRenderWindow GetRenderers]
312     set numRenderers [$renderers GetNumberOfItems]
313
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]
319
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} {
327             set RendererFound 1
328             break
329         }
330     }
331
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
337     }
338
339     #vtkFnMPR(ColorLookupTable) SetWindow 255
340     #vtkFnMPR(ColorLookupTable) SetLevel 128
341
342     vtkFnMPR(ColorLookupTable) SetWindow $vFnMPR(intmax)
343     vtkFnMPR(ColorLookupTable) SetLevel [expr $vFnMPR(intmax) / 4]
344
345     Render
346 }
347
348 proc Wireframe {} {
349     global CurrentRenderer
350
351     set actors [$CurrentRenderer GetActors]
352
353     $actors InitTraversal
354     set actor [$actors GetNextItem]
355     while { $actor != "" } {
356         [$actor GetProperty] SetRepresentationToWireframe
357         set actor [$actors GetNextItem]
358     }
359
360     Render
361 }
362
363 proc Surface {} {
364     global CurrentRenderer
365
366     set actors [$CurrentRenderer GetActors]
367
368     $actors InitTraversal
369     set actor [$actors GetNextItem]
370     while { $actor != "" } {
371         [$actor GetProperty] SetRepresentationToSurface
372         set actor [$actors GetNextItem]
373     }
374
375     Render
376 }
377
378
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} {
382    global vFnMPR
383
384    # get the widgets dimensions
385    set width [lindex [$widget configure -width] 4]
386    set height [lindex [$widget configure -height] 4]
387
388    # get the old window level values
389    set window [GetWidgetVariableValue $widget Window]
390    set level [GetWidgetVariableValue $widget Level]
391
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]
395
396    # compute normalized delta
397    set dx [expr 4.0 * ($x - $start_x) / $width]
398    set dy [expr 4.0 * ($start_y - $y) / $height]
399
400    # scale by current values 
401    set dx [expr $dx * $window]
402    set dy [expr $dy * $level]
403
404    #puts "   update: ($x, $y), dx = $dx, dy = $dy"
405
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]}
409
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]
414    } else {
415       set new_level [expr $level - $dy]
416    }
417
418    # zero window or level can trap the value.
419    # put a limit of 1 / 100 value
420
421
422    # if window is negative, then delta level should flip (down is dark).
423    if {$new_window < 0.0} {set dy [expr -$dy]}
424
425    vtkFnMPR(ColorLookupTable) SetWindow $new_window
426    vtkFnMPR(ColorLookupTable) SetLevel $new_level
427
428    .frText.t insert end "Window : $new_window  -  Level : $new_level\n"
429
430    Render
431 }