TuioContainer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. public const int TUIO_ACCELERATING = 1;
  33. public const int TUIO_ADDED = 0;
  34. public const int TUIO_DECELERATING = 2;
  35. public const int TUIO_REMOVED = 4;
  36. public const int TUIO_STOPPED = 3;
  37. protected float motion_accel;
  38. protected float motion_speed;
  39. protected List<TuioPoint> path;
  40. protected long session_id;
  41. /**
  42. * The X-axis velocity value.
  43. */
  44. protected int state;
  45. protected float x_speed;
  46. /**
  47. * The Y-axis velocity value.
  48. */
  49. protected float y_speed;
  50. /**
  51. * The motion speed value.
  52. */
  53. /**
  54. * The motion acceleration value.
  55. */
  56. /**
  57. * A Vector of TuioPoints containing all the previous positions of the TUIO component.
  58. */
  59. /**
  60. * Defines the ADDED state.
  61. */
  62. /**
  63. * Defines the ACCELERATING state.
  64. */
  65. /**
  66. * Defines the DECELERATING state.
  67. */
  68. /**
  69. * Defines the STOPPED state.
  70. */
  71. /**
  72. * Defines the REMOVED state.
  73. */
  74. /**
  75. * Reflects the current state of the TuioComponent
  76. */
  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 float getMotionAccel()
  146. {
  147. return motion_accel;
  148. }
  149. public float getMotionSpeed()
  150. {
  151. return motion_speed;
  152. }
  153. public List<TuioPoint> getPath()
  154. {
  155. return path;
  156. }
  157. public TuioPoint getPosition()
  158. {
  159. return new TuioPoint(xpos, ypos);
  160. }
  161. public long getSessionID()
  162. {
  163. return session_id;
  164. }
  165. public int getTuioState()
  166. {
  167. return state;
  168. }
  169. public float getXSpeed()
  170. {
  171. return x_speed;
  172. }
  173. public float getYSpeed()
  174. {
  175. return y_speed;
  176. }
  177. public bool isMoving()
  178. {
  179. if ((state == TUIO_ACCELERATING) || (state == TUIO_DECELERATING)) return true;
  180. else return false;
  181. }
  182. public void remove(TuioTime ttime)
  183. {
  184. currentTime = ttime;
  185. state = TUIO_REMOVED;
  186. }
  187. public void stop(TuioTime ttime)
  188. {
  189. update(ttime, this.xpos, this.ypos);
  190. }
  191. public new void update(TuioTime ttime, float xp, float yp)
  192. {
  193. TuioPoint lastPoint = path[path.Count - 1];
  194. base.update(ttime, xp, yp);
  195. TuioTime diffTime = currentTime - lastPoint.getTuioTime();
  196. float dt = diffTime.getTotalMilliseconds() / 1000.0f;
  197. float dx = this.xpos - lastPoint.getX();
  198. float dy = this.ypos - lastPoint.getY();
  199. float dist = (float)Math.Sqrt(dx * dx + dy * dy);
  200. float last_motion_speed = this.motion_speed;
  201. this.x_speed = dx / dt;
  202. this.y_speed = dy / dt;
  203. this.motion_speed = dist / dt;
  204. this.motion_accel = (motion_speed - last_motion_speed) / dt;
  205. path.Add(new TuioPoint(currentTime, xpos, ypos));
  206. if (motion_accel > 0) state = TUIO_ACCELERATING;
  207. else if (motion_accel < 0) state = TUIO_DECELERATING;
  208. else state = TUIO_STOPPED;
  209. }
  210. /**
  211. * This method is used to calculate the speed and acceleration values of
  212. * TuioContainers with unchanged positions.
  213. */
  214. /**
  215. * Takes a TuioTime argument and assigns it along with the provided
  216. * X and Y coordinate, X and Y velocity and acceleration
  217. * to the private TuioContainer attributes.
  218. *
  219. * @param ttime the TuioTime to assign
  220. * @param xp the X coordinate to assign
  221. * @param yp the Y coordinate to assign
  222. * @param xs the X velocity to assign
  223. * @param ys the Y velocity to assign
  224. * @param ma the acceleration to assign
  225. */
  226. public void update(TuioTime ttime, float xp, float yp, float xs, float ys, float ma)
  227. {
  228. base.update(ttime, xp, yp);
  229. x_speed = xs;
  230. y_speed = ys;
  231. motion_speed = (float)Math.Sqrt(x_speed * x_speed + y_speed * y_speed);
  232. motion_accel = ma;
  233. path.Add(new TuioPoint(currentTime, xpos, ypos));
  234. if (motion_accel > 0) state = TUIO_ACCELERATING;
  235. else if (motion_accel < 0) state = TUIO_DECELERATING;
  236. else state = TUIO_STOPPED;
  237. }
  238. /**
  239. * Assigns the provided X and Y coordinate, X and Y velocity and acceleration
  240. * to the private TuioContainer attributes. The TuioTime time stamp remains unchanged.
  241. *
  242. * @param xp the X coordinate to assign
  243. * @param yp the Y coordinate to assign
  244. * @param xs the X velocity to assign
  245. * @param ys the Y velocity to assign
  246. * @param ma the acceleration to assign
  247. */
  248. public void update(float xp, float yp, float xs, float ys, float ma)
  249. {
  250. base.update(xp, yp);
  251. x_speed = xs;
  252. y_speed = ys;
  253. motion_speed = (float)Math.Sqrt(x_speed * x_speed + y_speed * y_speed);
  254. motion_accel = ma;
  255. path.Add(new TuioPoint(currentTime, xpos, ypos));
  256. if (motion_accel > 0) state = TUIO_ACCELERATING;
  257. else if (motion_accel < 0) state = TUIO_DECELERATING;
  258. else state = TUIO_STOPPED;
  259. }
  260. /**
  261. * Takes the atttibutes of the provided TuioContainer
  262. * and assigs these values to this TuioContainer.
  263. * The TuioTime time stamp of this TuioContainer remains unchanged.
  264. *
  265. * @param tcon the TuioContainer to assign
  266. */
  267. public void update(TuioContainer tcon)
  268. {
  269. base.update(tcon.getX(), tcon.getY());
  270. x_speed = tcon.getXSpeed();
  271. y_speed = tcon.getYSpeed();
  272. motion_speed = (float)Math.Sqrt(x_speed * x_speed + y_speed * y_speed);
  273. motion_accel = tcon.getMotionAccel();
  274. path.Add(new TuioPoint(currentTime, xpos, ypos));
  275. if (motion_accel > 0) state = TUIO_ACCELERATING;
  276. else if (motion_accel < 0) state = TUIO_DECELERATING;
  277. else state = TUIO_STOPPED;
  278. }
  279. /**
  280. * Assigns the REMOVE state to this TuioContainer and sets
  281. * its TuioTime time stamp to the provided TuioTime argument.
  282. *
  283. * @param ttime the TuioTime to assign
  284. */
  285. /**
  286. * Returns the Session ID of this TuioContainer.
  287. * @return the Session ID of this TuioContainer
  288. */
  289. /**
  290. * Returns the X velocity of this TuioContainer.
  291. * @return the X velocity of this TuioContainer
  292. */
  293. /**
  294. * Returns the Y velocity of this TuioContainer.
  295. * @return the Y velocity of this TuioContainer
  296. */
  297. /**
  298. * Returns the position of this TuioContainer.
  299. * @return the position of this TuioContainer
  300. */
  301. /**
  302. * Returns the path of this TuioContainer.
  303. * @return the path of this TuioContainer
  304. */
  305. /**
  306. * Returns the motion speed of this TuioContainer.
  307. * @return the motion speed of this TuioContainer
  308. */
  309. /**
  310. * Returns the motion acceleration of this TuioContainer.
  311. * @return the motion acceleration of this TuioContainer
  312. */
  313. /**
  314. * Returns the TUIO state of this TuioContainer.
  315. * @return the TUIO state of this TuioContainer
  316. */
  317. /**
  318. * Returns true of this TuioContainer is moving.
  319. * @return true of this TuioContainer is moving
  320. */
  321. }
  322. }