TuioCommunicator.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using bbiwarg.Recognition.TouchRecognition;
  9. using bbiwarg.Recognition.PalmRecognition;
  10. using bbiwarg.Utility;
  11. using bbiwarg.Input.InputHandling;
  12. using TUIO;
  13. namespace bbiwarg.TUIO
  14. {
  15. /// <summary>
  16. /// TuioCommunicator sends generated touch events and palm grid coordinates to tuio clients using a <see cref="TuioServer"/>.
  17. /// </summary>
  18. class TuioCommunicator
  19. {
  20. /// <summary>
  21. /// the tuio server
  22. /// </summary>
  23. private TuioServer server;
  24. /// <summary>
  25. /// dictionary of lists of tuio cursors indexed by touch id
  26. /// </summary>
  27. private Dictionary<int, TuioCursor> tcursors;
  28. /// <summary>
  29. /// dictionary of lists of tuio objects indexed by palm id
  30. /// </summary>
  31. private Dictionary<int, List<TuioObject>> tobjects;
  32. /// <summary>
  33. /// Constructs a TuioCommunicator.
  34. /// </summary>
  35. /// <param name="host">server ip</param>
  36. /// <param name="port">server port</param>
  37. public TuioCommunicator(string host, int port)
  38. {
  39. server = new TuioServer(host, port);
  40. tcursors = new Dictionary<int, TuioCursor>();
  41. tobjects = new Dictionary<int, List<TuioObject>>();
  42. }
  43. /// <summary>
  44. /// Handles the event that a new frame is finished processing by updating the cursors and objects and sending them.
  45. /// </summary>
  46. /// <param name="sender">event sender</param>
  47. /// <param name="e">event arguments</param>
  48. public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
  49. {
  50. server.initFrame();
  51. FrameData frameData = e.FrameData;
  52. lock (frameData)
  53. {
  54. if (frameData.ResetFlag)
  55. reset();
  56. foreach (TouchEvent te in frameData.TouchEvents)
  57. {
  58. switch (te.Type)
  59. {
  60. case TouchEventType.Down:
  61. TuioCursor tcur = server.addTuioCursor(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  62. tcursors.Add(te.Touch.TrackID, tcur);
  63. break;
  64. case TouchEventType.Move:
  65. server.updateTuioCursor(tcursors[te.Touch.TrackID], te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  66. break;
  67. case TouchEventType.Up:
  68. server.removeTuioCursor(tcursors[te.Touch.TrackID]);
  69. tcursors.Remove(te.Touch.TrackID);
  70. break;
  71. }
  72. }
  73. List<int> updatedIDs = new List<int>();
  74. foreach (Palm palm in frameData.TrackedPalms)
  75. {
  76. if (tobjects.Keys.Contains(palm.TrackID))
  77. {
  78. // update / move
  79. List<TuioObject> palmTobjs = tobjects[palm.TrackID];
  80. Vector2D[] corners = palm.Quad.Corners;
  81. for (int i = 0; i < 4; i++)
  82. server.updateTuioObject(palmTobjs[i], corners[i].X, corners[i].Y);
  83. updatedIDs.Add(palm.TrackID);
  84. }
  85. else
  86. {
  87. // add / create
  88. List<TuioObject> palmTobjs = new List<TuioObject>();
  89. Vector2D[] corners = palm.Quad.Corners;
  90. for (int i = 0; i < 4; i++)
  91. palmTobjs.Add(server.addTuioObject(corners[i].X, corners[i].Y, palm.TrackID + 0.1f*i));
  92. tobjects.Add(palm.TrackID, palmTobjs);
  93. updatedIDs.Add(palm.TrackID);
  94. }
  95. }
  96. // remove
  97. List<int> ids = tobjects.Keys.ToList();
  98. for (int i = ids.Count - 1; i >= 0;i-- )
  99. {
  100. int id = ids[i];
  101. if (!updatedIDs.Contains(id))
  102. {
  103. foreach (TuioObject tobj in tobjects[id])
  104. server.removeTuioObject(tobj);
  105. tobjects.Remove(id);
  106. }
  107. }
  108. }
  109. server.commitFrame();
  110. }
  111. /// <summary>
  112. /// Closes the server.
  113. /// </summary>
  114. public void close()
  115. {
  116. server.close();
  117. }
  118. /// <summary>
  119. /// Resets the server by removing all cursors and objects.
  120. /// </summary>
  121. public void reset()
  122. {
  123. foreach (int id in tcursors.Keys)
  124. server.removeTuioCursor(tcursors[id]);
  125. tcursors.Clear();
  126. foreach (int id in tobjects.Keys)
  127. foreach (TuioObject tobj in tobjects[id])
  128. server.removeTuioObject(tobj);
  129. tobjects.Clear();
  130. }
  131. /// <summary>
  132. /// Tries to parse an ip address string.
  133. /// </summary>
  134. /// <param name="ipIn">the ip address string to parse</param>
  135. /// <param name="ipOut">out parameter for the ip address in *.*.*.* format if ipIn is valid and null otherwise</param>
  136. /// <returns>true iff ipIn is a valid ip address</returns>
  137. public static bool tryParseIPAddress(String ipIn, out String ipOut)
  138. {
  139. IPAddress ipAddress;
  140. bool result = (IPAddress.TryParse(ipIn, out ipAddress) && ipAddress.AddressFamily == AddressFamily.InterNetwork);
  141. if (result)
  142. ipOut = ipAddress.ToString();
  143. else
  144. ipOut = null;
  145. return result;
  146. }
  147. /// <summary>
  148. /// Tries to parse a port string.
  149. /// </summary>
  150. /// <param name="portIn">the port string to parse</param>
  151. /// <param name="portOut">out parameter for the port as an Int16 if portIn is valid and undefined otherwise</param>
  152. /// <returns>true iff portIn is a valid port</returns>
  153. public static bool tryParsePort(String portIn, out Int16 portOut)
  154. {
  155. return Int16.TryParse(portIn, out portOut);
  156. }
  157. }
  158. }