source: branches/uq/gui/scripts/mesh.tcl @ 4797

Last change on this file since 4797 was 4797, checked in by gah, 6 years ago

sync with 1.4-pre

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 {
1211        cells
1212        hexahedrons
1213        lines
1214        polygons
1215        pyramids
1216        quads
1217        tetrahedrons
1218        triangles
1219        trianglestrips
1220        vertices
1221        wedges
1222    } {
1223        set data [$_xmlobj get $path.unstructured.$type]
1224        if { $data != "" } {
1225            incr numCells
1226        }
1227    }
1228    # The generic <cells> tag requires there be a <celltypes> tag too.
1229    set celltypes [$_xmlobj get $path.unstructured.celltypes]
1230    if { $numCells == 0 && $celltypes != "" } {
1231        puts stderr "WARNING: bad unstuctured grid \"$path\": no <cells> description found."
1232        return 0
1233    }
1234    if { $numCells > 1 } {
1235        puts stderr "WARNING: bad unstructured grid \"$path\": too many <cells>, <triangles>, <quads>... descriptions found: should be only one."
1236        return 0
1237    }
1238    foreach type { cells
1239        vertices lines polygons trianglestrips
1240        triangles quads
1241        tetrahedrons hexahedrons wedges pyramids } {
1242        set data [$_xmlobj get $path.unstructured.$type]
1243        if { $data != "" } {
1244            break
1245        }
1246    }
1247    # Step 2: Allow points to be specified as <points> or
1248    #         <xcoords>, <ycoords>, <zcoords>.  Split and convert into
1249    #         3 vectors, one for each coordinate.
1250    if { $_dim == 1 } {
1251        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1252        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1253        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1254        set data    [$_xmlobj get $path.unstructured.points]
1255        if { $ycoords != "" } {
1256            put stderr "can't specify <ycoords> with a 1D mesh"
1257            return 0
1258        }
1259        if { $zcoords != "" } {
1260            put stderr "can't specify <zcoords> with a 1D mesh"
1261            return 0
1262        }
1263        if { $xcoords != "" } {
1264            set xv [blt::vector create \#auto]
1265            $xv set $xcoords
1266        } elseif { $data != "" } {
1267            Rappture::ReadPoints $data dim points
1268            if { $points == "" } {
1269                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1270                return 0
1271            }
1272            if { $dim != 1 } {
1273                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1274                return 0
1275            }
1276            set xv [blt::vector create \#auto]
1277            $xv set $points
1278        } else {
1279            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1280            return 0
1281        }
1282        set yv [blt::vector create \#auto]
1283        set zv [blt::vector create \#auto]
1284        $yv seq 0 0 [$xv length];       # Make an all zeroes vector.
1285        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
1286    } elseif { $_dim == 2 } {
1287        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1288        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1289        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1290        set data    [$_xmlobj get $path.unstructured.points]
1291        if { $zcoords != "" } {
1292            put stderr "can't specify <zcoords> with a 2D mesh"
1293            return 0
1294        }
1295        if { $xcoords != "" && $ycoords != "" } {
1296            set xv [blt::vector create \#auto]
1297            set yv [blt::vector create \#auto]
1298            $xv set $xcoords
1299            $yv set $ycoords
1300        } elseif { $data != "" } {
1301            Rappture::ReadPoints $data dim points
1302            if { $points == "" } {
1303                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1304                return 0
1305            }
1306            if { $dim != 2 } {
1307                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1308                return 0
1309            }
1310            set all [blt::vector create \#auto]
1311            set xv [blt::vector create \#auto]
1312            set yv [blt::vector create \#auto]
1313            $all set $points
1314            $all split $xv $yv
1315            blt::vector destroy $all
1316        } else {
1317            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1318            return 0
1319        }
1320        set zv [blt::vector create \#auto]
1321        $zv seq 0 0 [$xv length];       # Make an all zeroes vector.
1322    } elseif { $_dim == 3 } {
1323        set xcoords [$_xmlobj get $path.unstructured.xcoords]
1324        set ycoords [$_xmlobj get $path.unstructured.ycoords]
1325        set zcoords [$_xmlobj get $path.unstructured.zcoords]
1326        set data    [$_xmlobj get $path.unstructured.points]
1327        if { $xcoords != "" && $ycoords != "" && $zcoords != "" } {
1328            set xv [blt::vector create \#auto]
1329            set yv [blt::vector create \#auto]
1330            set zv [blt::vector create \#auto]
1331            $xv set $xcoords
1332            $yv set $ycoords
1333            $zv set $zcoords
1334        } elseif { $data != "" }  {
1335            Rappture::ReadPoints $data dim points
1336            if { $points == "" } {
1337                puts stderr "WARNING: bad unstructured grid \"$path\": no <points> found."
1338                return 0
1339            }
1340            if { $dim != 3 } {
1341                puts stderr "WARNING: bad unstructured grid \"$path\": \# of coordinates per point is \"$dim\": does not agree with dimension specified for mesh \"$_dim\""
1342                return 0
1343            }
1344            set all [blt::vector create \#auto]
1345            set xv [blt::vector create \#auto]
1346            set yv [blt::vector create \#auto]
1347            set zv [blt::vector create \#auto]
1348            $all set $points
1349            $all split $xv $yv $zv
1350            blt::vector destroy $all
1351        } else {
1352            puts stderr "WARNING: bad unstructured grid \"$path\": no points specified."
1353            return 0
1354        }
1355    }
1356    set _numPoints [$xv length]
1357
1358    # Step 3: Write the points and cells as vtk data.
1359    if { $numCells == 0 } {
1360        set result [WritePointCloud $path $xv $yv $zv]
1361    } elseif { $type == "cells" } {
1362        set cells [$_xmlobj get $path.unstructured.cells]
1363        if { $cells == "" } {
1364            puts stderr "WARNING: bad unstructured grid \"$path\": no cells found."
1365            return 0
1366        }
1367        set result [WriteHybridCells $path $xv $yv $zv $cells $celltypes]
1368    } else {
1369        set cmd "Write[string totitle $type]"
1370        set cells [$_xmlobj get $path.unstructured.$type]
1371        if { $cells == "" } {
1372            puts stderr "WARNING: bad unstructured grid \"$path\": no cells found."
1373            return 0
1374        }
1375        set result [$cmd $path $xv $yv $zv $cells]
1376    }
1377    # Clean up.
1378    blt::vector destroy $xv $yv $zv
1379    return $result
1380}
1381
1382# ----------------------------------------------------------------------
1383# USAGE: ReadNodesElements <path>
1384#
1385# Used internally to build a mesh representation based on nodes and
1386# elements stored in the XML.
1387# ----------------------------------------------------------------------
1388itcl::body Rappture::Mesh::ReadNodesElements {path} {
1389    set _type "nodeselements"
1390    set count 0
1391
1392    # Scan for nodes.  Each node represents a vertex.
1393    set data {}
1394    foreach cname [$_xmlobj children -type node $path] {
1395        append data "[$_xmlobj get $path.$cname]\n"
1396    }   
1397    Rappture::ReadPoints $data _dim points
1398    if { $_dim == 2 } {
1399        set all [blt::vector create \#auto]
1400        set xv [blt::vector create \#auto]
1401        set yv [blt::vector create \#auto]
1402        set zv [blt::vector create \#auto]
1403        $all set $points
1404        $all split $xv $yv
1405        set _numPoints [$xv length]
1406        set _limits(x) [$xv limits]
1407        set _limits(y) [$yv limits]
1408        set _limits(z) [list 0 0]
1409        # 2D Dataset. All Z coordinates are 0
1410        $zv seq 0.0 0.0 $_numPoints
1411        $all merge $xv $yv $zv
1412        set points [$all range 0 end]
1413        blt::vector destroy $all $xv $yv $zv
1414    } elseif { $_dim == 3 } {
1415        set all [blt::vector create \#auto]
1416        set xv [blt::vector create \#auto]
1417        set yv [blt::vector create \#auto]
1418        set zv [blt::vector create \#auto]
1419        $all set $points
1420        $all split $xv $yv $zv
1421        set _numPoints [$xv length]
1422        set _limits(x) [$xv limits]
1423        set _limits(y) [$yv limits]
1424        set _limits(z) [$zv limits]
1425        set points [$all range 0 end]
1426        blt::vector destroy $all $xv $yv $zv
1427    } else {
1428        error "bad dimension \"$_dim\" for nodes mesh"
1429    }
1430    array set node2celltype {
1431        3 5
1432        4 10
1433        8 12
1434        6 13
1435        5 14
1436    }
1437    set count 0
1438    set _numCells 0
1439    set celltypes {}
1440    set data {}
1441    # Next scan for elements.  Each element represents a cell.
1442    foreach cname [$_xmlobj children -type element $path] {
1443        set nodeList [$_mesh get $cname.nodes]
1444        set numNodes [llength $nodeList]
1445        if { ![info exists node2celltype($numNodes)] } {
1446            puts stderr "WARNING: bad nodes/elements mesh \$path\": unknown number of indices \"$_numNodes\": should be 3, 4, 5, 6, or 8"
1447            return 0
1448        }
1449        set celltype $node2celltype($numNodes)
1450        append celltypes "  $celltype\n"
1451        if { $celltype == 12 } {
1452            # Formerly used voxels instead of hexahedrons. We're converting
1453            # it here to be backward compatible and still fault-tolerant of
1454            # non-axis aligned cells.
1455            set newList {}
1456            foreach i { 0 1 3 2 4 5 7 6 } {
1457                lappend newList [lindex $nodeList $i]
1458            }
1459            set nodeList $newList
1460        }
1461        append data "  $numNodes $nodeList\n"
1462        incr _numCells
1463        incr count $numNodes
1464        incr count;                     # One extra for the VTK celltype id.
1465    }
1466
1467    append out "DATASET UNSTRUCTURED_GRID\n"
1468    append out "POINTS $_numPoints double\n"
1469    append out $points
1470    append out "\n"
1471    append out "CELLS $_numCells $count\n"
1472    append out $data
1473    append out "CELL_TYPES $_numCells\n"
1474    append out $celltypes
1475    append out "\n"
1476    set _vtkdata $out
1477    set _isValid 1
1478}
1479
1480itcl::body Rappture::Mesh::GetCellType { name } {
1481    array set name2type {
1482        "vertex"          1
1483        "polyvertex"      2
1484        "line"            3
1485        "polyline"        4
1486        "triangle"        5
1487        "trianglestrip"   6
1488        "polygon"         7
1489        "pixel"           8
1490        "quad"            9
1491        "tetrahedron"     10
1492        "voxel"           11
1493        "hexahedron"      12
1494        "wedge"           13
1495        "pyramid"         14
1496        "pentagonalprism" 15
1497        "hexagonalprism"  16
1498    }
1499    if { [info exists name2type($name)] } {
1500        return $name2type($name)
1501    }
1502    if { [string is int $name] == 1 && $name >= 1 && $name <= 16 } {
1503        return $name
1504    }
1505    error "invalid celltype \"$name\""
1506}
1507
1508itcl::body Rappture::Mesh::GetNumIndices { type } {
1509    array set type2indices {
1510        1       1
1511        2       0
1512        3       2
1513        4       0
1514        5       3
1515        6       0
1516        7       0
1517        8       4
1518        9       4
1519        10      4
1520        11      8
1521        12      8
1522        13      6
1523        14      5
1524        15      10
1525        16      12
1526    }
1527    if { [info exists type2indices($type)] } {
1528        return $type2indices($type)
1529    }
1530    error "invalid celltype \"$type\""
1531}
Note: See TracBrowser for help on using the repository browser.