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