TuioContainer.cs 9.0 KB

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