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 org.ximtec.igesture.io.tuio.TuioTime;
24  import org.ximtec.igesture.io.tuio.interfaces.AbstractTuioObject;
25  
26  /**
27   * The TuioObject class encapsulates /tuio/2Dobj TUIO objects.
28   *
29   * @author Martin Kaltenbrunner
30   * @version 1.4
31   */ 
32  public class TuioObject extends TuioContainer implements AbstractTuioObject{
33  	
34  	/**
35  	 * The individual symbol ID number that is assigned to each TuioObject.
36  	 */ 
37  	protected int symbol_id;
38  	/**
39  	 * The rotation angle value.
40  	 */ 
41  	protected float angle;
42  	/**
43  	 * The rotation speed value.
44  	 */ 
45  	protected float rotation_speed;
46  	/**
47  	 * The rotation acceleration value.
48  	 */ 
49  	protected float rotation_accel;
50  	/**
51  	 * Defines the ROTATING state.
52  	 */ 
53  	public static final int TUIO_ROTATING = 5;
54  	
55  	/**
56  	 * This constructor takes a TuioTime argument and assigns it along with the provided 
57  	 * Session ID, Symbol ID, X and Y coordinate and angle to the newly created TuioObject.
58  	 *
59  	 * @param	ttime	the TuioTime to assign
60  	 * @param	si	the Session ID  to assign
61  	 * @param	sym	the Symbol ID  to assign
62  	 * @param	xp	the X coordinate to assign
63  	 * @param	yp	the Y coordinate to assign
64  	 * @param	a	the angle to assign
65  	 */
66  	public TuioObject (TuioTime ttime, long si, int sym, float xp, float yp, float a) {
67  		super(ttime, si,xp,yp);
68  		symbol_id = sym;
69  		angle = a;
70  		rotation_speed = 0.0f;
71  		rotation_accel = 0.0f;
72  	}
73  
74  	/**
75  	 * This constructor takes the provided Session ID, Symbol ID, X and Y coordinate 
76  	 * and angle, and assigns these values to the newly created TuioObject.
77  	 *
78  	 * @param	si	the Session ID  to assign
79  	 * @param	sym	the Symbol ID  to assign
80  	 * @param	xp	the X coordinate to assign
81  	 * @param	yp	the Y coordinate to assign
82  	 * @param	a	the angle to assign
83  	 */	
84  	public TuioObject (long si, int sym, float xp, float yp, float a) {
85  		super(si,xp,yp);
86  		symbol_id = sym;
87  		angle = a;
88  		rotation_speed = 0.0f;
89  		rotation_accel = 0.0f;
90  	}
91  
92  	/**
93  	 * This constructor takes the attributes of the provided TuioObject 
94  	 * and assigns these values to the newly created TuioObject.
95  	 *
96  	 * @param	tobj	the TuioObject to assign
97  	 */
98  	public TuioObject (TuioObject tobj) {
99  		super(tobj);
100 		symbol_id = tobj.getSymbolID();
101 		angle = tobj.getAngle();
102 		rotation_speed = 0.0f;
103 		rotation_accel = 0.0f;
104 	}
105 
106 	/**
107 	 * Takes a TuioTime argument and assigns it along with the provided 
108 	 * X and Y coordinate, angle, X and Y velocity, motion acceleration,
109 	 * rotation speed and rotation acceleration to the private TuioObject attributes.
110 	 *
111 	 * @param	ttime	the TuioTime to assign
112 	 * @param	xp	the X coordinate to assign
113 	 * @param	yp	the Y coordinate to assign
114 	 * @param	a	the angle coordinate to assign
115 	 * @param	xs	the X velocity to assign
116 	 * @param	ys	the Y velocity to assign
117 	 * @param	rs	the rotation velocity to assign
118 	 * @param	ma	the motion acceleration to assign
119 	 * @param	ra	the rotation acceleration to assign
120 	 */
121 	public void update (TuioTime ttime, float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra) {
122 		super.update(ttime,xp,yp,xs,ys,ma);
123 		angle = a;
124 		rotation_speed = rs;
125 		rotation_accel = ra;
126 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
127 	}
128 
129 	/**
130 	 * Assigns the provided X and Y coordinate, angle, X and Y velocity, motion acceleration
131 	 * rotation velocity and rotation acceleration to the private TuioContainer attributes.
132 	 * The TuioTime time stamp remains unchanged.
133 	 *
134 	 * @param	xp	the X coordinate to assign
135 	 * @param	yp	the Y coordinate to assign
136 	 * @param	a	the angle coordinate to assign
137 	 * @param	xs	the X velocity to assign
138 	 * @param	ys	the Y velocity to assign
139 	 * @param	rs	the rotation velocity to assign
140 	 * @param	ma	the motion acceleration to assign
141 	 * @param	ra	the rotation acceleration to assign
142 	 */
143 	public void update (float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra) {
144 		super.update(xp,yp,xs,ys,ma);
145 		angle = a;
146 		rotation_speed = rs;
147 		rotation_accel = ra;
148 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
149 	}
150 	
151 	/**
152 	 * Takes a TuioTime argument and assigns it along with the provided 
153 	 * X and Y coordinate and angle to the private TuioObject 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 	 * @param	a	the angle coordinate to assign
160 	 */
161 	public void update (TuioTime ttime, float xp, float yp, float a) {
162 		TuioPoint lastPoint = path.lastElement();
163 		super.update(ttime,xp,yp);
164 
165 		TuioTime diffTime = currentTime.subtract(lastPoint.getTuioTime());
166 		float dt = diffTime.getTotalMilliseconds()/1000.0f;
167 		float last_angle = angle;
168 		float last_rotation_speed = rotation_speed;
169 		angle = a;
170 		
171 		float da = (this.angle-last_angle)/(2.0f*(float)Math.PI);
172 		if (da>0.75f) da-=1.0f;
173 		else if (da<-0.75f) da+=1.0f;
174 		
175 		rotation_speed = da/dt;
176 		rotation_accel = (rotation_speed - last_rotation_speed)/dt;
177 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
178 	}
179 	
180 	/**
181 	 * Takes the attributes of the provided TuioObject 
182 	 * and assigns these values to this TuioObject.
183 	 * The TuioTime time stamp of this TuioContainer remains unchanged.
184 	 *
185 	 * @param	tobj	the TuioContainer to assign
186 	 */	
187 	public void update (TuioObject tobj) {
188 		super.update(tobj);
189 		angle = tobj.getAngle();
190 		rotation_speed = tobj.getRotationSpeed();
191 		rotation_accel = tobj.getRotationAccel();
192 		if ((rotation_accel!=0) && (state!=TUIO_STOPPED)) state = TUIO_ROTATING;
193 	}
194 	
195 	/**
196 	 * This method is used to calculate the speed and acceleration values of a
197 	 * TuioObject with unchanged position and angle.
198 	 *
199 	 * @param	ttime	the TuioTime to assign
200 	 */
201 	public void stop (TuioTime ttime) {
202 		update(ttime,xpos,ypos, angle);
203 	}
204 
205 	/**
206 	 * Returns the symbol ID of this TuioObject.
207 	 * @return	the symbol ID of this TuioObject
208 	 */
209 	public int getSymbolID() {
210 		return symbol_id;
211 	}
212 		
213 	/**
214 	 * Returns the rotation angle of this TuioObject.
215 	 * @return	the rotation angle of this TuioObject
216 	 */
217 	public float getAngle() {
218 		return angle;
219 	}
220 
221 	/**
222 	 * Returns the rotation angle in degrees of this TuioObject.
223 	 * @return	the rotation angle in degrees of this TuioObject
224 	 */
225 	public float getAngleDegrees() {
226 		return angle/(float)Math.PI*180.0f;
227 	}
228 	
229 	/**
230 	 * Returns the rotation speed of this TuioObject.
231 	 * @return	the rotation speed of this TuioObject
232 	 */
233 	public float getRotationSpeed() {
234 		return rotation_speed;
235 	}
236 		
237 	/**
238 	 * Returns the rotation acceleration of this TuioObject.
239 	 * @return	the rotation acceleration of this TuioObject
240 	 */
241 	public float getRotationAccel() {
242 		return rotation_accel;
243 	}
244 
245 	/**
246 	 * Returns true of this TuioObject is moving.
247 	 * @return	true of this TuioObject is moving
248 	 */
249 	public boolean isMoving() { 
250 		if ((state==TUIO_ACCELERATING) || (state==TUIO_DECELERATING) || (state==TUIO_ROTATING)) return true;
251 		else return false;
252 	}
253 	
254 }