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  
22  package org.ximtec.igesture.io.tuio.tuio2D;
23  
24  import org.ximtec.igesture.io.tuio.TuioTime;
25  import org.ximtec.igesture.io.tuio.interfaces.AbstractTuioPoint;
26  
27  /**
28   * The TuioPoint class on the one hand is a simple container and utility class to handle TUIO positions in general, 
29   * on the other hand the TuioPoint is the base class for the TuioCursor and TuioObject classes.
30   *
31   * @author Martin Kaltenbrunner
32   * @version 1.4
33   */ 
34  public class TuioPoint implements AbstractTuioPoint{
35  	
36  	/**
37  	 * X coordinate, representated as a floating point value in a range of 0..1  
38  	 */
39  	protected float xpos;
40  	/**
41  	 * Y coordinate, representated as a floating point value in a range of 0..1  
42  	 */
43  	protected float ypos;
44  	/**
45  	 * The time stamp of the last update represented as TuioTime (time since session start)
46  	 */
47  	protected TuioTime currentTime;
48  	/**
49  	 * The creation time of this TuioPoint represented as TuioTime (time since session start)
50  	 */
51  	protected TuioTime startTime;
52  	
53  	/**
54  	 * The default constructor takes no arguments and sets   
55  	 * its coordinate attributes to zero and its time stamp to the current session time.
56  	 */
57  	public TuioPoint() {
58  		xpos = 0.0f;
59  		ypos = 0.0f;
60  		currentTime = TuioTime.getSessionTime();
61  		startTime = new TuioTime(currentTime);
62  	}
63  	
64  	/**
65  	 * This constructor takes two floating point coordinate arguments and sets   
66  	 * its coordinate attributes to these values and its time stamp to the current session time.
67  	 *
68  	 * @param	xp	the X coordinate to assign
69  	 * @param	yp	the Y coordinate to assign
70  	 */
71  	public TuioPoint(float xp, float yp) {
72  		xpos = xp;
73  		ypos = yp;
74  		currentTime = TuioTime.getSessionTime();
75  		startTime = new TuioTime(currentTime);
76  	}
77  
78  	/**
79  	 * This constructor takes a TuioPoint argument and sets its coordinate attributes 
80  	 * to the coordinates of the provided TuioPoint and its time stamp to the current session time.
81  	 *
82  	 * @param	tpoint	the TuioPoint to assign
83  	 */
84  	public TuioPoint(TuioPoint tpoint) {
85  		xpos = tpoint.getX();
86  		ypos = tpoint.getY();
87  		currentTime = TuioTime.getSessionTime();
88  		startTime = new TuioTime(currentTime);
89  	}
90  	
91  	/**
92  	 * This constructor takes a TuioTime object and two floating point coordinate arguments and sets   
93  	 * its coordinate attributes to these values and its time stamp to the provided TUIO time object.
94  	 *
95  	 * @param	ttime	the TuioTime to assign
96  	 * @param	xp	the X coordinate to assign
97  	 * @param	yp	the Y coordinate to assign
98  	 */
99  	public TuioPoint(TuioTime ttime, float xp, float yp) {
100 		xpos = xp;
101 		ypos = yp;
102 		currentTime = new TuioTime(ttime);
103 		startTime = new TuioTime(currentTime);
104 	}
105 	
106 	/**
107 	 * Takes a TuioPoint argument and updates its coordinate attributes 
108 	 * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
109 	 *
110 	 * @param	tpoint	the TuioPoint to assign
111 	 */
112 	public void update(TuioPoint tpoint) {
113 		xpos = tpoint.getX();
114 		ypos = tpoint.getY();
115 	}
116 
117 	/**
118 	 * Takes two floating point coordinate arguments and updates its coordinate attributes 
119 	 * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
120 	 *
121 	 * @param	xp	the X coordinate to assign
122 	 * @param	yp	the Y coordinate to assign
123 	 */
124 	public void update(float xp, float yp) {
125 		xpos = xp;
126 		ypos = yp;
127 	}
128 	
129 	/**
130 	 * Takes a TuioTime object and two floating point coordinate arguments and updates its coordinate attributes 
131 	 * to the coordinates of the provided TuioPoint and its time stamp to the provided TUIO time object.
132 	 *
133 	 * @param	ttime	the TuioTime to assign
134 	 * @param	xp	the X coordinate to assign
135 	 * @param	yp	the Y coordinate to assign
136 	 */
137 	public void update(TuioTime ttime, float xp, float yp) {
138 		xpos = xp;
139 		ypos = yp;
140 		currentTime = new TuioTime(ttime);
141 	}
142 	
143 	/**
144 	 * Returns the X coordinate of this TuioPoint. 
145 	 * @return	the X coordinate of this TuioPoint
146 	 */
147 	public float getX() {
148 		return xpos;
149 	}
150 	
151 	/**
152 	 * Returns the Y coordinate of this TuioPoint. 
153 	 * @return	the Y coordinate of this TuioPoint
154 	 */
155 	public float getY() {
156 		return ypos;
157 	}
158 	
159 	/**
160 	 * Returns the distance to the provided coordinates 
161 	 *
162 	 * @param	xp	the X coordinate of the distant point
163 	 * @param	yp	the Y coordinate of the distant point
164 	 * @return	the distance to the provided coordinates
165 	 */
166 	public float getDistance(float xp, float yp) {
167 		float dx = xpos-xp;
168 		float dy = ypos-yp;
169 		return (float)Math.sqrt(dx*dx+dy*dy);
170 	}
171 
172 	/**
173 	 * Returns the distance to the provided TuioPoint 
174 	 *
175 	 * @param	tpoint	the distant TuioPoint
176 	 * @return	the distance to the provided TuioPoint
177 	 */
178 	public float getDistance(TuioPoint tpoint) {
179 		return getDistance(tpoint.getX(),tpoint.getY());
180 	}
181 
182 	/**
183 	 * Returns the angle to the provided coordinates 
184 	 *
185 	 * @param	xp	the X coordinate of the distant point
186 	 * @param	yp	the Y coordinate of the distant point
187 	 * @return	the angle to the provided coordinates
188 	 */
189 	public float getAngle(float xp, float yp) {
190 		
191 		float side = xpos-xp;
192 		float height = ypos-yp;
193 		float distance = getDistance(xp,yp);
194 		
195 		float angle = (float)(Math.asin(side/distance)+Math.PI/2);
196 		if (height<0) angle = 2.0f*(float)Math.PI-angle;
197 		
198 		return angle;
199 	}
200 	
201 	/**
202 	 * Returns the angle to the provided TuioPoint 
203 	 *
204 	 * @param	tpoint	the distant TuioPoint
205 	 * @return	the angle to the provided TuioPoint
206 	 */
207 	public float getAngle(TuioPoint tpoint) {						
208 		return getAngle(tpoint.getX(),tpoint.getY());
209 	}
210 
211 	/**
212 	 * Returns the angle in degrees to the provided coordinates 
213 	 *
214 	 * @param	xp	the X coordinate of the distant point
215 	 * @param	yp	the Y coordinate of the distant point
216 	 * @return	the angle in degrees to the provided TuioPoint
217 	 */
218 	public float getAngleDegrees(float xp, float yp) {		
219 		return (getAngle(xp,yp)/(float)Math.PI)*180.0f;
220 	}
221 	
222 	/**
223 	 * Returns the angle in degrees to the provided TuioPoint 
224 	 *
225 	 * @param	tpoint	the distant TuioPoint
226 	 * @return	the angle in degrees to the provided TuioPoint
227 	 */
228 	public float getAngleDegrees(TuioPoint tpoint) {		
229 		return (getAngle(tpoint)/(float)Math.PI)*180.0f;
230 	}
231 	
232 	/**
233 	 * Returns the X coordinate in pixels relative to the provided screen width. 
234 	 *
235 	 * @param	width	the screen width
236 	 * @return	the X coordinate of this TuioPoint in pixels relative to the provided screen width
237 	 */
238 	public int getScreenX(int width) {
239 		return (int)Math.round(xpos*width);
240 	}
241 	
242 	/**
243 	 * Returns the Y coordinate in pixels relative to the provided screen height. 
244 	 *
245 	 * @param	height	the screen height
246 	 * @return	the Y coordinate of this TuioPoint in pixels relative to the provided screen height
247 	 */
248 	public int getScreenY(int height) {
249 		return (int)Math.round(ypos*height);
250 	}
251 	
252 	/**
253 	 * Returns the time stamp of this TuioPoint as TuioTime. 
254 	 *
255 	 * @return	the time stamp of this TuioPoint as TuioTime
256 	 */
257 	public TuioTime getTuioTime() {
258 		return new TuioTime(currentTime);
259 	}
260 	
261 	/**
262 	 * Returns the start time of this TuioPoint as TuioTime. 
263 	 *
264 	 * @return	the start time of this TuioPoint as TuioTime
265 	 */
266 	public TuioTime getStartTime() {
267 		return new TuioTime(startTime);
268 	}
269 }