TuioObject.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_TUIOOBJECT_H
  18. #define INCLUDED_TUIOOBJECT_H
  19. #include <math.h>
  20. #include "TuioContainer.h"
  21. #define TUIO_ROTATING 5
  22. namespace TUIO {
  23. /**
  24. * The TuioObject class encapsulates /tuio/2Dobj TUIO objects.
  25. *
  26. * @author Martin Kaltenbrunner
  27. * @version 1.4
  28. */
  29. class TuioObject: public TuioContainer {
  30. protected:
  31. /**
  32. * The individual symbol ID number that is assigned to each TuioObject.
  33. */
  34. int symbol_id;
  35. /**
  36. * The rotation angle value.
  37. */
  38. float angle;
  39. /**
  40. * The rotation speed value.
  41. */
  42. float rotation_speed;
  43. /**
  44. * The rotation acceleration value.
  45. */
  46. float rotation_accel;
  47. public:
  48. /**
  49. * This constructor takes a TuioTime argument and assigns it along with the provided
  50. * Session ID, Symbol ID, X and Y coordinate and angle to the newly created TuioObject.
  51. *
  52. * @param ttime the TuioTime to assign
  53. * @param si the Session ID to assign
  54. * @param sym the Symbol ID to assign
  55. * @param xp the X coordinate to assign
  56. * @param yp the Y coordinate to assign
  57. * @param a the angle to assign
  58. */
  59. TuioObject (TuioTime ttime, long si, int sym, float xp, float yp, float a):TuioContainer(ttime, si, xp, yp) {
  60. symbol_id = sym;
  61. angle = a;
  62. rotation_speed = 0.0f;
  63. rotation_accel = 0.0f;
  64. };
  65. /**
  66. * This constructor takes the provided Session ID, Symbol ID, X and Y coordinate
  67. * and angle, and assigs these values to the newly created TuioObject.
  68. *
  69. * @param si the Session ID to assign
  70. * @param sym the Symbol ID to assign
  71. * @param xp the X coordinate to assign
  72. * @param yp the Y coordinate to assign
  73. * @param a the angle to assign
  74. */
  75. TuioObject (long si, int sym, float xp, float yp, float a):TuioContainer(si, xp, yp) {
  76. symbol_id = sym;
  77. angle = a;
  78. rotation_speed = 0.0f;
  79. rotation_accel = 0.0f;
  80. };
  81. /**
  82. * This constructor takes the atttibutes of the provided TuioObject
  83. * and assigs these values to the newly created TuioObject.
  84. *
  85. * @param tobj the TuioObject to assign
  86. */
  87. TuioObject (TuioObject *tobj):TuioContainer(tobj) {
  88. symbol_id = tobj->getSymbolID();
  89. angle = tobj->getAngle();
  90. rotation_speed = 0.0f;
  91. rotation_accel = 0.0f;
  92. };
  93. /**
  94. * The destructor is doing nothing in particular.
  95. */
  96. ~TuioObject() {};
  97. /**
  98. * Takes a TuioTime argument and assigns it along with the provided
  99. * X and Y coordinate, angle, X and Y velocity, motion acceleration,
  100. * rotation speed and rotation acceleration to the private TuioObject attributes.
  101. *
  102. * @param ttime the TuioTime to assign
  103. * @param xp the X coordinate to assign
  104. * @param yp the Y coordinate to assign
  105. * @param a the angle coordinate to assign
  106. * @param xs the X velocity to assign
  107. * @param ys the Y velocity to assign
  108. * @param rs the rotation velocity to assign
  109. * @param ma the motion acceleration to assign
  110. * @param ra the rotation acceleration to assign
  111. */
  112. void update (TuioTime ttime, float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra) {
  113. TuioContainer::update(ttime,xp,yp,xs,ys,ma);
  114. angle = a;
  115. rotation_speed = rs;
  116. rotation_accel = ra;
  117. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  118. };
  119. /**
  120. * Assigns the provided X and Y coordinate, angle, X and Y velocity, motion acceleration
  121. * rotation velocity and rotation acceleration to the private TuioContainer attributes.
  122. * The TuioTime time stamp remains unchanged.
  123. *
  124. * @param xp the X coordinate to assign
  125. * @param yp the Y coordinate to assign
  126. * @param a the angle coordinate to assign
  127. * @param xs the X velocity to assign
  128. * @param ys the Y velocity to assign
  129. * @param rs the rotation velocity to assign
  130. * @param ma the motion acceleration to assign
  131. * @param ra the rotation acceleration to assign
  132. */
  133. void update (float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra) {
  134. TuioContainer::update(xp,yp,xs,ys,ma);
  135. angle = a;
  136. rotation_speed = rs;
  137. rotation_accel = ra;
  138. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  139. };
  140. /**
  141. * Takes a TuioTime argument and assigns it along with the provided
  142. * X and Y coordinate and angle to the private TuioObject attributes.
  143. * The speed and accleration values are calculated accordingly.
  144. *
  145. * @param ttime the TuioTime to assign
  146. * @param xp the X coordinate to assign
  147. * @param yp the Y coordinate to assign
  148. * @param a the angle coordinate to assign
  149. */
  150. void update (TuioTime ttime, float xp, float yp, float a) {
  151. TuioPoint lastPoint = path.back();
  152. TuioContainer::update(ttime,xp,yp);
  153. TuioTime diffTime = currentTime - lastPoint.getTuioTime();
  154. float dt = diffTime.getTotalMilliseconds()/1000.0f;
  155. float last_angle = angle;
  156. float last_rotation_speed = rotation_speed;
  157. angle = a;
  158. double da = (angle-last_angle)/(2*M_PI);
  159. if (da>M_PI*1.5) da-=(2*M_PI);
  160. else if (da<M_PI*1.5) da+=(2*M_PI);
  161. rotation_speed = (float)da/dt;
  162. rotation_accel = (rotation_speed - last_rotation_speed)/dt;
  163. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  164. };
  165. /**
  166. * This method is used to calculate the speed and acceleration values of a
  167. * TuioObject with unchanged position and angle.
  168. */
  169. void stop (TuioTime ttime) {
  170. update(ttime,xpos,ypos,angle);
  171. };
  172. /**
  173. * Takes the atttibutes of the provided TuioObject
  174. * and assigs these values to this TuioObject.
  175. * The TuioTime time stamp of this TuioContainer remains unchanged.
  176. *
  177. * @param tobj the TuioContainer to assign
  178. */
  179. void update (TuioObject *tobj) {
  180. TuioContainer::update(tobj);
  181. angle = tobj->getAngle();
  182. rotation_speed = tobj->getRotationSpeed();
  183. rotation_accel = tobj->getRotationAccel();
  184. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  185. };
  186. /**
  187. * Returns the symbol ID of this TuioObject.
  188. * @return the symbol ID of this TuioObject
  189. */
  190. int getSymbolID() {
  191. return symbol_id;
  192. };
  193. /**
  194. * Returns the rotation angle of this TuioObject.
  195. * @return the rotation angle of this TuioObject
  196. */
  197. float getAngle() {
  198. return angle;
  199. };
  200. /**
  201. * Returns the rotation angle in degrees of this TuioObject.
  202. * @return the rotation angle in degrees of this TuioObject
  203. */
  204. float getAngleDegrees() {
  205. return (float)(angle/M_PI*180);
  206. };
  207. /**
  208. * Returns the rotation speed of this TuioObject.
  209. * @return the rotation speed of this TuioObject
  210. */
  211. float getRotationSpeed() {
  212. return rotation_speed;
  213. };
  214. /**
  215. * Returns the rotation acceleration of this TuioObject.
  216. * @return the rotation acceleration of this TuioObject
  217. */
  218. float getRotationAccel() {
  219. return rotation_accel;
  220. };
  221. /**
  222. * Returns true of this TuioObject is moving.
  223. * @return true of this TuioObject is moving
  224. */
  225. virtual bool isMoving() {
  226. if ((state==TUIO_ACCELERATING) || (state==TUIO_DECELERATING) || (state==TUIO_ROTATING)) return true;
  227. else return false;
  228. };
  229. };
  230. };
  231. #endif