1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: videoparticle - mark a particle on a video frame |
---|
3 | # |
---|
4 | # ====================================================================== |
---|
5 | # AUTHOR: Derrick Kearney, Purdue University |
---|
6 | # Copyright (c) 2005-2010 Purdue Research Foundation |
---|
7 | # |
---|
8 | # See the file "license.terms" for information on usage and redistribution of |
---|
9 | # this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
10 | # ====================================================================== |
---|
11 | |
---|
12 | package require Itk |
---|
13 | package require BLT |
---|
14 | package require Img |
---|
15 | package require Rappture |
---|
16 | package require RapptureGUI |
---|
17 | |
---|
18 | itcl::class Rappture::VideoParticle { |
---|
19 | inherit itk::Widget |
---|
20 | |
---|
21 | itk_option define -halo halo Halo "10" |
---|
22 | itk_option define -name name Name "" |
---|
23 | itk_option define -color color Color "green" |
---|
24 | itk_option define -fncallback fncallback Fncallback "" |
---|
25 | itk_option define -trajcallback trajcallback Trajcallback "" |
---|
26 | |
---|
27 | constructor { args } { |
---|
28 | # defined below |
---|
29 | } |
---|
30 | destructor { |
---|
31 | # defined below |
---|
32 | } |
---|
33 | |
---|
34 | public method Add {args} |
---|
35 | public method Delete {args} |
---|
36 | public method Show {args} |
---|
37 | public method Hide {args} |
---|
38 | public method Link {args} |
---|
39 | public method Coords {} |
---|
40 | public method Frame {} |
---|
41 | |
---|
42 | public variable fncallback "" ;# framenumber callback - tells what frame we are on |
---|
43 | public variable trajcallback "" ;# trajectory callback - calculates and draws trajectory |
---|
44 | |
---|
45 | public method Move {status x y} |
---|
46 | public method Menu {args} |
---|
47 | |
---|
48 | public method drawVectors {args} |
---|
49 | public method next {args} |
---|
50 | public method prev {args} |
---|
51 | |
---|
52 | private variable _canvas "" ;# canvas which owns the particle |
---|
53 | private variable _name "" ;# id of the particle |
---|
54 | private variable _color "" ;# color of the particle |
---|
55 | private variable _framexy "" ;# list of frame numbers and coords |
---|
56 | ;# where the particle lives |
---|
57 | private variable _halo 0 ;# about the diameter of the particle |
---|
58 | private variable _menu "" ;# particle controls balloon widget |
---|
59 | private variable _nextnode "" ;# particle this particle points to |
---|
60 | private variable _prevnode "" ;# particle this particle is pointed to by |
---|
61 | private variable _link "" ;# tag of vector linking this and nextnode |
---|
62 | } |
---|
63 | |
---|
64 | itk::usual VideoParticle { |
---|
65 | keep -background -foreground -cursor -font |
---|
66 | keep -plotbackground -plotforeground |
---|
67 | } |
---|
68 | |
---|
69 | # ---------------------------------------------------------------------- |
---|
70 | # CONSTRUCTOR |
---|
71 | # ---------------------------------------------------------------------- |
---|
72 | itcl::body Rappture::VideoParticle::constructor {win args} { |
---|
73 | |
---|
74 | # setup the particle control menu |
---|
75 | set _menu $itk_component(hull).particlecontrols |
---|
76 | Rappture::Balloon ${_menu} -title "Particle Controls" |
---|
77 | set controls [${_menu} component inner] |
---|
78 | |
---|
79 | # Link control |
---|
80 | #button $controls.link -text Link \ |
---|
81 | # -relief flat -pady 0 -padx 0 -font "Arial 9" \ |
---|
82 | # -command [itcl::code $this Link] -overrelief flat \ |
---|
83 | # -activebackground grey90 |
---|
84 | |
---|
85 | # Delete control |
---|
86 | button $controls.delete -text Delete \ |
---|
87 | -relief flat -pady 0 -padx 0 -font "Arial 9" \ |
---|
88 | -command [itcl::code $this Delete frame] -overrelief flat \ |
---|
89 | -activebackground grey90 |
---|
90 | |
---|
91 | #grid $controls.link -column 0 -row 0 -sticky w |
---|
92 | grid $controls.delete -column 0 -row 1 -sticky w |
---|
93 | # grid $controls.rename -column 0 -row 2 -sticky w |
---|
94 | # grid $controls.recolor -column 0 -row 3 -sticky w |
---|
95 | |
---|
96 | grid columnconfigure $controls 0 -weight 1 |
---|
97 | |
---|
98 | set _canvas $win |
---|
99 | |
---|
100 | # finish configuring the particle |
---|
101 | eval itk_initialize $args |
---|
102 | } |
---|
103 | |
---|
104 | # ---------------------------------------------------------------------- |
---|
105 | # DESTRUCTOR |
---|
106 | # ---------------------------------------------------------------------- |
---|
107 | itcl::body Rappture::VideoParticle::destructor {} { |
---|
108 | } |
---|
109 | |
---|
110 | # ---------------------------------------------------------------------- |
---|
111 | # Add - add attributes to the particle |
---|
112 | # frame <frameNum> <x> <y> - add a frame and location |
---|
113 | # ---------------------------------------------------------------------- |
---|
114 | itcl::body Rappture::VideoParticle::Add {args} { |
---|
115 | set option [lindex $args 0] |
---|
116 | switch -- $option { |
---|
117 | "frame" { |
---|
118 | if {[llength $args] == 4} { |
---|
119 | foreach { frNum x y } [lrange $args 1 end] break |
---|
120 | if {([string is integer $frNum] != 1)} { |
---|
121 | error "bad value: \"$frNum\": frame number should be an integer" |
---|
122 | } |
---|
123 | if {([string is double $x] != 1)} { |
---|
124 | error "bad value: \"$frNum\": x coordinate should be a number" |
---|
125 | } |
---|
126 | if {([string is double $y] != 1)} { |
---|
127 | error "bad value: \"$frNum\": y coordinate should be a number" |
---|
128 | } |
---|
129 | # if the particle is alread in the frame, |
---|
130 | # update it's x,y coords. othrewise add it. |
---|
131 | set idx [lsearch ${_framexy} $frNum] |
---|
132 | if {$idx == -1} { |
---|
133 | lappend _framexy $frNum [list $x $y] |
---|
134 | } else { |
---|
135 | incr idx |
---|
136 | set _framexy [lreplace ${_framexy} $idx $idx [list $x $y]] |
---|
137 | } |
---|
138 | } else { |
---|
139 | error "wrong # args: should be \"frame <frameNumber> <x> <y>\"" |
---|
140 | } |
---|
141 | } |
---|
142 | default { |
---|
143 | error "bad option \"$option\": should be frame." |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | # ---------------------------------------------------------------------- |
---|
149 | # Delete - remove the particle |
---|
150 | # ---------------------------------------------------------------------- |
---|
151 | itcl::body Rappture::VideoParticle::Delete {args} { |
---|
152 | |
---|
153 | Menu deactivate |
---|
154 | |
---|
155 | set _framexy "" |
---|
156 | Hide particle |
---|
157 | |
---|
158 | # delete the vectors originating from this particle |
---|
159 | if {[string compare "" ${_nextnode}] != 0} { |
---|
160 | ${_canvas} delete ${_link} |
---|
161 | ${_nextnode} prev ${_prevnode} |
---|
162 | } |
---|
163 | |
---|
164 | # delete the vectors pointing to this particle |
---|
165 | if {[string compare "" ${_prevnode}] != 0} { |
---|
166 | ${_prevnode} next ${_nextnode} |
---|
167 | ${_prevnode} drawVectors |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | # ---------------------------------------------------------------------- |
---|
172 | # Show - draw the particle |
---|
173 | # particle - draw the particle on the canvas |
---|
174 | # name - popup a ballon with the name of this object |
---|
175 | # ---------------------------------------------------------------------- |
---|
176 | itcl::body Rappture::VideoParticle::Show {args} { |
---|
177 | set option [lindex $args 0] |
---|
178 | switch -- $option { |
---|
179 | "particle" { |
---|
180 | foreach {x y} [lindex ${_framexy} 1] break |
---|
181 | set coords [list [expr $x-${_halo}] [expr $y-${_halo}] \ |
---|
182 | [expr $x+${_halo}] [expr $y+${_halo}]] |
---|
183 | ${_canvas} create oval $coords \ |
---|
184 | -fill ${_color} \ |
---|
185 | -width 0 \ |
---|
186 | -tags "particle ${_name}" |
---|
187 | } |
---|
188 | name { |
---|
189 | |
---|
190 | } |
---|
191 | default { |
---|
192 | error "bad option \"$option\": should be \"frame <frameNumber>\"." |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | # ---------------------------------------------------------------------- |
---|
198 | # Hide |
---|
199 | # particle - remove the particle from where it is drawn |
---|
200 | # name - remove the popup with the name |
---|
201 | # ---------------------------------------------------------------------- |
---|
202 | itcl::body Rappture::VideoParticle::Hide {args} { |
---|
203 | set option [lindex $args 0] |
---|
204 | switch -- $option { |
---|
205 | "particle" { |
---|
206 | if {[llength $args] != 1} { |
---|
207 | error "wrong # args: should be \"particle\"" |
---|
208 | } |
---|
209 | puts "hiding ${_name}" |
---|
210 | ${_canvas} delete "${_name}" |
---|
211 | } |
---|
212 | name { |
---|
213 | |
---|
214 | } |
---|
215 | default { |
---|
216 | error "bad option \"$option\": should be \"particle or name\"." |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | # ---------------------------------------------------------------------- |
---|
222 | # Move - move the particle to a new location |
---|
223 | # ---------------------------------------------------------------------- |
---|
224 | itcl::body Rappture::VideoParticle::Move {status x y} { |
---|
225 | switch -- $status { |
---|
226 | "press" { |
---|
227 | } |
---|
228 | "motion" { |
---|
229 | } |
---|
230 | "release" { |
---|
231 | } |
---|
232 | default { |
---|
233 | error "bad option \"$option\": should be one of press, motion, release." |
---|
234 | } |
---|
235 | } |
---|
236 | set coords [list [expr $x-${_halo}] [expr $y-${_halo}] \ |
---|
237 | [expr $x+${_halo}] [expr $y+${_halo}]] |
---|
238 | eval ${_canvas} coords ${_name} $coords |
---|
239 | set _framexy [lreplace ${_framexy} 1 1 [list $x $y]] |
---|
240 | drawVectors |
---|
241 | if {[string compare "" ${_prevnode}] != 0} { |
---|
242 | ${_prevnode} drawVectors |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | # ---------------------------------------------------------------------- |
---|
247 | # Menu - popup a menu with the particle controls |
---|
248 | # create |
---|
249 | # activate x y |
---|
250 | # deactivate |
---|
251 | # ---------------------------------------------------------------------- |
---|
252 | itcl::body Rappture::VideoParticle::Menu {args} { |
---|
253 | set option [lindex $args 0] |
---|
254 | switch -- $option { |
---|
255 | "activate" { |
---|
256 | if {[llength $args] != 3} { |
---|
257 | error "wrong # args: should be \"activate <x> <y>\"" |
---|
258 | } |
---|
259 | foreach {x y} [lrange $args 1 end] break |
---|
260 | set dir "left" |
---|
261 | set x0 [winfo rootx ${_canvas}] |
---|
262 | set y0 [winfo rooty ${_canvas}] |
---|
263 | set w0 [winfo width ${_canvas}] |
---|
264 | set h0 [winfo height ${_canvas}] |
---|
265 | set x [expr $x0+$x] |
---|
266 | set y [expr $y0+$y] |
---|
267 | ${_menu} activate @$x,$y $dir |
---|
268 | } |
---|
269 | "deactivate" { |
---|
270 | ${_menu} deactivate |
---|
271 | } |
---|
272 | default { |
---|
273 | error "bad option \"$option\": should be one of activate, deactivate." |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | } |
---|
279 | |
---|
280 | # ---------------------------------------------------------------------- |
---|
281 | # Link - move the particle to a new location |
---|
282 | # ---------------------------------------------------------------------- |
---|
283 | itcl::body Rappture::VideoParticle::Link {args} { |
---|
284 | # add a new particle list of linked particles |
---|
285 | foreach {p} $args break |
---|
286 | $p prev $this |
---|
287 | next $p |
---|
288 | drawVectors |
---|
289 | } |
---|
290 | |
---|
291 | # ---------------------------------------------------------------------- |
---|
292 | # drawVectors - draw vectors from this particle |
---|
293 | # to all particles it is linked to. |
---|
294 | # ---------------------------------------------------------------------- |
---|
295 | itcl::body Rappture::VideoParticle::drawVectors {args} { |
---|
296 | |
---|
297 | if {[string compare "" $trajcallback] != 0} { |
---|
298 | uplevel \#0 $trajcallback $this ${_nextnode} |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | # ---------------------------------------------------------------------- |
---|
304 | # Coords - return the x and y coordinates as a list |
---|
305 | # for the first frame this particle appears in |
---|
306 | # ---------------------------------------------------------------------- |
---|
307 | itcl::body Rappture::VideoParticle::Coords {} { |
---|
308 | return [lindex ${_framexy} 1] |
---|
309 | } |
---|
310 | |
---|
311 | # ---------------------------------------------------------------------- |
---|
312 | # Frame - return the frame this particle appears in |
---|
313 | # ---------------------------------------------------------------------- |
---|
314 | itcl::body Rappture::VideoParticle::Frame {} { |
---|
315 | return [lindex ${_framexy} 0] |
---|
316 | } |
---|
317 | |
---|
318 | # ---------------------------------------------------------------------- |
---|
319 | # next - get/set the next particle |
---|
320 | # ---------------------------------------------------------------------- |
---|
321 | itcl::body Rappture::VideoParticle::next {args} { |
---|
322 | if {[llength $args] == 1} { |
---|
323 | # set the next node |
---|
324 | set _nextnode [lindex $args 0] |
---|
325 | # drawVectors |
---|
326 | } |
---|
327 | return ${_nextnode} |
---|
328 | } |
---|
329 | |
---|
330 | # ---------------------------------------------------------------------- |
---|
331 | # prev - get/set the prev particle |
---|
332 | # ---------------------------------------------------------------------- |
---|
333 | itcl::body Rappture::VideoParticle::prev {args} { |
---|
334 | if {[llength $args] == 1} { |
---|
335 | # set the prev node |
---|
336 | set _prevnode [lindex $args 0] |
---|
337 | } |
---|
338 | return ${_prevnode} |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | # ---------------------------------------------------------------------- |
---|
343 | # CONFIGURATION OPTION: -halo |
---|
344 | # ---------------------------------------------------------------------- |
---|
345 | itcl::configbody Rappture::VideoParticle::halo { |
---|
346 | if {[string is double $itk_option(-halo)] == 1} { |
---|
347 | set _halo $itk_option(-halo) |
---|
348 | } else { |
---|
349 | error "bad value: \"$itk_option(-halo)\": halo should be a number" |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | # ---------------------------------------------------------------------- |
---|
354 | # CONFIGURATION OPTION: -name |
---|
355 | # ---------------------------------------------------------------------- |
---|
356 | itcl::configbody Rappture::VideoParticle::name { |
---|
357 | set _name $itk_option(-name) |
---|
358 | } |
---|
359 | |
---|
360 | # ---------------------------------------------------------------------- |
---|
361 | # CONFIGURATION OPTION: -color |
---|
362 | # ---------------------------------------------------------------------- |
---|
363 | itcl::configbody Rappture::VideoParticle::color { |
---|
364 | if {[string compare "" $itk_option(-color)] != 0} { |
---|
365 | # FIXME how to tell if the color is valid? |
---|
366 | set _color $itk_option(-color) |
---|
367 | } else { |
---|
368 | error "bad value: \"$itk_option(-color)\": should be a valid color" |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | # ---------------------------------------------------------------------- |
---|