2
0

TuioPoint.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. TUIO C++ Library - part of the reacTIVision project
  3. http://reactivision.sourceforge.net/
  4. Copyright (c) 2005-2009 Martin Kaltenbrunner <[email protected]>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #ifndef INCLUDED_TUIOPOINT_H
  18. #define INCLUDED_TUIOPOINT_H
  19. #include "TuioTime.h"
  20. #include <iostream>
  21. #ifndef M_PI
  22. #define M_PI 3.14159265358979323846
  23. #endif
  24. namespace TUIO {
  25. /**
  26. * The TuioPoint class on the one hand is a simple container and utility class to handle TUIO positions in general,
  27. * on the other hand the TuioPoint is the base class for the TuioCursor and TuioObject classes.
  28. *
  29. * @author Martin Kaltenbrunner
  30. * @version 1.4
  31. */
  32. class TuioPoint {
  33. protected:
  34. /**
  35. * X coordinate, representated as a floating point value in a range of 0..1
  36. */
  37. float xpos;
  38. /**
  39. * X coordinate, representated as a floating point value in a range of 0..1
  40. */
  41. float ypos;
  42. /**
  43. * The time stamp of the last update represented as TuioTime (time since session start)
  44. */
  45. TuioTime currentTime;
  46. /**
  47. * The creation time of this TuioPoint represented as TuioTime (time since session start)
  48. */
  49. TuioTime startTime;
  50. public:
  51. /**
  52. * The default constructor takes no arguments and sets
  53. * its coordinate attributes to zero and its time stamp to the current session time.
  54. */
  55. TuioPoint (float xp, float yp) {
  56. xpos = xp;
  57. ypos = yp;
  58. currentTime = TuioTime::getSessionTime();
  59. startTime = currentTime;
  60. };
  61. /**
  62. * This constructor takes a TuioTime object and two floating point coordinate arguments and sets
  63. * its coordinate attributes to these values and its time stamp to the provided TUIO time object.
  64. *
  65. * @param ttime the TuioTime to assign
  66. * @param xp the X coordinate to assign
  67. * @param yp the Y coordinate to assign
  68. */
  69. TuioPoint (TuioTime ttime, float xp, float yp) {
  70. xpos = xp;
  71. ypos = yp;
  72. currentTime = ttime;
  73. startTime = currentTime;
  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. TuioPoint (TuioPoint *tpoint) {
  82. xpos = tpoint->getX();
  83. ypos = tpoint->getY();
  84. currentTime = TuioTime::getSessionTime();
  85. startTime = currentTime;
  86. };
  87. /**
  88. * The destructor is doing nothing in particular.
  89. */
  90. ~TuioPoint(){};
  91. /**
  92. * Takes a TuioPoint argument and updates its coordinate attributes
  93. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  94. *
  95. * @param tpoint the TuioPoint to assign
  96. */
  97. void update (TuioPoint *tpoint) {
  98. xpos = tpoint->getX();
  99. ypos = tpoint->getY();
  100. };
  101. /**
  102. * Takes two floating point coordinate arguments and updates its coordinate attributes
  103. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  104. *
  105. * @param xp the X coordinate to assign
  106. * @param yp the Y coordinate to assign
  107. */
  108. void update (float xp, float yp) {
  109. xpos = xp;
  110. ypos = yp;
  111. };
  112. /**
  113. * Takes a TuioTime object and two floating point coordinate arguments and updates its coordinate attributes
  114. * to the coordinates of the provided TuioPoint and its time stamp to the provided TUIO time object.
  115. *
  116. * @param ttime the TuioTime to assign
  117. * @param xp the X coordinate to assign
  118. * @param yp the Y coordinate to assign
  119. */
  120. void update (TuioTime ttime, float xp, float yp) {
  121. xpos = xp;
  122. ypos = yp;
  123. currentTime = ttime;
  124. };
  125. /**
  126. * Returns the X coordinate of this TuioPoint.
  127. * @return the X coordinate of this TuioPoint
  128. */
  129. float getX() {
  130. return xpos;
  131. };
  132. /**
  133. * Returns the Y coordinate of this TuioPoint.
  134. * @return the Y coordinate of this TuioPoint
  135. */
  136. float getY() {
  137. return ypos;
  138. };
  139. /**
  140. * Returns the distance to the provided coordinates
  141. *
  142. * @param xp the X coordinate of the distant point
  143. * @param yp the Y coordinate of the distant point
  144. * @return the distance to the provided coordinates
  145. */
  146. float getDistance(float xp, float yp) {
  147. float dx = xpos-xp;
  148. float dy = ypos-yp;
  149. return sqrtf(dx*dx+dy*dy);
  150. }
  151. /**
  152. * Returns the distance to the provided TuioPoint
  153. *
  154. * @param tpoint the distant TuioPoint
  155. * @return the distance to the provided TuioPoint
  156. */
  157. float getDistance(TuioPoint *tpoint) {
  158. return getDistance(tpoint->getX(),tpoint->getY());
  159. }
  160. /**
  161. * Returns the angle to the provided coordinates
  162. *
  163. * @param xp the X coordinate of the distant point
  164. * @param yp the Y coordinate of the distant point
  165. * @return the angle to the provided coordinates
  166. */
  167. float getAngle(float xp, float yp) {
  168. float side = xpos-xp;
  169. float height = ypos-yp;
  170. float distance = getDistance(xp,yp);
  171. float angle = (float)(asin(side/distance)+M_PI/2);
  172. if (height<0) angle = 2.0f*(float)M_PI-angle;
  173. return angle;
  174. }
  175. /**
  176. * Returns the angle to the provided TuioPoint
  177. *
  178. * @param tpoint the distant TuioPoint
  179. * @return the angle to the provided TuioPoint
  180. */
  181. float getAngle(TuioPoint *tpoint) {
  182. return getAngle(tpoint->getX(),tpoint->getY());
  183. }
  184. /**
  185. * Returns the angle in degrees to the provided coordinates
  186. *
  187. * @param xp the X coordinate of the distant point
  188. * @param yp the Y coordinate of the distant point
  189. * @return the angle in degrees to the provided TuioPoint
  190. */
  191. float getAngleDegrees(float xp, float yp) {
  192. return ((getAngle(xp,yp)/(float)M_PI)*180.0f);
  193. }
  194. /**
  195. * Returns the angle in degrees to the provided TuioPoint
  196. *
  197. * @param tpoint the distant TuioPoint
  198. * @return the angle in degrees to the provided TuioPoint
  199. */
  200. float getAngleDegrees(TuioPoint *tpoint) {
  201. return ((getAngle(tpoint)/(float)M_PI)*180.0f);
  202. }
  203. /**
  204. * Returns the X coordinate in pixels relative to the provided screen width.
  205. *
  206. * @param width the screen width
  207. * @return the X coordinate of this TuioPoint in pixels relative to the provided screen width
  208. */
  209. int getScreenX(int width) {
  210. return (int)floor(xpos*width+0.5f);
  211. };
  212. /**
  213. * Returns the Y coordinate in pixels relative to the provided screen height.
  214. *
  215. * @param height the screen height
  216. * @return the Y coordinate of this TuioPoint in pixels relative to the provided screen height
  217. */
  218. int getScreenY(int height) {
  219. return (int)floor(ypos*height+0.5f);
  220. };
  221. /**
  222. * Returns current time stamp of this TuioPoint as TuioTime
  223. *
  224. * @return the time stamp of this TuioPoint as TuioTime
  225. */
  226. TuioTime getTuioTime() {
  227. return currentTime;
  228. };
  229. /**
  230. * Returns the start time of this TuioPoint as TuioTime.
  231. *
  232. * @return the start time of this TuioPoint as TuioTime
  233. */
  234. TuioTime getStartTime() {
  235. return startTime;
  236. };
  237. };
  238. };
  239. #endif