View Javadoc

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