TuioCommunicator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. class TuioCommunicator
  16. {
  17. private TuioServer server;
  18. private Dictionary<int, TuioCursor> tcursors;
  19. private Dictionary<int, List<TuioObject>> tobjects;
  20. public TuioCommunicator(string host, int port)
  21. {
  22. server = new TuioServer(host, port);
  23. tcursors = new Dictionary<int, TuioCursor>();
  24. tobjects = new Dictionary<int, List<TuioObject>>();
  25. }
  26. public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
  27. {
  28. server.initFrame();
  29. FrameData frameData = e.FrameData;
  30. lock (frameData)
  31. {
  32. if (frameData.ResetFlag)
  33. reset();
  34. foreach (TouchEvent te in frameData.TouchEvents)
  35. {
  36. switch (te.Type)
  37. {
  38. case TouchEventType.Down:
  39. TuioCursor tcur = server.addTuioCursor(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  40. tcursors.Add(te.Touch.TrackID, tcur);
  41. break;
  42. case TouchEventType.Move:
  43. server.updateTuioCursor(tcursors[te.Touch.TrackID], te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
  44. break;
  45. case TouchEventType.Up:
  46. server.removeTuioCursor(tcursors[te.Touch.TrackID]);
  47. tcursors.Remove(te.Touch.TrackID);
  48. break;
  49. }
  50. }
  51. List<int> updatedIDs = new List<int>();
  52. foreach (Palm palm in frameData.TrackedPalms)
  53. {
  54. if (tobjects.Keys.Contains(palm.TrackID))
  55. {
  56. // update / move
  57. List<TuioObject> palmTobjs = tobjects[palm.TrackID];
  58. Vector2D[] corners = palm.Quad.Corners;
  59. for (int i = 0; i < 4; i++)
  60. server.updateTuioObject(palmTobjs[i], corners[i].X, corners[i].Y);
  61. updatedIDs.Add(palm.TrackID);
  62. }
  63. else
  64. {
  65. // add / create
  66. List<TuioObject> palmTobjs = new List<TuioObject>();
  67. Vector2D[] corners = palm.Quad.Corners;
  68. for (int i = 0; i < 4; i++)
  69. palmTobjs.Add(server.addTuioObject(corners[i].X, corners[i].Y, palm.TrackID + 0.1f*i));
  70. tobjects.Add(palm.TrackID, palmTobjs);
  71. updatedIDs.Add(palm.TrackID);
  72. }
  73. }
  74. // remove
  75. List<int> ids = tobjects.Keys.ToList();
  76. for (int i = ids.Count - 1; i >= 0;i-- )
  77. {
  78. int id = ids[i];
  79. if (!updatedIDs.Contains(id))
  80. {
  81. foreach (TuioObject tobj in tobjects[id])
  82. server.removeTuioObject(tobj);
  83. tobjects.Remove(id);
  84. }
  85. }
  86. }
  87. server.commitFrame();
  88. }
  89. public void close()
  90. {
  91. server.close();
  92. }
  93. public void reset()
  94. {
  95. foreach (int id in tcursors.Keys)
  96. server.removeTuioCursor(tcursors[id]);
  97. tcursors.Clear();
  98. foreach (int id in tobjects.Keys)
  99. foreach (TuioObject tobj in tobjects[id])
  100. server.removeTuioObject(tobj);
  101. tobjects.Clear();
  102. }
  103. public static bool tryParseIPAddress(String ipIn, out String ipOut)
  104. {
  105. IPAddress ipAddress;
  106. bool result = (IPAddress.TryParse(ipIn, out ipAddress) && ipAddress.AddressFamily == AddressFamily.InterNetwork);
  107. if (result)
  108. ipOut = ipAddress.ToString();
  109. else
  110. ipOut = null;
  111. return result;
  112. }
  113. public static bool tryParsePort(String portIn, out Int16 portOut)
  114. {
  115. return Int16.TryParse(portIn, out portOut);
  116. }
  117. }
  118. }