source: trunk/gui/scripts/field.tcl @ 4502

Last change on this file since 4502 was 4502, checked in by ldelgass, 10 years ago

Cleanups. Remove unused variable, move check for <DX> header to C routine,
don't overwrite _dim written by VTK reader.

File size: 59.5 KB
RevLine 
[3330]1# -*- mode: tcl; indent-tabs-mode: nil -*-
[2504]2
[1]3# ----------------------------------------------------------------------
4#  COMPONENT: field - extracts data from an XML description of a field
5#
6#  This object represents one field in an XML description of a device.
7#  It simplifies the process of extracting data vectors that represent
8#  the field.
9# ======================================================================
10#  AUTHOR:  Michael McLennan, Purdue University
[3177]11#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
[115]12#
13#  See the file "license.terms" for information on usage and
14#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
[1]15# ======================================================================
16
[3406]17# TODO:
[3955]18#    Vector field limits are wrong: need to compute magnitude limits and
19#    component-wise limits.
[1]20
[3330]21#
22# Possible field dataset types:
23#
24# 2D Datasets
25#       vtk             (range of z-axis is zero).
[3955]26#       unirect2d       (deprecated)
[3330]27#       cloud           (x,y point coordinates) (deprecated)
28#       mesh
29# 3D Datasets
30#       vtk
[3955]31#       unirect3d       (deprecated)
[3330]32#       cloud           (x,y,z coordinates) (deprecated)
33#       mesh
34#       dx              (FIXME: make dx-to-vtk converter work)
35#       ucd avs
36#
37# Viewers:
38#       Format     Dim  Description                     Viewer          Server
39#       vtk         2   vtk file data.                  contour         vtkvis
40#       vtk         3   vtk file data.                  isosurface      vtkvis
41#       mesh        2   points-on-mesh                  heightmap       vtkvis
42#       mesh        3   points-on-mesh                  isosurface      vtkvis
43#       dx          3   DX                              volume          nanovis
44#       unirect2d   2   unirect3d + extents > 1 flow    flow            nanovis
45#       unirect3d   3   unirect2d + extents > 1 flow    flow            nanovis
46#       
[3955]47# With <views>, can specify which viewer for specific datasets.  So it's OK
48# for the same dataset to be viewed in more than one way.
[3406]49#  o Any 2D dataset can be viewed as a contour/heightmap.
[3701]50#  o Any 3D dataset can be viewed as a isosurface.
[3955]51#  o Any 2D dataset with vector data can be streamlines or flow. 
[3406]52#  o Any 3D uniform rectilinear dataset can be viewed as a volume.
53#  o Any 3D dataset with vector data can be streamlines or flow.
[3330]54#
[3406]55# Need <views> to properly do things like qdot: volume with a polydata
56# transparent shell.  The view will combine the two objects <field> and
57# <drawing> (??) into a single viewer.
[3330]58#
[3406]59package require Itcl
60package require BLT
61
62namespace eval Rappture {
63    # forward declaration
64}
65
[1]66itcl::class Rappture::Field {
[3754]67    protected variable _dim 0;          # Dimension of the mesh
[3571]68    private variable _xmlobj "";        # ref to XML obj with field data
69    private variable _limits;           # maps axis name => {z0 z1} limits
70    private variable _field ""
71    private variable _comp2fldName ;    # cname => field names.
[3776]72    private variable _comp2type ;       # cname => type (e.g. "vectors")
[3684]73    private variable _comp2size ;       # cname => # of components in element
[3707]74    private variable _comp2assoc;       # cname => association (e.g. pointdata)
[3571]75    private variable _fld2Components;   # field name => number of components
76    private variable _fld2Label;        # field name => label
77    private variable _fld2Units;        # field name => units
[3330]78    private variable _hints
[3405]79    private variable _viewer "";        # Hints which viewer to use
[3546]80    private variable _xv "";            # For 1D meshes only.  Holds the points
[3571]81    private variable _isValid 0;        # Indicates if the field contains
82                                        # valid data.
[3594]83    private variable _isValidComponent; #  Array of valid components found
[4417]84    private variable _alwaysConvertDX 0;
85
[3330]86    constructor {xmlobj path} {
87        # defined below
88    }
89    destructor {
90        # defined below
91    }
[3571]92    public method blob { cname }
[11]93    public method components {args}
[3571]94    public method controls {option args}
95    public method extents {{cname -overall}}
[3684]96    public method numComponents {cname}
[3429]97    public method fieldlimits {}
[3930]98    public method valueLimits { cname }
[3571]99    public method flowhints { cname }
[1]100    public method hints {{key ""}}
[1189]101    public method isunirect2d {}
[1373]102    public method isunirect3d {}
[3571]103    public method limits {axis}
104    public method mesh {{cname -overall}}
105    public method style { cname }
[2504]106    public method type {}
[3571]107    public method values { cname }
[3330]108    public method vtkdata {cname}
[4207]109    public method xErrorValues { cname } {
110    }
111    public method yErrorValues { cname } {
112    }
[3571]113
[3405]114    public method fieldnames { cname } {
[3571]115        if { ![info exists _comp2fldName($cname)] } {
[3405]116            return ""
117        }
[3571]118        return $_comp2fldName($cname)
[3405]119    }
120    public method fieldinfo { fname } {
[3571]121        lappend out $_fld2Label($fname)
122        lappend out $_fld2Units($fname)
123        lappend out $_fld2Components($fname)
[3405]124        return $out
125    }
[3571]126    public method isvalid {} {
127        return $_isValid
128    }
129    public method viewer {} {
130        return $_viewer
131    }
[3330]132    protected method Build {}
[11]133    protected method _getValue {expr}
[1]134
[2744]135    private variable _path "";          # Path of this object in the XML
136    private variable _units ""   ;      # system of units for this field
[1]137    private variable _zmax 0     ;# length of the device
138
[11]139    private variable _comp2dims  ;# maps component name => dimensionality
140    private variable _comp2xy    ;# maps component name => x,y vectors
[3330]141    private variable _comp2vtk   ;# maps component name => vtk file data
[447]142    private variable _comp2dx    ;# maps component name => OpenDX data
[839]143    private variable _comp2unirect2d ;# maps component name => unirect2d obj
[1463]144    private variable _comp2unirect3d ;# maps component name => unirect3d obj
[136]145    private variable _comp2style ;# maps component name => style settings
[1]146    private variable _comp2cntls ;# maps component name => x,y control points
[1373]147    private variable _comp2extents
[3330]148    private variable _comp2limits;      #  Array of limits per component
[2504]149    private variable _type ""
[1441]150    private variable _comp2flowhints
[3330]151    private variable _comp2mesh
[1]152    private common _counter 0    ;# counter for unique vector names
[3027]153
[3684]154    private method AvsToVtk { cname contents }
[3965]155    private method DicomToVtk { cname contents }
[3330]156    private method BuildPointsOnMesh { cname }
[3701]157    protected method GetAssociation { cname }
158    protected method GetTypeAndSize { cname }
[3699]159    protected method ReadVtkDataSet { cname contents }
[3917]160    private method InitHints {}
[3699]161
[3585]162    private method VerifyVtkDataSet { contents }
[4101]163    private method VectorLimits { vector vectorsize {comp -1} }
[3330]164    private variable _values ""
[1]165}
166
167# ----------------------------------------------------------------------
168# CONSTRUCTOR
169# ----------------------------------------------------------------------
[11]170itcl::body Rappture::Field::constructor {xmlobj path} {
[3491]171    package require vtk
[11]172    if {![Rappture::library isvalid $xmlobj]} {
[1570]173        error "bad value \"$xmlobj\": should be Rappture::library"
[1]174    }
[11]175    set _xmlobj $xmlobj
[2616]176    set _path $path
[3571]177    set _field [$xmlobj element -as object $path]
178    set _units [$_field get units]
[1]179
[87]180    set xunits [$xmlobj get units]
181    if {"" == $xunits || "arbitrary" == $xunits} {
[1570]182        set xunits "um"
[87]183    }
184
[1]185    # determine the overall size of the device
186    set z0 [set z1 0]
[11]187    foreach elem [$_xmlobj children components] {
[1570]188        switch -glob -- $elem {
189            box* {
190                if {![regexp {[0-9]$} $elem]} {
191                    set elem "${elem}0"
192                }
193                set z0 [$_xmlobj get components.$elem.corner0]
194                set z0 [Rappture::Units::convert $z0 \
195                    -context $xunits -to $xunits -units off]
[11]196
[1570]197                set z1 [$_xmlobj get components.$elem.corner1]
198                set z1 [Rappture::Units::convert $z1 \
199                    -context $xunits -to $xunits -units off]
[11]200
[1570]201                set _limits($elem) [list $z0 $z1]
202            }
203        }
[1]204    }
205    set _zmax $z1
206
207    # build up vectors for various components of the field
[3330]208    Build
[3917]209    InitHints
[1]210}
211
212# ----------------------------------------------------------------------
213# DESTRUCTOR
214# ----------------------------------------------------------------------
215itcl::body Rappture::Field::destructor {} {
[3571]216    itcl::delete object $_field
[11]217    # don't destroy the _xmlobj! we don't own it!
[1]218
[11]219    foreach name [array names _comp2xy] {
[1570]220        eval blt::vector destroy $_comp2xy($name)
[1]221    }
[839]222    foreach name [array names _comp2unirect2d] {
[1570]223        itcl::delete object $_comp2unirect2d($name)
[839]224    }
[1373]225    foreach name [array names _comp2unirect3d] {
[1570]226        itcl::delete object $_comp2unirect3d($name)
[1373]227    }
[1441]228    foreach name [array names _comp2flowhints] {
[1570]229        itcl::delete object $_comp2flowhints($name)
[1441]230    }
[3330]231    foreach name [array names _comp2mesh] {
232        # Data is in the form of a mesh and a vector.
233        foreach { mesh vector } $_comp2mesh($name) break
234        # Release the mesh (may be shared)
235        set class [$mesh info class]
236        ${class}::release $mesh
237        # Destroy the vector
238        blt::vector destroy $vector
239    }
[1]240}
241
242# ----------------------------------------------------------------------
[136]243# USAGE: components ?-name|-dimensions|-style? ?<pattern>?
[1]244#
[11]245# Returns a list of names or types for the various components of
246# this field.  If the optional glob-style <pattern> is specified,
247# then it returns only the components with names matching the pattern.
[1]248# ----------------------------------------------------------------------
[11]249itcl::body Rappture::Field::components {args} {
250    Rappture::getopts args params {
[1570]251        flag what -name default
252        flag what -dimensions
253        flag what -style
254        flag what -particles
255        flag what -flow
256        flag what -box
[11]257    }
258
259    set pattern *
260    if {[llength $args] > 0} {
[1570]261        set pattern [lindex $args 0]
262        set args [lrange $args 1 end]
[11]263    }
264    if {[llength $args] > 0} {
[1570]265        error "wrong # args: should be \"components ?switches? ?pattern?\""
[11]266    }
267
[3571]268    # There's only one dimension of the field.  Components can't have
269    # different dimensions in the same field.  They would by definition be
270    # using different meshes and viewers.
271    if { $params(what) == "-dimensions" } {
272        return "${_dim}D"
273    }
274    # BE CAREFUL: return component names in proper order
[1]275    set rlist ""
[3581]276    set components {}
277    # First compile a list of valid components that match the pattern
278    foreach cname [$_field children -type component] {
279        if { ![info exists _isValidComponent($cname)] } {
280            continue
281        }
282        if { [string match $pattern $cname] } {
283            lappend components $cname
284        }
285    }
286    # Now handle the tests.
[3571]287    switch -- $params(what) {
288        -name {
[3581]289            set rlist $components
[1570]290        }
[3571]291        -style {
292            foreach cname $components {
293                if { [info exists _comp2style($cname)] } {
294                    lappend rlist $_comp2style($cname)
295                }
296            }
297        }
[1]298    }
299    return $rlist
300}
301
302# ----------------------------------------------------------------------
[11]303# USAGE: mesh ?<name>?
[1]304#
305# Returns a list {xvec yvec} for the specified field component <name>.
306# If the name is not specified, then it returns the vectors for the
307# overall field (sum of all components).
308# ----------------------------------------------------------------------
[3330]309itcl::body Rappture::Field::mesh {{cname -overall}} {
310    if {$cname == "-overall" || $cname == "component0"} {
311        set cname [lindex [components -name] 0]
[9]312    }
[3330]313    if {[info exists _comp2xy($cname)]} {
314        return [lindex $_comp2xy($cname) 0]  ;# return xv
[1]315    }
[3330]316    if { [info exists _comp2vtk($cname)] } {
317        # FIXME: extract mesh from VTK file data.
[3546]318        if { $_comp2dims($cname) == "1D" } {
319            return $_xv
320        }
[3330]321        error "method \"mesh\" is not implemented for VTK file data"
[2504]322    }
[3330]323    if {[info exists _comp2dx($cname)]} {
[4493]324        return ""  ;# no mesh -- it's embedded in the blob data
[447]325    }
[3330]326    if {[info exists _comp2mesh($cname)]} {
[2727]327        return ""  ;# no mesh -- it's embedded in the value data
328    }
[3330]329    if {[info exists _comp2unirect2d($cname)]} {
330        set mobj [lindex $_comp2unirect2d($cname) 0]
[1570]331        return [$mobj mesh]
[839]332    }
[3330]333    if {[info exists _comp2unirect3d($cname)]} {
334        set mobj [lindex $_comp2unirect3d($cname) 0]
[1570]335        return [$mobj mesh]
[1373]336    }
[3571]337    error "can't get field mesh: Unknown component \"$cname\": should be one of [join [lsort [array names _comp2dims]] {, }]"
[1]338}
339
340# ----------------------------------------------------------------------
[11]341# USAGE: values ?<name>?
342#
343# Returns a list {xvec yvec} for the specified field component <name>.
344# If the name is not specified, then it returns the vectors for the
345# overall field (sum of all components).
346# ----------------------------------------------------------------------
[3571]347itcl::body Rappture::Field::values {cname} {
[3330]348    if {$cname == "component0"} {
349        set cname "component"
[11]350    }
[3330]351    if {[info exists _comp2xy($cname)]} {
352        return [lindex $_comp2xy($cname) 1]  ;# return yv
[11]353    }
[3330]354    # VTK file data
355    if { [info exists _comp2vtk($cname)] } {
356        # FIXME: extract the values from the VTK file data
[3546]357        if { $_comp2dims($cname) == "1D" } {
358            return $_values
359        }
[3330]360        error "method \"values\" is not implemented for vtk file data"
[3027]361    }
[3330]362    # Points-on-mesh
363    if { [info exists _comp2mesh($cname)] } {
364        set vector [lindex $_comp2mesh($cname) 1]
365        return [$vector range 0 end]
[2504]366    }
[3330]367    if {[info exists _comp2dx($cname)]} {
[4489]368        error "method \"values\" is not implemented for dx file data"
[11]369    }
[3330]370    if {[info exists _comp2unirect2d($cname)]} {
371        return [$_comp2unirect2d($cname) values]
[447]372    }
[3330]373    if {[info exists _comp2unirect3d($cname)]} {
374        return [$_comp2unirect3d($cname) blob]
[839]375    }
[3571]376    error "can't get field values. Unknown component \"$cname\": should be [join [lsort [array names _comp2dims]] {, }]"
[11]377}
378
379# ----------------------------------------------------------------------
[839]380# USAGE: blob ?<name>?
381#
382# Returns a string representing the blob of data for the mesh and values.
383# ----------------------------------------------------------------------
[3571]384itcl::body Rappture::Field::blob {cname} {
[3330]385    if {$cname == "component0"} {
386        set cname "component"
[839]387    }
[3330]388    if {[info exists _comp2xy($cname)]} {
[1570]389        return ""
[839]390    }
[3330]391    if { [info exists _comp2vtk($cname)] } {
392        error "blob not implemented for VTK file data"
[839]393    }
[3330]394    if {[info exists _comp2dx($cname)]} {
[4493]395        return $_comp2dx($cname)  ;# return gzipped, base64-encoded DX data
[2727]396    }
[3330]397    if {[info exists _comp2unirect2d($cname)]} {
[3365]398        set blob [$_comp2unirect2d($cname) blob]
399        lappend blob "values" $_values
400        return $blob
[3027]401    }
[3330]402    if {[info exists _comp2unirect3d($cname)]} {
403        return [$_comp2unirect3d($cname) blob]
[2504]404    }
[3571]405    error "can't get field blob: Unknown component \"$cname\": should be one of [join [lsort [array names _comp2dims]] {, }]"
[839]406}
407
[4489]408# ----------------------------------------------------------------------
409# USAGE: valueLimits <cname>
410#
411# Returns an array for the requested component with a list {min max}
412# representing the limits for each axis.
413# ----------------------------------------------------------------------
[3930]414itcl::body Rappture::Field::valueLimits { cname } {
415    if { [info exists _comp2limits($cname)] } {
416        return $_comp2limits($cname)
417    }
418    return ""
419}
420
[839]421# ----------------------------------------------------------------------
[11]422# USAGE: limits <axis>
423#
424# Returns a list {min max} representing the limits for the specified
425# axis.
426# ----------------------------------------------------------------------
[64]427itcl::body Rappture::Field::limits {which} {
428    set min ""
429    set max ""
[3330]430    blt::vector tmp zero
[64]431
[3330]432    foreach cname [array names _comp2dims] {
433        switch -- $_comp2dims($cname) {
[1570]434            1D {
435                switch -- $which {
[3330]436                    x - xlin {
437                        set pos 0; set log 0; set axis x
438                    }
439                    xlog {
440                        set pos 0; set log 1; set axis x
441                    }
442                    y - ylin - v - vlin {
443                        set pos 1; set log 0; set axis y
444                    }
445                    ylog - vlog {
446                        set pos 1; set log 1; set axis y
447                    }
[1570]448                    default {
[3571]449                        error "bad axis \"$which\": should be x, xlin, xlog, y, ylin, ylog, v, vlin, vlog"
[1570]450                    }
451                }
[11]452
[3330]453                set vname [lindex $_comp2xy($cname) $pos]
[11]454
[1570]455                if {$log} {
456                    # on a log scale, use abs value and ignore 0's
457                    $vname dup tmp
458                    $vname dup zero
459                    zero expr {tmp == 0}            ;# find the 0's
460                    tmp expr {abs(tmp)}             ;# get the abs value
461                    tmp expr {tmp + zero*max(tmp)}  ;# replace 0's with abs max
[3330]462                    set axisMin [blt::vector expr min(tmp)]
463                    set axisMax [blt::vector expr max(tmp)]
[1570]464                } else {
[4251]465                    set axisMin [$vname min]
466                    set axisMax [$vname max]
[1570]467                }
[16]468
[1570]469                if {"" == $min} {
[3330]470                    set min $axisMin
471                } elseif {$axisMin < $min} {
472                    set min $axisMin
[1570]473                }
474                if {"" == $max} {
[3330]475                    set max $axisMax
476                } elseif {$axisMax > $max} {
477                    set max $axisMax
[1570]478                }
479            }
480            2D - 3D {
[3940]481                if {[info exists _comp2limits($cname)]} {
[3330]482                    array set limits $_comp2limits($cname)
483                    switch -- $which {
[1570]484                        x - xlin - xlog {
[3330]485                            set axis x
486                            foreach {axisMin axisMax} $limits(x) break
[1570]487                        }
488                        y - ylin - ylog {
[3330]489                            set axis y
490                            foreach {axisMin axisMax} $limits(y) break
[1570]491                        }
492                        z - zlin - zlog {
[3481]493                            set axis z
[3330]494                            foreach {axisMin axisMax} $limits(z) break
[1570]495                        }
496                        v - vlin - vlog {
[3330]497                            set axis v
498                            foreach {axisMin axisMax} $limits(v) break
499                        }
500                        default {
501                            if { ![info exists limits($which)] } {
502                                error "limits: unknown axis \"$which\""
503                            }
504                            set axis v
505                            foreach {axisMin axisMax} $limits($which) break
506                        }
507                    }
[1570]508                } else {
[3330]509                    set axisMin 0  ;# HACK ALERT! must be OpenDX data
510                    set axisMax 1
511                    set axis v
[1570]512                }
513            }
514        }
[3330]515        if { "" == $min || $axisMin < $min } {
516            set min $axisMin
[1570]517        }
[3330]518        if { "" == $max || $axisMax > $max } {
519            set max $axisMax
[1570]520        }
[11]521    }
[64]522    blt::vector destroy tmp zero
[3571]523    set val [$_field get "${axis}axis.min"]
[64]524    if {"" != $val && "" != $min} {
[1570]525        if {$val > $min} {
526            # tool specified this min -- don't go any lower
527            set min $val
528        }
[64]529    }
[3571]530    set val [$_field get "${axis}axis.max"]
[64]531    if {"" != $val && "" != $max} {
[1570]532        if {$val < $max} {
533            # tool specified this max -- don't go any higher
534            set max $val
535        }
[64]536    }
537    return [list $min $max]
[11]538}
539
540# ----------------------------------------------------------------------
[3429]541# USAGE: fieldlimits
[3428]542#
543# Returns a list {min max} representing the limits for the specified
544# axis.
545# ----------------------------------------------------------------------
[3429]546itcl::body Rappture::Field::fieldlimits {} {
[3428]547    foreach cname [array names _comp2limits] {
548        array set limits $_comp2limits($cname)
[4423]549        foreach fname [fieldnames $cname] {
[3428]550            if { ![info exists limits($fname)] } {
[3573]551                puts stderr "ERROR: field \"$fname\" unknown in \"$cname\""
[3428]552                continue
553            }
554            foreach {min max} $limits($fname) break
555            if { ![info exists overall($fname)] } {
556                set overall($fname) $limits($fname)
557                continue
558            }
559            foreach {omin omax} $overall($fname) break
560            if { $min < $omin } {
561                set omin $min
562            }
563            if { $max > $omax } {
564                set omax $max
565            }
566            set overall($fname) [list $min $max]
567        }
568    }
569    if { [info exists overall] } {
570        return [array get overall]
571    }
572    return ""
573}
574 
575# ----------------------------------------------------------------------
[1]576# USAGE: controls get ?<name>?
[729]577# USAGE: controls validate <path> <value>
[1]578# USAGE: controls put <path> <value>
579#
580# Returns a list {path1 x1 y1 val1  path2 x2 y2 val2 ...} representing
581# control points for the specified field component <name>.
582# ----------------------------------------------------------------------
583itcl::body Rappture::Field::controls {option args} {
584    switch -- $option {
[1570]585        get {
[3330]586            set cname [lindex $args 0]
587            if {[info exists _comp2cntls($cname)]} {
588                return $_comp2cntls($cname)
[1570]589            }
590            return ""
591        }
592        validate {
593            set path [lindex $args 0]
594            set value [lindex $args 1]
595            set units [$_xmlobj get $path.units]
[729]596
[1570]597            if {"" != $units} {
598                set nv [Rappture::Units::convert \
599                    $value -context $units -to $units -units off]
600            } else {
601                set nv $value
602            }
603            if {![string is double $nv]
604                  || [regexp -nocase {^(inf|nan)$} $nv]} {
605                error "Value out of range"
606            }
[729]607
[1570]608            set rawmin [$_xmlobj get $path.min]
609            if {"" != $rawmin} {
610                set minv $rawmin
611                if {"" != $units} {
612                    set minv [Rappture::Units::convert \
613                        $minv -context $units -to $units -units off]
614                    set nv [Rappture::Units::convert \
615                        $value -context $units -to $units -units off]
616                }
617                # fix for the case when the user tries to
618                # compare values like minv=-500 nv=-0600
619                set nv [format "%g" $nv]
620                set minv [format "%g" $minv]
[729]621
[1570]622                if {$nv < $minv} {
623                    error "Minimum value allowed here is $rawmin"
624                }
625            }
[729]626
[1570]627            set rawmax [$_xmlobj get $path.max]
628            if {"" != $rawmax} {
629                set maxv $rawmax
630                if {"" != $units} {
631                    set maxv [Rappture::Units::convert \
632                        $maxv -context $units -to $units -units off]
633                    set nv [Rappture::Units::convert \
634                        $value -context $units -to $units -units off]
635                }
636                # fix for the case when the user tries to
637                # compare values like maxv=-500 nv=-0600
638                set nv [format "%g" $nv]
639                set maxv [format "%g" $maxv]
[729]640
[1570]641                if {$nv > $maxv} {
642                    error "Maximum value allowed here is $rawmax"
643                }
644            }
[729]645
[1570]646            return "ok"
647        }
648        put {
649            set path [lindex $args 0]
650            set value [lindex $args 1]
651            $_xmlobj put $path.current $value
[3330]652            Build
[1570]653        }
654        default {
[3571]655            error "bad field controls option \"$option\": should be get or put"
[1570]656        }
[1]657    }
658}
659
660# ----------------------------------------------------------------------
661# USAGE: hints ?<keyword>?
662#
663# Returns a list of key/value pairs for various hints about plotting
664# this field.  If a particular <keyword> is specified, then it returns
665# the hint for that <keyword>, if it exists.
666# ----------------------------------------------------------------------
667itcl::body Rappture::Field::hints {{keyword ""}} {
[3330]668    if { $keyword != "" } {
669        if {[info exists _hints($keyword)]} {
670            return $_hints($keyword)
[1570]671        }
672        return ""
[1]673    }
[3330]674    return [array get _hints]
[1]675}
676
[3917]677
[1]678# ----------------------------------------------------------------------
[3917]679# USAGE: InitHints
680#
681# Returns a list of key/value pairs for various hints about plotting
682# this field.  If a particular <keyword> is specified, then it returns
683# the hint for that <keyword>, if it exists.
684# ----------------------------------------------------------------------
685itcl::body Rappture::Field::InitHints {} {
686    foreach {key path} {
687        camera          camera.position
688        color           about.color
689        default         about.default
690        group           about.group
691        label           about.label
692        scale           about.scale
693        seeds           about.seeds
694        style           about.style
695        type            about.type
696        xlabel          about.xaxis.label
697        ylabel          about.yaxis.label
698        zlabel          about.zaxis.label
699        xunits          about.xaxis.units
700        yunits          about.yaxis.units
701        zunits          about.zaxis.units
702        units           units
703        updir           updir
704        vectors         about.vectors
705    } {
706        set str [$_field get $path]
707        if { "" != $str } {
708            set _hints($key) $str
709        }
710    }
711    foreach cname [components] {
712        if { ![info exists _comp2mesh($cname)] } {
713            continue
714        }
715        set mesh [lindex $_comp2mesh($cname) 0]
716        foreach axis {x y z} {
717            if { ![info exists _hints(${axis}units)] } {
718                set _hints(${axis}units) [$mesh units $axis]
719            }
720            if { ![info exists _hints(${axis}label)] } {
721                set _hints(${axis}label) [$mesh label $axis]
722            }
723        }
724    }
725    foreach {key path} {
726        toolid          tool.id
727        toolname        tool.name
728        toolcommand     tool.execute
729        tooltitle       tool.title
730        toolrevision    tool.version.application.revision
731    } {
732        set str [$_xmlobj get $path]
733        if { "" != $str } {
734            set _hints($key) $str
735        }
736    }
737    # Set toolip and path hints
738    set _hints(path) $_path
739    if { [info exists _hints(group)] && [info exists _hints(label)] } {
740        # pop-up help for each curve
741        set _hints(tooltip) $_hints(label)
742    }
743}
744
745# ----------------------------------------------------------------------
[3330]746# USAGE: Build
[1]747#
748# Used internally to build up the vector representation for the
749# field when the object is first constructed, or whenever the field
750# data changes.  Discards any existing vectors and builds everything
751# from scratch.
752# ----------------------------------------------------------------------
[3330]753itcl::body Rappture::Field::Build {} {
754
755    # Discard any existing data
[11]756    foreach name [array names _comp2xy] {
[1570]757        eval blt::vector destroy $_comp2xy($name)
[1]758    }
[3330]759    array unset _comp2vtk
[839]760    foreach name [array names _comp2unirect2d] {
[1570]761        eval itcl::delete object $_comp2unirect2d($name)
[839]762    }
[1463]763    foreach name [array names _comp2unirect3d] {
[1570]764        eval itcl::delete object $_comp2unirect3d($name)
[1463]765    }
[11]766    catch {unset _comp2xy}
[447]767    catch {unset _comp2dx}
[11]768    catch {unset _comp2dims}
[136]769    catch {unset _comp2style}
[839]770    array unset _comp2unirect2d
[1373]771    array unset _comp2unirect3d
772    array unset _comp2extents
[2504]773    array unset _dataobj2type
[1]774    #
775    # Scan through the components of the field and create
776    # vectors for each part.
777    #
[3571]778    array unset _isValidComponent
779    foreach cname [$_field children -type component] {
[1570]780        set type ""
[3571]781        if { ([$_field element $cname.constant] != "" &&
782              [$_field element $cname.domain] != "") ||
783              [$_field element $cname.xy] != "" } {
[1570]784            set type "1D"
[3571]785        } elseif { [$_field element $cname.mesh] != "" &&
786                   [$_field element $cname.values] != ""} {
[1570]787            set type "points-on-mesh"
[3571]788        } elseif { [$_field element $cname.vtk] != ""} {
[3585]789            set type "vtk"
[3571]790            set viewer [$_field get "about.view"]
[3330]791            if { $viewer != "" } {
792                set _viewer $viewer
793            }
[3571]794        } elseif {[$_field element $cname.opendx] != ""} {
[2744]795            global env
796            if { [info exists env(VTKVOLUME)] } {
[3893]797                set _viewer "vtkvolume"
[3885]798            }
799            set type "opendx"
[3571]800        } elseif {[$_field element $cname.dx] != ""} {
[2744]801            global env
802            if { [info exists env(VTKVOLUME)] } {
[3893]803                set _viewer "vtkvolume"
[2744]804            }
[3885]805            set type "dx"
[3571]806        } elseif {[$_field element $cname.ucd] != ""} {
[3546]807            set type "ucd"
[3960]808        } elseif {[$_field element $cname.dicom] != ""} {
809            set type "dicom"
[3330]810        }
[1570]811        set _comp2style($cname) ""
[3571]812        if { $type == "" } {
[4388]813            puts stderr "WARNING: Ignoring field component \"$_path.$cname\": no data found."
[3571]814            continue
815        }
[1570]816        # Save the extents of the component
[3571]817        if { [$_field element $cname.extents] != "" } {
818            set extents [$_field get $cname.extents]
[1570]819        } else {
820            set extents 1
821        }
822        set _comp2extents($cname) $extents
[2744]823        set _type $type
[3684]824
825        GetTypeAndSize $cname
826        GetAssociation $cname
827        if { $_comp2size($cname) > 1 } {
828            set viewer [$_field get "about.view"]
829            if { $viewer == "" } {
830                set _viewer "glyphs"
831            }
832        }
[1570]833        if {$type == "1D"} {
834            #
835            # 1D data can be represented as 2 BLT vectors,
836            # one for x and the other for y.
837            #
838            set xv ""
839            set yv ""
[3594]840            set _dim 1
[3571]841            set val [$_field get $cname.constant]
[1570]842            if {$val != ""} {
[3571]843                set domain [$_field get $cname.domain]
[1570]844                if {$domain == "" || ![info exists _limits($domain)]} {
845                    set z0 0
846                    set z1 $_zmax
847                } else {
848                    foreach {z0 z1} $_limits($domain) { break }
849                }
850                set xv [blt::vector create x$_counter]
851                $xv append $z0 $z1
[894]852
[1570]853                foreach {val pcomp} [_getValue $val] break
854                set yv [blt::vector create y$_counter]
855                $yv append $val $val
[894]856
[1570]857                if {$pcomp != ""} {
858                    set zm [expr {0.5*($z0+$z1)}]
859                    set _comp2cntls($cname) \
860                        [list $pcomp $zm $val "$val$_units"]
861                }
862            } else {
[3571]863                set xydata [$_field get $cname.xy]
[1570]864                if {"" != $xydata} {
865                    set xv [blt::vector create x$_counter]
866                    set yv [blt::vector create y$_counter]
867                    set tmp [blt::vector create \#auto]
868                    $tmp set $xydata
869                    $tmp split $xv $yv
870                    blt::vector destroy $tmp
871                }
872            }
[894]873
[1570]874            if {$xv != "" && $yv != ""} {
875                # sort x-coords in increasing order
876                $xv sort $yv
[3594]877                set _dim 1
[1570]878                set _comp2dims($cname) "1D"
879                set _comp2xy($cname) [list $xv $yv]
880                incr _counter
881            }
882        } elseif {$type == "points-on-mesh"} {
[3571]883            if { ![BuildPointsOnMesh $cname] } {
884                continue;               # Ignore this component
885            }
[1570]886        } elseif {$type == "vtk"} {
[3571]887            set contents [$_field get $cname.vtk]
888            if { $contents == "" } {
[4389]889                puts stderr "WARNING: No data for \"$_path.$cname.vtk\""
[3571]890                continue;               # Ignore this component
891            }
892            ReadVtkDataSet $cname $contents
893            set _comp2vtk($cname) $contents
894            set _comp2style($cname) [$_field get $cname.style]
[2592]895            incr _counter
[3571]896        } elseif {$type == "dx" || $type == "opendx" } {
[1570]897            #
898            # HACK ALERT!  Extract gzipped, base64-encoded OpenDX
899            # data.  Assume that it's 3D.  Pass it straight
900            # off to the NanoVis visualizer.
901            #
[3893]902            set viewer [$_field get "about.view"]
903            if { $viewer != "" } {
904                set _viewer $viewer
905            }
906            if { $_viewer == "" } {
[4489]907                if {[$_field element $cname.flow] != ""} {
908                    set _viewer "flowvis"
909                } else {
910                    set _viewer "nanovis"
911                }
[3893]912            }
[3571]913            set _dim 3
[1570]914            set _comp2dims($cname) "3D"
[3930]915            set data [$_field get -decode no $cname.$type]
[3940]916            set contents [Rappture::encoding::decode -as zb64 $data]
917            if { $contents == "" } {
[4388]918                puts stderr "WARNING: No data for \"$_path.$cname.$type\""
[3930]919                continue;               # Ignore this component
920            }
[3939]921            if 0 {
922                set f [open /tmp/$_path.$cname.dx "w"]
923                puts -nonewline $f $contents
924                close $f
925            }
926            if { [catch { Rappture::DxToVtk $contents } vtkdata] == 0 } {
[4120]927                ReadVtkDataSet $cname $vtkdata
[4502]928                if 0 {
929                    set f [open /tmp/$_path.$cname.vtk "w"]
930                    puts -nonewline $f $vtkdata
931                    close $f
932                }
[3939]933            } else {
934                puts stderr "Can't parse dx data: $vtkdata"
935            }
[4417]936            if { $_alwaysConvertDX ||
937                 ($_viewer != "nanovis" && $_viewer != "flowvis") } {
[3867]938                set _type "vtk"
939                set _comp2vtk($cname) $vtkdata
940            } else {
941                set _type "dx"
[4120]942                set _comp2dx($cname) $data
[2744]943            }
[3571]944            set _comp2style($cname) [$_field get $cname.style]
945            if {[$_field element $cname.flow] != ""} {
[1570]946                set _comp2flowhints($cname) \
[3571]947                    [Rappture::FlowHints ::\#auto $_field $cname $_units]
[1570]948            }
949            incr _counter
[3960]950        } elseif { $type == "dicom"} {
951            set contents [$_field get $cname.dicom]
952            if { $contents == "" } {
[3966]953                continue;               # Ignore this component
[3960]954            }
[3966]955            set viewer [$_field get "about.view"]
956            if { $viewer != "" } {
957                set _viewer $viewer
958            }
[4126]959            set vtkdata [DicomToVtk $cname $contents]
[3964]960            if { $_viewer == "" } {
[3972]961                set _viewer [expr {($_dim == 3) ? "vtkvolume" : "vtkimage"}]
[3964]962            }
[3960]963            set _comp2vtk($cname) $vtkdata
964            set _comp2style($cname) [$_field get $cname.style]
965            incr _counter
[3571]966        } elseif { $type == "ucd"} {
967            set contents [$_field get $cname.ucd]
968            if { $contents == "" } {
[3966]969                continue;               # Ignore this component
[1570]970            }
[3330]971            set vtkdata [AvsToVtk $cname $contents]
[3571]972            ReadVtkDataSet $cname $vtkdata
973            set _comp2vtk($cname) $vtkdata
974            set _comp2style($cname) [$_field get $cname.style]
[3330]975            incr _counter
[1570]976        }
[3571]977        set _isValidComponent($cname) 1
[3966]978        #puts stderr "Field $cname type is: $_type"
[11]979    }
[3571]980    if { [array size _isValidComponent] == 0 } {
[4388]981        puts stderr "ERROR: All components of field \"$_path\" are invalid."
[3571]982        return 0
983    }
[3330]984    # Sanity check.  Verify that all components of the field have the same
985    # dimension.
986    set dim ""
987    foreach cname [array names _comp2dims] {
988        if { $dim == "" } {
989            set dim $_comp2dims($cname)
990            continue
991        }
992        if { $dim != $_comp2dims($cname) } {
[4388]993            puts stderr "WARNING: A field can't have components of different dimensions: [join [array get _comp2dims] ,]"
[3571]994            return 0
[3330]995        }
996    }
[3966]997
[3405]998    # FIXME: about.scalars and about.vectors are temporary.  With views
[3406]999    #        the label and units for each field will be specified there.
[3405]1000    #
[3406]1001    # FIXME: Test that every <field><component> has the same field names,
1002    #        units, components.
1003    #
[3405]1004    # Override what we found in the VTK file with names that the user
1005    # selected.  We override the field label and units.
[3571]1006    foreach { fname label units } [$_field get about.scalars] {
1007        if { ![info exists _fld2Name($fname)] } {
1008            set _fld2Name($fname) $fname
1009            set _fld2Components($fname) 1
[3405]1010        }
[3571]1011        set _fld2Label($fname) $label
1012        set _fld2Units($fname) $units
[3405]1013    }
[3571]1014    foreach { fname label units } [$_field get about.vectors] {
1015        if { ![info exists _fld2Name($fname)] } {
1016            set _fld2Name($fname) $fname
[3406]1017            # We're just marking the field as vector (> 1) for now.
[3571]1018            set _fld2Components($fname) 3
[3405]1019        }
[3571]1020        set _fld2Label($fname) $label
1021        set _fld2Units($fname) $units
[3405]1022    }
[3571]1023    set _isValid 1
1024    return 1
[11]1025}
[1]1026
[11]1027# ----------------------------------------------------------------------
1028# USAGE: _getValue <expr>
1029#
1030# Used internally to get the value for an expression <expr>.  Returns
1031# a list of the form {val parameterPath}, where val is the numeric
1032# value of the expression, and parameterPath is the XML path to the
1033# parameter representing the value, or "" if the <expr> does not
1034# depend on any parameters.
1035# ----------------------------------------------------------------------
1036itcl::body Rappture::Field::_getValue {expr} {
1037    #
1038    # First, look for the expression among the <parameter>'s
1039    # associated with the device.
1040    #
1041    set found 0
1042    foreach pcomp [$_xmlobj children parameters] {
[1570]1043        set id [$_xmlobj element -as id parameters.$pcomp]
1044        if {[string equal $id $expr]} {
1045            set val [$_xmlobj get parameters.$pcomp.current]
1046            if {"" == $val} {
1047                set val [$_xmlobj get parameters.$pcomp.default]
1048            }
1049            if {"" != $val} {
1050                set expr $val
1051                set found 1
1052                break
1053            }
1054        }
[1]1055    }
[11]1056    if {$found} {
[1570]1057        set pcomp "parameters.$pcomp"
[11]1058    } else {
[1570]1059        set pcomp ""
[11]1060    }
1061
1062    if {$_units != ""} {
[1570]1063        set expr [Rappture::Units::convert $expr \
1064            -context $_units -to $_units -units off]
[11]1065    }
1066
1067    return [list $expr $pcomp]
[1]1068}
[1189]1069
1070#
[1373]1071# isunirect2d  --
1072#
1073# Returns if the field is a unirect2d object. 
1074#
[1189]1075itcl::body Rappture::Field::isunirect2d { } {
1076    return [expr [array size _comp2unirect2d] > 0]
1077}
[1373]1078
1079#
1080# isunirect3d  --
1081#
1082# Returns if the field is a unirect3d object. 
1083#
1084itcl::body Rappture::Field::isunirect3d { } {
1085    return [expr [array size _comp2unirect3d] > 0]
1086}
1087
1088#
[1441]1089# flowhints  --
1090#
1091# Returns the hints associated with a flow vector field. 
1092#
1093itcl::body Rappture::Field::flowhints { cname } {
1094    if { [info exists _comp2flowhints($cname)] } {
[1570]1095        return $_comp2flowhints($cname)
[1441]1096    }
1097    return ""
1098}
1099
1100#
[2504]1101# style  --
1102#
1103# Returns the style associated with a component of the field. 
1104#
1105itcl::body Rappture::Field::style { cname } {
1106    if { [info exists _comp2style($cname)] } {
1107        return $_comp2style($cname)
1108    }
1109    return ""
1110}
1111
1112#
1113# type  --
1114#
[3955]1115# Returns the data storage type of the field.
[2504]1116#
[3955]1117# FIXME: What are the valid types?
1118#
[2504]1119itcl::body Rappture::Field::type {} {
1120    return $_type
1121}
1122
1123#
[3684]1124# numComponents --
1125#
[4177]1126# Returns the number of components in the field component.
[3684]1127#
1128itcl::body Rappture::Field::numComponents {cname} {
1129    set name $cname
1130    regsub -all { } $name {_} name
1131    if {[info exists _fld2Components($name)] } {
1132        return $_fld2Components($name)
1133    }
1134    return 1;                           # Default to scalar.
1135}
1136
1137#
[1373]1138# extents --
1139#
1140# Returns if the field is a unirect2d object. 
1141#
[3330]1142itcl::body Rappture::Field::extents {{cname -overall}} {
1143    if {$cname == "-overall" } {
[1570]1144        set max 0
[3571]1145        foreach cname [$_field children -type component] {
[1570]1146            if { ![info exists _comp2unirect3d($cname)] &&
1147                 ![info exists _comp2extents($cname)] } {
1148                continue
1149            }
1150            set value $_comp2extents($cname)
1151            if { $max < $value } {
1152                set max $value
1153            }
1154        }
1155        return $max
[1373]1156    }
[3330]1157    if { $cname == "component0"} {
1158        set cname [lindex [components -name] 0]
[1373]1159    }
[3330]1160    return $_comp2extents($cname)
[1373]1161}
[2718]1162
[3585]1163itcl::body Rappture::Field::VerifyVtkDataSet { contents } {
1164    package require vtk
1165
1166    set reader $this-datasetreader
1167    vtkDataSetReader $reader
1168
1169    # Write the contents to a file just in case it's binary.
1170    set tmpfile file[pid].vtk
1171    set f [open "$tmpfile" "w"]
1172    fconfigure $f -translation binary -encoding binary
1173    puts $f $contents
1174    close $f
1175
1176    $reader SetFileName $tmpfile
[3703]1177    $reader ReadAllNormalsOn
1178    $reader ReadAllTCoordsOn
[3585]1179    $reader ReadAllScalarsOn
[3703]1180    $reader ReadAllColorScalarsOn
[3585]1181    $reader ReadAllVectorsOn
[3703]1182    $reader ReadAllTensorsOn
[3585]1183    $reader ReadAllFieldsOn
1184    $reader Update
[3707]1185    file delete $tmpfile
1186
[3585]1187    set dataset [$reader GetOutput]
1188    set dataAttrs [$dataset GetPointData]
1189    if { $dataAttrs == ""} {
[4388]1190        puts stderr "WARNING: No point data found in \"$_path\""
[3707]1191        rename $reader ""
[3585]1192        return 0
1193    }
1194    rename $reader ""
1195}
1196
[3330]1197itcl::body Rappture::Field::ReadVtkDataSet { cname contents } {
[3027]1198    package require vtk
1199
1200    set reader $this-datasetreader
1201    vtkDataSetReader $reader
1202
1203    # Write the contents to a file just in case it's binary.
1204    set tmpfile file[pid].vtk
1205    set f [open "$tmpfile" "w"]
1206    fconfigure $f -translation binary -encoding binary
1207    puts $f $contents
1208    close $f
[3416]1209
[3027]1210    $reader SetFileName $tmpfile
[3703]1211    $reader ReadAllNormalsOn
1212    $reader ReadAllTCoordsOn
[3027]1213    $reader ReadAllScalarsOn
[3703]1214    $reader ReadAllColorScalarsOn
[3027]1215    $reader ReadAllVectorsOn
[3703]1216    $reader ReadAllTensorsOn
[3027]1217    $reader ReadAllFieldsOn
1218    $reader Update
[3707]1219    file delete $tmpfile
1220
[3027]1221    set dataset [$reader GetOutput]
1222    set limits {}
1223    foreach {xmin xmax ymin ymax zmin zmax} [$dataset GetBounds] break
[3330]1224    # Figure out the dimension of the mesh from the bounds.
1225    set _dim 0
1226    if { $xmax > $xmin } {
1227        incr _dim
1228    }
1229    if { $ymax > $ymin } {
1230        incr _dim
1231    }
1232    if { $zmax > $zmin } {
1233        incr _dim
1234    }
1235    if { $_viewer == "" } {
1236        if { $_dim == 2 } {
1237            set _viewer contour
1238        } else {
1239            set _viewer isosurface
1240        }
1241    }
1242    set _comp2dims($cname) ${_dim}D
[3546]1243    if { $_dim < 2 } {
[3707]1244        set numPoints [$dataset GetNumberOfPoints]
[3546]1245        set xv [blt::vector create \#auto]
1246        for { set i 0 } { $i < $numPoints } { incr i } {
[3707]1247            set point [$dataset GetPoint $i]
[3546]1248            $xv append [lindex $point 0]
1249        }
1250        set yv [blt::vector create \#auto]
[3707]1251        set dataAttrs [$dataset GetPointData]
1252        if { $dataAttrs == ""} {
[4388]1253            puts stderr "WARNING: No point data found in \"$_path\""
[3707]1254            rename $reader ""
1255            return 0
1256        }
1257        set array [$dataAttrs GetScalars]
1258        if { $array == ""} {
[4388]1259            puts stderr "WARNING: No scalar point data found in \"$_path\""
[3707]1260            rename $reader ""
1261            return 0
1262        }
1263        set numTuples [$array GetNumberOfTuples]
1264        for { set i 0 } { $i < $numTuples } { incr i } {
1265            $yv append [$array GetComponent $i 0]
1266        }
1267        $xv sort $yv
[3546]1268        set _comp2xy($cname) [list $xv $yv]
1269    }
[3330]1270    lappend limits x [list $xmin $xmax]
1271    lappend limits y [list $ymin $ymax]
1272    lappend limits z [list $zmin $zmax]
[3027]1273    set dataAttrs [$dataset GetPointData]
1274    if { $dataAttrs == ""} {
[4388]1275        puts stderr "WARNING: No point data found in \"$_path\""
[3707]1276        rename $reader ""
[3571]1277        return 0
[3027]1278    }
[3330]1279    set vmin 0
1280    set vmax 1
1281    set numArrays [$dataAttrs GetNumberOfArrays]
1282    if { $numArrays > 0 } {
1283        for {set i 0} {$i < [$dataAttrs GetNumberOfArrays] } {incr i} {
1284            set array [$dataAttrs GetArray $i]
[3405]1285            set fname  [$dataAttrs GetArrayName $i]
[3707]1286            foreach {min max} [$array GetRange -1] break
[4101]1287            if {$i == 0} {
1288                set vmin $min
1289                set vmax $max
1290            }
[3405]1291            lappend limits $fname [list $min $max]
[3571]1292            set _fld2Units($fname) ""
1293            set _fld2Label($fname) $fname
[3684]1294            # Let the VTK file override the <type> designated.
[3571]1295            set _fld2Components($fname) [$array GetNumberOfComponents]
1296            lappend _comp2fldName($cname) $fname
[3330]1297        }
[3027]1298    }
[3700]1299   
[3330]1300    lappend limits v [list $vmin $vmax]
1301    set _comp2limits($cname) $limits
[3707]1302
[3416]1303    rename $reader ""
[3027]1304}
1305
[3330]1306#
1307# vtkdata --
1308#
1309#       Returns a string representing the mesh and field data for a specific
1310#       component in the legacy VTK file format.
1311#
1312itcl::body Rappture::Field::vtkdata {cname} {
1313    if {$cname == "component0"} {
1314        set cname "component"
1315    }
1316    # DX: Convert DX to VTK
1317    if {[info exists _comp2dx($cname)]} {
[3757]1318        set data $_comp2dx($cname)
1319        set data [Rappture::encoding::decode $data]
1320        return [Rappture::DxToVtk $data]
[3330]1321    }
1322    # Unirect3d: isosurface
1323    if {[info exists _comp2unirect3d($cname)]} {
1324        return [$_comp2unirect3d($cname) vtkdata]
1325    }
1326    # VTK file data:
1327    if { [info exists _comp2vtk($cname)] } {
1328        return $_comp2vtk($cname)
1329    }
1330    # Points on mesh:  Construct VTK file output.
1331    if { [info exists _comp2mesh($cname)] } {
1332        # Data is in the form mesh and vector
1333        foreach {mesh vector} $_comp2mesh($cname) break
[3460]1334        set label $cname
[3421]1335        regsub -all { } $label {_} label
[3330]1336        append out "# vtk DataFile Version 3.0\n"
1337        append out "[hints label]\n"
1338        append out "ASCII\n"
1339        append out [$mesh vtkdata]
[3684]1340
1341        if { $_comp2assoc($cname) == "pointdata" } {
1342            set vtkassoc "POINT_DATA"
1343        } elseif { $_comp2assoc($cname) == "celldata" } {
1344            set vtkassoc "CELL_DATA"
1345        } elseif { $_comp2assoc($cname) == "fielddata" } {
[3700]1346            set vtkassoc "FIELD"
[3684]1347        } else {
1348            error "unknown association \"$_comp2assoc($cname)\""
1349        }
1350        set elemSize [numComponents $cname]
1351        set numValues [expr [$vector length] / $elemSize]
[3701]1352        if { $_comp2assoc($cname) == "fielddata" } {
1353            append out "$vtkassoc FieldData 1\n"
1354            append out "$label $elemSize $numValues double\n"
[3684]1355        } else {
[3701]1356            append out "$vtkassoc $numValues\n"
1357            if { $_comp2type($cname) == "colorscalars" } {
1358                # Must be float for ASCII, unsigned char for BINARY
1359                append out "COLOR_SCALARS $label $elemSize\n"
1360            } elseif { $_comp2type($cname) == "normals" } {
1361                # elemSize must equal 3
1362                append out "NORMALS $label double\n"
1363            } elseif { $_comp2type($cname) == "scalars" } {
1364                # elemSize can be 1, 2, 3 or 4
1365                append out "SCALARS $label double $elemSize\n"
1366                append out "LOOKUP_TABLE default\n"
1367            } elseif { $_comp2type($cname) == "tcoords" } {
1368                # elemSize must be 1, 2, or 3
1369                append out "TEXTURE_COORDINATES $label $elemSize double\n"
1370            } elseif { $_comp2type($cname) == "tensors" } {
1371                # elemSize must equal 9
1372                append out "TENSORS $label double\n"
1373            } elseif { $_comp2type($cname) == "vectors" } {
1374                # elemSize must equal 3
1375                append out "VECTORS $label double\n"
1376            } else {
1377                error "unknown element type \"$_comp2type($cname)\""
1378            }
[3684]1379        }
[3700]1380        append out [$vector range 0 end]
1381        append out "\n"
[3585]1382        if 0 {
1383            VerifyVtkDataSet $out
1384        }
[3330]1385        return $out
1386    }
1387    error "can't find vtkdata for $cname. This method should only be called by the vtkheightmap widget"
1388}
1389
1390#
1391# BuildPointsOnMesh --
1392#
1393#       Parses the field XML description to build a mesh and values vector
1394#       representing the field.  Right now we handle the deprecated types
1395#       of "cloud", "unirect2d", and "unirect3d" (mostly for flows).
1396#
1397itcl::body Rappture::Field::BuildPointsOnMesh {cname} {
1398    #
1399    # More complex 2D/3D data is represented by a mesh
1400    # object and an associated vector for field values.
1401    #
[3571]1402    set path [$_field get $cname.mesh]
[3330]1403    if {[$_xmlobj element $path] == ""} {
1404        # Unknown mesh designated.
[3571]1405        return 0
[3330]1406    }
[3684]1407    set viewer [$_field get "about.view"]
1408    if { $viewer != "" } {
1409        set _viewer $viewer
1410    }
[3330]1411    set element [$_xmlobj element -as type $path]
[3405]1412    set name $cname
1413    regsub -all { } $name {_} name
[3571]1414    set _fld2Label($name) $name
[3405]1415    set label [hints zlabel]
1416    if { $label != "" } {
[3571]1417        set _fld2Label($name) $label
[3405]1418    }
[3571]1419    set _fld2Units($name) [hints zunits]
[3684]1420    set _fld2Components($name) $_comp2size($cname)
[3571]1421    lappend _comp2fldName($cname) $name
[3330]1422
1423    # Handle bizarre cases that hopefully will be deprecated.
1424    if { $element == "unirect3d" } {
1425        # Special case: unirect3d (should be deprecated) + flow.
[3571]1426        if { [$_field element $cname.extents] != "" } {
[3684]1427            set vectorsize [$_field get $cname.extents]
[3330]1428        } else {
[3684]1429            set vectorsize 1
[3330]1430        }
[3955]1431        set _type unirect3d
[3330]1432        set _dim 3
[3557]1433        if { $_viewer == "" } {
1434            set _viewer flowvis
1435        }
[3330]1436        set _comp2dims($cname) "3D"
1437        set _comp2unirect3d($cname) \
[3684]1438            [Rappture::Unirect3d \#auto $_xmlobj $_field $cname $vectorsize]
[3571]1439        set _comp2style($cname) [$_field get $cname.style]
[3940]1440        set limits {}
1441        foreach axis { x y z } {
1442            lappend limits $axis [$_comp2unirect3d($cname) limits $axis]
1443        }
1444        # Get the data limits
1445        set vector [$_comp2unirect3d($cname) valuesObj]
[4101]1446        set minmax [VectorLimits $vector $vectorsize]
1447        lappend limits $cname $minmax
1448        lappend limits v      $minmax
[3940]1449        set _comp2limits($cname) $limits
[3571]1450        if {[$_field element $cname.flow] != ""} {
[3330]1451            set _comp2flowhints($cname) \
[3571]1452                [Rappture::FlowHints ::\#auto $_field $cname $_units]
[3330]1453        }
1454        incr _counter
[3571]1455        return 1
[3330]1456    }
[3571]1457    if { $element == "unirect2d" && [$_field element $cname.flow] != "" } {
[3330]1458        # Special case: unirect2d (normally deprecated) + flow.
[3571]1459        if { [$_field element $cname.extents] != "" } {
[3684]1460            set vectorsize [$_field get $cname.extents]
[3330]1461        } else {
[3684]1462            set vectorsize 1
[3330]1463        }
[3955]1464        set _type unirect2d
[3330]1465        set _dim 2
[3557]1466        if { $_viewer == "" } {
1467            set _viewer "flowvis"
1468        }
[3330]1469        set _comp2dims($cname) "2D"
1470        set _comp2unirect2d($cname) \
1471            [Rappture::Unirect2d \#auto $_xmlobj $path]
[3571]1472        set _comp2style($cname) [$_field get $cname.style]
[3330]1473        set _comp2flowhints($cname) \
[3571]1474            [Rappture::FlowHints ::\#auto $_field $cname $_units]
1475        set _values [$_field get $cname.values]
[3365]1476        set limits {}
[4030]1477        foreach axis { x y z } {
[3365]1478            lappend limits $axis [$_comp2unirect2d($cname) limits $axis]
1479        }
1480        set xv [blt::vector create \#auto]
1481        $xv set $_values
[4101]1482        set minmax [VectorLimits $xv $vectorsize]
1483        lappend limits $cname $minmax
1484        lappend limits v $minmax
[3365]1485        blt::vector destroy $xv
1486        set _comp2limits($cname) $limits
[3330]1487        incr _counter
[3571]1488        return 1
[3330]1489    }
1490    switch -- $element {
1491        "cloud" {
1492            set mesh [Rappture::Cloud::fetch $_xmlobj $path]
[3955]1493            set _type cloud
[3330]1494        }
1495        "mesh" {
1496            set mesh [Rappture::Mesh::fetch $_xmlobj $path]
[3955]1497            set _type mesh
[3330]1498        }           
1499        "unirect2d" {
[3557]1500            if { $_viewer == "" } {
1501                set _viewer "heightmap"
1502            }
[3330]1503            set mesh [Rappture::Unirect2d::fetch $_xmlobj $path]
[3955]1504            set _type unirect2d
[3330]1505        }
1506    }
[3571]1507    if { ![$mesh isvalid] } {
[3852]1508        puts stderr "Mesh is invalid"
[3571]1509        return 0
1510    }
[3330]1511    set _dim [$mesh dimensions]
[3850]1512    if { $_dim == 3 } {
1513        set dim 0
1514        foreach axis {x y z} {
1515            foreach {min max} [$mesh limits $axis] {
1516                if { $min < $max } {
1517                    incr dim
1518                }
1519            }
1520        }
[4030]1521        if { $dim != 3 } {
[3850]1522            set _dim $dim
1523        }
1524    }
[4030]1525
[3330]1526    if {$_dim == 1} {
[3585]1527        # 1D data: Create vectors for graph widget.
[3330]1528        # Is this used anywhere?
1529        #
1530        # OOPS!  This is 1D data
1531        # Forget the cloud/field -- store BLT vectors
1532        #
1533        # Is there a natural growth path in generating output from 1D to
1534        # higher dimensions?  If there isn't, let's kill this in favor
1535        # or explicitly using a <curve> instead.  Otherwise, the features
1536        # (methods such as xmarkers) or the <curve> need to be added
1537        # to the <field>.
1538        #
1539        set xv [blt::vector create x$_counter]
1540        set yv [blt::vector create y$_counter]
1541       
1542        $yv set [$mesh points]
1543        $xv seq 0 1 [$yv length]
1544        # sort x-coords in increasing order
1545        $xv sort $yv
1546        set _comp2dims($cname) "1D"
1547        set _comp2xy($cname) [list $xv $yv]
1548        incr _counter
[3571]1549        return 1
1550    }
1551    if {$_dim == 2} {
[3585]1552        # 2D data: By default surface or contour plot using heightmap widget.
[3571]1553        set v [blt::vector create \#auto]
1554        $v set [$_field get $cname.values]
1555        if { [$v length] == 0 } {
1556            return 0
1557        }
[3585]1558        if { $_viewer == "" } {
1559            set _viewer "contour"
1560        }
[4388]1561        set numFieldValues [$v length]
[4394]1562        set numComponentsPerTuple [numComponents $cname]
1563        if { [expr $numFieldValues % $numComponentsPerTuple] != 0 } {
1564            puts stderr "ERROR: Number of field values ($numFieldValues) not divisble by elemsize ($numComponentsPerTuple)"
[4388]1565            return 0
1566        }
[4394]1567        set numFieldTuples [expr $numFieldValues / $numComponentsPerTuple]
1568        if { $_comp2assoc($cname) == "pointdata" } {
1569            set numPoints [$mesh numpoints]
1570            if { $numPoints != $numFieldTuples } {
1571                puts stderr "ERROR: Number of points in mesh ($numPoints) and number of field tuples ($numFieldTuples) don't agree"
1572                return 0
1573            }
1574        } elseif { $_comp2assoc($cname) == "celldata" } {
1575            set numCells [$mesh numcells]
1576            if { $numCells != $numFieldTuples } {
1577                puts stderr "ERROR: Number of cells in mesh ($numCells) and number of field tuples ($numFieldTuples) don't agree"
1578                return 0
1579            }
1580        }
[3330]1581        set _comp2dims($cname) "[$mesh dimensions]D"
[3571]1582        set _comp2mesh($cname) [list $mesh $v]
1583        set _comp2style($cname) [$_field get $cname.style]
[3955]1584        if {[$_field element $cname.flow] != ""} {
1585            set _comp2flowhints($cname) \
1586                [Rappture::FlowHints ::\#auto $_field $cname $_units]
1587        }
[3330]1588        incr _counter
1589        array unset _comp2limits $cname
[4030]1590        foreach axis { x y z } {
[3996]1591            lappend _comp2limits($cname) $axis [$mesh limits $axis]
1592        }
[4101]1593        set minmax [VectorLimits $v $_comp2size($cname)]
1594        lappend _comp2limits($cname) $cname $minmax
1595        lappend _comp2limits($cname) v $minmax
[3571]1596        return 1
1597    }
1598    if {$_dim == 3} {
[3585]1599        # 3D data: By default isosurfaces plot using isosurface widget.
[3557]1600        if { $_viewer == "" } {
1601            set _viewer "isosurface"
1602        }
[3571]1603        set v [blt::vector create \#auto]
1604        $v set [$_field get $cname.values]
1605        if { [$v length] == 0 } {
1606            return 0
1607        }
[4388]1608        set numFieldValues [$v length]
[4394]1609        set numComponentsPerTuple [numComponents $cname]
1610        if { [expr $numFieldValues % $numComponentsPerTuple] != 0 } {
1611            puts stderr "ERROR: Number of field values ($numFieldValues) not divisble by elemsize ($numComponentsPerTuple)"
[4388]1612            return 0
1613        }
[4394]1614        set numFieldTuples [expr $numFieldValues / $numComponentsPerTuple]
1615        if { $_comp2assoc($cname) == "pointdata" } {
1616            set numPoints [$mesh numpoints]
1617            if { $numPoints != $numFieldTuples } {
1618                puts stderr "ERROR: Number of points in mesh ($numPoints) and number of field tuples ($numFieldTuples) don't agree"
1619                return 0
1620            }
1621        } elseif { $_comp2assoc($cname) == "celldata" } {
1622            set numCells [$mesh numcells]
1623            if { $numCells != $numFieldTuples } {
1624                puts stderr "ERROR: Number of cells in mesh ($numCells) and number of field tuples ($numFieldTuples) don't agree"
1625                return 0
1626            }
1627        }
[3571]1628        set _comp2dims($cname) "[$mesh dimensions]D"
1629        set _comp2mesh($cname) [list $mesh $v]
1630        set _comp2style($cname) [$_field get $cname.style]
[3955]1631        if {[$_field element $cname.flow] != ""} {
1632            set _comp2flowhints($cname) \
1633                [Rappture::FlowHints ::\#auto $_field $cname $_units]
1634        }
[3571]1635        incr _counter
[3996]1636        foreach axis { x y z } {
1637            lappend _comp2limits($cname) $axis [$mesh limits $axis]
1638        }
[4101]1639        set minmax [VectorLimits $v $_comp2size($cname)]
1640        lappend _comp2limits($cname) $cname $minmax
1641        lappend _comp2limits($cname) v $minmax
[3571]1642        return 1
[3330]1643    }
[3405]1644    error "unhandled case in field dim=$_dim element=$element"
[3330]1645}
1646
[3546]1647itcl::body Rappture::Field::AvsToVtk { cname contents } {
[3330]1648    package require vtk
1649
1650    set reader $this-datasetreader
1651    vtkAVSucdReader $reader
1652
1653    # Write the contents to a file just in case it's binary.
[3546]1654    set tmpfile $cname[pid].ucd
[3330]1655    set f [open "$tmpfile" "w"]
1656    fconfigure $f -translation binary -encoding binary
1657    puts $f $contents
1658    close $f
1659    $reader SetFileName $tmpfile
1660    $reader Update
1661    file delete $tmpfile
1662
[3546]1663    set tmpfile $cname[pid].vtk
[3330]1664    set writer $this-datasetwriter
1665    vtkDataSetWriter $writer
1666    $writer SetInputConnection [$reader GetOutputPort]
1667    $writer SetFileName $tmpfile
1668    $writer Write
1669    rename $reader ""
1670    rename $writer ""
[3546]1671
[3330]1672    set f [open "$tmpfile" "r"]
1673    fconfigure $f -translation binary -encoding binary
1674    set vtkdata [read $f]
1675    close $f
[3548]1676    file delete $tmpfile
[3330]1677    return $vtkdata
1678}
[3684]1679
[3965]1680itcl::body Rappture::Field::DicomToVtk { cname path } {
[3960]1681    package require vtk
1682
[3965]1683    if { ![file exists $path] } {
1684        puts stderr "path \"$path\" doesn't exist."
1685        return 0
1686    }
[4120]1687
1688    if { [file isdir $path] } {
1689        set files [glob -nocomplain $path/*.dcm]
1690        if { [llength $files] == 0 } {
1691            set files [glob -nocomplain $path/*]
1692            if { [llength $files] == 0 } {
1693                puts stderr "no dicom files found in \"$path\""
1694                return 0
1695            }
1696        }
[4126]1697
1698        #array set data [Rappture::DicomToVtk files $files]
1699        array set data [Rappture::DicomToVtk dir $path]
[4120]1700    } else {
1701        array set data [Rappture::DicomToVtk files [list $path]]
1702    }
1703
1704    foreach key [array names data] {
1705        if {$key == "vtkdata"} {
1706            if {1} {
1707                set f [open /tmp/$cname.vtk "w"]
1708                fconfigure $f -translation binary -encoding binary
1709                puts -nonewline $f $data(vtkdata)
1710                close $f
1711            }
1712        } else {
1713            puts stderr "$key = \"$data($key)\""
1714        }
1715    }
[4126]1716
1717    # Save viewer choice
1718    set viewer $_viewer
[4120]1719    ReadVtkDataSet $cname $data(vtkdata)
[4126]1720    # Restore viewer choice (ReadVtkDataSet wants to set it to contour/isosurface)
1721    set _viewer $viewer
[4120]1722    return $data(vtkdata)
1723}
1724
[3684]1725itcl::body Rappture::Field::GetTypeAndSize { cname } {
1726    array set type2components {
[3697]1727        "colorscalars"         4
1728        "normals"              3
1729        "scalars"              1
1730        "tcoords"              2
1731        "tensors"              9
1732        "vectors"              3
[3684]1733    }
1734    set type [$_field get $cname.elemtype]
1735    if { $type == "" } {
[3699]1736        set type "scalars"
[3684]1737    }
1738    if { ![info exists type2components($type)] } {
1739        error "unknown <elemtype> \"$type\" in field"
1740    }
1741    set size [$_field get $cname.elemsize]
1742    if { $size == "" } {
1743        set size $type2components($type)
1744    }
1745    set _comp2type($cname) $type
1746    set _comp2size($cname) $size
1747}
1748
1749itcl::body Rappture::Field::GetAssociation { cname } {
1750    set assoc [$_field get $cname.association]
1751    if { $assoc == "" } {
1752        set _comp2assoc($cname) "pointdata"
1753        return
1754    }
1755    switch -- $assoc {
1756        "pointdata" - "celldata" - "fielddata" {
1757            set _comp2assoc($cname) $assoc
1758            return
1759        }
1760        default {
1761            error "unknown field association \"$assoc\""
1762        }
1763    }
1764}
[4101]1765
1766#
1767# Compute the per-component limits or limits of vector magnitudes
1768#
1769itcl::body Rappture::Field::VectorLimits {vector vectorsize {comp -1}} {
1770    if {$vectorsize == 1} {
1771        set minmax [$vector limits]
1772    } else {
1773        set len [$vector length]
1774        if {[expr $len % $vectorsize] != 0} {
1775            error "Invalid vectorsize: $vectorsize"
1776        }
1777        if {$comp > $vectorsize-1} {
1778            error "Invalid vector component: $comp"
1779        }
1780        set numTuples [expr ($len/$vectorsize)]
1781        for {set i 0} {$i < $numTuples} {incr i} {
1782            if {$comp >= 0} {
1783                set idx [expr ($i * $vectorsize + $comp)]
1784                set val [$vector index $idx]
1785            } else {
1786                set idx [expr ($i * $vectorsize)]
1787                set mag 0
1788                for {set j 0} {$j < $vectorsize} {incr j} {
1789                    set val [$vector index $idx]
1790                    set mag [expr ($mag + $val * $val)]
1791                    incr idx
1792                }
1793                set val [expr (sqrt($mag))]
1794            }
1795            if (![info exists minmax]) {
1796                set minmax [list $val $val]
1797            } else {
1798                if {$val < [lindex $minmax 0]} {
1799                    lset minmax 0 $val
1800                }
1801                if {$val > [lindex $minmax 1]} {
1802                    lset minmax 1 $val
1803                }
1804            }
1805        }
1806    }
1807    return $minmax
1808}
Note: See TracBrowser for help on using the repository browser.