source: branches/1.3/gui/scripts/mesh.tcl @ 4512

Last change on this file since 4512 was 4512, checked in by gah, 10 years ago

test and fixes for meshviewer, add rappture (non-viewer) bug fixes and features

File size: 46.9 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2
3# ----------------------------------------------------------------------
4#  COMPONENT: mesh - represents a structured mesh for a device
5#
6#  This object represents a mesh in an XML description of simulator
7#  output.  A mesh is a structured arrangement of points, as elements
8#  composed of nodes representing coordinates.  This is a little
9#  different from a cloud, which is an unstructured arrangement
10#  (shotgun blast) of points.
11# ======================================================================
12#  AUTHOR:  Michael McLennan, Purdue University
13#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
14#
15#  See the file "license.terms" for information on usage and
16#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17# ======================================================================
18package require Itcl
19
20namespace eval Rappture {
21    # forward declaration
22}
23
24itcl::class Rappture::Mesh {
25    private variable _xmlobj ""  ;      # Ref to XML obj with device data
26    private variable _mesh ""    ;      # Lib obj representing this mesh
27    private variable _dim       0;      # Dimension of mesh (1, 2, or 3)
28    private variable _type "";          # Indicates the type of mesh.
29    private variable _axis2units;       # System of units for x, y, z
30    private variable _axis2labels;      #
31    private variable _hints
32    private variable _limits        ;   # Array of mesh limits. Keys are
33                                        # xmin, xmax, ymin, ymax, ...
34    private variable _numPoints 0   ;   # # of points in mesh
35    private variable _numCells 0   ;    # # of cells in mesh
36    private variable _vtkdata "";       # Mesh in vtk file format.
37    private variable _isValid 0;        # Indicates if the mesh is valid.
38    constructor {xmlobj path} {
39        # defined below
40    }
41    destructor {
42        # defined below
43    }
44    public method points {}
45    public method elements {}
46    public method mesh {{-type "vtk"}}
47    public method size {{what -points}}
48    public method dimensions {}
49    public method limits {which}
50    public method units { axis }
51    public method label { axis }
52    public method hints {{key ""}}
53    public method isvalid {} {
54        return $_isValid
55    }
56    public proc fetch {xmlobj path}
57    public proc release {obj}
58    public method vtkdata {{what -partial}}
59    public method type {} {
60        return $_type
61    }
62    public method numpoints {} {
63        return $_numPoints
64    }
65    public method numcells {} {
66        return $_numCells
67    }
68
69    private common _xp2obj       ;      # used for fetch/release ref counting
70    private common _obj2ref      ;      # used for fetch/release ref counting
71    private variable _xv        ""
72    private variable _yv        ""
73    private variable _zv        ""
74    private variable _xCoords   "";     # For the blt contour only
75    private variable _yCoords   "";     # For the blt contour only
76   
77    private method ReadNodesElements {path}
78    private method GetDimension { path }
79    private method GetDouble { path }
80    private method GetInt { path }
81    private method InitHints {}
82    private method ReadGrid { path }
83    private method ReadUnstructuredGrid { path }
84    private method ReadVtk { path }
85    private method WriteTriangles { path xv yv zv triangles }
86    private method WriteQuads { path xv yv zv quads }
87    private method WriteVertices { path xv yv zv vertices }
88    private method WriteLines { path xv yv zv lines }
89    private method WritePolygons { path xv yv zv polygons }
90    private method WriteTriangleStrips { path xv yv zv trianglestrips }
91    private method WriteTetrahedrons { path xv yv zv tetrahedrons }
92    private method WriteHexahedrons { path xv yv zv hexhedrons }
93    private method WriteWedges { path xv yv zv wedges }
94    private method WritePyramids { path xv yv zv pyramids }
95    private method WriteHybridCells { path xv yv zv cells celltypes }
96    private method WritePointCloud { path xv yv zv }
97    private method GetCellType { name }
98    private method GetNumIndices { type }
99}
100
101# ----------------------------------------------------------------------
102# USAGE: Rappture::Mesh::fetch <xmlobj> <path>
103#
104# Clients use this instead of a constructor to fetch the Mesh for
105# a particular <path> in the <xmlobj>.  When the client is done with
106# the mesh, he calls "release" to decrement the reference count.
107# When the mesh is no longer needed, it is cleaned up automatically.
108# ----------------------------------------------------------------------
109itcl::body Rappture::Mesh::fetch {xmlobj path} {
110    set handle "$xmlobj|$path"
111    if {[info exists _xp2obj($handle)]} {
112        set obj $_xp2obj($handle)
113        incr _obj2ref($obj)
114        return $obj
115    }
116    set obj [Rappture::Mesh ::#auto $xmlobj $path]
117    set _xp2obj($handle) $obj
118    set _obj2ref($obj) 1
119    return $obj
120}
121
122# ----------------------------------------------------------------------
123# USAGE: Rappture::Mesh::release <obj>
124#
125# Clients call this when they're no longer using a Mesh fetched
126# previously by the "fetch" proc.  This decrements the reference
127# count for the mesh and destroys the object when it is no longer
128# in use.
129# ----------------------------------------------------------------------
130itcl::body Rappture::Mesh::release {obj} {
131    if {[info exists _obj2ref($obj)]} {
132        incr _obj2ref($obj) -1
133        if {$_obj2ref($obj) <= 0} {
134            unset _obj2ref($obj)
135            foreach handle [array names _xp2obj] {
136                if {$_xp2obj($handle) == $obj} {
137                    unset _xp2obj($handle)
138                }
139            }
140            itcl::delete object $obj
141        }
142    } else {
143        error "can't find reference count for $obj"
144    }
145}
146
147# ----------------------------------------------------------------------
148# CONSTRUCTOR
149# ----------------------------------------------------------------------
150itcl::body Rappture::Mesh::constructor {xmlobj path} {
151    package require vtk
152    if {![Rappture::library isvalid $xmlobj]} {
153        error "bad value \"$xmlobj\": should be Rappture::library"
154    }
155    set _xmlobj $xmlobj
156    set _mesh [$xmlobj element -as object $path]
157
158    # Initialize mesh bounds to empty
159    foreach axis {x y z} {
160        set _limits($axis) ""
161    }
162    set units [$_mesh get units]
163    set first [lindex $units 0]
164    foreach u $units axis { x y z } {
165        if { $u != "" } {
166            set _axis2units($axis) $u
167        } else {
168            set _axis2units($axis) $first
169        }
170    }
171    foreach label [$_mesh get labels] axis { x y z } {
172        if { $label != "" } {
173            set _axis2labels($axis) $label
174        } else {
175            set _axis2labels($axis) [string toupper $axis]
176        }
177    }
178
179    # Meshes comes in a variety of flavors
180    #
181    # Dimensionality is determined from the <dimension> tag. 
182    #
183    # <vtk> described mesh
184    # <element> +  <node> definitions
185    # <grid>            rectangular mesh
186    # <unstructured>    homogeneous cell type mesh.
187
188    # Check that only one mesh type was defined.
189    set subcount 0
190    foreach cname [$_mesh children] {
191        foreach type { vtk grid unstructured } {
192            if { $cname == $type } {
193                incr subcount
194                break
195            }
196        }
197    }
198    if {[$_mesh element "node"] != "" ||
199        [$_mesh element "element"] != ""} {
200        incr subcount
201    }
202
203    if { $subcount == 0 } {
204        puts stderr "WARNING: no mesh specified for \"$path\"."
205        return
206    }
207    if { $subcount > 1 } {
208        puts stderr "WARNING: too many mesh types specified for \"$path\"."
209        return
210    }
211    set result 0
212    if { [$_mesh element "vtk"] != ""} {
213        set result [ReadVtk $path]
214    } elseif {[$_mesh element "grid"] != "" } {
215        set result [ReadGrid $path]
216    } elseif {[$_mesh element "unstructured"] != "" } {
217        set result [ReadUnstructuredGrid $path]
218    } elseif {[$_mesh element "node"] != "" && [$_mesh element "element"] != ""} {
219        set result [ReadNodesElements $path]
220    }
221    set _isValid $result
222    InitHints
223}
224
225# ----------------------------------------------------------------------
226# DESTRUCTOR
227# ----------------------------------------------------------------------
228itcl::body Rappture::Mesh::destructor {} {
229    # don't destroy the _xmlobj! we don't own it!
230    itcl::delete object $_mesh
231
232    if { $_xCoords != "" } {
233        blt::vector destroy $_xCoords
234    }
235    if { $_yCoords != "" } {
236        blt::vector destroy $_yCoords
237    }
238}
239
240#
241# vtkdata --
242#
243#       This is called by the field object to generate a VTK file to send to
244#       the remote render server.  Returns the vtkDataSet object containing
245#       (at this point) just the mesh.  The field object doesn't know (or
246#       care) what type of mesh is used.  The field object will add field
247#       arrays before generating output to send to the remote render server.
248#
249itcl::body Rappture::Mesh::vtkdata {{what -partial}} {
250    if {$what == "-full"} {
251        append out "# vtk DataFile Version 3.0\n"
252        append out "[hints label]\n"
253        append out "ASCII\n"
254        append out $_vtkdata
255        return $out
256    } else {
257        return $_vtkdata
258    }
259}
260
261# ----------------------------------------------------------------------
262# USAGE: points
263#
264# Returns the vtk object containing the points for this mesh.
265# ----------------------------------------------------------------------
266itcl::body Rappture::Mesh::points {} {
267    return ""
268}
269
270#
271# units --
272#
273#       Returns the units of the given axis.
274#
275itcl::body Rappture::Mesh::units { axis } {
276    if { ![info exists _axis2units($axis)] } {
277        return ""
278    }
279    return $_axis2units($axis)
280}
281
282#
283# label --
284#
285#       Returns the label of the given axis.
286#
287itcl::body Rappture::Mesh::label { axis } {
288    if { ![info exists _axis2labels($axis)] } {
289        return ""
290    }
291    return $_axis2labels($axis)
292}
293
294# ----------------------------------------------------------------------
295# USAGE: elements
296#
297# Returns a list of the form {plist r plist r ...} for each element
298# in this mesh.  Each plist is a list of {x y x y ...} coordinates
299# for the mesh.
300# ----------------------------------------------------------------------
301itcl::body Rappture::Mesh::elements {} {
302    # build a map for region number => region type
303    foreach comp [$_mesh children -type region] {
304        set id [$_mesh element -as id $comp]
305        set regions($id) [$_mesh get $comp]
306    }
307    set regions() "unknown"
308
309    set rlist ""
310    foreach comp [$_mesh children -type element] {
311        set rid [$_mesh get $comp.region]
312
313        #
314        # HACK ALERT!
315        #
316        # Prophet puts out nodes in a funny "Z" shaped order,
317        # not in proper clockwise fashion.  Switch the last
318        # two nodes for now to make them correct.
319        #
320        set nlist [$_mesh get $comp.nodes]
321        set n2 [lindex $nlist 2]
322        set n3 [lindex $nlist 3]
323        set nlist [lreplace $nlist 2 3 $n3 $n2]
324        lappend nlist [lindex $nlist 0]
325
326        set plist ""
327        foreach nid $nlist {
328            eval lappend plist [$_mesh get node($nid)]
329        }
330        lappend rlist $plist $regions($rid)
331    }
332    return $rlist
333}
334
335# ----------------------------------------------------------------------
336# USAGE: mesh
337#
338# Returns the vtk object representing the mesh.
339# ----------------------------------------------------------------------
340itcl::body Rappture::Mesh::mesh { {type "vtk"} } {
341    switch $type {
342        "vtk" {
343            return ""
344        }
345        default {
346            error "Requested mesh type \"$type\" is unknown."
347        }
348    }
349}
350
351# ----------------------------------------------------------------------
352# USAGE: size ?-points|-elements?
353#
354# Returns the number of points in this mesh.
355# ----------------------------------------------------------------------
356itcl::body Rappture::Mesh::size {{what -points}} {
357    switch -- $what {
358        -points {
359            return $_numPoints
360        }
361        -elements {
362            return $_numCells
363        }
364        default {
365            error "bad option \"$what\": should be -points or -elements"
366        }
367    }
368}
369
370# ----------------------------------------------------------------------
371# USAGE: dimensions
372#
373# Returns the number of dimensions for this object: 1, 2, or 3.
374# ----------------------------------------------------------------------
375itcl::body Rappture::Mesh::dimensions {} {
376    return $_dim
377}
378
379# ----------------------------------------------------------------------
380# USAGE: limits x|y|z
381#
382# Returns the {min max} coords for the limits of the specified axis.
383# ----------------------------------------------------------------------
384itcl::body Rappture::Mesh::limits {axis} {
385    if {![info exists _limits($axis)]} {
386        error "bad axis \"$which\": should be x, y, z"
387    }
388    return $_limits($axis)
389}
390
391# ----------------------------------------------------------------------
392# USAGE: hints ?<keyword>?
393#
394# Returns a list of key/value pairs for various hints about plotting
395# this field.  If a particular <keyword> is specified, then it returns
396# the hint for that <keyword>, if it exists.
397# ----------------------------------------------------------------------
398itcl::body Rappture::Mesh::hints {{keyword ""}} {
399    if {$keyword != ""} {
400        if {[info exists _hints($keyword)]} {
401            return $_hints($keyword)
402        }
403        return ""
404    }
405    return [array get _hints]
406}
407
408# ----------------------------------------------------------------------
409# USAGE: InitHints
410#
411# Returns a list of key/value pairs for various hints about plotting
412# this mesh.  If a particular <keyword> is specified, then it returns
413# the hint for that <keyword>, if it exists.
414# ----------------------------------------------------------------------
415itcl::body Rappture::Mesh::InitHints {} {
416    foreach {key path} {
417        camera       camera.position
418        color        about.color
419        label        about.label
420        style        about.style
421        units        units
422    } {
423        set str [$_mesh get $path]
424        if {"" != $str} {
425            set _hints($key) $str
426        }
427    }
428}
429
430itcl::body Rappture::Mesh::GetDimension { path } {
431    set string [$_xmlobj get $path.dim]
432    if { $string == "" } {
433        puts stderr "WARNING: no tag <dim> found in mesh \"$path\"."
434        return 0
435    }
436    if { [scan $string "%d" _dim] == 1 } {
437        if { $_dim == 1 || $_dim == 2 || $_dim == 3 } {
438            return 1
439        }
440    }
441    puts stderr "WARNING: bad <dim> tag value \"$string\": should be 1, 2 or 3."
442    return 0
443}
444
445itcl::body Rappture::Mesh::GetDouble { path } {
446    set string [$_xmlobj get $path]
447    if { [scan $string "%g" value] != 1 } {
448        puts stderr "ERROR: can't get double value \"$string\" of \"$path\""
449        return 0.0
450    }
451    return $value
452}
453
454itcl::body Rappture::Mesh::GetInt { path } {
455    set string [$_xmlobj get $path]
456    if { [scan $string "%d" value] != 1 } {
457        puts stderr "ERROR: can't get integer value \"$string\" of \"$path\""
458        return 0.0
459    }
460    return $value
461}
462
463itcl::body Rappture::Mesh::ReadVtk { path } {
464    set _type "vtk"
465
466    if { ![GetDimension $path] } {
467        return 0
468    }
469    # Create a VTK file with the mesh in it. 
470    set _vtkdata [$_xmlobj get $path.vtk]
471    append out "# vtk DataFile Version 3.0\n"
472    append out "mesh\n"
473    append out "ASCII\n"
474    append out "$_vtkdata\n"
475
476     # Write the contents to a file just in case it's binary.
477    set tmpfile file[pid].vtk
478    set f [open "$tmpfile" "w"]
479    fconfigure $f -translation binary -encoding binary
480    puts $f $out
481    close $f
482
483    # Read the data back into a vtk dataset and query the bounds.
484    set reader $this-datasetreader
485    vtkDataSetReader $reader
486    $reader SetFileName $tmpfile
487    $reader Update
488    set output [$reader GetOutput]
489    set _numPoints [$output GetNumberOfPoints]
490    set _numCells [$output GetNumberOfCells]
491    foreach { xmin xmax ymin ymax zmin zmax } [$output GetBounds] break
492    set _limits(x) [list $xmin $xmax]
493    set _limits(y) [list $ymin $ymax]
494    set _limits(z) [list $zmin $zmax]
495    file delete $tmpfile
496    rename $output ""
497    rename $reader ""
498    return 1
499}
500
501itcl::body Rappture::Mesh::ReadGrid { path } {
502    set _type "grid"
503
504    if { ![GetDimension $path] } {
505        return 0
506    }
507    set numUniform 0
508    set numRectilinear 0
509    set numCurvilinear 0
510    foreach axis { x y z } {
511        set min    [$_xmlobj get "$path.grid.${axis}axis.min"]
512        set max    [$_xmlobj get "$path.grid.${axis}axis.max"]
513        set num    [$_xmlobj get "$path.grid.${axis}axis.numpoints"]
514        set coords [$_xmlobj get "$path.grid.${axis}coords"]
515        set dim    [$_xmlobj get "$path.grid.${axis}dim"]
516        if { $min != "" && $max != "" && $num != "" && $num > 0 } {
517            set ${axis}Min $min
518            set ${axis}Max $max
519            set ${axis}Num $num
520            incr numUniform
521        } elseif { $coords != "" } {
522            incr numRectilinear
523            set ${axis}Coords $coords
524        } elseif { $dim != "" } {
525            set ${axis}Num $dim
526            incr numCurvilinear
527        }
528    }
529    set _dim [expr $numRectilinear + $numUniform + $numCurvilinear]
530    if { $_dim == 0 } {
531        # No data found.
532        puts stderr "WARNING: bad grid \"$path\": no data found"
533        return 0
534    }
535    if { $numCurvilinear > 0 } {
536        # This is the 2D/3D curilinear case. We found <xdim>, <ydim>, or <zdim>
537        if { $numRectilinear > 0 || $numUniform > 0 } {
538            puts stderr "WARNING: bad grid \"$path\": can't mix curvilinear and rectilinear grids."
539            return 0
540        }
541        set points [$_xmlobj get $path.grid.points]
542        if { $points == "" } {
543            puts stderr "WARNING: bad grid \"$path\": no <points> found."
544            return 0
545        }
546        if { ![info exists xNum] } {
547            puts stderr "WARNING: bad grid \"$path\": invalid dimensions for curvilinear grid: missing <xdim> from grid description."
548            return 0
549        }
550        set all [blt::vector create \#auto]
551        set xv [blt::vector create \#auto]
552        set yv [blt::vector create \#auto]
553        set zv [blt::vector create \#auto]
554        $all set $points
555        set numCoords [$all length]
556        if { [info exists zNum] } {
557            set _dim 3
558            set _numPoints [expr $xNum * $yNum * $zNum]
559            set _numCells [expr ($xNum - 1) * ($yNum - 1) * ($zNum - 1)]
560            if { ($_numPoints*3) != $numCoords } {
561                puts stderr "WARNING: bad grid \"$path\": invalid grid: \# of points does not match dimensions <xdim> * <ydim> * <zdim>"
562                return 0
563            }
564            if { ($numCoords % 3) != 0 } {
565                puts stderr "WARNING: bad grid \"$path\": wrong \# of coordinates for 3D grid"
566                return 0
567            }
568            $all split $xv $yv $zv
569            foreach axis {x y z} {
570                set vector [set ${axis}v]
571                set _limits($axis) [$vector limits]
572            }
573            append out "DATASET STRUCTURED_GRID\n"
574            append out "DIMENSIONS $xNum $yNum $zNum\n"
575            append out "POINTS $_numPoints double\n"
576            append out [$all range 0 end]
577            append out "\n"
578            set _vtkdata $out
579        } elseif { [info exists yNum] } {
580            set _dim 2
581            set _numPoints [expr $xNum * $yNum]
582            set _numCells [expr ($xNum - 1) * ($yNum - 1)]
583            if { ($_numPoints*2) != $numCoords } {
584                puts stderr "WARNING: bad grid \"$path\": \# of points does not match dimensions <xdim> * <ydim>"
585                return 0
586            }
587            if { ($numCoords % 2) != 0 } {
588                puts stderr "WARNING: bad grid \"$path\": wrong \# of coordinates for 2D grid"
589                return 0
590            }
591            foreach axis {x y} {
592                set vector [set ${axis}v]
593                set _limits($axis) [$vector limits]
594            }
595            set _limits(z) [list 0 0]
596            $zv seq 0 0 [$xv length]
597            $all merge $xv $yv $zv
598            append out "DATASET STRUCTURED_GRID\n"
599            append out "DIMENSIONS $xNum $yNum 1\n"
600            append out "POINTS $_numPoints double\n"
601            append out [$all range 0 end]
602            append out "\n"
603            set _vtkdata $out
604        } else {
605            set _dim 1
606            set _numPoints $xNum
607            set _numCells [expr $xNum - 1]
608            if { $_numPoints != $numCoords } {
609                puts stderr "WARNING: bad grid \"$path\": \# of points does not match <xdim>"
610                return 0
611            }
612            set _limits(x) [$xv limits]
613            set _limits(y) [list 0 0]
614            set _limits(z) [list 0 0]
615            $yv seq 0 0 [$xv length]
616            $zv seq 0 0 [$xv length]
617            $all merge $xv $yv $zv
618            append out "DATASET STRUCTURED_GRID\n"
619            append out "DIMENSIONS $xNum 1 1\n"
620            append out "POINTS $_numPoints double\n"
621            append out [$all range 0 end]
622            append out "\n"
623            set _vtkdata $out
624        }
625        blt::vector destroy $all $xv $yv $zv
626        return 1
627    }
628    if { $numRectilinear == 0 && $numUniform > 0} {
629        # This is the special case where all axes 2D/3D are uniform. 
630        # This results in a STRUCTURED_POINTS
631        if { $_dim == 1 } {
632            set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
633            set _numPoints $xNum
634            set _numCells [expr $xNum - 1]
635            append out "DATASET STRUCTURED_POINTS\n"
636            append out "DIMENSIONS $xNum 1 1\n"
637            append out "ORIGIN $xMin 0 0\n"
638            append out "SPACING $xSpace 0 0\n"
639            set _vtkdata $out
640            set _limits(x) [list $xMin $xMax]
641            set _limits(y) [list 0 0]
642            set _limits(z) [list 0 0]
643        } elseif { $_dim == 2 } {
644            set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
645            set ySpace [expr ($yMax - $yMin) / double($yNum - 1)]
646            set _numPoints [expr $xNum * $yNum]
647            set _numCells [expr ($xNum - 1) * ($yNum - 1)]
648            append out "DATASET STRUCTURED_POINTS\n"
649            append out "DIMENSIONS $xNum $yNum 1\n"
650            append out "ORIGIN $xMin $yMin 0\n"
651            append out "SPACING $xSpace $ySpace 0\n"
652            set _vtkdata $out
653            foreach axis {x y} {
654                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
655            }
656            set _limits(z) [list 0 0]
657        } elseif { $_dim == 3 } {
658            set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
659            set ySpace [expr ($yMax - $yMin) / double($yNum - 1)]
660            set zSpace [expr ($zMax - $zMin) / double($zNum - 1)]
661            set _numPoints [expr $xNum * $yNum * $zNum]
662            set _numCells [expr ($xNum - 1) * ($yNum - 1) * ($zNum - 1)]
663            append out "DATASET STRUCTURED_POINTS\n"
664            append out "DIMENSIONS $xNum $yNum $zNum\n"
665            append out "ORIGIN $xMin $yMin $zMin\n"
666            append out "SPACING $xSpace $ySpace $zSpace\n"
667            set _vtkdata $out
668            foreach axis {x y z} {
669                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
670            }
671        } else {
672            puts stderr "WARNING: bad grid \"$path\": bad dimension \"$_dim\""
673            return 0
674        }
675        return 1
676    }
677    # This is the hybrid case.  Some axes are uniform, others are nonuniform.
678    set xv [blt::vector create \#auto]
679    if { [info exists xMin] } {
680        $xv seq $xMin $xMax $xNum
681    } else {
682        $xv set [$_xmlobj get $path.grid.xcoords]
683        set xMin [$xv min]
684        set xMax [$xv max]
685        set xNum [$xv length]
686    }
687    set yv [blt::vector create \#auto]
688    if { $_dim > 1 } {
689        if { [info exists yMin] } {
690            $yv seq $yMin $yMax $yNum
691        } else {
692            $yv set [$_xmlobj get $path.grid.ycoords]
693            set yMin [$yv min]
694            set yMax [$yv max]
695            set yNum [$yv length]
696        }
697    } else {
698        set yNum 1
699    }
700    set zv [blt::vector create \#auto]
701    if { $_dim == 3 } {
702        if { [info exists zMin] } {
703            $zv seq $zMin $zMax $zNum
704        }  else {
705            $zv set [$_xmlobj get $path.grid.zcoords]
706            set zMin [$zv min]
707            set zMax [$zv max]
708            set zNum [$zv length]
709        }
710    } else {
711        set zNum 1
712    }
713    if { $_dim == 3 } {
714        set _numPoints [expr $xNum * $yNum * $zNum]
715        set _numCells [expr ($xNum - 1) * ($yNum - 1) * ($zNum - 1)]
716        append out "DATASET RECTILINEAR_GRID\n"
717        append out "DIMENSIONS $xNum $yNum $zNum\n"
718        append out "X_COORDINATES $xNum double\n"
719        append out [$xv range 0 end]
720        append out "\n"
721        append out "Y_COORDINATES $yNum double\n"
722        append out [$yv range 0 end]
723        append out "\n"
724        append out "Z_COORDINATES $zNum double\n"
725        append out [$zv range 0 end]
726        append out "\n"
727        set _vtkdata $out
728        foreach axis {x y z} {
729            if { [info exists ${axis}Min] } {
730                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
731            }
732        }
733    } elseif { $_dim == 2 } {
734        set _numPoints [expr $xNum * $yNum]
735        set _numCells [expr ($xNum - 1) * ($yNum - 1)]
736        append out "DATASET RECTILINEAR_GRID\n"
737        append out "DIMENSIONS $xNum $yNum 1\n"
738        append out "X_COORDINATES $xNum double\n"
739        append out [$xv range 0 end]
740        append out "\n"
741        append out "Y_COORDINATES $yNum double\n"
742        append out [$yv range 0 end]
743        append out "\n"
744        append out "Z_COORDINATES 1 double\n"
745        append out "0\n"
746        foreach axis {x y} {
747            if { [info exists ${axis}Min] } {
748                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
749            }
750        }
751        set _limits(z) [list 0 0]
752        set _vtkdata $out
753    } elseif { $_dim == 1 } {
754        set _numPoints $xNum
755        set _numCells [expr $xNum - 1]
756        append out "DATASET RECTILINEAR_GRID\n"
757        append out "DIMENSIONS $xNum 1 1\n"
758        append out "X_COORDINATES $xNum double\n"
759        append out [$xv range 0 end]
760        append out "\n"
761        append out "Y_COORDINATES 1 double\n"
762        append out "0\n"
763        append out "Z_COORDINATES 1 double\n"
764        append out "0\n"
765        if { [info exists xMin] } {
766            set _limits(x) [list $xMin $xMax]
767        }
768        set _limits(y) [list 0 0]
769        set _limits(z) [list 0 0]
770        set _vtkdata $out
771    } else {
772        puts stderr "WARNING: bad grid \"$path\": invalid dimension \"$_dim\""
773        return 0
774    }
775    blt::vector destroy $xv $yv $zv
776    return 1
777}
778
779itcl::body Rappture::Mesh::WritePointCloud { path xv yv zv } {
780    set _type "cloud"
781    set _numPoints [$xv length]
782    append out "DATASET POLYDATA\n"
783    append out "POINTS $_numPoints double\n"
784    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
785        append out "$x $y $z\n"
786    }
787    set _vtkdata $out
788    set _limits(x) [$xv limits]
789    if { $_dim > 1 } {
790        set _limits(y) [$yv limits]
791    } else {
792        set _limits(y) [list 0 0]
793    }
794    if { $_dim == 3 } {
795        set _limits(z) [$zv limits]
796    } else {
797        set _limits(z) [list 0 0]
798    }
799    return 1
800}
801
802itcl::body Rappture::Mesh::WriteTriangles { path xv yv zv triangles } {
803    set _type "triangles"
804    set _numPoints [$xv length]
805    set _numCells 0
806    set data {}
807    set celltypes {}
808    foreach { a b c } $triangles {
809        append data " 3 $a $b $c\n"
810        append celltypes "5\n"
811        incr _numCells
812    }
813    append out "DATASET UNSTRUCTURED_GRID\n"
814    append out "POINTS $_numPoints double\n"
815    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
816        append out " $x $y $z\n"
817    }
818    set count [expr $_numCells * 4]
819    append out "CELLS $_numCells $count\n"
820    append out $data
821    append out "CELL_TYPES $_numCells\n"
822    append out $celltypes
823    set _limits(x) [$xv limits]
824    set _limits(y) [$yv limits]
825    if { $_dim == 3 } {
826        set _limits(z) [$zv limits]
827    } else {
828        set _limits(z) [list 0 0]
829    }
830    set _vtkdata $out
831    return 1
832}
833
834itcl::body Rappture::Mesh::WriteQuads { path xv yv zv quads } {
835    set _type "quads"
836    set _numPoints [$xv length]
837    set _numCells 0
838    set data {}
839    set celltypes {}
840    foreach { a b c d } $quads {
841        append data " 4 $a $b $c $d\n"
842        append celltypes "9\n"
843        incr _numCells
844    }
845    append out "DATASET UNSTRUCTURED_GRID\n"
846    append out "POINTS $_numPoints double\n"
847    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
848        append out " $x $y $z\n"
849    }
850    set count [expr $_numCells * 5]
851    append out "CELLS $_numCells $count\n"
852    append out $data
853    append out "CELL_TYPES $_numCells\n"
854    append out $celltypes
855    set _limits(x) [$xv limits]
856    set _limits(y) [$yv limits]
857    if { $_dim == 3 } {
858        set _limits(z) [$zv limits]
859    } else {
860        set _limits(z) [list 0 0]
861    }
862    set _vtkdata $out
863    return 1
864}
865
866itcl::body Rappture::Mesh::WriteVertices { path xv yv zv vertices } {
867    set _type "vertices"
868    set _numPoints [$xv length]
869    set _numCells 0
870    set data {}
871    set lines [split $vertices \n]
872    set count 0
873    foreach { line } $lines {
874        set numIndices [llength $line]
875        if { $numIndices == 0 } {
876            continue
877        }
878        append data " $numIndices $line\n"
879        incr _numCells
880        set count [expr $count + $numIndices + 1]
881    }
882    append out "DATASET POLYDATA\n"
883    append out "POINTS $_numPoints double\n"
884    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
885        append out " $x $y $z\n"
886    }
887    append out "VERTICES $_numCells $count\n"
888    append out $data
889    set _limits(x) [$xv limits]
890    set _limits(y) [$yv limits]
891    if { $_dim == 3 } {
892        set _limits(z) [$zv limits]
893    } else {
894        set _limits(z) [list 0 0]
895    }
896    set _vtkdata $out
897    return 1
898}
899
900itcl::body Rappture::Mesh::WriteLines { path xv yv zv polylines } {
901    set _type "lines"
902    set _numPoints [$xv length]
903    set _numCells 0
904    set data {}
905    set lines [split $polylines \n]
906    set count 0
907    foreach { line } $lines {
908        set numIndices [llength $line]
909        if { $numIndices == 0 } {
910            continue
911        }
912        append data " $numIndices $line\n"
913        incr _numCells
914        set count [expr $count + $numIndices + 1]
915    }
916    append out "DATASET POLYDATA\n"
917    append out "POINTS $_numPoints double\n"
918    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
919        append out " $x $y $z\n"
920    }
921    append out "LINES $_numCells $count\n"
922    append out $data
923    set _limits(x) [$xv limits]
924    set _limits(y) [$yv limits]
925    if { $_dim == 3 } {
926        set _limits(z) [$zv limits]
927    } else {
928        set _limits(z) [list 0 0]
929    }
930    set _vtkdata $out
931    return 1
932}
933
934itcl::body Rappture::Mesh::WritePolygons { path xv yv zv polygons } {
935    set _type "polygons"
936    set _numPoints [$xv length]
937    set _numCells 0
938    set data {}
939    set lines [split $polygons \n]
940    set count 0
941    foreach { line } $lines {
942        set numIndices [llength $line]
943        if { $numIndices == 0 } {
944            continue
945        }
946        append data " $numIndices $line\n"
947        incr _numCells
948        set count [expr $count + $numIndices + 1]
949    }
950    append out "DATASET POLYDATA\n"
951    append out "POINTS $_numPoints double\n"
952    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
953        append out " $x $y $z\n"
954    }
955    append out "POLYGONS $_numCells $count\n"
956    append out $data
957    set _limits(x) [$xv limits]
958    set _limits(y) [$yv limits]
959    if { $_dim == 3 } {
960        set _limits(z) [$zv limits]
961    } else {
962        set _limits(z) [list 0 0]
963    }
964    set _vtkdata $out
965    return 1
966}
967
968itcl::body Rappture::Mesh::WriteTriangleStrips { path xv yv zv trianglestrips } {
969    set _type "trianglestrips"
970    set _numPoints [$xv length]
971    set _numCells 0
972    set data {}
973    set lines [split $trianglestrips \n]
974    set count 0
975    foreach { line } $lines {
976        set numIndices [llength $line]
977        if { $numIndices == 0 } {
978            continue
979        }
980        append data " $numIndices $line\n"
981        incr _numCells
982        set count [expr $count + $numIndices + 1]
983    }
984    append out "DATASET POLYDATA\n"
985    append out "POINTS $_numPoints double\n"
986    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
987        append out " $x $y $z\n"
988    }
989    append out "TRIANGLE_STRIPS $_numCells $count\n"
990    append out $data
991    set _limits(x) [$xv limits]
992    set _limits(y) [$yv limits]
993    if { $_dim == 3 } {
994        set _limits(z) [$zv limits]
995    } else {
996        set _limits(z) [list 0 0]
997    }
998    set _vtkdata $out
999    return 1
1000}
1001
1002itcl::body Rappture::Mesh::WriteTetrahedrons { path xv yv zv tetras } {
1003    set _type "tetrahedrons"
1004    set _numPoints [$xv length]
1005    set _numCells 0
1006    set data {}
1007    set celltypes {}
1008    foreach { a b c d } $tetras {
1009        append data " 4 $a $b $c $d\n"
1010        append celltypes "10\n"
1011        incr _numCells
1012    }
1013    append out "DATASET UNSTRUCTURED_GRID\n"
1014    append out "POINTS $_numPoints double\n"
1015    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1016        append out " $x $y $z\n"
1017    }
1018    set count [expr $_numCells * 5]
1019    append out "CELLS $_numCells $count\n"
1020    append out $data
1021    append out "CELL_TYPES $_numCells\n"
1022    append out $celltypes
1023    set _limits(x) [$xv limits]
1024    set _limits(y) [$yv limits]
1025    set _limits(z) [$zv limits]
1026
1027    set _vtkdata $out
1028    return 1
1029}
1030
1031itcl::body Rappture::Mesh::WriteHexahedrons { path xv yv zv hexas } {
1032    set _type "hexahedrons"
1033    set _numPoints [$xv length]
1034    set _numCells 0
1035    set data {}
1036    set celltypes {}
1037    foreach { a b c d e f g h } $hexas {
1038        append data " 8 $a $b $c $d $e $f $g $h\n"
1039        append celltypes "12\n"
1040        incr _numCells
1041    }
1042    append out "DATASET UNSTRUCTURED_GRID\n"
1043    append out "POINTS $_numPoints double\n"
1044    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1045        append out " $x $y $z\n"
1046    }
1047    set count [expr $_numCells * 9]
1048    append out "CELLS $_numCells $count\n"
1049    append out $data
1050    append out "CELL_TYPES $_numCells\n"
1051    append out $celltypes
1052    set _limits(x) [$xv limits]
1053    set _limits(y) [$yv limits]
1054    set _limits(z) [$zv limits]
1055
1056    set _vtkdata $out
1057    return 1
1058}
1059
1060itcl::body Rappture::Mesh::WriteWedges { path xv yv zv wedges } {
1061    set _type "wedges"
1062    set _numPoints [$xv length]
1063    set _numCells 0
1064    set data {}
1065    set celltypes {}
1066    foreach { a b c d e f } $wedges {
1067        append data " 6 $a $b $c $d $e $f\n"
1068        append celltypes "13\n"
1069        incr _numCells
1070    }
1071    append out "DATASET UNSTRUCTURED_GRID\n"
1072    append out "POINTS $_numPoints double\n"
1073    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1074        append out " $x $y $z\n"
1075    }
1076    set count [expr $_numCells * 7]
1077    append out "CELLS $_numCells $count\n"
1078    append out $data
1079    append out "CELL_TYPES $_numCells\n"
1080    append out $celltypes
1081    set _limits(x) [$xv limits]
1082    set _limits(y) [$yv limits]
1083    set _limits(z) [$zv limits]
1084
1085    set _vtkdata $out
1086    return 1
1087}
1088
1089itcl::body Rappture::Mesh::WritePyramids { path xv yv zv pyramids } {
1090    set _type "pyramids"
1091    set _numPoints [$xv length]
1092    set _numCells 0
1093    set data {}
1094    set celltypes {}
1095    foreach { a b c d e } $pyramids {
1096        append data " 5 $a $b $c $d $e\n"
1097        append celltypes "14\n"
1098        incr _numCells
1099    }
1100    append out "DATASET UNSTRUCTURED_GRID\n"
1101    append out "POINTS $_numPoints double\n"
1102    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1103        append out " $x $y $z\n"
1104    }
1105    set count [expr $_numCells * 6]
1106    append out "CELLS $_numCells $count\n"
1107    append out $data
1108    append out "CELL_TYPES $_numCells\n"
1109    append out $celltypes
1110    set _limits(x) [$xv limits]
1111    set _limits(y) [$yv limits]
1112    set _limits(z) [$zv limits]
1113
1114    set _vtkdata $out
1115    return 1
1116}
1117
1118itcl::body Rappture::Mesh::WriteHybridCells { path xv yv zv cells celltypes } {
1119    set _type "unstructured"
1120    set lines [split $cells \n]
1121    set numCellTypes [llength $celltypes]
1122    if { $numCellTypes == 1 } {
1123        set celltype [GetCellType $celltypes]
1124    }
1125
1126    set _numPoints [$xv length]
1127    set data {}
1128    set count 0
1129    set _numCells 0
1130    set celltypes {}
1131    foreach line $lines {
1132        set length [llength $line]
1133        if { $length == 0 } {
1134            continue
1135        }
1136        if { $numCellTypes > 1 } {
1137            set cellType [GetCellType [lindex $cellTypes $_numCells]]
1138        }
1139        set numIndices [GetNumIndices $celltype]
1140        if { $numIndices > 0 && $numIndices != $length } {
1141            puts stderr "WARNING: bad unstructured grid \"$path\": wrong \# of indices specified for celltype $celltype on line \"$line\""
1142            return 0
1143        } else {
1144            set numIndices $length
1145        }
1146        append data " $numIndices $line\n"
1147        lappend celltypes $celltype
1148        incr count $length;         # Include the indices
1149        incr count;                 # and the number of indices
1150        incr _numCells
1151    }
1152    append out "DATASET UNSTRUCTURED_GRID\n"
1153    append out "POINTS $_numPoints double\n"
1154    set all [blt::vector create \#auto]
1155    $all merge $xv $yv $zv
1156    append out [$all range 0 end]
1157    blt::vector destroy $all
1158    append out "CELLS $_numCells $count\n"
1159    append out $data
1160    append out "CELL_TYPES $_numCells\n"
1161    append out $celltypes
1162    set _limits(x) [$xv limits]
1163    set _limits(y) [$yv limits]
1164    if { $_dim < 3 } {
1165        set _limits(z) [list 0 0]
1166    } else {
1167        set _limits(z) [$zv limits]
1168    }
1169
1170    set _vtkdata $out
1171    return 1
1172}
1173
1174itcl::body Rappture::Mesh::ReadUnstructuredGrid { path } {
1175    set _type "unstructured"
1176
1177    if { ![GetDimension $path] } {
1178        return 0
1179    }
1180    # Step 1: Verify that there's only one cell tag of any kind.
1181    set numCells 0
1182    foreach type {
1183        cells
1184        hexahedrons
1185        lines
1186        polygons
1187        pyramids
1188        quads
1189        tetrahedrons
1190        triangles
1191        trianglestrips
1192        vertices
1193        wedges
1194    } {
1195        set data [$_xmlobj get $path.unstructured.$type]
1196        if { $data != "" } {
1197            incr numCells
1198        }
1199    }
1200    # The generic <cells> tag requires there be a <celltypes> tag too.
1201    set celltypes [$_xmlobj get $path.unstructured.celltypes]
1202    if { $numCells == 0 && $celltypes != "" } {
1203        puts stderr "WARNING: bad unstuctured grid \"$path\": no <cells> description found."
1204        return 0
1205    }
1206    if { $numCells > 1 } {
1207        puts stderr "WARNING: bad unstructured grid \"$path\": too many <cells>, <triangles>, <quads>... descriptions found: should be only one."
1208        return 0
1209    }
1210    foreach type { cells
1211        vertices lines polygons trianglestrips
1212        triangles quads
1213        tetrahedrons hexahedrons wedges pyramids } {
1214        set data [$_xmlobj get $path.unstructured.$type]
1215        if { $data != "" } {
1216            break
1217        }
1218    }
1219    # Step 2: Allow points to be specified as <points> or
1220    #         <xcoords>, <ycoords>, <zcoords>.  Split and convert into
1221    #         3 vectors, one for each coordinate.
1222    if { $_dim == 1 } {
1223        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1224        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1225        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1226        set data    [$_xmlobj get $path.unstructured.points]
1227        if { $ycoords != "" } {
1228            put stderr "can't specify <ycoords> with a 1D mesh"
1229            return 0
1230        }
1231        if { $zcoords != "" } {
1232            put stderr "can't specify <zcoords> with a 1D mesh"
1233            return 0
1234        }
1235        if { $xcoords != "" } {
1236            set xv [blt::vector create \#auto]
1237            $xv set $xcoords
1238        } elseif { $data != "" } {
1239            Rappture::ReadPoints $data dim points
1240            if { $points == "" } {
1241                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1242                return 0
1243            }
1244            if { $dim != 1 } {
1245                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1246                return 0
1247            }
1248            set xv [blt::vector create \#auto]
1249            $xv set $points
1250        } else {
1251            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1252            return 0
1253        }
1254        set yv [blt::vector create \#auto]
1255        set zv [blt::vector create \#auto]
1256        $yv seq 0 0 [$xv length];       # Make an all zeroes vector.
1257        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
1258    } elseif { $_dim == 2 } {
1259        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1260        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1261        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1262        set data    [$_xmlobj get $path.unstructured.points]
1263        if { $zcoords != "" } {
1264            put stderr "can't specify <zcoords> with a 2D mesh"
1265            return 0
1266        }
1267        if { $xcoords != "" && $ycoords != "" } {
1268            set xv [blt::vector create \#auto]
1269            set yv [blt::vector create \#auto]
1270            $xv set $xcoords
1271            $yv set $ycoords
1272        } elseif { $data != "" } {
1273            Rappture::ReadPoints $data dim points
1274            if { $points == "" } {
1275                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1276                return 0
1277            }
1278            if { $dim != 2 } {
1279                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1280                return 0
1281            }
1282            set all [blt::vector create \#auto]
1283            set xv [blt::vector create \#auto]
1284            set yv [blt::vector create \#auto]
1285            $all set $points
1286            $all split $xv $yv
1287            blt::vector destroy $all
1288        } else {
1289            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1290            return 0
1291        }
1292        set zv [blt::vector create \#auto]
1293        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
1294    } elseif { $_dim == 3 } {
1295        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1296        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1297        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1298        set data    [$_xmlobj get $path.unstructured.points]
1299        if { $xcoords != "" && $ycoords != "" && $zcoords != "" } {
1300            set xv [blt::vector create \#auto]
1301            set yv [blt::vector create \#auto]
1302            set zv [blt::vector create \#auto]
1303            $xv set $xcoords
1304            $yv set $ycoords
1305            $zv set $zcoords
1306        } elseif { $data != "" }  {
1307            Rappture::ReadPoints $data dim points
1308            if { $points == "" } {
1309                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1310                return 0
1311            }
1312            if { $dim != 3 } {
1313                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1314                return 0
1315            }
1316            set all [blt::vector create \#auto]
1317            set xv [blt::vector create \#auto]
1318            set yv [blt::vector create \#auto]
1319            set zv [blt::vector create \#auto]
1320            $all set $points
1321            $all split $xv $yv $zv
1322            blt::vector destroy $all
1323        } else {
1324            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1325            return 0
1326        }
1327    }
1328    set _numPoints [$xv length]
1329
1330    # Step 3: Write the points and cells as vtk data.
1331    if { $numCells == 0 } {
1332        set result [WritePointCloud $path $xv $yv $zv]
1333    } elseif { $type == "cells" } {
1334        set cells [$_xmlobj get $path.unstructured.cells]
1335        if { $cells == "" } {
1336            puts stderr "WARNING: bad unstructured grid \"$path\": no cells found."
1337            return 0
1338        }
1339        set result [WriteHybridCells $path $xv $yv $zv $cells $celltypes]
1340    } else {
1341        set cmd "Write[string totitle $type]"
1342        set cells [$_xmlobj get $path.unstructured.$type]
1343        if { $cells == "" } {
1344            puts stderr "WARNING: bad unstructured grid \"$path\": no cells found."
1345            return 0
1346        }
1347        set result [$cmd $path $xv $yv $zv $cells]
1348    }
1349    # Clean up.
1350    blt::vector destroy $xv $yv $zv
1351    return $result
1352}
1353
1354# ----------------------------------------------------------------------
1355# USAGE: ReadNodesElements <path>
1356#
1357# Used internally to build a mesh representation based on nodes and
1358# elements stored in the XML.
1359# ----------------------------------------------------------------------
1360itcl::body Rappture::Mesh::ReadNodesElements {path} {
1361    set _type "nodeselements"
1362    set count 0
1363
1364    # Scan for nodes.  Each node represents a vertex.
1365    set data {}
1366    foreach cname [$_xmlobj children -type node $path] {
1367        append data "[$_xmlobj get $path.$cname]\n"
1368    }   
1369    Rappture::ReadPoints $data _dim points
1370    if { $_dim == 2 } {
1371        set all [blt::vector create \#auto]
1372        set xv [blt::vector create \#auto]
1373        set yv [blt::vector create \#auto]
1374        set zv [blt::vector create \#auto]
1375        $all set $points
1376        $all split $xv $yv
1377        set _numPoints [$xv length]
1378        set _limits(x) [$xv limits]
1379        set _limits(y) [$yv limits]
1380        set _limits(z) [list 0 0]
1381        # 2D Dataset. All Z coordinates are 0
1382        $zv seq 0.0 0.0 $_numPoints
1383        $all merge $xv $yv $zv
1384        set points [$all range 0 end]
1385        blt::vector destroy $all $xv $yv $zv
1386    } elseif { $_dim == 3 } {
1387        set all [blt::vector create \#auto]
1388        set xv [blt::vector create \#auto]
1389        set yv [blt::vector create \#auto]
1390        set zv [blt::vector create \#auto]
1391        $all set $points
1392        $all split $xv $yv $zv
1393        set _numPoints [$xv length]
1394        set _limits(x) [$xv limits]
1395        set _limits(y) [$yv limits]
1396        set _limits(z) [$zv limits]
1397        set points [$all range 0 end]
1398        blt::vector destroy $all $xv $yv $zv
1399    } else {
1400        error "bad dimension \"$_dim\" for nodes mesh"
1401    }
1402    array set node2celltype {
1403        3 5
1404        4 10
1405        8 12
1406        6 13
1407        5 14
1408    }
1409    set count 0
1410    set _numCells 0
1411    set celltypes {}
1412    set data {}
1413    # Next scan for elements.  Each element represents a cell.
1414    foreach cname [$_xmlobj children -type element $path] {
1415        set nodeList [$_mesh get $cname.nodes]
1416        set numNodes [llength $nodeList]
1417        if { ![info exists node2celltype($numNodes)] } {
1418            puts stderr "WARNING: bad nodes/elements mesh \$path\": unknown number of indices \"$_numNodes\": should be 3, 4, 5, 6, or 8"
1419            return 0
1420        }
1421        set celltype $node2celltype($numNodes)
1422        append celltypes "  $celltype\n"
1423        if { $celltype == 12 } {
1424            # Formerly used voxels instead of hexahedrons. We're converting
1425            # it here to be backward compatible and still fault-tolerant of
1426            # non-axis aligned cells.
1427            set newList {}
1428            foreach i { 0 1 3 2 4 5 7 6 } {
1429                lappend newList [lindex $nodeList $i]
1430            }
1431            set nodeList $newList
1432        }
1433        append data "  $numNodes $nodeList\n"
1434        incr _numCells
1435        incr count $numNodes
1436        incr count;                     # One extra for the VTK celltype id.
1437    }
1438
1439    append out "DATASET UNSTRUCTURED_GRID\n"
1440    append out "POINTS $_numPoints double\n"
1441    append out $points
1442    append out "\n"
1443    append out "CELLS $_numCells $count\n"
1444    append out $data
1445    append out "CELL_TYPES $_numCells\n"
1446    append out $celltypes
1447    append out "\n"
1448    set _vtkdata $out
1449    set _isValid 1
1450}
1451
1452itcl::body Rappture::Mesh::GetCellType { name } {
1453    array set name2type {
1454        "vertex"          1
1455        "polyvertex"      2
1456        "line"            3
1457        "polyline"        4
1458        "triangle"        5
1459        "trianglestrip"   6
1460        "polygon"         7
1461        "pixel"           8
1462        "quad"            9
1463        "tetrahedron"     10
1464        "voxel"           11
1465        "hexahedron"      12
1466        "wedge"           13
1467        "pyramid"         14
1468        "pentagonalprism" 15
1469        "hexagonalprism"  16
1470    }
1471    if { [info exists name2type($name)] } {
1472        return $name2type($name)
1473    }
1474    if { [string is int $name] == 1 && $name >= 1 && $name <= 16 } {
1475        return $name
1476    }
1477    error "invalid celltype \"$name\""
1478}
1479
1480itcl::body Rappture::Mesh::GetNumIndices { type } {
1481    array set type2indices {
1482        1       1
1483        2       0
1484        3       2
1485        4       0
1486        5       3
1487        6       0
1488        7       0
1489        8       4
1490        9       4
1491        10      4
1492        11      8
1493        12      8
1494        13      6
1495        14      5
1496        15      10
1497        16      12
1498    }
1499    if { [info exists type2indices($type)] } {
1500        return $type2indices($type)
1501    }
1502    error "invalid celltype \"$type\""
1503}
Note: See TracBrowser for help on using the repository browser.