source: trunk/gui/scripts/contourresult.tcl @ 1837

Last change on this file since 1837 was 1837, checked in by gah, 14 years ago

add -tkwait flag to molvisviewer addmethod

File size: 36.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: contourresult - contour plot in a ResultSet
3#
4#  This widget is a contour plot for 2D meshes with a scalar value.
5#  It is normally used in the ResultViewer to show results from the
6#  run of a Rappture tool.  Use the "add" and "delete" methods to
7#  control the dataobjs showing on the plot.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2005  Purdue Research Foundation
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16package require vtk
17package require vtkinteraction
18package require BLT
19package require Img
20
21option add *ContourResult.width 4i widgetDefault
22option add *ContourResult.height 4i widgetDefault
23option add *ContourResult.foreground black widgetDefault
24option add *ContourResult.controlBackground gray widgetDefault
25option add *ContourResult.controlDarkBackground #999999 widgetDefault
26option add *ContourResult.plotBackground black widgetDefault
27option add *ContourResult.plotForeground white widgetDefault
28option add *ContourResult.font \
29    -*-helvetica-medium-r-normal-*-12-* widgetDefault
30
31itcl::class Rappture::ContourResult {
32    inherit itk::Widget
33
34    itk_option define -plotforeground plotForeground Foreground ""
35    itk_option define -plotbackground plotBackground Background ""
36
37    constructor {args} { # defined below }
38    destructor { # defined below }
39
40    public method add {dataobj {settings ""}}
41    public method get {}
42    public method delete {args}
43    public method scale {args}
44    public method parameters {title args} { # do nothing }
45    public method download {option args}
46
47    protected method _rebuild {}
48    protected method _clear {}
49    protected method _zoom {option}
50    protected method _move {option x y}
51    protected method _slice {option args}
52    protected method _3dView {theta phi}
53    protected method _fixLimits {}
54    protected method _slicertip {axis}
55    protected method _color2rgb {color}
56
57    private variable _dlist ""     ;# list of data objects
58    private variable _dims ""      ;# dimensionality of data objects
59    private variable _obj2color    ;# maps dataobj => plotting color
60    private variable _obj2width    ;# maps dataobj => line width
61    private variable _obj2raise    ;# maps dataobj => raise flag 0/1
62    private variable _obj2vtk      ;# maps dataobj => vtk objects
63    private variable _actors       ;# list of actors for each renderer
64    private variable _lights       ;# list of lights for each renderer
65    private variable _click        ;# info used for _move operations
66    private variable _slicer       ;# vtk transform used for 3D slice plane
67    private variable _limits       ;# autoscale min/max for all axes
68    private variable _view         ;# view params for 3D view
69    private variable _download ""  ;# snapshot for download
70}
71
72itk::usual ContourResult {
73    keep -background -foreground -cursor -font
74    keep -plotbackground -plotforeground
75}
76
77# ----------------------------------------------------------------------
78# CONSTRUCTOR
79# ----------------------------------------------------------------------
80itcl::body Rappture::ContourResult::constructor {args} {
81    option add hull.width hull.height
82    pack propagate $itk_component(hull) no
83
84    set _slicer(xplane) ""
85    set _slicer(yplane) ""
86    set _slicer(zplane) ""
87    set _slicer(xslice) ""
88    set _slicer(yslice) ""
89    set _slicer(zslice) ""
90    set _slicer(readout) ""
91    set _view(theta) 0
92    set _view(phi) 0
93
94    foreach val {xmin xmax ymin ymax zmin zmax vmin vmax} {
95        set _limits($val) ""
96    }
97
98    itk_component add controls {
99        frame $itk_interior.cntls
100    } {
101        usual
102        rename -background -controlbackground controlBackground Background
103    }
104    pack $itk_component(controls) -side right -fill y
105
106    itk_component add zoom {
107        frame $itk_component(controls).zoom
108    } {
109        usual
110        rename -background -controlbackground controlBackground Background
111    }
112    pack $itk_component(zoom) -side top
113
114    itk_component add reset {
115        button $itk_component(zoom).reset \
116            -borderwidth 1 -padx 1 -pady 1 \
117            -bitmap [Rappture::icon reset] \
118            -command [itcl::code $this _zoom reset]
119    } {
120        usual
121        ignore -borderwidth
122        rename -highlightbackground -controlbackground controlBackground Background
123    }
124    pack $itk_component(reset) -padx 4 -pady 4
125    Rappture::Tooltip::for $itk_component(reset) "Reset the view to the default zoom level"
126
127    itk_component add zoomin {
128        button $itk_component(zoom).zin \
129            -borderwidth 1 -padx 1 -pady 1 \
130            -bitmap [Rappture::icon zoomin] \
131            -command [itcl::code $this _zoom in]
132    } {
133        usual
134        ignore -borderwidth
135        rename -highlightbackground -controlbackground controlBackground Background
136    }
137    pack $itk_component(zoomin) -padx 4 -pady 4
138    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
139
140    itk_component add zoomout {
141        button $itk_component(zoom).zout \
142            -borderwidth 1 -padx 1 -pady 1 \
143            -bitmap [Rappture::icon zoomout] \
144            -command [itcl::code $this _zoom out]
145    } {
146        usual
147        ignore -borderwidth
148        rename -highlightbackground -controlbackground controlBackground Background
149    }
150    pack $itk_component(zoomout) -padx 4 -pady 4
151    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
152
153    #
154    # Create slicer controls...
155    #
156    itk_component add slicers {
157        frame $itk_component(controls).slicers
158    } {
159        usual
160        rename -background -controlbackground controlBackground Background
161    }
162    pack $itk_component(slicers) -side bottom -padx 4 -pady 4
163    grid rowconfigure $itk_component(slicers) 1 -weight 1
164
165    #
166    # X-value slicer...
167    #
168    itk_component add xslice {
169        label $itk_component(slicers).xslice \
170            -borderwidth 1 -relief raised -padx 1 -pady 1 \
171            -bitmap [Rappture::icon x]
172    } {
173        usual
174        ignore -borderwidth
175        rename -highlightbackground -controlbackground controlBackground Background
176    }
177    bind $itk_component(xslice) <ButtonPress> \
178        [itcl::code $this _slice axis x toggle]
179    Rappture::Tooltip::for $itk_component(xslice) \
180        "Toggle the X cut plane on/off"
181    grid $itk_component(xslice) -row 0 -column 0 -sticky ew -padx 1
182
183    itk_component add xslicer {
184        ::scale $itk_component(slicers).xval -from 100 -to 0 \
185            -width 10 -orient vertical -showvalue off -state disabled \
186            -borderwidth 1 -highlightthickness 0 \
187            -command [itcl::code $this _slice move x]
188    } {
189        usual
190        ignore -borderwidth
191        ignore -highlightthickness
192        rename -highlightbackground -controlbackground controlBackground Background
193        rename -troughcolor -controldarkbackground controlDarkBackground Background
194    }
195    grid $itk_component(xslicer) -row 1 -column 0 -padx 1
196    Rappture::Tooltip::for $itk_component(xslicer) \
197        "@[itcl::code $this _slicertip x]"
198
199    #
200    # Y-value slicer...
201    #
202    itk_component add yslice {
203        label $itk_component(slicers).yslice \
204            -borderwidth 1 -relief raised -padx 1 -pady 1 \
205            -bitmap [Rappture::icon y]
206    } {
207        usual
208        ignore -borderwidth
209        rename -highlightbackground -controlbackground controlBackground Background
210    }
211    bind $itk_component(yslice) <ButtonPress> \
212        [itcl::code $this _slice axis y toggle]
213    Rappture::Tooltip::for $itk_component(yslice) \
214        "Toggle the Y cut plane on/off"
215    grid $itk_component(yslice) -row 0 -column 1 -sticky ew -padx 1
216
217    itk_component add yslicer {
218        ::scale $itk_component(slicers).yval -from 100 -to 0 \
219            -width 10 -orient vertical -showvalue off -state disabled \
220            -borderwidth 1 -highlightthickness 0 \
221            -command [itcl::code $this _slice move y]
222    } {
223        usual
224        ignore -borderwidth
225        ignore -highlightthickness
226        rename -highlightbackground -controlbackground controlBackground Background
227        rename -troughcolor -controldarkbackground controlDarkBackground Background
228    }
229    grid $itk_component(yslicer) -row 1 -column 1 -padx 1
230    Rappture::Tooltip::for $itk_component(yslicer) \
231        "@[itcl::code $this _slicertip y]"
232
233    #
234    # Z-value slicer...
235    #
236    itk_component add zslice {
237        label $itk_component(slicers).zslice \
238            -borderwidth 1 -relief raised -padx 1 -pady 1 \
239            -bitmap [Rappture::icon z]
240    } {
241        usual
242        ignore -borderwidth
243        rename -highlightbackground -controlbackground controlBackground Background
244    }
245    grid $itk_component(zslice) -row 0 -column 2 -sticky ew -padx 1
246    bind $itk_component(zslice) <ButtonPress> \
247        [itcl::code $this _slice axis z toggle]
248    Rappture::Tooltip::for $itk_component(zslice) \
249        "Toggle the Z cut plane on/off"
250
251    itk_component add zslicer {
252        ::scale $itk_component(slicers).zval -from 100 -to 0 \
253            -width 10 -orient vertical -showvalue off -state disabled \
254            -borderwidth 1 -highlightthickness 0 \
255            -command [itcl::code $this _slice move z]
256    } {
257        usual
258        ignore -borderwidth
259        ignore -highlightthickness
260        rename -highlightbackground -controlbackground controlBackground Background
261        rename -troughcolor -controldarkbackground controlDarkBackground Background
262    }
263    grid $itk_component(zslicer) -row 1 -column 2 -padx 1
264    Rappture::Tooltip::for $itk_component(zslicer) \
265        "@[itcl::code $this _slicertip z]"
266
267    #
268    # RENDERING AREA
269    #
270    itk_component add area {
271        frame $itk_interior.area
272    }
273    pack $itk_component(area) -expand yes -fill both
274
275    vtkRenderer $this-ren
276    vtkRenderWindow $this-renWin
277    $this-renWin AddRenderer $this-ren
278    $this-renWin LineSmoothingOn
279    $this-renWin PolygonSmoothingOn
280    vtkRenderWindowInteractor $this-iren
281    $this-iren SetRenderWindow $this-renWin
282
283    itk_component add plot {
284        vtkTkRenderWidget $itk_component(area).plot -rw $this-renWin \
285            -width 1 -height 1
286    } {
287    }
288    pack $itk_component(plot) -expand yes -fill both
289
290
291    vtkRenderer $this-ren2
292    vtkRenderWindow $this-renWin2
293    $this-renWin2 AddRenderer $this-ren2
294    vtkRenderWindowInteractor $this-iren2
295    $this-iren2 SetRenderWindow $this-renWin2
296
297    itk_component add legend {
298        vtkTkRenderWidget $itk_component(area).legend -rw $this-renWin2 \
299            -width 1 -height 40
300    } {
301    }
302    pack $itk_component(legend) -side bottom -fill x
303
304    #
305    # Create a photo for download snapshots
306    #
307    set _download [image create photo]
308
309    eval itk_initialize $args
310}
311
312# ----------------------------------------------------------------------
313# DESTRUCTOR
314# ----------------------------------------------------------------------
315itcl::body Rappture::ContourResult::destructor {} {
316    _clear
317    after cancel [itcl::code $this _rebuild]
318
319    rename $this-renWin ""
320    rename $this-ren ""
321    rename $this-iren ""
322
323    rename $this-renWin2 ""
324    rename $this-ren2 ""
325    rename $this-iren2 ""
326
327    image delete $_download
328}
329
330# ----------------------------------------------------------------------
331# USAGE: add <dataobj> ?<settings>?
332#
333# Clients use this to add a data object to the plot.  The optional
334# <settings> are used to configure the plot.  Allowed settings are
335# -color, -brightness, -width, -linestyle, and -raise.
336# ----------------------------------------------------------------------
337itcl::body Rappture::ContourResult::add {dataobj {settings ""}} {
338    array set params {
339        -color auto
340        -width 1
341        -linestyle solid
342        -brightness 0
343        -raise 0
344        -description ""
345        -param ""
346        -tkwait no
347    }
348    foreach {opt val} $settings {
349        if {![info exists params($opt)]} {
350            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
351        }
352        set params($opt) $val
353    }
354    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
355        # can't handle -autocolors yet
356        set params(-color) black
357    }
358
359    set pos [lsearch -exact $dataobj $_dlist]
360    if {$pos < 0} {
361        lappend _dlist $dataobj
362        set _obj2color($dataobj) $params(-color)
363        set _obj2width($dataobj) $params(-width)
364        set _obj2raise($dataobj) $params(-raise)
365
366        after cancel [itcl::code $this _rebuild]
367        after idle [itcl::code $this _rebuild]
368    }
369}
370
371# ----------------------------------------------------------------------
372# USAGE: get
373#
374# Clients use this to query the list of objects being plotted, in
375# order from bottom to top of this result.
376# ----------------------------------------------------------------------
377itcl::body Rappture::ContourResult::get {} {
378    # put the dataobj list in order according to -raise options
379    set dlist $_dlist
380    foreach obj $dlist {
381        if {[info exists _obj2raise($obj)] && $_obj2raise($obj)} {
382            set i [lsearch -exact $dlist $obj]
383            if {$i >= 0} {
384                set dlist [lreplace $dlist $i $i]
385                lappend dlist $obj
386            }
387        }
388    }
389    return $dlist
390}
391
392# ----------------------------------------------------------------------
393# USAGE: delete ?<dataobj1> <dataobj2> ...?
394#
395# Clients use this to delete a dataobj from the plot.  If no dataobjs
396# are specified, then all dataobjs are deleted.
397# ----------------------------------------------------------------------
398itcl::body Rappture::ContourResult::delete {args} {
399    if {[llength $args] == 0} {
400        set args $_dlist
401    }
402
403    # delete all specified dataobjs
404    set changed 0
405    foreach dataobj $args {
406        set pos [lsearch -exact $_dlist $dataobj]
407        if {$pos >= 0} {
408            set _dlist [lreplace $_dlist $pos $pos]
409            catch {unset _obj2color($dataobj)}
410            catch {unset _obj2width($dataobj)}
411            catch {unset _obj2raise($dataobj)}
412            set changed 1
413        }
414    }
415
416    # if anything changed, then rebuild the plot
417    if {$changed} {
418        after cancel [itcl::code $this _rebuild]
419        after idle [itcl::code $this _rebuild]
420    }
421}
422
423# ----------------------------------------------------------------------
424# USAGE: scale ?<data1> <data2> ...?
425#
426# Sets the default limits for the overall plot according to the
427# limits of the data for all of the given <data> objects.  This
428# accounts for all objects--even those not showing on the screen.
429# Because of this, the limits are appropriate for all objects as
430# the user scans through data in the ResultSet viewer.
431# ----------------------------------------------------------------------
432itcl::body Rappture::ContourResult::scale {args} {
433    foreach val {xmin xmax ymin ymax zmin zmax vmin vmax} {
434        set _limits($val) ""
435    }
436    foreach obj $args {
437        foreach axis {x y z v} {
438            foreach {min max} [$obj limits $axis] break
439            if {"" != $min && "" != $max} {
440                if {"" == $_limits(${axis}min)} {
441                    set _limits(${axis}min) $min
442                    set _limits(${axis}max) $max
443                } else {
444                    if {$min < $_limits(${axis}min)} {
445                        set _limits(${axis}min) $min
446                    }
447                    if {$max > $_limits(${axis}max)} {
448                        set _limits(${axis}max) $max
449                    }
450                }
451            }
452        }
453    }
454    _fixLimits
455}
456
457# ----------------------------------------------------------------------
458# USAGE: download coming
459# USAGE: download controls <downloadCommand>
460# USAGE: download now
461#
462# Clients use this method to create a downloadable representation
463# of the plot.  Returns a list of the form {ext string}, where
464# "ext" is the file extension (indicating the type of data) and
465# "string" is the data itself.
466# ----------------------------------------------------------------------
467itcl::body Rappture::ContourResult::download {option args} {
468    switch $option {
469        coming {
470            if {[catch {blt::winop snap $itk_component(area) $_download}]} {
471                $_download configure -width 1 -height 1
472                $_download put #000000
473            }
474        }
475        controls {
476            # no controls for this download yet
477            return ""
478        }
479        now {
480            # Get the image data (as base64) and decode it back to binary.
481            # This is better than writing to temporary files.  When we switch
482            # to the BLT picture image it won't be necessary to decode the
483            # image data.
484            set bytes [$_download data -format "jpeg -quality 100"]
485            set bytes [Rappture::encoding::decode -as b64 $bytes]
486            return [list .jpg $bytes]
487        }
488        default {
489            error "bad option \"$option\": should be coming, controls, now"
490        }
491    }
492}
493
494# ----------------------------------------------------------------------
495# USAGE: _rebuild
496#
497# Called automatically whenever something changes that affects the
498# data in the widget.  Clears any existing data and rebuilds the
499# widget to display new data.
500# ----------------------------------------------------------------------
501itcl::body Rappture::ContourResult::_rebuild {} {
502    _clear
503    set id 0
504
505    # determine the dimensionality from the topmost (raised) object
506    set dlist [get]
507    set dataobj [lindex $dlist end]
508    if {$dataobj != ""} {
509        set _dims [lindex [lsort [$dataobj components -dimensions]] end]
510    } else {
511        set _dims "0D"
512    }
513
514    #
515    # LOOKUP TABLE FOR COLOR CONTOURS
516    #
517    # use vmin/vmax if possible, otherwise get from data
518    if {$_limits(vmin) == "" || $_limits(vmax) == ""} {
519        set v0 0
520        set v1 1
521        if {[info exists _obj2vtk($dataobj)]} {
522            set pd [lindex $_obj2vtk($dataobj) 0]
523            if {"" != $pd} {
524                foreach {v0 v1} [$pd GetScalarRange] break
525            }
526        }
527    } else {
528        set v0 $_limits(vmin)
529        set v1 $_limits(vmax)
530    }
531
532    set lu $this-lookup$id
533    vtkLookupTable $lu
534    $lu SetTableRange $v0 $v1
535    $lu SetHueRange 0.66667 0.0
536    $lu Build
537
538    lappend _obj2vtk($dataobj) $lu
539
540    if {$_dims == "3D"} {
541        #
542        # 3D LIGHTS (on both sides of all three axes)
543        #
544        set x0 $_limits(xmin)
545        set x1 $_limits(xmax)
546        set xm [expr {0.5*($x0+$x1)}]
547        set y0 $_limits(ymin)
548        set y1 $_limits(ymax)
549        set ym [expr {0.5*($y0+$y1)}]
550        set z0 $_limits(zmin)
551        set z1 $_limits(zmax)
552        set zm [expr {0.5*($z0+$z1)}]
553        set xr [expr {$x1-$x0}]
554        set yr [expr {$y1-$y0}]
555        set zr [expr {$z1-$z0}]
556
557        set lt $this-light$id
558        vtkLight $lt
559        $lt SetColor 1 1 1
560        $lt SetAttenuationValues 0 0 0
561        $lt SetFocalPoint $xm $ym $zm
562        $lt SetLightTypeToHeadlight
563        $this-ren AddLight $lt
564        lappend _lights($this-ren) $lt
565
566    } else {
567    }
568
569    # scan through all data objects and build the contours
570    set firstobj 1
571    foreach dataobj [get] {
572        foreach comp [$dataobj components] {
573            #
574            # Add color contours.
575            #
576            if {$firstobj} {
577                if {$_dims == "3D"} {
578                    pack $itk_component(slicers) -side bottom -padx 4 -pady 4
579                    pack $itk_component(reset) -side left
580                    pack $itk_component(zoomin) -side left
581                    pack $itk_component(zoomout) -side left
582
583                    #
584                    # 3D DATA SET
585                    #
586                    set mesh [$dataobj mesh $comp]
587                    if {"" == $mesh} {
588                        set x [expr {[winfo rootx $itk_component(area)]+10}]
589                        set y [expr {[winfo rooty $itk_component(area)]+10}]
590                        Rappture::Tooltip::cue @$x,$y "This data requires the visualization server, and that appears to be down.  Please try your simulation again later."
591                        return
592                    }
593                    switch -- [$mesh GetClassName] {
594                      vtkPoints {
595                        # handle cloud of 3D points
596                        set pd $this-polydata$id
597                        vtkPolyData $pd
598                        $pd SetPoints $mesh
599                        [$pd GetPointData] SetScalars [$dataobj values $comp]
600
601                        set tr $this-triangles$id
602                        vtkDelaunay3D $tr
603                        $tr SetInput $pd
604                        $tr SetTolerance 0.0000000000001
605                        set source [$tr GetOutput]
606
607                        set mp $this-mapper$id
608                        vtkPolyDataMapper $mp
609
610                        lappend _obj2vtk($dataobj) $pd $tr $mp
611                      }
612                      vtkUnstructuredGrid {
613                        # handle 3D grid with connectivity
614                        set gr $this-grdata$id
615                        vtkUnstructuredGrid $gr
616                        $gr ShallowCopy $mesh
617                        [$gr GetPointData] SetScalars [$dataobj values $comp]
618                        set source $gr
619
620                        lappend _obj2vtk($dataobj) $gr
621                      }
622                      vtkRectilinearGrid {
623                        # handle 3D grid with connectivity
624                        set gr $this-grdata$id
625                        vtkRectilinearGrid $gr
626                        $gr ShallowCopy $mesh
627                        [$gr GetPointData] SetScalars [$dataobj values $comp]
628                        set source $gr
629
630                        lappend _obj2vtk($dataobj) $gr
631                      }
632                      default {
633                        error "don't know how to handle [$mesh GetClassName] data"
634                      }
635                    }
636
637                    #
638                    # 3D ISOSURFACES
639                    #
640                    set iso $this-iso$id
641                    vtkContourFilter $iso
642                      $iso SetInput $source
643
644                    set mp $this-isomap$id
645                    vtkPolyDataMapper $mp
646                      $mp SetInput [$iso GetOutput]
647
648                    set ac $this-isoactor$id
649                    vtkActor $ac
650                      $ac SetMapper $mp
651                      [$ac GetProperty] SetOpacity 0.3
652                      [$ac GetProperty] SetDiffuse 0.5
653                      [$ac GetProperty] SetAmbient 0.7
654                      [$ac GetProperty] SetSpecular 10.0
655                      [$ac GetProperty] SetSpecularPower 200.0
656                    $this-ren AddActor $ac
657
658                    lappend _obj2vtk($dataobj) $iso $mp $ac
659                    lappend _actors($this-ren) $ac
660
661                    catch {unset style}
662                    array set style [lindex [$dataobj components -style $comp] 0]
663                    if {[info exists style(-color)]} {
664                        $mp ScalarVisibilityOff  ;# take color from actor
665                        eval [$ac GetProperty] SetColor [_color2rgb $style(-color)]
666                    }
667
668                    if {[info exists style(-opacity)]} {
669                        [$ac GetProperty] SetOpacity $style(-opacity)
670                    }
671
672                    set levels 5
673                    if {[info exists style(-levels)]} {
674                        set levels $style(-levels)
675                    }
676                    if {$levels == 1} {
677                        $iso SetValue 0 [expr {0.5*($v1-$v0)+$v0}]
678                    } else {
679                        $iso GenerateValues [expr {$levels+2}] $v0 $v1
680                    }
681
682                    #
683                    # 3D CUT PLANES
684                    #
685                    if {$id == 0} {
686                        foreach axis {x y z} norm {{1 0 0} {0 1 0} {0 0 1}} {
687                            set pl $this-${axis}cutplane$id
688                            vtkPlane $pl
689                            eval $pl SetNormal $norm
690                            set _slicer(${axis}plane) $pl
691
692                            set ct $this-${axis}cutter$id
693                            vtkCutter $ct
694                            $ct SetInput $source
695                            $ct SetCutFunction $pl
696
697                            set mp $this-${axis}cutmapper$id
698                            vtkPolyDataMapper $mp
699                            $mp SetInput [$ct GetOutput]
700                            $mp SetScalarRange $v0 $v1
701                            $mp SetLookupTable $lu
702
703                            lappend _obj2vtk($dataobj) $pl $ct $mp
704
705                            set ac $this-${axis}actor$id
706                            vtkActor $ac
707                            $ac VisibilityOff
708                            $ac SetMapper $mp
709                            $ac SetPosition 0 0 0
710                            [$ac GetProperty] SetColor 0 0 0
711                            set _slicer(${axis}slice) $ac
712
713                            $this-ren AddActor $ac
714                            lappend _actors($this-ren) $ac
715                            lappend _obj2vtk($dataobj) $ac
716                        }
717
718                        #
719                        # CUT PLANE READOUT
720                        #
721                        set tx $this-text$id
722                        vtkTextMapper $tx
723                        set tp [$tx GetTextProperty]
724                        eval $tp SetColor [_color2rgb $itk_option(-plotforeground)]
725                        $tp SetVerticalJustificationToTop
726                        set _slicer(readout) $tx
727
728                        set txa $this-texta$id
729                        vtkActor2D $txa
730                        $txa SetMapper $tx
731                        [$txa GetPositionCoordinate] \
732                            SetCoordinateSystemToNormalizedDisplay
733                        [$txa GetPositionCoordinate] SetValue 0.02 0.98
734
735                        $this-ren AddActor $txa
736                        lappend _actors($this-ren) $txa
737
738                        lappend _obj2vtk($dataobj) $tx $txa
739
740                        # turn off all slicers by default
741                        foreach axis {x y z} {
742                            $itk_component(${axis}slicer) configure -state normal
743                            $itk_component(${axis}slicer) set 50
744                            _slice move $axis 50
745                            _slice axis $axis off
746                        }
747                    }
748
749                } else {
750                    pack forget $itk_component(slicers)
751                    pack $itk_component(reset) -side top
752                    pack $itk_component(zoomin) -side top
753                    pack $itk_component(zoomout) -side top
754
755                    set pd $this-polydata$id
756                    vtkPolyData $pd
757                    $pd SetPoints [$dataobj mesh $comp]
758                    [$pd GetPointData] SetScalars [$dataobj values $comp]
759
760                    set tr $this-triangles$id
761                    vtkDelaunay2D $tr
762                    $tr SetInput $pd
763                    $tr SetTolerance 0.0000000000001
764                    set source [$tr GetOutput]
765
766                    set mp $this-mapper$id
767                    vtkPolyDataMapper $mp
768                    $mp SetInput $source
769                    $mp SetScalarRange $v0 $v1
770                    $mp SetLookupTable $lu
771
772                    set ac $this-actor$id
773                    vtkActor $ac
774                    $ac SetMapper $mp
775                    $ac SetPosition 0 0 0
776                    [$ac GetProperty] SetColor 0 0 0
777                    $this-ren AddActor $ac
778                    lappend _actors($this-ren) $ac
779
780                    lappend _obj2vtk($dataobj) $pd $tr $mp $ac
781                }
782            } else {
783                #
784                # Add color lines
785                #
786                set cf $this-clfilter$id
787                vtkContourFilter $cf
788                $cf SetInput $source
789                $cf GenerateValues 20 $v0 $v1
790
791                set mp $this-clmapper$id
792                vtkPolyDataMapper $mp
793                $mp SetInput [$cf GetOutput]
794                $mp SetScalarRange $v0 $v1
795                $mp SetLookupTable $lu
796
797                set ac $this-clactor$id
798                vtkActor $ac
799                $ac SetMapper $mp
800                [$ac GetProperty] SetColor 1 1 1
801                $ac SetPosition 0 0 0
802                $this-ren AddActor $ac
803                lappend _actors($this-ren) $ac
804
805                lappend _obj2vtk($dataobj) $cf $mp $ac
806            }
807
808            #
809            # Add an outline around the data
810            #
811            if {$id == 0} {
812                set olf $this-olfilter$id
813                vtkOutlineFilter $olf
814                $olf SetInput $source
815
816                set olm $this-olmapper$id
817                vtkPolyDataMapper $olm
818                $olm SetInput [$olf GetOutput]
819
820                set ola $this-olactor$id
821                vtkActor $ola
822                $ola SetMapper $olm
823                eval [$ola GetProperty] SetColor [_color2rgb $itk_option(-plotforeground)]
824                $this-ren AddActor $ola
825                lappend _actors($this-ren) $ola
826
827                lappend _obj2vtk($dataobj) $olf $olm $ola
828
829                if {$_dims == "3D"} {
830                    # pick a good scale factor for text
831                    if {$xr < $yr} {
832                        set tscale [expr {0.1*$xr}]
833                    } else {
834                        set tscale [expr {0.1*$yr}]
835                    }
836
837                    foreach {i axis px py pz rx ry rz} {
838                        0  x   $xm   0   0   90   0   0
839                        1  y     0 $ym   0   90 -90   0
840                        2  z   $x1   0 $zm   90   0 -45
841                    } {
842                        set length "[expr {[set ${axis}1]-[set ${axis}0]}]"
843
844                        set vtx $this-${axis}label$id
845                        vtkVectorText $vtx
846                        $vtx SetText "$axis"
847
848                        set vmp $this-${axis}lmap$id
849                        vtkPolyDataMapper $vmp
850                        $vmp SetInput [$vtx GetOutput]
851
852                        set vac $this-${axis}lact$id
853                        vtkActor $vac
854                        $vac SetMapper $vmp
855                        $vac SetPosition [expr $px] [expr $py] [expr $pz]
856                        $vac SetOrientation $rx $ry $rz
857                        $vac SetScale $tscale
858                        $this-ren AddActor $vac
859
860                        lappend _obj2vtk($dataobj) $vtx $vmp $vac
861                        lappend _actors($this-ren) $vac
862
863                        $vmp Update
864                        foreach {xx0 xx1 yy0 yy1 zz0 zz1} [$vac GetBounds] break
865                        switch -- $axis {
866                          x {
867                            set dx [expr {-0.5*($xx1-$xx0)}]
868                            set dy 0
869                            set dz [expr {1.3*($zz0-$zz1)}]
870                          }
871                          y {
872                            set dx 0
873                            set dy [expr {0.5*($yy1-$yy0)}]
874                            set dz [expr {$zz0-$zz1}]
875                          }
876                          z {
877                            set dx [expr {0.2*$tscale}]
878                            set dy $dx
879                            set dz [expr {-0.5*($zz1-$zz0)}]
880                          }
881                        }
882                        $vac AddPosition $dx $dy $dz
883                    }
884                }
885            }
886
887            #
888            # Add a legend with the scale.
889            #
890            if {$id == 0} {
891                set lg $this-legend$id
892                vtkScalarBarActor $lg
893                $lg SetLookupTable $lu
894                [$lg GetPositionCoordinate] SetCoordinateSystemToNormalizedViewport
895                [$lg GetPositionCoordinate] SetValue 0.1 0.1
896                $lg SetOrientationToHorizontal
897                $lg SetWidth 0.8
898                $lg SetHeight 1.0
899
900                set tp [$lg GetLabelTextProperty]
901                eval $tp SetColor [_color2rgb $itk_option(-plotforeground)]
902                $tp BoldOff
903                $tp ItalicOff
904                $tp ShadowOff
905                #eval $tp SetShadowColor [_color2rgb gray]
906
907                $this-ren2 AddActor2D $lg
908                lappend _actors($this-ren2) $lg
909                lappend _obj2vtk($dataobj) $lg
910            }
911
912            incr id
913        }
914        set firstobj 0
915    }
916    _fixLimits
917    _zoom reset
918
919    #
920    # HACK ALERT!  A single ResetCamera doesn't seem to work for
921    #   some contour data.  You have to do it multiple times to
922    #   get to the right zoom factor on data.  I hope 20 times is
923    #   enough.  I hate Vtk sometimes...
924    #
925    for {set i 0} {$i < 20} {incr i} {
926        $this-ren ResetCamera
927        [$this-ren GetActiveCamera] Zoom 1.5
928    }
929
930    # prevent interactions -- use our own
931    blt::busy hold $itk_component(area) -cursor left_ptr
932    bind $itk_component(area)_Busy <ButtonPress> \
933        [itcl::code $this _move click %x %y]
934    bind $itk_component(area)_Busy <B1-Motion> \
935        [itcl::code $this _move drag %x %y]
936    bind $itk_component(area)_Busy <ButtonRelease> \
937        [itcl::code $this _move release %x %y]
938}
939
940# ----------------------------------------------------------------------
941# USAGE: _clear
942#
943# Used internally to clear the drawing area and tear down all vtk
944# objects in the current scene.
945# ----------------------------------------------------------------------
946itcl::body Rappture::ContourResult::_clear {} {
947    # clear out any old constructs
948    foreach ren [array names _actors] {
949        foreach actor $_actors($ren) {
950            $ren RemoveActor $actor
951        }
952        set _actors($ren) ""
953    }
954    foreach ren [array names _lights] {
955        foreach light $_lights($ren) {
956            $ren RemoveLight $light
957            rename $light ""
958        }
959        set _lights($ren) ""
960    }
961    foreach dataobj [array names _obj2vtk] {
962        foreach cmd $_obj2vtk($dataobj) {
963            rename $cmd ""
964        }
965        set _obj2vtk($dataobj) ""
966    }
967    set _slicer(xplane) ""
968    set _slicer(yplane) ""
969    set _slicer(zplane) ""
970    set _slicer(xslice) ""
971    set _slicer(yslice) ""
972    set _slicer(zslice) ""
973    set _slicer(readout) ""
974}
975
976# ----------------------------------------------------------------------
977# USAGE: _zoom in
978# USAGE: _zoom out
979# USAGE: _zoom reset
980#
981# Called automatically when the user clicks on one of the zoom
982# controls for this widget.  Changes the zoom for the current view.
983# ----------------------------------------------------------------------
984itcl::body Rappture::ContourResult::_zoom {option} {
985    switch -- $option {
986        in {
987            [$this-ren GetActiveCamera] Zoom 1.25
988            $this-renWin Render
989        }
990        out {
991            [$this-ren GetActiveCamera] Zoom 0.8
992            $this-renWin Render
993        }
994        reset {
995            if {$_dims == "3D"} {
996                [$this-ren GetActiveCamera] SetViewAngle 30
997                $this-ren ResetCamera
998                _3dView 45 45
999            } else {
1000                $this-ren ResetCamera
1001                [$this-ren GetActiveCamera] Zoom 1.5
1002            }
1003            $this-renWin Render
1004            $this-renWin2 Render
1005        }
1006    }
1007}
1008
1009# ----------------------------------------------------------------------
1010# USAGE: _move click <x> <y>
1011# USAGE: _move drag <x> <y>
1012# USAGE: _move release <x> <y>
1013#
1014# Called automatically when the user clicks/drags/releases in the
1015# plot area.  Moves the plot according to the user's actions.
1016# ----------------------------------------------------------------------
1017itcl::body Rappture::ContourResult::_move {option x y} {
1018    switch -- $option {
1019        click {
1020            blt::busy configure $itk_component(area) -cursor fleur
1021            set _click(x) $x
1022            set _click(y) $y
1023            set _click(theta) $_view(theta)
1024            set _click(phi) $_view(phi)
1025        }
1026        drag {
1027            if {[array size _click] == 0} {
1028                _move click $x $y
1029            } else {
1030                set w [winfo width $itk_component(plot)]
1031                set h [winfo height $itk_component(plot)]
1032                set scalex [expr {$_limits(xmax)-$_limits(xmin)}]
1033                set scaley [expr {$_limits(ymax)-$_limits(ymin)}]
1034                set dx [expr {double($x-$_click(x))/$w*$scalex}]
1035                set dy [expr {double($y-$_click(y))/$h*$scaley}]
1036
1037                if {$_dims == "2D"} {
1038                    #
1039                    # Shift the contour plot in 2D
1040                    #
1041                    foreach actor $_actors($this-ren) {
1042                        foreach {ax ay az} [$actor GetPosition] break
1043                        $actor SetPosition [expr {$ax+$dx}] [expr {$ay-$dy}] 0
1044                    }
1045                    $this-renWin Render
1046                } elseif {$_dims == "3D"} {
1047                    #
1048                    # Rotate the camera in 3D
1049                    #
1050                    set theta [expr {$_view(theta) - $dy*180}]
1051                    if {$theta < 2} { set theta 2 }
1052                    if {$theta > 178} { set theta 178 }
1053                    set phi [expr {$_view(phi) - $dx*360}]
1054
1055                    _3dView $theta $phi
1056                    $this-renWin Render
1057                }
1058                set _click(x) $x
1059                set _click(y) $y
1060            }
1061        }
1062        release {
1063            _move drag $x $y
1064            blt::busy configure $itk_component(area) -cursor left_ptr
1065            catch {unset _click}
1066        }
1067        default {
1068            error "bad option \"$option\": should be click, drag, release"
1069        }
1070    }
1071}
1072
1073# ----------------------------------------------------------------------
1074# USAGE: _slice axis x|y|z ?on|off|toggle?
1075# USAGE: _slice move x|y|z <newval>
1076#
1077# Called automatically when the user drags the slider to move the
1078# cut plane that slices 3D data.  Gets the current value from the
1079# slider and moves the cut plane to the appropriate point in the
1080# data set.
1081# ----------------------------------------------------------------------
1082itcl::body Rappture::ContourResult::_slice {option args} {
1083    if {$_slicer(xplane) == ""} {
1084        # no slicer? then bail out!
1085        return
1086    }
1087    switch -- $option {
1088        axis {
1089            if {[llength $args] < 1 || [llength $args] > 2} {
1090                error "wrong # args: should be \"_slice axis x|y|z ?on|off|toggle?\""
1091            }
1092            set axis [lindex $args 0]
1093            set op [lindex $args 1]
1094            if {$op == ""} { set op "on" }
1095
1096            if {[$itk_component(${axis}slice) cget -relief] == "raised"} {
1097                set current "off"
1098            } else {
1099                set current "on"
1100            }
1101
1102            if {$op == "toggle"} {
1103                if {$current == "on"} { set op "off" } else { set op "on" }
1104            }
1105
1106            if {$op} {
1107                $itk_component(${axis}slicer) configure -state normal
1108                $_slicer(${axis}slice) VisibilityOn
1109                $itk_component(${axis}slice) configure -relief sunken
1110            } else {
1111                $itk_component(${axis}slicer) configure -state disabled
1112                $_slicer(${axis}slice) VisibilityOff
1113                $itk_component(${axis}slice) configure -relief raised
1114            }
1115            $this-renWin Render
1116        }
1117        move {
1118            if {[llength $args] != 2} {
1119                error "wrong # args: should be \"_slice move x|y|z newval\""
1120            }
1121            set axis [lindex $args 0]
1122            set newval [lindex $args 1]
1123
1124            set xm [expr {0.5*($_limits(xmax)+$_limits(xmin))}]
1125            set ym [expr {0.5*($_limits(ymax)+$_limits(ymin))}]
1126            set zm [expr {0.5*($_limits(zmax)+$_limits(zmin))}]
1127
1128            set newval [expr {0.01*($newval-50)
1129                *($_limits(${axis}max)-$_limits(${axis}min))
1130                  + 0.5*($_limits(${axis}max)+$_limits(${axis}min))}]
1131
1132            # show the current value in the readout
1133            if {$_slicer(readout) != ""} {
1134                $_slicer(readout) SetInput "$axis = $newval"
1135            }
1136
1137            # keep a little inside the volume, or the slice will disappear!
1138            if {$newval == $_limits(${axis}min)} {
1139                set range [expr {$_limits(${axis}max)-$_limits(${axis}min)}]
1140                set newval [expr {$newval + 1e-6*$range}]
1141            }
1142
1143            # xfer new value to the proper dimension and move the cut plane
1144            set ${axis}m $newval
1145            $_slicer(${axis}plane) SetOrigin $xm $ym $zm
1146
1147            $this-renWin Render
1148        }
1149        default {
1150            error "bad option \"$option\": should be axis or move"
1151        }
1152    }
1153}
1154
1155# ----------------------------------------------------------------------
1156# USAGE: _3dView <theta> <phi>
1157#
1158# Used internally to change the position of the camera for 3D data
1159# sets.  Sets the camera according to the angles <theta> (angle from
1160# the z-axis) and <phi> (angle from the x-axis in the x-y plane).
1161# Both angles are in degrees.
1162# ----------------------------------------------------------------------
1163itcl::body Rappture::ContourResult::_3dView {theta phi} {
1164    set deg2rad 0.0174532927778
1165    set xn [expr {sin($theta*$deg2rad)*cos($phi*$deg2rad)}]
1166    set yn [expr {sin($theta*$deg2rad)*sin($phi*$deg2rad)}]
1167    set zn [expr {cos($theta*$deg2rad)}]
1168
1169    set xm [expr {0.5*($_limits(xmax)+$_limits(xmin))}]
1170    set ym [expr {0.5*($_limits(ymax)+$_limits(ymin))}]
1171    set zm [expr {0.5*($_limits(zmax)+$_limits(zmin))}]
1172
1173    set cam [$this-ren GetActiveCamera]
1174    set zoom [$cam GetViewAngle]
1175    $cam SetViewAngle 30
1176
1177    $cam SetFocalPoint $xm $ym $zm
1178    $cam SetPosition [expr {$xm-$xn}] [expr {$ym-$yn}] [expr {$zm+$zn}]
1179    $cam ComputeViewPlaneNormal
1180    $cam SetViewUp 0 0 1  ;# z-dir is up
1181    $cam OrthogonalizeViewUp
1182    $this-ren ResetCamera
1183    $cam SetViewAngle $zoom
1184
1185    set _view(theta) $theta
1186    set _view(phi) $phi
1187}
1188
1189# ----------------------------------------------------------------------
1190# USAGE: _fixLimits
1191#
1192# Used internally to apply automatic limits to the axes for the
1193# current plot.
1194# ----------------------------------------------------------------------
1195itcl::body Rappture::ContourResult::_fixLimits {} {
1196    $this-ren ResetCamera
1197    [$this-ren GetActiveCamera] Zoom 1.5
1198    $this-renWin Render
1199    $this-renWin2 Render
1200}
1201
1202# ----------------------------------------------------------------------
1203# USAGE: _slicertip <axis>
1204#
1205# Used internally to generate a tooltip for the x/y/z slicer controls.
1206# Returns a message that includes the current slicer value.
1207# ----------------------------------------------------------------------
1208itcl::body Rappture::ContourResult::_slicertip {axis} {
1209    set val [$itk_component(${axis}slicer) get]
1210    set val [expr {0.01*($val-50)
1211        *($_limits(${axis}max)-$_limits(${axis}min))
1212          + 0.5*($_limits(${axis}max)+$_limits(${axis}min))}]
1213    return "Move the [string toupper $axis] cut plane.\nCurrently:  $axis = $val"
1214}
1215
1216# ----------------------------------------------------------------------
1217# USAGE: _color2rgb <color>
1218#
1219# Used internally to convert a color name to a set of {r g b} values
1220# needed for vtk.  Each r/g/b component is scaled in the range 0-1.
1221# ----------------------------------------------------------------------
1222itcl::body Rappture::ContourResult::_color2rgb {color} {
1223    foreach {r g b} [winfo rgb $itk_component(hull) $color] break
1224    set r [expr {$r/65535.0}]
1225    set g [expr {$g/65535.0}]
1226    set b [expr {$b/65535.0}]
1227    return [list $r $g $b]
1228}
1229
1230# ----------------------------------------------------------------------
1231# CONFIGURATION OPTION: -plotbackground
1232# ----------------------------------------------------------------------
1233itcl::configbody Rappture::ContourResult::plotbackground {
1234    foreach {r g b} [_color2rgb $itk_option(-plotbackground)] break
1235    $this-ren SetBackground $r $g $b
1236    $this-renWin Render
1237    $this-ren2 SetBackground $r $g $b
1238    $this-renWin2 Render
1239}
1240
1241# ----------------------------------------------------------------------
1242# CONFIGURATION OPTION: -plotforeground
1243# ----------------------------------------------------------------------
1244itcl::configbody Rappture::ContourResult::plotforeground {
1245    after cancel [itcl::code $this _rebuild]
1246    after idle [itcl::code $this _rebuild]
1247}
Note: See TracBrowser for help on using the repository browser.