TuioContainer.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_TUIOCONTAINER_H
  18. #define INCLUDED_TUIOCONTAINER_H
  19. #include <list>
  20. #include <math.h>
  21. #include "TuioPoint.h"
  22. #include <iostream>
  23. #define TUIO_ADDED 0
  24. #define TUIO_ACCELERATING 1
  25. #define TUIO_DECELERATING 2
  26. #define TUIO_STOPPED 3
  27. #define TUIO_REMOVED 4
  28. namespace TUIO {
  29. /**
  30. * The abstract TuioContainer class defines common attributes that apply to both subclasses {@link TuioObject} and {@link TuioCursor}.
  31. *
  32. * @author Martin Kaltenbrunner
  33. * @version 1.4
  34. */
  35. class TuioContainer: public TuioPoint {
  36. protected:
  37. /**
  38. * The unique session ID number that is assigned to each TUIO object or cursor.
  39. */
  40. long session_id;
  41. /**
  42. * The X-axis velocity value.
  43. */
  44. float x_speed;
  45. /**
  46. * The Y-axis velocity value.
  47. */
  48. float y_speed;
  49. /**
  50. * The motion speed value.
  51. */
  52. float motion_speed;
  53. /**
  54. * The motion acceleration value.
  55. */
  56. float motion_accel;
  57. /**
  58. * A List of TuioPoints containing all the previous positions of the TUIO component.
  59. */
  60. std::list<TuioPoint> path;
  61. /**
  62. * Reflects the current state of the TuioComponent
  63. */
  64. int state;
  65. public:
  66. /**
  67. * This constructor takes a TuioTime argument and assigns it along with the provided
  68. * Session ID, X and Y coordinate to the newly created TuioContainer.
  69. *
  70. * @param ttime the TuioTime to assign
  71. * @param si the Session ID to assign
  72. * @param xp the X coordinate to assign
  73. * @param yp the Y coordinate to assign
  74. */
  75. TuioContainer (TuioTime ttime, long si, float xp, float yp):TuioPoint(ttime, xp,yp) {
  76. session_id = si;
  77. x_speed = 0.0f;
  78. y_speed = 0.0f;
  79. motion_speed = 0.0f;
  80. motion_accel = 0.0f;
  81. TuioPoint p(currentTime,xpos,ypos);
  82. path.push_back(p);
  83. state = TUIO_ADDED;
  84. };
  85. /**
  86. * This constructor takes the provided Session ID, X and Y coordinate
  87. * and assigs these values to the newly created TuioContainer.
  88. *
  89. * @param si the Session ID to assign
  90. * @param xp the X coordinate to assign
  91. * @param yp the Y coordinate to assign
  92. */
  93. TuioContainer (long si, float xp, float yp):TuioPoint(xp,yp) {
  94. session_id = si;
  95. x_speed = 0.0f;
  96. y_speed = 0.0f;
  97. motion_speed = 0.0f;
  98. motion_accel = 0.0f;
  99. TuioPoint p(currentTime,xpos,ypos);
  100. path.push_back(p);
  101. state = TUIO_ADDED;
  102. };
  103. /**
  104. * This constructor takes the atttibutes of the provided TuioContainer
  105. * and assigs these values to the newly created TuioContainer.
  106. *
  107. * @param tcon the TuioContainer to assign
  108. */
  109. TuioContainer (TuioContainer *tcon):TuioPoint(tcon) {
  110. session_id = tcon->getSessionID();
  111. x_speed = 0.0f;
  112. y_speed = 0.0f;
  113. motion_speed = 0.0f;
  114. motion_accel = 0.0f;
  115. TuioPoint p(currentTime,xpos,ypos);
  116. path.push_back(p);
  117. state = TUIO_ADDED;
  118. };
  119. /**
  120. * The destructor is doing nothing in particular.
  121. */
  122. virtual ~TuioContainer(){};
  123. /**
  124. * Takes a TuioTime argument and assigns it along with the provided
  125. * X and Y coordinate to the private TuioContainer attributes.
  126. * The speed and accleration values are calculated accordingly.
  127. *
  128. * @param ttime the TuioTime to assign
  129. * @param xp the X coordinate to assign
  130. * @param yp the Y coordinate to assign
  131. */
  132. virtual void update (TuioTime ttime, float xp, float yp) {
  133. TuioPoint lastPoint = path.back();
  134. TuioPoint::update(ttime,xp, yp);
  135. TuioTime diffTime = currentTime - lastPoint.getTuioTime();
  136. float dt = diffTime.getTotalMilliseconds()/1000.0f;
  137. float dx = xpos - lastPoint.getX();
  138. float dy = ypos - lastPoint.getY();
  139. float dist = sqrt(dx*dx+dy*dy);
  140. float last_motion_speed = motion_speed;
  141. x_speed = dx/dt;
  142. y_speed = dy/dt;
  143. motion_speed = dist/dt;
  144. motion_accel = (motion_speed - last_motion_speed)/dt;
  145. TuioPoint p(currentTime,xpos,ypos);
  146. path.push_back(p);
  147. if (motion_accel>0) state = TUIO_ACCELERATING;
  148. else if (motion_accel<0) state = TUIO_DECELERATING;
  149. else state = TUIO_STOPPED;
  150. };
  151. /**
  152. * This method is used to calculate the speed and acceleration values of
  153. * TuioContainers with unchanged positions.
  154. */
  155. virtual void stop(TuioTime ttime) {
  156. update(ttime,xpos,ypos);
  157. };
  158. /**
  159. * Takes a TuioTime argument and assigns it along with the provided
  160. * X and Y coordinate, X and Y velocity and acceleration
  161. * to the private TuioContainer attributes.
  162. *
  163. * @param ttime the TuioTime to assign
  164. * @param xp the X coordinate to assign
  165. * @param yp the Y coordinate to assign
  166. * @param xs the X velocity to assign
  167. * @param ys the Y velocity to assign
  168. * @param ma the acceleration to assign
  169. */
  170. virtual void update (TuioTime ttime, float xp, float yp, float xs, float ys, float ma) {
  171. TuioPoint::update(ttime,xp, yp);
  172. x_speed = xs;
  173. y_speed = ys;
  174. motion_speed = (float)sqrt(x_speed*x_speed+y_speed*y_speed);
  175. motion_accel = ma;
  176. TuioPoint p(currentTime,xpos,ypos);
  177. path.push_back(p);
  178. if (motion_accel>0) state = TUIO_ACCELERATING;
  179. else if (motion_accel<0) state = TUIO_DECELERATING;
  180. else state = TUIO_STOPPED;
  181. };
  182. /**
  183. * Assigns the provided X and Y coordinate, X and Y velocity and acceleration
  184. * to the private TuioContainer attributes. The TuioTime time stamp remains unchanged.
  185. *
  186. * @param xp the X coordinate to assign
  187. * @param yp the Y coordinate to assign
  188. * @param xs the X velocity to assign
  189. * @param ys the Y velocity to assign
  190. * @param ma the acceleration to assign
  191. */
  192. virtual void update (float xp, float yp, float xs, float ys, float ma) {
  193. TuioPoint::update(xp,yp);
  194. x_speed = xs;
  195. y_speed = ys;
  196. motion_speed = (float)sqrt(x_speed*x_speed+y_speed*y_speed);
  197. motion_accel = ma;
  198. path.pop_back();
  199. TuioPoint p(currentTime,xpos,ypos);
  200. path.push_back(p);
  201. if (motion_accel>0) state = TUIO_ACCELERATING;
  202. else if (motion_accel<0) state = TUIO_DECELERATING;
  203. else state = TUIO_STOPPED;
  204. };
  205. /**
  206. * Takes the atttibutes of the provided TuioContainer
  207. * and assigs these values to this TuioContainer.
  208. * The TuioTime time stamp of this TuioContainer remains unchanged.
  209. *
  210. * @param tcon the TuioContainer to assign
  211. */
  212. virtual void update (TuioContainer *tcon) {
  213. TuioPoint::update(tcon);
  214. x_speed = tcon->getXSpeed();
  215. y_speed = tcon->getYSpeed();
  216. motion_speed = tcon->getMotionSpeed();
  217. motion_accel = tcon->getMotionAccel();
  218. TuioPoint p(tcon->getTuioTime(),xpos,ypos);
  219. path.push_back(p);
  220. if (motion_accel>0) state = TUIO_ACCELERATING;
  221. else if (motion_accel<0) state = TUIO_DECELERATING;
  222. else state = TUIO_STOPPED;
  223. };
  224. /**
  225. * Assigns the REMOVE state to this TuioContainer and sets
  226. * its TuioTime time stamp to the provided TuioTime argument.
  227. *
  228. * @param ttime the TuioTime to assign
  229. */
  230. virtual void remove(TuioTime ttime) {
  231. currentTime = ttime;
  232. state = TUIO_REMOVED;
  233. }
  234. /**
  235. * Returns the Session ID of this TuioContainer.
  236. * @return the Session ID of this TuioContainer
  237. */
  238. virtual long getSessionID() {
  239. return session_id;
  240. };
  241. /**
  242. * Returns the X velocity of this TuioContainer.
  243. * @return the X velocity of this TuioContainer
  244. */
  245. virtual float getXSpeed() {
  246. return x_speed;
  247. };
  248. /**
  249. * Returns the Y velocity of this TuioContainer.
  250. * @return the Y velocity of this TuioContainer
  251. */
  252. virtual float getYSpeed() {
  253. return y_speed;
  254. };
  255. /**
  256. * Returns the position of this TuioContainer.
  257. * @return the position of this TuioContainer
  258. */
  259. virtual TuioPoint getPosition() {
  260. TuioPoint p(xpos,ypos);
  261. return p;
  262. };
  263. /**
  264. * Returns the path of this TuioContainer.
  265. * @return the path of this TuioContainer
  266. */
  267. virtual std::list<TuioPoint> getPath() {
  268. return path;
  269. };
  270. /**
  271. * Returns the motion speed of this TuioContainer.
  272. * @return the motion speed of this TuioContainer
  273. */
  274. virtual float getMotionSpeed() {
  275. return motion_speed;
  276. };
  277. /**
  278. * Returns the motion acceleration of this TuioContainer.
  279. * @return the motion acceleration of this TuioContainer
  280. */
  281. virtual float getMotionAccel() {
  282. return motion_accel;
  283. };
  284. /**
  285. * Returns the TUIO state of this TuioContainer.
  286. * @return the TUIO state of this TuioContainer
  287. */
  288. virtual int getTuioState() {
  289. return state;
  290. };
  291. /**
  292. * Returns true of this TuioContainer is moving.
  293. * @return true of this TuioContainer is moving
  294. */
  295. virtual bool isMoving() {
  296. if ((state==TUIO_ACCELERATING) || (state==TUIO_DECELERATING)) return true;
  297. else return false;
  298. };
  299. };
  300. };
  301. #endif