source: trunk/gui/scripts/mesh.tcl @ 4687

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

Add check for grid with swapped min/max

File size: 47.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            if {$min > $max} {
521                puts stderr "ERROR: grid $axis axis minimum larger than maximum"
522                return 0
523            }
524            incr numUniform
525        } elseif { $coords != "" } {
526            incr numRectilinear
527            set ${axis}Coords $coords
528        } elseif { $dim != "" } {
529            set ${axis}Num $dim
530            incr numCurvilinear
531        }
532    }
533    set _dim [expr $numRectilinear + $numUniform + $numCurvilinear]
534    if { $_dim == 0 } {
535        # No data found.
536        puts stderr "WARNING: bad grid \"$path\": no data found"
537        return 0
538    }
539    if { $numCurvilinear > 0 } {
540        # This is the 2D/3D curilinear case. We found <xdim>, <ydim>, or <zdim>
541        if { $numRectilinear > 0 || $numUniform > 0 } {
542            puts stderr "WARNING: bad grid \"$path\": can't mix curvilinear and rectilinear grids."
543            return 0
544        }
545        set points [$_xmlobj get $path.grid.points]
546        if { $points == "" } {
547            puts stderr "WARNING: bad grid \"$path\": no <points> found."
548            return 0
549        }
550        if { ![info exists xNum] } {
551            puts stderr "WARNING: bad grid \"$path\": invalid dimensions for curvilinear grid: missing <xdim> from grid description."
552            return 0
553        }
554        set all [blt::vector create \#auto]
555        set xv [blt::vector create \#auto]
556        set yv [blt::vector create \#auto]
557        set zv [blt::vector create \#auto]
558        $all set $points
559        set numCoords [$all length]
560        if { [info exists zNum] } {
561            set _dim 3
562            set _numPoints [expr $xNum * $yNum * $zNum]
563            set _numCells [expr ($xNum > 1 ? ($xNum - 1) : 1) * ($yNum > 1 ? ($yNum - 1) : 1) * ($zNum > 1 ? ($zNum - 1) : 1)]
564            if { ($_numPoints*3) != $numCoords } {
565                puts stderr "WARNING: bad grid \"$path\": invalid grid: \# of points does not match dimensions <xdim> * <ydim> * <zdim>"
566                return 0
567            }
568            if { ($numCoords % 3) != 0 } {
569                puts stderr "WARNING: bad grid \"$path\": wrong \# of coordinates for 3D grid"
570                return 0
571            }
572            $all split $xv $yv $zv
573            foreach axis {x y z} {
574                set vector [set ${axis}v]
575                set _limits($axis) [$vector limits]
576            }
577            append out "DATASET STRUCTURED_GRID\n"
578            append out "DIMENSIONS $xNum $yNum $zNum\n"
579            append out "POINTS $_numPoints double\n"
580            append out [$all range 0 end]
581            append out "\n"
582            set _vtkdata $out
583        } elseif { [info exists yNum] } {
584            set _dim 2
585            set _numPoints [expr $xNum * $yNum]
586            set _numCells [expr ($xNum > 1 ? ($xNum - 1) : 1) * ($yNum > 1 ? ($yNum - 1) : 1)]
587            if { ($_numPoints*2) != $numCoords } {
588                puts stderr "WARNING: bad grid \"$path\": \# of points does not match dimensions <xdim> * <ydim>"
589                return 0
590            }
591            if { ($numCoords % 2) != 0 } {
592                puts stderr "WARNING: bad grid \"$path\": wrong \# of coordinates for 2D grid"
593                return 0
594            }
595            foreach axis {x y} {
596                set vector [set ${axis}v]
597                set _limits($axis) [$vector limits]
598            }
599            set _limits(z) [list 0 0]
600            $zv seq 0 0 [$xv length]
601            $all merge $xv $yv $zv
602            append out "DATASET STRUCTURED_GRID\n"
603            append out "DIMENSIONS $xNum $yNum 1\n"
604            append out "POINTS $_numPoints double\n"
605            append out [$all range 0 end]
606            append out "\n"
607            set _vtkdata $out
608        } else {
609            set _dim 1
610            set _numPoints $xNum
611            set _numCells [expr $xNum - 1]
612            if { $_numPoints != $numCoords } {
613                puts stderr "WARNING: bad grid \"$path\": \# of points does not match <xdim>"
614                return 0
615            }
616            set _limits(x) [$xv limits]
617            set _limits(y) [list 0 0]
618            set _limits(z) [list 0 0]
619            $yv seq 0 0 [$xv length]
620            $zv seq 0 0 [$xv length]
621            $all merge $xv $yv $zv
622            append out "DATASET STRUCTURED_GRID\n"
623            append out "DIMENSIONS $xNum 1 1\n"
624            append out "POINTS $_numPoints double\n"
625            append out [$all range 0 end]
626            append out "\n"
627            set _vtkdata $out
628        }
629        blt::vector destroy $all $xv $yv $zv
630        return 1
631    }
632    if { $numRectilinear == 0 && $numUniform > 0} {
633        # This is the special case where all axes 2D/3D are uniform. 
634        # This results in a STRUCTURED_POINTS
635        if { $_dim == 1 } {
636            if {$xNum == 1} {
637                set xSpace 0
638            } else {
639                set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
640            }
641            set _numPoints $xNum
642            set _numCells [expr $xNum - 1]
643            append out "DATASET STRUCTURED_POINTS\n"
644            append out "DIMENSIONS $xNum 1 1\n"
645            append out "ORIGIN $xMin 0 0\n"
646            append out "SPACING $xSpace 0 0\n"
647            set _vtkdata $out
648            set _limits(x) [list $xMin $xMax]
649            set _limits(y) [list 0 0]
650            set _limits(z) [list 0 0]
651        } elseif { $_dim == 2 } {
652            if {$xNum == 1} {
653                set xSpace 0
654            } else {
655                set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
656            }
657            if {$yNum == 1} {
658                set ySpace 0
659            } else {
660                set ySpace [expr ($yMax - $yMin) / double($yNum - 1)]
661            }
662            set _numPoints [expr $xNum * $yNum]
663            set _numCells [expr ($xNum > 1 ? ($xNum - 1) : 1) * ($yNum > 1 ? ($yNum - 1) : 1)]
664            append out "DATASET STRUCTURED_POINTS\n"
665            append out "DIMENSIONS $xNum $yNum 1\n"
666            append out "ORIGIN $xMin $yMin 0\n"
667            append out "SPACING $xSpace $ySpace 0\n"
668            set _vtkdata $out
669            foreach axis {x y} {
670                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
671            }
672            set _limits(z) [list 0 0]
673        } elseif { $_dim == 3 } {
674            if {$xNum == 1} {
675                set xSpace 0
676            } else {
677                set xSpace [expr ($xMax - $xMin) / double($xNum - 1)]
678            }
679            if {$yNum == 1} {
680                set ySpace 0
681            } else {
682                set ySpace [expr ($yMax - $yMin) / double($yNum - 1)]
683            }
684            if {$zNum == 1} {
685                set zSpace 0
686            } else {
687                set zSpace [expr ($zMax - $zMin) / double($zNum - 1)]
688            }
689            set _numPoints [expr $xNum * $yNum * $zNum]
690            set _numCells [expr ($xNum > 1 ? ($xNum - 1) : 1) * ($yNum > 1 ? ($yNum - 1) : 1) * ($zNum > 1 ? ($zNum - 1) : 1)]
691            append out "DATASET STRUCTURED_POINTS\n"
692            append out "DIMENSIONS $xNum $yNum $zNum\n"
693            append out "ORIGIN $xMin $yMin $zMin\n"
694            append out "SPACING $xSpace $ySpace $zSpace\n"
695            set _vtkdata $out
696            foreach axis {x y z} {
697                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
698            }
699        } else {
700            puts stderr "WARNING: bad grid \"$path\": bad dimension \"$_dim\""
701            return 0
702        }
703        return 1
704    }
705    # This is the hybrid case.  Some axes are uniform, others are nonuniform.
706    set xv [blt::vector create \#auto]
707    if { [info exists xMin] } {
708        $xv seq $xMin $xMax $xNum
709    } else {
710        $xv set [$_xmlobj get $path.grid.xcoords]
711        set xMin [$xv min]
712        set xMax [$xv max]
713        set xNum [$xv length]
714    }
715    set yv [blt::vector create \#auto]
716    if { $_dim > 1 } {
717        if { [info exists yMin] } {
718            $yv seq $yMin $yMax $yNum
719        } else {
720            $yv set [$_xmlobj get $path.grid.ycoords]
721            set yMin [$yv min]
722            set yMax [$yv max]
723            set yNum [$yv length]
724        }
725    } else {
726        set yNum 1
727    }
728    set zv [blt::vector create \#auto]
729    if { $_dim == 3 } {
730        if { [info exists zMin] } {
731            $zv seq $zMin $zMax $zNum
732        }  else {
733            $zv set [$_xmlobj get $path.grid.zcoords]
734            set zMin [$zv min]
735            set zMax [$zv max]
736            set zNum [$zv length]
737        }
738    } else {
739        set zNum 1
740    }
741    if { $_dim == 3 } {
742        set _numPoints [expr $xNum * $yNum * $zNum]
743        set _numCells [expr ($xNum > 1 ? ($xNum - 1) : 1) * ($yNum > 1 ? ($yNum - 1) : 1) * ($zNum > 1 ? ($zNum - 1) : 1)]
744        append out "DATASET RECTILINEAR_GRID\n"
745        append out "DIMENSIONS $xNum $yNum $zNum\n"
746        append out "X_COORDINATES $xNum double\n"
747        append out [$xv range 0 end]
748        append out "\n"
749        append out "Y_COORDINATES $yNum double\n"
750        append out [$yv range 0 end]
751        append out "\n"
752        append out "Z_COORDINATES $zNum double\n"
753        append out [$zv range 0 end]
754        append out "\n"
755        set _vtkdata $out
756        foreach axis {x y z} {
757            if { [info exists ${axis}Min] } {
758                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
759            }
760        }
761    } elseif { $_dim == 2 } {
762        set _numPoints [expr $xNum * $yNum]
763        set _numCells [expr ($xNum > 1 ? ($xNum - 1) : 1) * ($yNum > 1 ? ($yNum - 1) : 1)]
764        append out "DATASET RECTILINEAR_GRID\n"
765        append out "DIMENSIONS $xNum $yNum 1\n"
766        append out "X_COORDINATES $xNum double\n"
767        append out [$xv range 0 end]
768        append out "\n"
769        append out "Y_COORDINATES $yNum double\n"
770        append out [$yv range 0 end]
771        append out "\n"
772        append out "Z_COORDINATES 1 double\n"
773        append out "0\n"
774        foreach axis {x y} {
775            if { [info exists ${axis}Min] } {
776                set _limits($axis) [list [set ${axis}Min] [set ${axis}Max]]
777            }
778        }
779        set _limits(z) [list 0 0]
780        set _vtkdata $out
781    } elseif { $_dim == 1 } {
782        set _numPoints $xNum
783        set _numCells [expr $xNum - 1]
784        append out "DATASET RECTILINEAR_GRID\n"
785        append out "DIMENSIONS $xNum 1 1\n"
786        append out "X_COORDINATES $xNum double\n"
787        append out [$xv range 0 end]
788        append out "\n"
789        append out "Y_COORDINATES 1 double\n"
790        append out "0\n"
791        append out "Z_COORDINATES 1 double\n"
792        append out "0\n"
793        if { [info exists xMin] } {
794            set _limits(x) [list $xMin $xMax]
795        }
796        set _limits(y) [list 0 0]
797        set _limits(z) [list 0 0]
798        set _vtkdata $out
799    } else {
800        puts stderr "WARNING: bad grid \"$path\": invalid dimension \"$_dim\""
801        return 0
802    }
803    blt::vector destroy $xv $yv $zv
804    return 1
805}
806
807itcl::body Rappture::Mesh::WritePointCloud { path xv yv zv } {
808    set _type "cloud"
809    set _numPoints [$xv length]
810    append out "DATASET POLYDATA\n"
811    append out "POINTS $_numPoints double\n"
812    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
813        append out "$x $y $z\n"
814    }
815    set _vtkdata $out
816    set _limits(x) [$xv limits]
817    if { $_dim > 1 } {
818        set _limits(y) [$yv limits]
819    } else {
820        set _limits(y) [list 0 0]
821    }
822    if { $_dim == 3 } {
823        set _limits(z) [$zv limits]
824    } else {
825        set _limits(z) [list 0 0]
826    }
827    return 1
828}
829
830itcl::body Rappture::Mesh::WriteTriangles { path xv yv zv triangles } {
831    set _type "triangles"
832    set _numPoints [$xv length]
833    set _numCells 0
834    set data {}
835    set celltypes {}
836    foreach { a b c } $triangles {
837        append data " 3 $a $b $c\n"
838        append celltypes "5\n"
839        incr _numCells
840    }
841    append out "DATASET UNSTRUCTURED_GRID\n"
842    append out "POINTS $_numPoints double\n"
843    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
844        append out " $x $y $z\n"
845    }
846    set count [expr $_numCells * 4]
847    append out "CELLS $_numCells $count\n"
848    append out $data
849    append out "CELL_TYPES $_numCells\n"
850    append out $celltypes
851    set _limits(x) [$xv limits]
852    set _limits(y) [$yv limits]
853    if { $_dim == 3 } {
854        set _limits(z) [$zv limits]
855    } else {
856        set _limits(z) [list 0 0]
857    }
858    set _vtkdata $out
859    return 1
860}
861
862itcl::body Rappture::Mesh::WriteQuads { path xv yv zv quads } {
863    set _type "quads"
864    set _numPoints [$xv length]
865    set _numCells 0
866    set data {}
867    set celltypes {}
868    foreach { a b c d } $quads {
869        append data " 4 $a $b $c $d\n"
870        append celltypes "9\n"
871        incr _numCells
872    }
873    append out "DATASET UNSTRUCTURED_GRID\n"
874    append out "POINTS $_numPoints double\n"
875    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
876        append out " $x $y $z\n"
877    }
878    set count [expr $_numCells * 5]
879    append out "CELLS $_numCells $count\n"
880    append out $data
881    append out "CELL_TYPES $_numCells\n"
882    append out $celltypes
883    set _limits(x) [$xv limits]
884    set _limits(y) [$yv limits]
885    if { $_dim == 3 } {
886        set _limits(z) [$zv limits]
887    } else {
888        set _limits(z) [list 0 0]
889    }
890    set _vtkdata $out
891    return 1
892}
893
894itcl::body Rappture::Mesh::WriteVertices { path xv yv zv vertices } {
895    set _type "vertices"
896    set _numPoints [$xv length]
897    set _numCells 0
898    set data {}
899    set lines [split $vertices \n]
900    set count 0
901    foreach { line } $lines {
902        set numIndices [llength $line]
903        if { $numIndices == 0 } {
904            continue
905        }
906        append data " $numIndices $line\n"
907        incr _numCells
908        set count [expr $count + $numIndices + 1]
909    }
910    append out "DATASET POLYDATA\n"
911    append out "POINTS $_numPoints double\n"
912    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
913        append out " $x $y $z\n"
914    }
915    append out "VERTICES $_numCells $count\n"
916    append out $data
917    set _limits(x) [$xv limits]
918    set _limits(y) [$yv limits]
919    if { $_dim == 3 } {
920        set _limits(z) [$zv limits]
921    } else {
922        set _limits(z) [list 0 0]
923    }
924    set _vtkdata $out
925    return 1
926}
927
928itcl::body Rappture::Mesh::WriteLines { path xv yv zv polylines } {
929    set _type "lines"
930    set _numPoints [$xv length]
931    set _numCells 0
932    set data {}
933    set lines [split $polylines \n]
934    set count 0
935    foreach { line } $lines {
936        set numIndices [llength $line]
937        if { $numIndices == 0 } {
938            continue
939        }
940        append data " $numIndices $line\n"
941        incr _numCells
942        set count [expr $count + $numIndices + 1]
943    }
944    append out "DATASET POLYDATA\n"
945    append out "POINTS $_numPoints double\n"
946    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
947        append out " $x $y $z\n"
948    }
949    append out "LINES $_numCells $count\n"
950    append out $data
951    set _limits(x) [$xv limits]
952    set _limits(y) [$yv limits]
953    if { $_dim == 3 } {
954        set _limits(z) [$zv limits]
955    } else {
956        set _limits(z) [list 0 0]
957    }
958    set _vtkdata $out
959    return 1
960}
961
962itcl::body Rappture::Mesh::WritePolygons { path xv yv zv polygons } {
963    set _type "polygons"
964    set _numPoints [$xv length]
965    set _numCells 0
966    set data {}
967    set lines [split $polygons \n]
968    set count 0
969    foreach { line } $lines {
970        set numIndices [llength $line]
971        if { $numIndices == 0 } {
972            continue
973        }
974        append data " $numIndices $line\n"
975        incr _numCells
976        set count [expr $count + $numIndices + 1]
977    }
978    append out "DATASET POLYDATA\n"
979    append out "POINTS $_numPoints double\n"
980    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
981        append out " $x $y $z\n"
982    }
983    append out "POLYGONS $_numCells $count\n"
984    append out $data
985    set _limits(x) [$xv limits]
986    set _limits(y) [$yv limits]
987    if { $_dim == 3 } {
988        set _limits(z) [$zv limits]
989    } else {
990        set _limits(z) [list 0 0]
991    }
992    set _vtkdata $out
993    return 1
994}
995
996itcl::body Rappture::Mesh::WriteTriangleStrips { path xv yv zv trianglestrips } {
997    set _type "trianglestrips"
998    set _numPoints [$xv length]
999    set _numCells 0
1000    set data {}
1001    set lines [split $trianglestrips \n]
1002    set count 0
1003    foreach { line } $lines {
1004        set numIndices [llength $line]
1005        if { $numIndices == 0 } {
1006            continue
1007        }
1008        append data " $numIndices $line\n"
1009        incr _numCells
1010        set count [expr $count + $numIndices + 1]
1011    }
1012    append out "DATASET POLYDATA\n"
1013    append out "POINTS $_numPoints double\n"
1014    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1015        append out " $x $y $z\n"
1016    }
1017    append out "TRIANGLE_STRIPS $_numCells $count\n"
1018    append out $data
1019    set _limits(x) [$xv limits]
1020    set _limits(y) [$yv limits]
1021    if { $_dim == 3 } {
1022        set _limits(z) [$zv limits]
1023    } else {
1024        set _limits(z) [list 0 0]
1025    }
1026    set _vtkdata $out
1027    return 1
1028}
1029
1030itcl::body Rappture::Mesh::WriteTetrahedrons { path xv yv zv tetras } {
1031    set _type "tetrahedrons"
1032    set _numPoints [$xv length]
1033    set _numCells 0
1034    set data {}
1035    set celltypes {}
1036    foreach { a b c d } $tetras {
1037        append data " 4 $a $b $c $d\n"
1038        append celltypes "10\n"
1039        incr _numCells
1040    }
1041    append out "DATASET UNSTRUCTURED_GRID\n"
1042    append out "POINTS $_numPoints double\n"
1043    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1044        append out " $x $y $z\n"
1045    }
1046    set count [expr $_numCells * 5]
1047    append out "CELLS $_numCells $count\n"
1048    append out $data
1049    append out "CELL_TYPES $_numCells\n"
1050    append out $celltypes
1051    set _limits(x) [$xv limits]
1052    set _limits(y) [$yv limits]
1053    set _limits(z) [$zv limits]
1054
1055    set _vtkdata $out
1056    return 1
1057}
1058
1059itcl::body Rappture::Mesh::WriteHexahedrons { path xv yv zv hexas } {
1060    set _type "hexahedrons"
1061    set _numPoints [$xv length]
1062    set _numCells 0
1063    set data {}
1064    set celltypes {}
1065    foreach { a b c d e f g h } $hexas {
1066        append data " 8 $a $b $c $d $e $f $g $h\n"
1067        append celltypes "12\n"
1068        incr _numCells
1069    }
1070    append out "DATASET UNSTRUCTURED_GRID\n"
1071    append out "POINTS $_numPoints double\n"
1072    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1073        append out " $x $y $z\n"
1074    }
1075    set count [expr $_numCells * 9]
1076    append out "CELLS $_numCells $count\n"
1077    append out $data
1078    append out "CELL_TYPES $_numCells\n"
1079    append out $celltypes
1080    set _limits(x) [$xv limits]
1081    set _limits(y) [$yv limits]
1082    set _limits(z) [$zv limits]
1083
1084    set _vtkdata $out
1085    return 1
1086}
1087
1088itcl::body Rappture::Mesh::WriteWedges { path xv yv zv wedges } {
1089    set _type "wedges"
1090    set _numPoints [$xv length]
1091    set _numCells 0
1092    set data {}
1093    set celltypes {}
1094    foreach { a b c d e f } $wedges {
1095        append data " 6 $a $b $c $d $e $f\n"
1096        append celltypes "13\n"
1097        incr _numCells
1098    }
1099    append out "DATASET UNSTRUCTURED_GRID\n"
1100    append out "POINTS $_numPoints double\n"
1101    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1102        append out " $x $y $z\n"
1103    }
1104    set count [expr $_numCells * 7]
1105    append out "CELLS $_numCells $count\n"
1106    append out $data
1107    append out "CELL_TYPES $_numCells\n"
1108    append out $celltypes
1109    set _limits(x) [$xv limits]
1110    set _limits(y) [$yv limits]
1111    set _limits(z) [$zv limits]
1112
1113    set _vtkdata $out
1114    return 1
1115}
1116
1117itcl::body Rappture::Mesh::WritePyramids { path xv yv zv pyramids } {
1118    set _type "pyramids"
1119    set _numPoints [$xv length]
1120    set _numCells 0
1121    set data {}
1122    set celltypes {}
1123    foreach { a b c d e } $pyramids {
1124        append data " 5 $a $b $c $d $e\n"
1125        append celltypes "14\n"
1126        incr _numCells
1127    }
1128    append out "DATASET UNSTRUCTURED_GRID\n"
1129    append out "POINTS $_numPoints double\n"
1130    foreach x [$xv range 0 end] y [$yv range 0 end] z [$zv range 0 end] {
1131        append out " $x $y $z\n"
1132    }
1133    set count [expr $_numCells * 6]
1134    append out "CELLS $_numCells $count\n"
1135    append out $data
1136    append out "CELL_TYPES $_numCells\n"
1137    append out $celltypes
1138    set _limits(x) [$xv limits]
1139    set _limits(y) [$yv limits]
1140    set _limits(z) [$zv limits]
1141
1142    set _vtkdata $out
1143    return 1
1144}
1145
1146itcl::body Rappture::Mesh::WriteHybridCells { path xv yv zv cells celltypes } {
1147    set _type "unstructured"
1148    set lines [split $cells \n]
1149    set numCellTypes [llength $celltypes]
1150    if { $numCellTypes == 1 } {
1151        set celltype [GetCellType $celltypes]
1152    }
1153
1154    set _numPoints [$xv length]
1155    set data {}
1156    set count 0
1157    set _numCells 0
1158    set celltypes {}
1159    foreach line $lines {
1160        set length [llength $line]
1161        if { $length == 0 } {
1162            continue
1163        }
1164        if { $numCellTypes > 1 } {
1165            set cellType [GetCellType [lindex $cellTypes $_numCells]]
1166        }
1167        set numIndices [GetNumIndices $celltype]
1168        if { $numIndices > 0 && $numIndices != $length } {
1169            puts stderr "WARNING: bad unstructured grid \"$path\": wrong \# of indices specified for celltype $celltype on line \"$line\""
1170            return 0
1171        } else {
1172            set numIndices $length
1173        }
1174        append data " $numIndices $line\n"
1175        lappend celltypes $celltype
1176        incr count $length;         # Include the indices
1177        incr count;                 # and the number of indices
1178        incr _numCells
1179    }
1180    append out "DATASET UNSTRUCTURED_GRID\n"
1181    append out "POINTS $_numPoints double\n"
1182    set all [blt::vector create \#auto]
1183    $all merge $xv $yv $zv
1184    append out [$all range 0 end]
1185    blt::vector destroy $all
1186    append out "CELLS $_numCells $count\n"
1187    append out $data
1188    append out "CELL_TYPES $_numCells\n"
1189    append out $celltypes
1190    set _limits(x) [$xv limits]
1191    set _limits(y) [$yv limits]
1192    if { $_dim < 3 } {
1193        set _limits(z) [list 0 0]
1194    } else {
1195        set _limits(z) [$zv limits]
1196    }
1197
1198    set _vtkdata $out
1199    return 1
1200}
1201
1202itcl::body Rappture::Mesh::ReadUnstructuredGrid { path } {
1203    set _type "unstructured"
1204
1205    if { ![GetDimension $path] } {
1206        return 0
1207    }
1208    # Step 1: Verify that there's only one cell tag of any kind.
1209    set numCells 0
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            incr numCells
1217        }
1218    }
1219    # The generic <cells> tag requires there be a <celltypes> tag too.
1220    set celltypes [$_xmlobj get $path.unstructured.celltypes]
1221    if { $numCells == 0 && $celltypes != "" } {
1222        puts stderr "WARNING: bad unstuctured grid \"$path\": no <cells> description found."
1223        return 0
1224    }
1225    if { $numCells > 1 } {
1226        puts stderr "WARNING: bad unstructured grid \"$path\": too many <cells>, <triangles>, <quads>... descriptions found: should be only one."
1227        return 0
1228    }
1229    foreach type { cells
1230        vertices lines polygons trianglestrips
1231        triangles quads
1232        tetrahedrons hexahedrons wedges pyramids } {
1233        set data [$_xmlobj get $path.unstructured.$type]
1234        if { $data != "" } {
1235            break
1236        }
1237    }
1238    # Step 2: Allow points to be specified as <points> or
1239    #         <xcoords>, <ycoords>, <zcoords>.  Split and convert into
1240    #         3 vectors, one for each coordinate.
1241    if { $_dim == 1 } {
1242        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1243        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1244        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1245        set data    [$_xmlobj get $path.unstructured.points]
1246        if { $ycoords != "" } {
1247            put stderr "can't specify <ycoords> with a 1D mesh"
1248            return 0
1249        }
1250        if { $zcoords != "" } {
1251            put stderr "can't specify <zcoords> with a 1D mesh"
1252            return 0
1253        }
1254        if { $xcoords != "" } {
1255            set xv [blt::vector create \#auto]
1256            $xv set $xcoords
1257        } elseif { $data != "" } {
1258            Rappture::ReadPoints $data dim points
1259            if { $points == "" } {
1260                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1261                return 0
1262            }
1263            if { $dim != 1 } {
1264                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1265                return 0
1266            }
1267            set xv [blt::vector create \#auto]
1268            $xv set $points
1269        } else {
1270            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1271            return 0
1272        }
1273        set yv [blt::vector create \#auto]
1274        set zv [blt::vector create \#auto]
1275        $yv seq 0 0 [$xv length];       # Make an all zeroes vector.
1276        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
1277    } elseif { $_dim == 2 } {
1278        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1279        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1280        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1281        set data    [$_xmlobj get $path.unstructured.points]
1282        if { $zcoords != "" } {
1283            put stderr "can't specify <zcoords> with a 2D mesh"
1284            return 0
1285        }
1286        if { $xcoords != "" && $ycoords != "" } {
1287            set xv [blt::vector create \#auto]
1288            set yv [blt::vector create \#auto]
1289            $xv set $xcoords
1290            $yv set $ycoords
1291        } elseif { $data != "" } {
1292            Rappture::ReadPoints $data dim points
1293            if { $points == "" } {
1294                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1295                return 0
1296            }
1297            if { $dim != 2 } {
1298                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1299                return 0
1300            }
1301            set all [blt::vector create \#auto]
1302            set xv [blt::vector create \#auto]
1303            set yv [blt::vector create \#auto]
1304            $all set $points
1305            $all split $xv $yv
1306            blt::vector destroy $all
1307        } else {
1308            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1309            return 0
1310        }
1311        set zv [blt::vector create \#auto]
1312        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
1313    } elseif { $_dim == 3 } {
1314        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1315        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1316        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1317        set data    [$_xmlobj get $path.unstructured.points]
1318        if { $xcoords != "" && $ycoords != "" && $zcoords != "" } {
1319            set xv [blt::vector create \#auto]
1320            set yv [blt::vector create \#auto]
1321            set zv [blt::vector create \#auto]
1322            $xv set $xcoords
1323            $yv set $ycoords
1324            $zv set $zcoords
1325        } elseif { $data != "" }  {
1326            Rappture::ReadPoints $data dim points
1327            if { $points == "" } {
1328                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1329                return 0
1330            }
1331            if { $dim != 3 } {
1332                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1333                return 0
1334            }
1335            set all [blt::vector create \#auto]
1336            set xv [blt::vector create \#auto]
1337            set yv [blt::vector create \#auto]
1338            set zv [blt::vector create \#auto]
1339            $all set $points
1340            $all split $xv $yv $zv
1341            blt::vector destroy $all
1342        } else {
1343            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1344            return 0
1345        }
1346    }
1347    set _numPoints [$xv length]
1348
1349    # Step 3: Write the points and cells as vtk data.
1350    if { $numCells == 0 } {
1351        set result [WritePointCloud $path $xv $yv $zv]
1352    } elseif { $type == "cells" } {
1353        set cells [$_xmlobj get $path.unstructured.cells]
1354        if { $cells == "" } {
1355            puts stderr "WARNING: bad unstructured grid \"$path\": no cells found."
1356            return 0
1357        }
1358        set result [WriteHybridCells $path $xv $yv $zv $cells $celltypes]
1359    } else {
1360        set cmd "Write[string totitle $type]"
1361        set cells [$_xmlobj get $path.unstructured.$type]
1362        if { $cells == "" } {
1363            puts stderr "WARNING: bad unstructured grid \"$path\": no cells found."
1364            return 0
1365        }
1366        set result [$cmd $path $xv $yv $zv $cells]
1367    }
1368    # Clean up.
1369    blt::vector destroy $xv $yv $zv
1370    return $result
1371}
1372
1373# ----------------------------------------------------------------------
1374# USAGE: ReadNodesElements <path>
1375#
1376# Used internally to build a mesh representation based on nodes and
1377# elements stored in the XML.
1378# ----------------------------------------------------------------------
1379itcl::body Rappture::Mesh::ReadNodesElements {path} {
1380    set _type "nodeselements"
1381    set count 0
1382
1383    # Scan for nodes.  Each node represents a vertex.
1384    set data {}
1385    foreach cname [$_xmlobj children -type node $path] {
1386        append data "[$_xmlobj get $path.$cname]\n"
1387    }   
1388    Rappture::ReadPoints $data _dim points
1389    if { $_dim == 2 } {
1390        set all [blt::vector create \#auto]
1391        set xv [blt::vector create \#auto]
1392        set yv [blt::vector create \#auto]
1393        set zv [blt::vector create \#auto]
1394        $all set $points
1395        $all split $xv $yv
1396        set _numPoints [$xv length]
1397        set _limits(x) [$xv limits]
1398        set _limits(y) [$yv limits]
1399        set _limits(z) [list 0 0]
1400        # 2D Dataset. All Z coordinates are 0
1401        $zv seq 0.0 0.0 $_numPoints
1402        $all merge $xv $yv $zv
1403        set points [$all range 0 end]
1404        blt::vector destroy $all $xv $yv $zv
1405    } elseif { $_dim == 3 } {
1406        set all [blt::vector create \#auto]
1407        set xv [blt::vector create \#auto]
1408        set yv [blt::vector create \#auto]
1409        set zv [blt::vector create \#auto]
1410        $all set $points
1411        $all split $xv $yv $zv
1412        set _numPoints [$xv length]
1413        set _limits(x) [$xv limits]
1414        set _limits(y) [$yv limits]
1415        set _limits(z) [$zv limits]
1416        set points [$all range 0 end]
1417        blt::vector destroy $all $xv $yv $zv
1418    } else {
1419        error "bad dimension \"$_dim\" for nodes mesh"
1420    }
1421    array set node2celltype {
1422        3 5
1423        4 10
1424        8 12
1425        6 13
1426        5 14
1427    }
1428    set count 0
1429    set _numCells 0
1430    set celltypes {}
1431    set data {}
1432    # Next scan for elements.  Each element represents a cell.
1433    foreach cname [$_xmlobj children -type element $path] {
1434        set nodeList [$_mesh get $cname.nodes]
1435        set numNodes [llength $nodeList]
1436        if { ![info exists node2celltype($numNodes)] } {
1437            puts stderr "WARNING: bad nodes/elements mesh \$path\": unknown number of indices \"$_numNodes\": should be 3, 4, 5, 6, or 8"
1438            return 0
1439        }
1440        set celltype $node2celltype($numNodes)
1441        append celltypes "  $celltype\n"
1442        if { $celltype == 12 } {
1443            # Formerly used voxels instead of hexahedrons. We're converting
1444            # it here to be backward compatible and still fault-tolerant of
1445            # non-axis aligned cells.
1446            set newList {}
1447            foreach i { 0 1 3 2 4 5 7 6 } {
1448                lappend newList [lindex $nodeList $i]
1449            }
1450            set nodeList $newList
1451        }
1452        append data "  $numNodes $nodeList\n"
1453        incr _numCells
1454        incr count $numNodes
1455        incr count;                     # One extra for the VTK celltype id.
1456    }
1457
1458    append out "DATASET UNSTRUCTURED_GRID\n"
1459    append out "POINTS $_numPoints double\n"
1460    append out $points
1461    append out "\n"
1462    append out "CELLS $_numCells $count\n"
1463    append out $data
1464    append out "CELL_TYPES $_numCells\n"
1465    append out $celltypes
1466    append out "\n"
1467    set _vtkdata $out
1468    set _isValid 1
1469}
1470
1471itcl::body Rappture::Mesh::GetCellType { name } {
1472    array set name2type {
1473        "vertex"          1
1474        "polyvertex"      2
1475        "line"            3
1476        "polyline"        4
1477        "triangle"        5
1478        "trianglestrip"   6
1479        "polygon"         7
1480        "pixel"           8
1481        "quad"            9
1482        "tetrahedron"     10
1483        "voxel"           11
1484        "hexahedron"      12
1485        "wedge"           13
1486        "pyramid"         14
1487        "pentagonalprism" 15
1488        "hexagonalprism"  16
1489    }
1490    if { [info exists name2type($name)] } {
1491        return $name2type($name)
1492    }
1493    if { [string is int $name] == 1 && $name >= 1 && $name <= 16 } {
1494        return $name
1495    }
1496    error "invalid celltype \"$name\""
1497}
1498
1499itcl::body Rappture::Mesh::GetNumIndices { type } {
1500    array set type2indices {
1501        1       1
1502        2       0
1503        3       2
1504        4       0
1505        5       3
1506        6       0
1507        7       0
1508        8       4
1509        9       4
1510        10      4
1511        11      8
1512        12      8
1513        13      6
1514        14      5
1515        15      10
1516        16      12
1517    }
1518    if { [info exists type2indices($type)] } {
1519        return $type2indices($type)
1520    }
1521    error "invalid celltype \"$type\""
1522}
Note: See TracBrowser for help on using the repository browser.