source: branches/blt4/gui/scripts/contourresult.tcl @ 2287

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