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