Ignore:
Timestamp:
Jul 9, 2014 8:33:55 PM (10 years ago)
Author:
ldelgass
Message:

Merge mesh/field fixes from trunk, add mesh viewer

Location:
branches/1.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.3

  • branches/1.3/gui/scripts/mesh.tcl

    r4472 r4474  
    2727    private variable _dim       0;      # Dimension of mesh (1, 2, or 3)
    2828    private variable _type "";          # Indicates the type of mesh.
    29     private variable _units "m m m" ;   # System of units for x, y, z
     29    private variable _axis2units;       # System of units for x, y, z
     30    private variable _axis2labels;      #
     31    private variable _hints
    3032    private variable _limits        ;   # Array of mesh limits. Keys are
    3133                                        # xmin, xmax, ymin, ymax, ...
     
    4648    public method dimensions {}
    4749    public method limits {which}
     50    public method units { axis }
     51    public method label { axis }
    4852    public method hints {{key ""}}
    4953    public method isvalid {} {
     
    5256    public proc fetch {xmlobj path}
    5357    public proc release {obj}
    54     public method vtkdata {}
     58    public method vtkdata {{what -partial}}
    5559    public method type {} {
    5660        return $_type
     
    5963        return $_numPoints
    6064    }
    61 
     65    public method numcells {} {
     66        return $_numCells
     67    }
    6268
    6369    private common _xp2obj       ;      # used for fetch/release ref counting
     
    7379    private method GetDouble { path }
    7480    private method GetInt { path }
     81    private method InitHints {}
    7582    private method ReadGrid { path }
    7683    private method ReadUnstructuredGrid { path }
     
    149156        set _limits($axis) ""
    150157    }
    151     set u [$_mesh get units]
    152     if {"" != $u} {
    153         while {[llength $u] < 3} {
    154             lappend u [lindex $u end]
    155         }
    156         set _units $u
     158    set units [$_mesh get units]
     159    set first [lindex $units 0]
     160    foreach u $units axis { x y z } {
     161        if { $u != "" } {
     162            set _axis2units($axis) $u
     163        } else {
     164            set _axis2units($axis) $first
     165        }
     166    }
     167    foreach label [$_mesh get labels] axis { x y z } {
     168        if { $label != "" } {
     169            set _axis2labels($axis) $label
     170        } else {
     171            set _axis2labels($axis) [string toupper $axis]
     172        }
    157173    }
    158174
     
    200216    }
    201217    set _isValid $result
     218    InitHints
    202219}
    203220
     
    226243#       arrays before generating output to send to the remote render server.
    227244#
    228 itcl::body Rappture::Mesh::vtkdata {} {
    229     return $_vtkdata
     245itcl::body Rappture::Mesh::vtkdata {{what -partial}} {
     246    if {$what == "-full"} {
     247        append out "# vtk DataFile Version 3.0\n"
     248        append out "[hints label]\n"
     249        append out "ASCII\n"
     250        append out $_vtkdata
     251        return $out
     252    } else {
     253        return $_vtkdata
     254    }
    230255}
    231256
     
    237262itcl::body Rappture::Mesh::points {} {
    238263    return ""
     264}
     265
     266#
     267# units --
     268#
     269#       Returns the units of the given axis.
     270#
     271itcl::body Rappture::Mesh::units { axis } {
     272    if { ![info exists _axis2units($axis)] } {
     273        return ""
     274    }
     275    return $_axis2units($axis)
     276}
     277
     278#
     279# label --
     280#
     281#       Returns the label of the given axis.
     282#
     283itcl::body Rappture::Mesh::label { axis } {
     284    if { ![info exists _axis2labels($axis)] } {
     285        return ""
     286    }
     287    return $_axis2labels($axis)
    239288}
    240289
     
    344393# ----------------------------------------------------------------------
    345394itcl::body Rappture::Mesh::hints {{keyword ""}} {
    346     foreach key {label color units} {
    347         set str [$_mesh get $key]
     395    if {$keyword != ""} {
     396        if {[info exists _hints($keyword)]} {
     397            return $_hints($keyword)
     398        }
     399        return ""
     400    }
     401    return [array get _hints]
     402}
     403
     404# ----------------------------------------------------------------------
     405# USAGE: InitHints
     406#
     407# Returns a list of key/value pairs for various hints about plotting
     408# this mesh.  If a particular <keyword> is specified, then it returns
     409# the hint for that <keyword>, if it exists.
     410# ----------------------------------------------------------------------
     411itcl::body Rappture::Mesh::InitHints {} {
     412    foreach {key path} {
     413        camera       camera.position
     414        color        about.color
     415        label        about.label
     416        style        about.style
     417        units        units
     418    } {
     419        set str [$_mesh get $path]
    348420        if {"" != $str} {
    349             set hints($key) $str
    350         }
    351     }
    352 
    353     if {$keyword != ""} {
    354         if {[info exists hints($keyword)]} {
    355             return $hints($keyword)
    356         }
    357         return ""
    358     }
    359     return [array get hints]
     421            set _hints($key) $str
     422        }
     423    }
    360424}
    361425
     
    367431    }
    368432    if { [scan $string "%d" _dim] == 1 } {
    369         if { $_dim == 2 || $_dim == 3 } {
     433        if { $_dim == 1 || $_dim == 2 || $_dim == 3 } {
    370434            return 1
    371435        }
    372436    }
    373     puts stderr "WARNING: bad <dim> tag value \"$string\": should be 2 or 3."
     437    puts stderr "WARNING: bad <dim> tag value \"$string\": should be 1, 2 or 3."
    374438    return 0
    375439}
     
    471535            return 0
    472536        }
    473         if { $numCurvilinear < 2 } {
    474             puts stderr "WARNING: bad grid \"$path\": curvilinear grid must be 2D or 3D."
    475             return 0
    476         }
    477537        set points [$_xmlobj get $path.grid.points]
    478538        if { $points == "" } {
     
    480540            return 0
    481541        }
    482         if { ![info exists xNum] || ![info exists yNum] } {
    483             puts stderr "WARNING: bad grid \"$path\": invalid dimensions for curvilinear grid: missing <xdim> or <ydim> from grid description."
     542        if { ![info exists xNum] } {
     543            puts stderr "WARNING: bad grid \"$path\": invalid dimensions for curvilinear grid: missing <xdim> from grid description."
    484544            return 0
    485545        }
     
    493553            set _dim 3
    494554            set _numPoints [expr $xNum * $yNum * $zNum]
     555            set _numCells [expr ($xNum - 1) * ($yNum - 1) * ($zNum - 1)]
    495556            if { ($_numPoints*3) != $numCoords } {
    496                 puts stderr "WARNING: bad grid \"$path\": invalid grid: \# of points does not match dimensions <xdim> * <ydim>"
     557                puts stderr "WARNING: bad grid \"$path\": invalid grid: \# of points does not match dimensions <xdim> * <ydim> * <zdim>"
    497558                return 0
    498559            }
     
    512573            append out "\n"
    513574            set _vtkdata $out
    514         } else {
     575        } elseif { [info exists yNum] } {
    515576            set _dim 2
    516577            set _numPoints [expr $xNum * $yNum]
     578            set _numCells [expr ($xNum - 1) * ($yNum - 1)]
    517579            if { ($_numPoints*2) != $numCoords } {
    518                 puts stderr "WARNING: bad grid \"$path\": \# of points does not match dimensions <xdim> * <ydim> * <zdim>"
     580                puts stderr "WARNING: bad grid \"$path\": \# of points does not match dimensions <xdim> * <ydim>"
    519581                return 0
    520582            }
     
    527589                set _limits($axis) [$vector limits]
    528590            }
     591            set _limits(z) [list 0 0]
    529592            $zv seq 0 0 [$xv length]
    530593            $all merge $xv $yv $zv
     
    535598            append out "\n"
    536599            set _vtkdata $out
     600        } else {
     601            set _dim 1
     602            set _numPoints $xNum
     603            set _numCells [expr $xNum - 1]
     604            if { $_numPoints != $numCoords } {
     605                puts stderr "WARNING: bad grid \"$path\": \# of points does not match <xdim>"
     606                return 0
     607            }
     608            set _limits(x) [$xv limits]
     609            set _limits(y) [list 0 0]
     610            set _limits(z) [list 0 0]
     611            $yv seq 0 0 [$xv length]
     612            $zv seq 0 0 [$xv length]
     613            $all merge $xv $yv $zv
     614            append out "DATASET STRUCTURED_GRID\n"
     615            append out "DIMENSIONS $xNum 1 1\n"
     616            append out "POINTS $_numPoints double\n"
     617            append out [$all range 0 end]
     618            append out "\n"
     619            set _vtkdata $out
    537620        }
    538621        blt::vector destroy $all $xv $yv $zv
     
    541624    if { $numRectilinear == 0 && $numUniform > 0} {
    542625        # This is the special case where all axes 2D/3D are uniform. 
    543         # This results in a STRUCTURE_POINTS
    544         if { $_dim == 2 } {
     626        # This results in a STRUCTURED_POINTS
     627        if { $_dim == 1 } {
     628            set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
     629            set _numPoints $xNum
     630            set _numCells [expr $xNum - 1]
     631            append out "DATASET STRUCTURED_POINTS\n"
     632            append out "DIMENSIONS $xNum 1 1\n"
     633            append out "ORIGIN $xMin 0 0\n"
     634            append out "SPACING $xSpace 0 0\n"
     635            set _vtkdata $out
     636            set _limits(x) [list $xMin $xMax]
     637            set _limits(y) [list 0 0]
     638            set _limits(z) [list 0 0]
     639        } elseif { $_dim == 2 } {
    545640            set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
    546641            set ySpace [expr ($yMax - $yMin) / double($yNum - 1)]
    547642            set _numPoints [expr $xNum * $yNum]
     643            set _numCells [expr ($xNum - 1) * ($yNum - 1)]
    548644            append out "DATASET STRUCTURED_POINTS\n"
    549645            append out "DIMENSIONS $xNum $yNum 1\n"
     
    554650                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
    555651            }
     652            set _limits(z) [list 0 0]
    556653        } elseif { $_dim == 3 } {
    557654            set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
     
    559656            set zSpace [expr ($zMax - $zMin) / double($zNum - 1)]
    560657            set _numPoints [expr $xNum * $yNum * $zNum]
     658            set _numCells [expr ($xNum - 1) * ($yNum - 1) * ($zNum - 1)]
    561659            append out "DATASET STRUCTURED_POINTS\n"
    562660            append out "DIMENSIONS $xNum $yNum $zNum\n"
     
    584682    }
    585683    set yv [blt::vector create \#auto]
    586     if { [info exists yMin] } {
    587         $yv seq $yMin $yMax $yNum
     684    if { $_dim > 1 } {
     685        if { [info exists yMin] } {
     686            $yv seq $yMin $yMax $yNum
     687        } else {
     688            $yv set [$_xmlobj get $path.grid.ycoords]
     689            set yMin [$yv min]
     690            set yMax [$yv max]
     691            set yNum [$yv length]
     692        }
    588693    } else {
    589         $yv set [$_xmlobj get $path.grid.ycoords]
    590         set yMin [$yv min]
    591         set yMax [$yv max]
    592         set yNum [$yv length]
     694        set yNum 1
    593695    }
    594696    set zv [blt::vector create \#auto]
     
    607709    if { $_dim == 3 } {
    608710        set _numPoints [expr $xNum * $yNum * $zNum]
     711        set _numCells [expr ($xNum - 1) * ($yNum - 1) * ($zNum - 1)]
    609712        append out "DATASET RECTILINEAR_GRID\n"
    610713        append out "DIMENSIONS $xNum $yNum $zNum\n"
     
    626729    } elseif { $_dim == 2 } {
    627730        set _numPoints [expr $xNum * $yNum]
     731        set _numCells [expr ($xNum - 1) * ($yNum - 1)]
    628732        append out "DATASET RECTILINEAR_GRID\n"
    629733        append out "DIMENSIONS $xNum $yNum 1\n"
     
    641745            }
    642746        }
     747        set _limits(z) [list 0 0]
     748        set _vtkdata $out
     749    } elseif { $_dim == 1 } {
     750        set _numPoints $xNum
     751        set _numCells [expr $xNum - 1]
     752        append out "DATASET RECTILINEAR_GRID\n"
     753        append out "DIMENSIONS $xNum 1 1\n"
     754        append out "X_COORDINATES $xNum double\n"
     755        append out [$xv range 0 end]
     756        append out "\n"
     757        append out "Y_COORDINATES 1 double\n"
     758        append out "0\n"
     759        append out "Z_COORDINATES 1 double\n"
     760        append out "0\n"
     761        if { [info exists xMin] } {
     762            set _limits(x) [list $xMin $xMax]
     763        }
     764        set _limits(y) [list 0 0]
     765        set _limits(z) [list 0 0]
    643766        set _vtkdata $out
    644767    } else {
     
    660783    set _vtkdata $out
    661784    set _limits(x) [$xv limits]
    662     set _limits(y) [$yv limits]
     785    if { $_dim > 1 } {
     786        set _limits(y) [$yv limits]
     787    } else {
     788        set _limits(y) [list 0 0]
     789    }
    663790    if { $_dim == 3 } {
    664791        set _limits(z) [$zv limits]
     792    } else {
     793        set _limits(z) [list 0 0]
    665794    }
    666795    return 1
     
    692821    if { $_dim == 3 } {
    693822        set _limits(z) [$zv limits]
     823    } else {
     824        set _limits(z) [list 0 0]
    694825    }
    695826    set _vtkdata $out
     
    722853    if { $_dim == 3 } {
    723854        set _limits(z) [$zv limits]
     855    } else {
     856        set _limits(z) [list 0 0]
    724857    }
    725858    set _vtkdata $out
     
    750883    set _limits(x) [$xv limits]
    751884    set _limits(y) [$yv limits]
    752     if { $_dim == 3 } {
    753         set _limits(z) [$zv limits]
    754     }
     885    set _limits(z) [$zv limits]
     886
    755887    set _vtkdata $out
    756888    return 1
     
    780912    set _limits(x) [$xv limits]
    781913    set _limits(y) [$yv limits]
    782     if { $_dim == 3 } {
    783         set _limits(z) [$zv limits]
    784     }
     914    set _limits(z) [$zv limits]
     915
    785916    set _vtkdata $out
    786917    return 1
     
    810941    set _limits(x) [$xv limits]
    811942    set _limits(y) [$yv limits]
    812     if { $_dim == 3 } {
    813         set _limits(z) [$zv limits]
    814     }
     943    set _limits(z) [$zv limits]
     944
    815945    set _vtkdata $out
    816946    return 1
     
    840970    set _limits(x) [$xv limits]
    841971    set _limits(y) [$yv limits]
    842     if { $_dim == 3 } {
    843         set _limits(z) [$zv limits]
    844     }
     972    set _limits(z) [$zv limits]
     973
    845974    set _vtkdata $out
    846975    return 1
     
    854983        set celltype [GetCellType $celltypes]
    855984    }
    856     if { $_dim == 2 } {
    857         set _numPoints [$xv length]
    858         set data {}
    859         set count 0
    860         set _numCells 0
    861         set celltypes {}
    862         foreach line $lines {
    863             set length [llength $line]
    864             if { $length == 0 } {
    865                 continue
    866             }
    867             if { $numCellTypes > 1 } {
    868                 set cellType [GetCellType [lindex $cellTypes $_numCells]]
    869             }
    870             set numIndices [GetNumIndices $celltype]
    871             if { $numIndices > 0 && $numIndices != $length } {
    872                 puts stderr "WARNING: bad unstructured grid \"$path\": wrong \# of indices specified for celltype $celltype on line \"$line\""
    873                 return 0
    874             }
    875             append data " $numIndices $line\n"
    876             lappend celltypes $celltype
    877             incr count $length;         # Include the indices
    878             incr count;                 # and the number of indices
    879             incr _numCells
    880         }
    881         append out "DATASET UNSTRUCTURED_GRID\n"
    882         append out "POINTS $_numPoints double\n"
    883         set all [blt::vector create \#auto]
    884         $all merge $xv $yv $zv
    885         append out [$all range 0 end]
    886         blt::vector destroy $all
    887         append out "CELLS $_numCells $count\n"
    888         append out $data
    889         append out "CELL_TYPES $_numCells\n"
    890         append out $celltypes
    891         set _limits(x) [$xv limits]
    892         set _limits(y) [$yv limits]
     985
     986    set _numPoints [$xv length]
     987    set data {}
     988    set count 0
     989    set _numCells 0
     990    set celltypes {}
     991    foreach line $lines {
     992        set length [llength $line]
     993        if { $length == 0 } {
     994            continue
     995        }
     996        if { $numCellTypes > 1 } {
     997            set cellType [GetCellType [lindex $cellTypes $_numCells]]
     998        }
     999        set numIndices [GetNumIndices $celltype]
     1000        if { $numIndices > 0 && $numIndices != $length } {
     1001            puts stderr "WARNING: bad unstructured grid \"$path\": wrong \# of indices specified for celltype $celltype on line \"$line\""
     1002            return 0
     1003        } else {
     1004            set numIndices $length
     1005        }
     1006        append data " $numIndices $line\n"
     1007        lappend celltypes $celltype
     1008        incr count $length;         # Include the indices
     1009        incr count;                 # and the number of indices
     1010        incr _numCells
     1011    }
     1012    append out "DATASET UNSTRUCTURED_GRID\n"
     1013    append out "POINTS $_numPoints double\n"
     1014    set all [blt::vector create \#auto]
     1015    $all merge $xv $yv $zv
     1016    append out [$all range 0 end]
     1017    blt::vector destroy $all
     1018    append out "CELLS $_numCells $count\n"
     1019    append out $data
     1020    append out "CELL_TYPES $_numCells\n"
     1021    append out $celltypes
     1022    set _limits(x) [$xv limits]
     1023    set _limits(y) [$yv limits]
     1024    if { $_dim < 3 } {
     1025        set _limits(z) [list 0 0]
    8931026    } else {
    894         set _numPoints [$xv length]
    895 
    896         set data {}
    897         set count 0
    898         set _numCells 0
    899         foreach line $lines {
    900             set length [llength $line]
    901             if { $length == 0 } {
    902                 continue
    903             }
    904             if { $numCellTypes > 1 } {
    905                 set cellType [GetCellType [lindex $cellTypes $_numCells]]
    906             }
    907             set numIndices [GetNumIndices $celltype]
    908             if { $numIndices > 0 && $numIndices != $length } {
    909                 puts stderr "WARNING: bad unstructured grid \"$path\": wrong \# of indices specified for celltype $celltype on line \"$line\""
    910                 return 0
    911             }
    912             append data " $length $line\n"
    913             incr count $length
    914             incr count
    915             incr _numCells
    916         }
    917         append out "DATASET UNSTRUCTURED_GRID\n"
    918         append out "POINTS $_numPoints double\n"
    919         set all [blt::vector create \#auto]
    920         $all merge $xv $yv $zv
    921         append out [$all range 0 end]
    922         blt::vector destroy $all
    923         append out "\n"
    924         append out "CELLS $_numCells $count\n"
    925         append out $data
    926         append out "CELL_TYPES $_numCells\n"
    927         append out $celltypes
    928         set _limits(x) [$xv limits]
    929         set _limits(y) [$yv limits]
    930         set _limits(z) [$zv limits]
    931     }
     1027        set _limits(z) [$zv limits]
     1028    }
     1029
    9321030    set _vtkdata $out
    9331031    return 1
     
    9691067    #         <xcoords>, <ycoords>, <zcoords>.  Split and convert into
    9701068    #         3 vectors, one for each coordinate.
    971     if { $_dim == 2 } {
     1069    if { $_dim == 1 } {
     1070        set xcoords [$_xmlobj get $path.unstructured.xcoords]
     1071        set ycoords [$_xmlobj get $path.unstructured.ycoords]
     1072        set zcoords [$_xmlobj get $path.unstructured.zcoords]
     1073        set data    [$_xmlobj get $path.unstructured.points]
     1074        if { $ycoords != "" } {
     1075            put stderr "can't specify <ycoords> with a 1D mesh"
     1076            return 0
     1077        }
     1078        if { $zcoords != "" } {
     1079            put stderr "can't specify <zcoords> with a 1D mesh"
     1080            return 0
     1081        }
     1082        if { $xcoords != "" } {
     1083            set xv [blt::vector create \#auto]
     1084            $xv set $xcoords
     1085        } elseif { $data != "" } {
     1086            Rappture::ReadPoints $data dim points
     1087            if { $points == "" } {
     1088                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
     1089                return 0
     1090            }
     1091            if { $dim != 1 } {
     1092                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
     1093                return 0
     1094            }
     1095            set xv [blt::vector create \#auto]
     1096            $xv set $points
     1097        } else {
     1098            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
     1099            return 0
     1100        }
     1101        set yv [blt::vector create \#auto]
     1102        set zv [blt::vector create \#auto]
     1103        $yv seq 0 0 [$xv length];       # Make an all zeroes vector.
     1104        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
     1105    } elseif { $_dim == 2 } {
    9721106        set xcoords [$_xmlobj get $path.unstructured.xcoords]
    9731107        set ycoords [$_xmlobj get $path.unstructured.ycoords]
     
    10721206# ----------------------------------------------------------------------
    10731207itcl::body Rappture::Mesh::ReadNodesElements {path} {
    1074     set type "nodeselements"
     1208    set _type "nodeselements"
    10751209    set count 0
    10761210
     
    10911225        set _limits(x) [$xv limits]
    10921226        set _limits(y) [$yv limits]
     1227        set _limits(z) [list 0 0]
    10931228        # 2D Dataset. All Z coordinates are 0
    10941229        $zv seq 0.0 0.0 $_numPoints
Note: See TracChangeset for help on using the changeset viewer.