|
1 /* |
|
2 TUIO Java backend - part of the reacTIVision project |
|
3 http://reactivision.sourceforge.net/ |
|
4 |
|
5 Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu> |
|
6 |
|
7 This program is free software; you can redistribute it and/or modify |
|
8 it under the terms of the GNU General Public License as published by |
|
9 the Free Software Foundation; either version 2 of the License, or |
|
10 (at your option) any later version. |
|
11 |
|
12 This program is distributed in the hope that it will be useful, |
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 GNU General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with this program; if not, write to the Free Software |
|
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
20 */ |
|
21 package TUIO; |
|
22 |
|
23 import java.util.*; |
|
24 |
|
25 /** |
|
26 * The abstract TuioContainer class defines common attributes that apply to both subclasses {@link TuioObject} and {@link TuioCursor}. |
|
27 * |
|
28 * @author Martin Kaltenbrunner |
|
29 * @version 1.4 |
|
30 */ |
|
31 abstract class TuioContainer extends TuioPoint { |
|
32 |
|
33 /** |
|
34 * The unique session ID number that is assigned to each TUIO object or cursor. |
|
35 */ |
|
36 protected long session_id; |
|
37 /** |
|
38 * The X-axis velocity value. |
|
39 */ |
|
40 protected float x_speed; |
|
41 /** |
|
42 * The Y-axis velocity value. |
|
43 */ |
|
44 protected float y_speed; |
|
45 /** |
|
46 * The motion speed value. |
|
47 */ |
|
48 protected float motion_speed; |
|
49 /** |
|
50 * The motion acceleration value. |
|
51 */ |
|
52 protected float motion_accel; |
|
53 /** |
|
54 * A Vector of TuioPoints containing all the previous positions of the TUIO component. |
|
55 */ |
|
56 protected Vector<TuioPoint> path; |
|
57 /** |
|
58 * Defines the ADDED state. |
|
59 */ |
|
60 public static final int TUIO_ADDED = 0; |
|
61 /** |
|
62 * Defines the ACCELERATING state. |
|
63 */ |
|
64 public static final int TUIO_ACCELERATING = 1; |
|
65 /** |
|
66 * Defines the DECELERATING state. |
|
67 */ |
|
68 public static final int TUIO_DECELERATING = 2; |
|
69 /** |
|
70 * Defines the STOPPED state. |
|
71 */ |
|
72 public static final int TUIO_STOPPED = 3; |
|
73 /** |
|
74 * Defines the REMOVED state. |
|
75 */ |
|
76 public static final int TUIO_REMOVED = 4; |
|
77 /** |
|
78 * Reflects the current state of the TuioComponent |
|
79 */ |
|
80 protected int state; |
|
81 |
|
82 /** |
|
83 * This constructor takes a TuioTime argument and assigns it along with the provided |
|
84 * Session ID, X and Y coordinate to the newly created TuioContainer. |
|
85 * |
|
86 * @param ttime the TuioTime to assign |
|
87 * @param si the Session ID to assign |
|
88 * @param xp the X coordinate to assign |
|
89 * @param yp the Y coordinate to assign |
|
90 */ |
|
91 TuioContainer(TuioTime ttime, long si, float xp, float yp) { |
|
92 super(ttime,xp,yp); |
|
93 |
|
94 session_id = si; |
|
95 x_speed = 0.0f; |
|
96 y_speed = 0.0f; |
|
97 motion_speed = 0.0f; |
|
98 motion_accel = 0.0f; |
|
99 |
|
100 path = new Vector<TuioPoint>(); |
|
101 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
102 state = TUIO_ADDED; |
|
103 } |
|
104 |
|
105 /** |
|
106 * This constructor takes the provided Session ID, X and Y coordinate |
|
107 * and assigs these values to the newly created TuioContainer. |
|
108 * |
|
109 * @param si the Session ID to assign |
|
110 * @param xp the X coordinate to assign |
|
111 * @param yp the Y coordinate to assign |
|
112 */ |
|
113 TuioContainer(long si, float xp, float yp) { |
|
114 super(xp,yp); |
|
115 |
|
116 session_id = si; |
|
117 x_speed = 0.0f; |
|
118 y_speed = 0.0f; |
|
119 motion_speed = 0.0f; |
|
120 motion_accel = 0.0f; |
|
121 |
|
122 path = new Vector<TuioPoint>(); |
|
123 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
124 state = TUIO_ADDED; |
|
125 } |
|
126 |
|
127 /** |
|
128 * This constructor takes the atttibutes of the provided TuioContainer |
|
129 * and assigs these values to the newly created TuioContainer. |
|
130 * |
|
131 * @param tcon the TuioContainer to assign |
|
132 */ |
|
133 TuioContainer(TuioContainer tcon) { |
|
134 super(tcon); |
|
135 |
|
136 session_id = tcon.getSessionID(); |
|
137 x_speed = 0.0f; |
|
138 y_speed = 0.0f; |
|
139 motion_speed = 0.0f; |
|
140 motion_accel = 0.0f; |
|
141 |
|
142 path = new Vector<TuioPoint>(); |
|
143 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
144 state = TUIO_ADDED; |
|
145 } |
|
146 |
|
147 /** |
|
148 * Takes a TuioTime argument and assigns it along with the provided |
|
149 * X and Y coordinate to the private TuioContainer attributes. |
|
150 * The speed and accleration values are calculated accordingly. |
|
151 * |
|
152 * @param ttime the TuioTime to assign |
|
153 * @param xp the X coordinate to assign |
|
154 * @param yp the Y coordinate to assign |
|
155 */ |
|
156 public void update(TuioTime ttime, float xp, float yp) { |
|
157 TuioPoint lastPoint = path.lastElement(); |
|
158 super.update(ttime,xp,yp); |
|
159 |
|
160 TuioTime diffTime = currentTime.subtract(lastPoint.getTuioTime()); |
|
161 float dt = diffTime.getTotalMilliseconds()/1000.0f; |
|
162 float dx = this.xpos - lastPoint.getX(); |
|
163 float dy = this.ypos - lastPoint.getY(); |
|
164 float dist = (float)Math.sqrt(dx*dx+dy*dy); |
|
165 float last_motion_speed = this.motion_speed; |
|
166 |
|
167 this.x_speed = dx/dt; |
|
168 this.y_speed = dy/dt; |
|
169 this.motion_speed = dist/dt; |
|
170 this.motion_accel = (motion_speed - last_motion_speed)/dt; |
|
171 |
|
172 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
173 if (motion_accel>0) state = TUIO_ACCELERATING; |
|
174 else if (motion_accel<0) state = TUIO_DECELERATING; |
|
175 else state = TUIO_STOPPED; |
|
176 } |
|
177 |
|
178 /** |
|
179 * This method is used to calculate the speed and acceleration values of |
|
180 * TuioContainers with unchanged positions. |
|
181 */ |
|
182 public void stop(TuioTime ttime) { |
|
183 update(ttime,xpos,ypos); |
|
184 } |
|
185 |
|
186 /** |
|
187 * Takes a TuioTime argument and assigns it along with the provided |
|
188 * X and Y coordinate, X and Y velocity and acceleration |
|
189 * to the private TuioContainer attributes. |
|
190 * |
|
191 * @param ttime the TuioTime to assign |
|
192 * @param xp the X coordinate to assign |
|
193 * @param yp the Y coordinate to assign |
|
194 * @param xs the X velocity to assign |
|
195 * @param ys the Y velocity to assign |
|
196 * @param ma the acceleration to assign |
|
197 */ |
|
198 public void update(TuioTime ttime, float xp, float yp , float xs, float ys, float ma) { |
|
199 super.update(ttime,xp,yp); |
|
200 x_speed = xs; |
|
201 y_speed = ys; |
|
202 motion_speed = (float)Math.sqrt(x_speed*x_speed+y_speed*y_speed); |
|
203 motion_accel = ma; |
|
204 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
205 if (motion_accel>0) state = TUIO_ACCELERATING; |
|
206 else if (motion_accel<0) state = TUIO_DECELERATING; |
|
207 else state = TUIO_STOPPED; |
|
208 } |
|
209 |
|
210 /** |
|
211 * Assigns the provided X and Y coordinate, X and Y velocity and acceleration |
|
212 * to the private TuioContainer attributes. The TuioTime time stamp remains unchanged. |
|
213 * |
|
214 * @param xp the X coordinate to assign |
|
215 * @param yp the Y coordinate to assign |
|
216 * @param xs the X velocity to assign |
|
217 * @param ys the Y velocity to assign |
|
218 * @param ma the acceleration to assign |
|
219 */ |
|
220 public void update(float xp, float yp,float xs,float ys,float ma) { |
|
221 super.update(xp,yp); |
|
222 x_speed = xs; |
|
223 y_speed = ys; |
|
224 motion_speed = (float)Math.sqrt(x_speed*x_speed+y_speed*y_speed); |
|
225 motion_accel = ma; |
|
226 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
227 if (motion_accel>0) state = TUIO_ACCELERATING; |
|
228 else if (motion_accel<0) state = TUIO_DECELERATING; |
|
229 else state = TUIO_STOPPED; |
|
230 } |
|
231 |
|
232 /** |
|
233 * Takes the atttibutes of the provided TuioContainer |
|
234 * and assigs these values to this TuioContainer. |
|
235 * The TuioTime time stamp of this TuioContainer remains unchanged. |
|
236 * |
|
237 * @param tcon the TuioContainer to assign |
|
238 */ |
|
239 public void update (TuioContainer tcon) { |
|
240 super.update(tcon); |
|
241 x_speed = tcon.getXSpeed(); |
|
242 y_speed = tcon.getYSpeed(); |
|
243 motion_speed = tcon.getMotionSpeed(); |
|
244 motion_accel = tcon.getMotionAccel(); |
|
245 path.addElement(new TuioPoint(currentTime,xpos,ypos)); |
|
246 if (motion_accel>0) state = TUIO_ACCELERATING; |
|
247 else if (motion_accel<0) state = TUIO_DECELERATING; |
|
248 else state = TUIO_STOPPED; |
|
249 } |
|
250 |
|
251 /** |
|
252 * Assigns the REMOVE state to this TuioContainer and sets |
|
253 * its TuioTime time stamp to the provided TuioTime argument. |
|
254 * |
|
255 * @param ttime the TuioTime to assign |
|
256 */ |
|
257 public void remove(TuioTime ttime) { |
|
258 currentTime = new TuioTime(ttime); |
|
259 state = TUIO_REMOVED; |
|
260 } |
|
261 |
|
262 /** |
|
263 * Returns the Session ID of this TuioContainer. |
|
264 * @return the Session ID of this TuioContainer |
|
265 */ |
|
266 public long getSessionID() { |
|
267 return session_id; |
|
268 } |
|
269 |
|
270 /** |
|
271 * Returns the X velocity of this TuioContainer. |
|
272 * @return the X velocity of this TuioContainer |
|
273 */ |
|
274 public float getXSpeed() { |
|
275 return x_speed; |
|
276 } |
|
277 |
|
278 /** |
|
279 * Returns the Y velocity of this TuioContainer. |
|
280 * @return the Y velocity of this TuioContainer |
|
281 */ |
|
282 public float getYSpeed() { |
|
283 return y_speed; |
|
284 } |
|
285 |
|
286 /** |
|
287 * Returns the position of this TuioContainer. |
|
288 * @return the position of this TuioContainer |
|
289 */ |
|
290 public TuioPoint getPosition() { |
|
291 return new TuioPoint(xpos,ypos); |
|
292 } |
|
293 |
|
294 /** |
|
295 * Returns the path of this TuioContainer. |
|
296 * @return the path of this TuioContainer |
|
297 */ |
|
298 public Vector<TuioPoint> getPath() { |
|
299 return path; |
|
300 } |
|
301 |
|
302 /** |
|
303 * Returns the motion speed of this TuioContainer. |
|
304 * @return the motion speed of this TuioContainer |
|
305 */ |
|
306 public float getMotionSpeed() { |
|
307 return motion_speed; |
|
308 } |
|
309 |
|
310 /** |
|
311 * Returns the motion acceleration of this TuioContainer. |
|
312 * @return the motion acceleration of this TuioContainer |
|
313 */ |
|
314 public float getMotionAccel() { |
|
315 return motion_accel; |
|
316 } |
|
317 |
|
318 /** |
|
319 * Returns the TUIO state of this TuioContainer. |
|
320 * @return the TUIO state of this TuioContainer |
|
321 */ |
|
322 public int getTuioState() { |
|
323 return state; |
|
324 } |
|
325 |
|
326 /** |
|
327 * Returns true of this TuioContainer is moving. |
|
328 * @return true of this TuioContainer is moving |
|
329 */ |
|
330 public boolean isMoving() { |
|
331 if ((state==TUIO_ACCELERATING) || (state==TUIO_DECELERATING)) return true; |
|
332 else return false; |
|
333 } |
|
334 |
|
335 } |