TuioObject.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. using TuioContainer::update;
  98. /**
  99. * Takes a TuioTime argument and assigns it along with the provided
  100. * X and Y coordinate, angle, X and Y velocity, motion acceleration,
  101. * rotation speed and rotation acceleration to the private TuioObject attributes.
  102. *
  103. * @param ttime the TuioTime to assign
  104. * @param xp the X coordinate to assign
  105. * @param yp the Y coordinate to assign
  106. * @param a the angle coordinate to assign
  107. * @param xs the X velocity to assign
  108. * @param ys the Y velocity to assign
  109. * @param rs the rotation velocity to assign
  110. * @param ma the motion acceleration to assign
  111. * @param ra the rotation acceleration to assign
  112. */
  113. void update (TuioTime ttime, float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra) {
  114. TuioContainer::update(ttime,xp,yp,xs,ys,ma);
  115. angle = a;
  116. rotation_speed = rs;
  117. rotation_accel = ra;
  118. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  119. };
  120. /**
  121. * Assigns the provided X and Y coordinate, angle, X and Y velocity, motion acceleration
  122. * rotation velocity and rotation acceleration to the private TuioContainer attributes.
  123. * The TuioTime time stamp remains unchanged.
  124. *
  125. * @param xp the X coordinate to assign
  126. * @param yp the Y coordinate to assign
  127. * @param a the angle coordinate to assign
  128. * @param xs the X velocity to assign
  129. * @param ys the Y velocity to assign
  130. * @param rs the rotation velocity to assign
  131. * @param ma the motion acceleration to assign
  132. * @param ra the rotation acceleration to assign
  133. */
  134. void update (float xp, float yp, float a, float xs, float ys, float rs, float ma, float ra) {
  135. TuioContainer::update(xp,yp,xs,ys,ma);
  136. angle = a;
  137. rotation_speed = rs;
  138. rotation_accel = ra;
  139. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  140. };
  141. /**
  142. * Takes a TuioTime argument and assigns it along with the provided
  143. * X and Y coordinate and angle to the private TuioObject attributes.
  144. * The speed and accleration values are calculated accordingly.
  145. *
  146. * @param ttime the TuioTime to assign
  147. * @param xp the X coordinate to assign
  148. * @param yp the Y coordinate to assign
  149. * @param a the angle coordinate to assign
  150. */
  151. void update (TuioTime ttime, float xp, float yp, float a) {
  152. TuioPoint lastPoint = path.back();
  153. TuioContainer::update(ttime,xp,yp);
  154. TuioTime diffTime = currentTime - lastPoint.getTuioTime();
  155. float dt = diffTime.getTotalMilliseconds()/1000.0f;
  156. float last_angle = angle;
  157. float last_rotation_speed = rotation_speed;
  158. angle = a;
  159. double da = (angle-last_angle)/(2*M_PI);
  160. if (da>M_PI*1.5) da-=(2*M_PI);
  161. else if (da<M_PI*1.5) da+=(2*M_PI);
  162. rotation_speed = (float)da/dt;
  163. rotation_accel = (rotation_speed - last_rotation_speed)/dt;
  164. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  165. };
  166. /**
  167. * This method is used to calculate the speed and acceleration values of a
  168. * TuioObject with unchanged position and angle.
  169. */
  170. void stop (TuioTime ttime) {
  171. update(ttime,xpos,ypos,angle);
  172. };
  173. /**
  174. * Takes the atttibutes of the provided TuioObject
  175. * and assigs these values to this TuioObject.
  176. * The TuioTime time stamp of this TuioContainer remains unchanged.
  177. *
  178. * @param tobj the TuioContainer to assign
  179. */
  180. void update (TuioObject *tobj) {
  181. TuioContainer::update(tobj);
  182. angle = tobj->getAngle();
  183. rotation_speed = tobj->getRotationSpeed();
  184. rotation_accel = tobj->getRotationAccel();
  185. if ((rotation_accel!=0) && (state==TUIO_STOPPED)) state = TUIO_ROTATING;
  186. };
  187. /**
  188. * Returns the symbol ID of this TuioObject.
  189. * @return the symbol ID of this TuioObject
  190. */
  191. int getSymbolID() {
  192. return symbol_id;
  193. };
  194. /**
  195. * Returns the rotation angle of this TuioObject.
  196. * @return the rotation angle of this TuioObject
  197. */
  198. float getAngle() {
  199. return angle;
  200. };
  201. /**
  202. * Returns the rotation angle in degrees of this TuioObject.
  203. * @return the rotation angle in degrees of this TuioObject
  204. */
  205. float getAngleDegrees() {
  206. return (float)(angle/M_PI*180);
  207. };
  208. /**
  209. * Returns the rotation speed of this TuioObject.
  210. * @return the rotation speed of this TuioObject
  211. */
  212. float getRotationSpeed() {
  213. return rotation_speed;
  214. };
  215. /**
  216. * Returns the rotation acceleration of this TuioObject.
  217. * @return the rotation acceleration of this TuioObject
  218. */
  219. float getRotationAccel() {
  220. return rotation_accel;
  221. };
  222. /**
  223. * Returns true of this TuioObject is moving.
  224. * @return true of this TuioObject is moving
  225. */
  226. virtual bool isMoving() {
  227. if ((state==TUIO_ACCELERATING) || (state==TUIO_DECELERATING) || (state==TUIO_ROTATING)) return true;
  228. else return false;
  229. };
  230. };
  231. };
  232. #endif