1 | |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: flowhints - represents a uniform rectangular 2-D mesh. |
---|
4 | # |
---|
5 | # This object represents one field in an XML description of a device. |
---|
6 | # It simplifies the process of extracting data vectors that represent |
---|
7 | # the field. |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: Michael McLennan, Purdue University |
---|
10 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and |
---|
13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # ====================================================================== |
---|
15 | |
---|
16 | package require Itcl |
---|
17 | package require BLT |
---|
18 | |
---|
19 | namespace eval Rappture { |
---|
20 | # empty |
---|
21 | } |
---|
22 | |
---|
23 | itcl::class Rappture::FlowHints { |
---|
24 | constructor {field cname units} { |
---|
25 | # defined below |
---|
26 | } |
---|
27 | destructor { |
---|
28 | # defined below |
---|
29 | } |
---|
30 | |
---|
31 | public method hints {} { return [array get _hints] } |
---|
32 | public method particles {} { return $_particles } |
---|
33 | public method boxes {} { return $_boxes } |
---|
34 | |
---|
35 | private method ConvertUnits { value } |
---|
36 | private method GetAxis { obj path varName } |
---|
37 | private method GetBoolean { obj path varName } |
---|
38 | private method GetCorner { obj path varName } |
---|
39 | private method GetPosition { obj path varName } |
---|
40 | private method GetSize { obj path varName } |
---|
41 | |
---|
42 | private variable _boxes ""; # List of boxes for the flow. |
---|
43 | private variable _particles ""; # List of particle injection planes. |
---|
44 | private variable _hints; # Array of settings for the flow. |
---|
45 | private variable _units "" |
---|
46 | } |
---|
47 | |
---|
48 | # ---------------------------------------------------------------------- |
---|
49 | # Constructor |
---|
50 | # ---------------------------------------------------------------------- |
---|
51 | itcl::body Rappture::FlowHints::constructor {field cname units} { |
---|
52 | if {[$field element $cname.flow] == ""} { |
---|
53 | puts stderr "no flow entry in $cname" |
---|
54 | return |
---|
55 | } |
---|
56 | array set _hints { |
---|
57 | "axis" "x" |
---|
58 | "description" "" |
---|
59 | "outline" "on" |
---|
60 | "position" "0.0%" |
---|
61 | "streams" "on" |
---|
62 | "arrows" "off" |
---|
63 | "volume" "on" |
---|
64 | "duration" "1:00" |
---|
65 | "speed" "1x" |
---|
66 | } |
---|
67 | set _units $units |
---|
68 | set f [$field element -as object $cname.flow] |
---|
69 | set _hints(name) [$field element -as id $cname.flow] |
---|
70 | foreach child [$f children] { |
---|
71 | set value [$f get $child] |
---|
72 | switch -glob -- $child { |
---|
73 | "label" { set _hints(label) $value } |
---|
74 | "description" { set _hints(description) $value } |
---|
75 | "outline" { GetBoolean $f $child _hints(outline) } |
---|
76 | "volume" { GetBoolean $f $child _hints(volume) } |
---|
77 | "streams" { GetBoolean $f $child _hints(streams) } |
---|
78 | "arrows" { GetBoolean $f $child _hints(arrows) } |
---|
79 | "axis" { GetAxis $f $child _hints(axis) } |
---|
80 | "speed" { set _hints(speed) $value } |
---|
81 | "duration" { set _hints(duration) $value } |
---|
82 | "position" { GetPosition $f $child _hints(position) } |
---|
83 | "particles*" { |
---|
84 | array unset data |
---|
85 | array set data { |
---|
86 | "axis" "x" |
---|
87 | "color" "blue" |
---|
88 | "description" "" |
---|
89 | "hide" "no" |
---|
90 | "label" "" |
---|
91 | "position" "0.0%" |
---|
92 | "size" "1.2" |
---|
93 | } |
---|
94 | set p [$f element -as object $child] |
---|
95 | set data(name) [$f element -as id $child] |
---|
96 | foreach child [$p children] { |
---|
97 | set value [$p get $child] |
---|
98 | switch -exact -- $child { |
---|
99 | "axis" { GetAxis $p axis data(axis) } |
---|
100 | "color" { set data(color) $value } |
---|
101 | "description" { set data(description) $value } |
---|
102 | "hide" { GetBoolean $p hide data(hide) } |
---|
103 | "label" { set data(label) $value } |
---|
104 | "position" { GetPosition $p position data(position)} |
---|
105 | "size" { GetSize $p size data(size)} |
---|
106 | } |
---|
107 | } |
---|
108 | if { $data(label) == "" } { |
---|
109 | set data(label) $data(name) |
---|
110 | } |
---|
111 | itcl::delete object $p |
---|
112 | lappend _particles [array get data] |
---|
113 | } |
---|
114 | "box*" { |
---|
115 | array unset data |
---|
116 | array set data { |
---|
117 | "color" "green" |
---|
118 | "description" "" |
---|
119 | "hide" "no" |
---|
120 | "label" "" |
---|
121 | "linewidth" "2" |
---|
122 | } |
---|
123 | set b [$f element -as object $child] |
---|
124 | set name [$f element -as id $child] |
---|
125 | set count 0 |
---|
126 | set data(name) $name |
---|
127 | set data(color) [$f get $child.color] |
---|
128 | foreach child [$b children] { |
---|
129 | set value [$b get $child] |
---|
130 | switch -glob -- $child { |
---|
131 | "color" { set data(color) $value } |
---|
132 | "description" { set data(description) $value } |
---|
133 | "hide" { GetBoolean $b hide data(hide) } |
---|
134 | "linewidth" { GetSize $b linewidth data(linewidth) } |
---|
135 | "label" { set data(label) $value } |
---|
136 | "corner*" { |
---|
137 | incr count |
---|
138 | GetCorner $b $child data(corner$count) |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | if { $data(label) == "" } { |
---|
143 | set data(label) $data(name) |
---|
144 | } |
---|
145 | itcl::delete object $b |
---|
146 | lappend _boxes [array get data] |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | itcl::delete object $f |
---|
151 | } |
---|
152 | |
---|
153 | itcl::body Rappture::FlowHints::ConvertUnits { value } { |
---|
154 | set cmd Rappture::Units::convert |
---|
155 | set n [scan $value "%g%s" number suffix] |
---|
156 | if { $n == 2 } { |
---|
157 | if { $suffix == "%" } { |
---|
158 | return $value |
---|
159 | } else { |
---|
160 | return [$cmd $number -context $suffix -to $_units -units off] |
---|
161 | } |
---|
162 | } elseif { [scan $value "%g" number] == 1 } { |
---|
163 | if { $_units == "" } { |
---|
164 | return $number |
---|
165 | } |
---|
166 | return [$cmd $number -context $_units -to $_units -units off] |
---|
167 | } |
---|
168 | return "" |
---|
169 | } |
---|
170 | |
---|
171 | itcl::body Rappture::FlowHints::GetPosition { obj path varName } { |
---|
172 | set value [$obj get $path] |
---|
173 | set pos [ConvertUnits $value] |
---|
174 | if { $pos == "" } { |
---|
175 | puts stderr "can't convert units \"$value\" of \"$path\"" |
---|
176 | } |
---|
177 | upvar $varName position |
---|
178 | set position $pos |
---|
179 | } |
---|
180 | |
---|
181 | itcl::body Rappture::FlowHints::GetAxis { obj path varName } { |
---|
182 | set value [$obj get $path] |
---|
183 | set value [string tolower $value] |
---|
184 | switch -- $value { |
---|
185 | "x" - "y" - "z" { |
---|
186 | upvar $varName axis |
---|
187 | set axis $value |
---|
188 | return |
---|
189 | } |
---|
190 | } |
---|
191 | puts stderr "invalid axis \"$value\" in \"$path\"" |
---|
192 | } |
---|
193 | |
---|
194 | itcl::body Rappture::FlowHints::GetCorner { obj path varName } { |
---|
195 | set value [$obj get $path] |
---|
196 | set coords "" |
---|
197 | if { [llength $value] != 3 } { |
---|
198 | puts stderr "wrong number of coordinates \"$value\" in \"$path\"" |
---|
199 | return "" |
---|
200 | } |
---|
201 | foreach coord $value { |
---|
202 | set v [ConvertUnits $coord] |
---|
203 | if { $v == "" } { |
---|
204 | puts stderr "can't convert units \"$value\" of \"$path\"" |
---|
205 | return "" |
---|
206 | } |
---|
207 | lappend coords $v |
---|
208 | } |
---|
209 | upvar $varName corner |
---|
210 | set corner $coords |
---|
211 | } |
---|
212 | |
---|
213 | itcl::body Rappture::FlowHints::GetBoolean { obj path varName } { |
---|
214 | set value [$obj get $path] |
---|
215 | if { [string is boolean $value] } { |
---|
216 | upvar $varName bool |
---|
217 | set bool [expr $value ? 1 : 0] |
---|
218 | return |
---|
219 | } |
---|
220 | puts stderr "invalid boolean \"$value\" in \"$path\"" |
---|
221 | } |
---|
222 | |
---|
223 | itcl::body Rappture::FlowHints::GetSize { obj path varName } { |
---|
224 | set string [$obj get $path] |
---|
225 | if { [scan $string "%d" value] != 1 || $value < 0 } { |
---|
226 | puts stderr "can't get size \"$string\" of \"$path\"" |
---|
227 | return |
---|
228 | } |
---|
229 | upvar $varName size |
---|
230 | set size $value |
---|
231 | } |
---|