123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using bbiwarg.Recognition.TouchRecognition;
- using bbiwarg.Recognition.PalmRecognition;
- using bbiwarg.Utility;
- using bbiwarg.Input.InputHandling;
- using TUIO;
- namespace bbiwarg.TUIO
- {
- /// <summary>
- /// TuioCommunicator sends generated touch events and palm grid coordinates to tuio clients using a <see cref="TuioServer"/>.
- /// </summary>
- class TuioCommunicator
- {
- /// <summary>
- /// the tuio server
- /// </summary>
- private TuioServer server;
- /// <summary>
- /// dictionary of lists of tuio cursors indexed by touch id
- /// </summary>
- private Dictionary<int, TuioCursor> tcursors;
- /// <summary>
- /// dictionary of lists of tuio objects indexed by palm id
- /// </summary>
- private Dictionary<int, List<TuioObject>> tobjects;
- /// <summary>
- /// Constructs a TuioCommunicator.
- /// </summary>
- /// <param name="host">server ip</param>
- /// <param name="port">server port</param>
- public TuioCommunicator(string host, int port)
- {
- server = new TuioServer(host, port);
- tcursors = new Dictionary<int, TuioCursor>();
- tobjects = new Dictionary<int, List<TuioObject>>();
- }
- /// <summary>
- /// Handles the event that a new frame is finished processing by updating the cursors and objects and sending them.
- /// </summary>
- /// <param name="sender">event sender</param>
- /// <param name="e">event arguments</param>
- public void handleNewFrameData(object sender, NewProcessedFrameEventArgs e)
- {
- server.initFrame();
- FrameData frameData = e.FrameData;
- lock (frameData)
- {
- if (frameData.ResetFlag)
- reset();
- foreach (TouchEvent te in frameData.TouchEvents)
- {
- switch (te.Type)
- {
- case TouchEventType.Down:
- TuioCursor tcur = server.addTuioCursor(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
- tcursors.Add(te.Touch.TrackID, tcur);
- break;
- case TouchEventType.Move:
- server.updateTuioCursor(tcursors[te.Touch.TrackID], te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
- break;
- case TouchEventType.Up:
- server.removeTuioCursor(tcursors[te.Touch.TrackID]);
- tcursors.Remove(te.Touch.TrackID);
- break;
- }
- }
- List<int> updatedIDs = new List<int>();
- foreach (Palm palm in frameData.TrackedPalms)
- {
- if (tobjects.Keys.Contains(palm.TrackID))
- {
- // update / move
- List<TuioObject> palmTobjs = tobjects[palm.TrackID];
- Vector2D[] corners = palm.Quad.Corners;
- for (int i = 0; i < 4; i++)
- server.updateTuioObject(palmTobjs[i], corners[i].X, corners[i].Y);
- updatedIDs.Add(palm.TrackID);
- }
- else
- {
- // add / create
- List<TuioObject> palmTobjs = new List<TuioObject>();
- Vector2D[] corners = palm.Quad.Corners;
- for (int i = 0; i < 4; i++)
- palmTobjs.Add(server.addTuioObject(corners[i].X, corners[i].Y, palm.TrackID + 0.1f*i));
- tobjects.Add(palm.TrackID, palmTobjs);
- updatedIDs.Add(palm.TrackID);
- }
- }
- // remove
- List<int> ids = tobjects.Keys.ToList();
- for (int i = ids.Count - 1; i >= 0;i-- )
- {
- int id = ids[i];
- if (!updatedIDs.Contains(id))
- {
- foreach (TuioObject tobj in tobjects[id])
- server.removeTuioObject(tobj);
- tobjects.Remove(id);
- }
- }
- }
- server.commitFrame();
- }
- /// <summary>
- /// Closes the server.
- /// </summary>
- public void close()
- {
- server.close();
- }
- /// <summary>
- /// Resets the server by removing all cursors and objects.
- /// </summary>
- public void reset()
- {
- foreach (int id in tcursors.Keys)
- server.removeTuioCursor(tcursors[id]);
- tcursors.Clear();
- foreach (int id in tobjects.Keys)
- foreach (TuioObject tobj in tobjects[id])
- server.removeTuioObject(tobj);
- tobjects.Clear();
- }
- /// <summary>
- /// Tries to parse an ip address string.
- /// </summary>
- /// <param name="ipIn">the ip address string to parse</param>
- /// <param name="ipOut">out parameter for the ip address in *.*.*.* format if ipIn is valid and null otherwise</param>
- /// <returns>true iff ipIn is a valid ip address</returns>
- public static bool tryParseIPAddress(String ipIn, out String ipOut)
- {
- IPAddress ipAddress;
- bool result = (IPAddress.TryParse(ipIn, out ipAddress) && ipAddress.AddressFamily == AddressFamily.InterNetwork);
- if (result)
- ipOut = ipAddress.ToString();
- else
- ipOut = null;
- return result;
- }
- /// <summary>
- /// Tries to parse a port string.
- /// </summary>
- /// <param name="portIn">the port string to parse</param>
- /// <param name="portOut">out parameter for the port as an Int16 if portIn is valid and undefined otherwise</param>
- /// <returns>true iff portIn is a valid port</returns>
- public static bool tryParsePort(String portIn, out Int16 portOut)
- {
- return Int16.TryParse(portIn, out portOut);
- }
- }
- }
|