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.tuio3D;
23
24 import org.ximtec.igesture.io.tuio.TuioTime;
25 import org.ximtec.igesture.io.tuio.interfaces.AbstractTuioPoint;
26
27 /**
28 * The TuioPoint3D class on the one hand is a simple container and utility class to handle TUIO positions in general,
29 * on the other hand the TuioPoint3D is the base class for the TuioCursor3D and TuioObject3D classes.
30 *
31 * @author Martin Kaltenbrunner, Bjorn Puype
32 * @version 1.4
33 */
34 public class TuioPoint3D implements AbstractTuioPoint {
35
36 /** XY plane */
37 public static final int XY = 0;
38 /** XZ plane */
39 public static final int XZ = 1;
40 /** YZ plane */
41 public static final int YZ = 2;
42
43 /**
44 * X coordinate, represented as a floating point value in a range of 0..1
45 */
46 protected float xpos;
47 /**
48 * Y coordinate, represented as a floating point value in a range of 0..1
49 */
50 protected float ypos;
51 /**
52 * Z coordinate, represented as a floating point value in a range of 0..1
53 */
54 protected float zpos;
55 /**
56 * The time stamp of the last update represented as TuioTime (time since session start)
57 */
58 protected TuioTime currentTime;
59 /**
60 * The creation time of this TuioPoint3D represented as TuioTime (time since session start)
61 */
62 protected TuioTime startTime;
63
64 /**
65 * The default constructor takes no arguments and sets
66 * its coordinate attributes to zero and its time stamp to the current session time.
67 */
68 public TuioPoint3D() {
69 xpos = 0.0f;
70 ypos = 0.0f;
71 zpos = 0.0f;
72 currentTime = TuioTime.getSessionTime();
73 startTime = new TuioTime(currentTime);
74 }
75
76 /**
77 * This constructor takes three floating point coordinate arguments and sets
78 * its coordinate attributes to these values and its time stamp to the current session time.
79 *
80 * @param xp the X coordinate to assign
81 * @param yp the Y coordinate to assign
82 * @param zp the Z coordinate to assign
83 */
84 public TuioPoint3D(float xp, float yp, float zp) {
85 xpos = xp;
86 ypos = yp;
87 zpos = zp;
88 currentTime = TuioTime.getSessionTime();
89 startTime = new TuioTime(currentTime);
90 }
91
92 /**
93 * This constructor takes a TuioPoint3D argument and sets its coordinate attributes
94 * to the coordinates of the provided TuioPoint3D and its time stamp to the current session time.
95 *
96 * @param tpoint the TuioPoint3D to assign
97 */
98 public TuioPoint3D(TuioPoint3D tpoint) {
99 xpos = tpoint.getX();
100 ypos = tpoint.getY();
101 zpos = tpoint.getZ();
102 currentTime = TuioTime.getSessionTime();
103 startTime = new TuioTime(currentTime);
104 }
105
106 /**
107 * This constructor takes a TuioTime object and three floating point coordinate arguments and sets
108 * its coordinate attributes to these values and its time stamp to the provided TUIO time object.
109 *
110 * @param ttime the TuioTime to assign
111 * @param xp the X coordinate to assign
112 * @param yp the Y coordinate to assign
113 * @param zp the Z coordinate to assign
114 */
115 public TuioPoint3D(TuioTime ttime, float xp, float yp, float zp) {
116 xpos = xp;
117 ypos = yp;
118 zpos = zp;
119 currentTime = new TuioTime(ttime);
120 startTime = new TuioTime(currentTime);
121 }
122
123 /**
124 * Takes a TuioPoint3D argument and updates its coordinate attributes
125 * to the coordinates of the provided TuioPoint3D and leaves its time stamp unchanged.
126 *
127 * @param tpoint the TuioPoint to assign
128 */
129 public void update(TuioPoint3D tpoint) {
130 xpos = tpoint.getX();
131 ypos = tpoint.getY();
132 zpos = tpoint.getZ();
133 }
134
135 /**
136 * Takes three floating point coordinate arguments and updates its coordinate attributes
137 * to the coordinates of the provided TuioPoint3D and leaves its time stamp unchanged.
138 *
139 * @param xp the X coordinate to assign
140 * @param yp the Y coordinate to assign
141 * @param zp the Z coordinate to assign
142 */
143 public void update(float xp, float yp, float zp) {
144 xpos = xp;
145 ypos = yp;
146 zpos = zp;
147 }
148
149 /**
150 * Takes a TuioTime object and three floating point coordinate arguments and updates its coordinate attributes
151 * to the coordinates of the provided TuioPoint3D and its time stamp to the provided TUIO time object.
152 *
153 * @param ttime the TuioTime to assign
154 * @param xp the X coordinate to assign
155 * @param yp the Y coordinate to assign
156 * @param zp the Z coordinate to assign
157 */
158 public void update(TuioTime ttime, float xp, float yp, float zp) {
159 xpos = xp;
160 ypos = yp;
161 zpos = zp;
162 currentTime = new TuioTime(ttime);
163 }
164
165 /**
166 * Returns the X coordinate of this TuioPoint3D.
167 * @return the X coordinate of this TuioPoint3D
168 */
169 public float getX() {
170 return xpos;
171 }
172
173 /**
174 * Returns the Y coordinate of this TuioPoint3D.
175 * @return the Y coordinate of this TuioPoint3D
176 */
177 public float getY() {
178 return ypos;
179 }
180
181 /**
182 * Returns the Z coordinate of this TuioPoint3D.
183 * @return the Z coordinate of this TuioPoint3D
184 */
185 public float getZ() {
186 return zpos;
187 }
188
189 /**
190 * Returns the distance to the provided coordinates
191 *
192 * @param xp the X coordinate of the distant point
193 * @param yp the Y coordinate of the distant point
194 * @param zp the Z coordinate of the distant point
195 * @return the distance to the provided coordinates
196 */
197 public float getDistance(float xp, float yp, float zp) {
198 float dx = xpos-xp;
199 float dy = ypos-yp;
200 float dz = zpos -zp;
201 return (float)Math.sqrt(dx*dx+dy*dy+dz*dz);
202 }
203
204 /**
205 * Returns the distance to the provided coordinates
206 *
207 * @param planes the planes of which to calculate the distance (XY, XZ or YZ)
208 * @param ap the coordinate of the first plane of the distant point
209 * @param bp the coordinate of the second plane of the distant point
210 * @return the distance to the provided coordinates
211 */
212 private float getDistance(int planes, float ap, float bp) {
213 float da = 0, db = 0;
214 switch(planes)
215 {
216 case XY:
217 da = xpos - ap;
218 db = ypos - bp;
219 break;
220 case XZ:
221 da = xpos - ap;
222 db = zpos - bp;
223 break;
224 case YZ:
225 da = ypos - ap;
226 db = zpos - bp;
227 break;
228 default:
229 System.err.println("Unknown planes entered");
230 break;
231 }
232 return (float)Math.sqrt(da*da+db*db);
233 }
234
235 /**
236 * Returns the distance to the provided TuioPoint3D
237 *
238 * @param tpoint the distant TuioPoint3D
239 * @return the distance to the provided TuioPoint3D
240 */
241 public float getDistance(TuioPoint3D tpoint) {
242 return getDistance(tpoint.getX(),tpoint.getY(),tpoint.getZ());
243 }
244
245 /**
246 * Returns the distance to the provided TuioPoint3D
247 *
248 * @param planes the planes of which to calculate the distance (XY, XZ or YZ)
249 * @param tpoint the distant TuioPoint3D
250 * @return the distance to the provided TuioPoint3D
251 */
252 private float getDistance(int planes, TuioPoint3D tpoint) {
253 float distance = 0;
254 switch(planes)
255 {
256 case XY:
257 distance = getDistance(XY, tpoint.getX(),tpoint.getY());
258 break;
259 case XZ:
260 distance = getDistance(XZ, tpoint.getX(),tpoint.getZ());
261 break;
262 case YZ:
263 distance = getDistance(YZ, tpoint.getY(),tpoint.getZ());
264 break;
265 default:
266 System.err.println("Unknown planes entered");
267 }
268 return distance;
269 }
270
271 /**
272 * Returns the angle to the provided coordinates
273 *
274 * @param planes the planes of which to calculate the angle (XY, XZ or YZ)
275 * @param ap the coordinate of the distant point in the first plane
276 * @param bp the coordinate of the distant point in the second plane
277 * @return the angle to the provided coordinates
278 */
279 public float getAngle(int planes, float ap, float bp) {
280
281 float side = 0, height = 0, distance = 0;
282
283 switch(planes)
284 {
285 case XY:
286 side = xpos - ap;
287 height = ypos -bp;
288 distance = getDistance(XY,ap,bp);
289 break;
290 case XZ:
291 side = xpos - ap;
292 height = zpos -bp;
293 distance = getDistance(XZ,ap,bp);
294 break;
295 case YZ:
296 side = ypos - ap;
297 height = zpos -bp;
298 distance = getDistance(YZ,ap,bp);
299 break;
300 default:
301 System.err.println("Unknown planes entered");
302 break;
303 }
304
305 float angle = (float)(Math.asin(side/distance)+Math.PI/2);
306 if (height<0) angle = 2.0f*(float)Math.PI-angle;
307
308 return angle;
309 }
310
311 /**
312 * Returns the angle to the provided TuioPoint3D
313 *
314 * @param planes the planes of which to calculate the angle (XY, XZ or YZ)
315 * @param tpoint the distant TuioPoint3D
316 * @return the angle to the provided TuioPoint3D
317 */
318 public float getAngle(int planes, TuioPoint3D tpoint) {
319 float angle = 0;
320 switch(planes)
321 {
322 case XY:
323 angle = getAngle(planes,tpoint.getX(),tpoint.getY());
324 break;
325 case XZ:
326 angle = getAngle(planes,tpoint.getX(),tpoint.getZ());
327 break;
328 case YZ:
329 angle = getAngle(planes,tpoint.getY(),tpoint.getZ());
330 break;
331 default:
332 System.err.println("Unknown planes entered");
333 }
334 return angle;
335 }
336
337 /**
338 * Returns the angle in degrees to the provided coordinates
339 *
340 * @param planes the planes of which to calculate the angle (XY, XZ or YZ)
341 * @param ap the coordinate of the first plane of the distant point
342 * @param bp the coordinate of the second plane of the distant point
343 * @return the angle in degrees to the provided TuioPoint3D
344 */
345 public float getAngleDegrees(int planes, float ap, float bp) {
346 return (getAngle(planes, ap, bp)/(float)Math.PI)*180.0f;
347 }
348
349 /**
350 * Returns the angle in degrees to the provided TuioPoint
351 *
352 * @param planes the planes of which to calculate the angle (XY, XZ or YZ)
353 * @param tpoint the distant TuioPoint
354 * @return the angle in degrees to the provided TuioPoint
355 */
356 public float getAngleDegrees(int planes, TuioPoint3D tpoint) {
357 return (getAngle(planes, tpoint)/(float)Math.PI)*180.0f;
358 }
359
360 /**
361 * Returns the X coordinate in pixels relative to the provided screen width.
362 *
363 * @param width the screen width
364 * @return the X coordinate of this TuioPoint3D in pixels relative to the provided screen width
365 */
366 public int getScreenX(int width) {
367 return (int)Math.round(xpos*width);
368 }
369
370 /**
371 * Returns the Y coordinate in pixels relative to the provided screen height.
372 *
373 * @param height the screen height
374 * @return the Y coordinate of this TuioPoint3D in pixels relative to the provided screen height
375 */
376 public int getScreenY(int height) {
377 return (int)Math.round(ypos*height);
378 }
379
380 public int getScreenZ(int z) {
381 return (int)Math.round(zpos*z);
382 }
383
384 /**
385 * Returns the time stamp of this TuioPoint3D as TuioTime.
386 *
387 * @return the time stamp of this TuioPoint3D as TuioTime
388 */
389 public TuioTime getTuioTime() {
390 return new TuioTime(currentTime);
391 }
392
393 /**
394 * Returns the start time of this TuioPoint3D as TuioTime.
395 *
396 * @return the start time of this TuioPoint3D as TuioTime
397 */
398 public TuioTime getStartTime() {
399 return new TuioTime(startTime);
400 }
401 }