TuioContainer.cs 11 KB

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