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 org.ximtec.igesture.io.tuio.TuioTime;
24  import org.ximtec.igesture.io.tuio.interfaces.AbstractTuioObject;
25  
26  /**
27   * The TuioObject class encapsulates /tuio/3Dobj TUIO objects.
28   *
29   * @author Martin Kaltenbrunner, Bjorn Puype
30   * @version 1.4
31   */ 
32  public class TuioObject3D extends TuioContainer3D implements AbstractTuioObject {
33  	
34  	/** XY plane */
35  	public static final int XY = 0;
36  	/** XZ plane */
37  	public static final int XZ = 1;
38  	/** YZ plane */
39  	public static final int YZ = 2;
40  	
41  	/**
42  	 * The individual symbol ID number that is assigned to each TuioObject.
43  	 */ 
44  	protected int symbol_id;
45  	/**
46  	 * The rotation angle value in XY plane.
47  	 */ 
48  	protected float angle_XY;
49  	/**
50  	 * The rotation angle value in XZ plane.
51  	 */ 
52  	protected float angle_XZ;
53  	/**
54  	 * The rotation angle value in YZ plane.
55  	 */ 
56  	protected float angle_YZ;
57  	/**
58  	 * The rotation speed value.
59  	 */ 
60  	protected float rotation_speed_XY;
61  	protected float rotation_speed_XZ;
62  	protected float rotation_speed_YZ;
63  	/**
64  	 * The rotation acceleration value.
65  	 */ 
66  	protected float rotation_accel;
67  	/**
68  	 * Defines the ROTATING state.
69  	 */ 
70  	public static final int TUIO_ROTATING = 5;
71  	
72  	/**
73  	 * This constructor takes a TuioTime argument and assigns it along with the provided 
74  	 * Session ID, Symbol ID, X, Y and Z coordinate and angles between the different planes to the newly created TuioObject3D.
75  	 *
76  	 * @param	ttime	the TuioTime to assign
77  	 * @param	si	the Session ID  to assign
78  	 * @param	sym	the Symbol ID  to assign
79  	 * @param	xp	the X coordinate to assign
80  	 * @param	yp	the Y coordinate to assign
81  	 * @param	zp	the Z coordinate to assign
82  	 * @param	a	the angle of the XY plane to assign
83  	 * @param	b	the angle of the XZ plane to assign
84  	 * @param	c	the angle of the YZ plane to assign
85  	 */
86  	public TuioObject3D (TuioTime ttime, long si, int sym, float xp, float yp, float zp, float a, float b, float c) {
87  		super(ttime, si,xp,yp,zp);
88  		symbol_id = sym;
89  		angle_XY = a;
90  		angle_XZ = b;
91  		angle_YZ = c;
92  		rotation_speed_XY = 0.0f;
93  		rotation_speed_XZ = 0.0f;
94  		rotation_speed_YZ = 0.0f;
95  		rotation_accel = 0.0f;
96  	}
97  
98  	/**
99  	 * This constructor takes the provided Session ID, Symbol ID, X, Y and Z coordinate 
100 	 * and angles between the different planes, and assigns these values to the newly created TuioObject3D.
101 	 *
102 	 * @param	si	the Session ID  to assign
103 	 * @param	sym	the Symbol ID  to assign
104 	 * @param	xp	the X coordinate to assign
105 	 * @param	yp	the Y coordinate to assign
106 	 * @param	zp	the Z coordinate to assign
107 	 * @param	a	the angle of the XY plane to assign
108 	 * @param	b	the angle of the XZ plane to assign
109 	 * @param	c	the angle of the YZ plane to assign
110 	 */	
111 	public TuioObject3D (long si, int sym, float xp, float yp, float zp, float a, float b, float c) {
112 		super(si,xp,yp,zp);
113 		symbol_id = sym;
114 		angle_XY = a;
115 		angle_XZ = b;
116 		angle_YZ = c;
117 		rotation_speed_XY = 0.0f;
118 		rotation_speed_XZ = 0.0f;
119 		rotation_speed_YZ = 0.0f;
120 		rotation_accel = 0.0f;
121 	}
122 
123 	/**
124 	 * This constructor takes the attributes of the provided TuioObject3D 
125 	 * and assigns these values to the newly created TuioObject3D.
126 	 *
127 	 * @param	tobj	the TuioObject3D to assign
128 	 */
129 	public TuioObject3D (TuioObject3D tobj) {
130 		super(tobj);
131 		symbol_id = tobj.getSymbolID();
132 		angle_XY = tobj.getAngle(XY);
133 		angle_XZ = tobj.getAngle(XZ);
134 		angle_YZ = tobj.getAngle(YZ);
135 		rotation_speed_XY = 0.0f;
136 		rotation_speed_XZ = 0.0f;
137 		rotation_speed_YZ = 0.0f;		
138 		rotation_accel = 0.0f;
139 	}
140 
141 	/**
142 	 * Takes a TuioTime argument and assigns it along with the provided 
143 	 * X, Y and Z coordinate, angle, X, Y and Z velocity, motion acceleration,
144 	 * rotation speed and rotation acceleration to the private TuioObject3D attributes.
145 	 *
146 	 * @param	ttime	the TuioTime to assign
147 	 * @param	xp	the X coordinate to assign
148 	 * @param	yp	the Y coordinate to assign
149 	 * @param	zp	the Z coordinate to assign
150 	 * @param	a	the angle coordinate to assign (XY plane)
151 	 * @param	b	the angle coordinate to assign (XZ plane)
152 	 * @param	c	the angle coordinate to assign (YZ plane)
153 	 * @param	xs	the X velocity to assign
154 	 * @param	ys	the Y velocity to assign
155 	 * @param	zs	the Z velocity to assign
156 	 * @param	rsXY	the rotation velocity to assign in the XY plane
157 	 * @param	rsXZ	the rotation velocity to assign in the XZ plane
158 	 * @param	rsYZ	the rotation velocity to assign in the YZ plane
159 	 * @param	ma	the motion acceleration to assign
160 	 * @param	ra	the rotation acceleration to assign
161 	 */
162 	public void update (TuioTime ttime, float xp, float yp, float zp, float a, float b, float c, float xs, float ys, float zs, float rsXY, float rsXZ, float rsYZ, float ma, float ra) {
163 		super.update(ttime,xp,yp,zp,xs,ys,zs,ma);
164 		angle_XY = a;
165 		angle_XZ = b;
166 		angle_YZ = c;
167 		rotation_speed_XY = rsXY;
168 		rotation_speed_XZ = rsXZ;
169 		rotation_speed_YZ = rsYZ;
170 		rotation_accel = ra;
171 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
172 	}
173 
174 	/**
175 	 * Assigns the provided X, Y and Z coordinate, angle, X, Y and Z velocity, motion acceleration
176 	 * rotation velocity and rotation acceleration to the private TuioContainer3D attributes.
177 	 * The TuioTime time stamp remains unchanged.
178 	 *
179 	 * @param	xp	the X coordinate to assign
180 	 * @param	yp	the Y coordinate to assign
181 	 * @param	zp	the Z coordinate to assign
182 	 * @param	a	the angle coordinate to assign (XY plane)
183 	 * @param	b	the angle coordinate to assign (XZ plane)
184 	 * @param	c	the angle coordinate to assign (YZ plane)
185 	 * @param	xs	the X velocity to assign
186 	 * @param	ys	the Y velocity to assign
187 	 * @param	zs	the Z velocity to assign
188 	 * @param	rsXY	the rotation velocity to assign in the XY plane
189 	 * @param	rsXZ	the rotation velocity to assign in the XZ plane
190 	 * @param	rsYZ	the rotation velocity to assign in the YZ plane
191 	 * @param	ma	the motion acceleration to assign
192 	 * @param	ra	the rotation acceleration to assign
193 	 */
194 	public void update (float xp, float yp, float zp, float a, float b, float c, float xs, float ys, float zs, float rsXY, float rsXZ, float rsYZ, float ma, float ra) {
195 		super.update(xp,yp,zp,xs,ys,zs,ma);
196 		angle_XY = a;
197 		angle_XZ = b;
198 		angle_YZ = c;
199 		rotation_speed_XY = rsXY;
200 		rotation_speed_XZ = rsXZ;
201 		rotation_speed_YZ = rsYZ;
202 		rotation_accel = ra;
203 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
204 	}
205 	
206 	/**
207 	 * Takes a TuioTime argument and assigns it along with the provided 
208 	 * X, Y and Z coordinate and angles to the private TuioObject3D attributes.
209 	 * The speed and acceleration values are calculated accordingly.
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	a	the angle coordinate to assign for XY plane
216 	 * @param	b	the angle coordinate to assign for XZ plane
217 	 * @param	c	the angle coordinate to assign for YZ plane
218 	 */
219 	public void update (TuioTime ttime, float xp, float yp, float zp, float a, float b, float c) {
220 		TuioPoint3D lastPoint = path.lastElement();
221 		super.update(ttime,xp,yp,zp);
222 		TuioTime diffTime = currentTime.subtract(lastPoint.getTuioTime());
223 		float dt = diffTime.getTotalMilliseconds()/1000.0f;
224 		//FIXME
225 		float last_angle_XY = angle_XY;
226 		float last_angle_XZ = angle_XZ;
227 		float last_angle_YZ = angle_YZ;
228 		float last_rotation_speed_XY = rotation_speed_XY;
229 		float last_rotation_speed_XZ = rotation_speed_XZ;
230 		float last_rotation_speed_YZ = rotation_speed_YZ;
231 		angle_XY = a;
232 		angle_XZ = b;
233 		angle_YZ = c;
234 		
235 		float da = (this.angle_XY-last_angle_XY)/(2.0f*(float)Math.PI);
236 		if (da>0.75f) da-=1.0f;
237 		else if (da<-0.75f) da+=1.0f;
238 		float db = (this.angle_XZ-last_angle_XZ)/(2.0f*(float)Math.PI);
239 		if (db>0.75f) db-=1.0f;
240 		else if (db<-0.75f) db+=1.0f;
241 		float dc = (this.angle_YZ-last_angle_YZ)/(2.0f*(float)Math.PI);
242 		if (dc>0.75f) dc-=1.0f;
243 		else if (dc<-0.75f) dc+=1.0f;
244 		
245 		float last_rotation_speed = (float) Math.sqrt(last_rotation_speed_XY*last_rotation_speed_XY+last_rotation_speed_XZ*last_rotation_speed_XZ+last_rotation_speed_YZ*last_rotation_speed_YZ);
246 		rotation_speed_XY = da/dt;
247 		rotation_speed_XZ = db/dt;
248 		rotation_speed_YZ = dc/dt;
249 		float rotation_speed = (float)Math.sqrt(rotation_speed_XY*rotation_speed_XY+rotation_speed_XZ*rotation_speed_XZ+rotation_speed_YZ*rotation_speed_YZ);
250 		//end FIXME
251 		rotation_accel = (rotation_speed - last_rotation_speed)/dt;
252 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
253 	}
254 	
255 	/**
256 	 * Takes the attributes of the provided TuioObject3D 
257 	 * and assigns these values to this TuioObject3D.
258 	 * The TuioTime time stamp of this TuioContainer3D remains unchanged.
259 	 *
260 	 * @param	tobj	the TuioContainer3D to assign
261 	 */	
262 	public void update (TuioObject3D tobj) {
263 		super.update(tobj);
264 		angle_XY = tobj.getAngle(TuioObject3D.XY);
265 		angle_XZ = tobj.getAngle(TuioObject3D.XZ);
266 		angle_YZ = tobj.getAngle(TuioObject3D.YZ);
267 		rotation_speed_XY = tobj.getRotationSpeedXY();
268 		rotation_speed_XZ = tobj.getRotationSpeedXZ();
269 		rotation_speed_YZ = tobj.getRotationSpeedYZ();
270 		rotation_accel = tobj.getRotationAccel();
271 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
272 	}
273 	
274 	/**
275 	 * This method is used to calculate the speed and acceleration values of a
276 	 * TuioObject3D with unchanged position and angle.
277 	 *
278 	 * @param	ttime	the TuioTime to assign
279 	 */
280 	public void stop (TuioTime ttime) {
281 		update(ttime,xpos,ypos,zpos,angle_XY,angle_XZ,angle_YZ);
282 	}
283 
284 	/**
285 	 * Returns the symbol ID of this TuioObject3D.
286 	 * @return	the symbol ID of this TuioObject3D
287 	 */
288 	public int getSymbolID() {
289 		return symbol_id;
290 	}
291 		
292 	/**
293 	 * Returns the requested rotation angle of this TuioObject3D.
294 	 * @param	planes	the planes of which to get the rotation angle.
295 	 * @return	the requested rotation angle of this TuioObject3D
296 	 */
297 	public float getAngle(int planes) {
298 		float angle = 0;
299 		switch(planes)
300 		{
301 		case XY:
302 			angle = angle_XY;
303 			break;
304 		case XZ:
305 			angle = angle_XZ;
306 			break;
307 		case YZ:
308 			angle = angle_YZ;
309 			break;
310 		default:
311 			System.err.println("Unknown entered planes");
312 		}
313 		return angle;
314 	}
315 
316 	/**
317 	 * Returns the rotation angle in degrees of this TuioObject3D.
318 	 * @param	planes	the planes of which to get the rotation angle. 
319 	 * @return	the rotation angle in degrees of this TuioObject3D
320 	 */
321 	public float getAngleDegrees(int planes) {
322 		return getAngle(planes)/(float)Math.PI*180.0f;
323 	}
324 	
325 	/**
326 	 * Returns the rotation speed of this TuioObject3D.
327 	 * @return	the rotation speed of this TuioObject3D
328 	 */
329 	public float getRotationSpeedXY() {
330 		return rotation_speed_XY;
331 	}
332 	public float getRotationSpeedXZ() {
333 		return rotation_speed_XZ;
334 	}
335 	public float getRotationSpeedYZ() {
336 		return rotation_speed_YZ;
337 	}
338 		
339 	/**
340 	 * Returns the rotation acceleration of this TuioObject3D.
341 	 * @return	the rotation acceleration of this TuioObject3D
342 	 */
343 	public float getRotationAccel() {
344 		return rotation_accel;
345 	}
346 	
347 	/**
348 	 * @return the symbol_id
349 	 */
350 	public int getSymbol_id() {
351 		return symbol_id;
352 	}
353 
354 	/**
355 	 * @return the angle_XY
356 	 */
357 	public float getAngleXY() {
358 		return angle_XY;
359 	}
360 
361 	/**
362 	 * @return the angle_XZ
363 	 */
364 	public float getAngleXZ() {
365 		return angle_XZ;
366 	}
367 
368 	/**
369 	 * @return the angle_YZ
370 	 */
371 	public float getAngleYZ() {
372 		return angle_YZ;
373 	}
374 
375 	/**
376 	 * Returns true of this TuioObject3D is moving.
377 	 * @return	true of this TuioObject3D is moving
378 	 */
379 	public boolean isMoving() { 
380 		if ((state==TUIO_ACCELERATING) || (state==TUIO_DECELERATING) || (state==TUIO_ROTATING)) return true;
381 		else return false;
382 	}
383 	
384 }