123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using BBIWARG.Input.InputHandling;
- using BBIWARG.Recognition.PalmRecognition;
- using BBIWARG.Recognition.TouchRecognition;
- using BBIWARG.Utility;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using TUIO;
- namespace BBIWARG.TUIO
- {
-
-
-
- internal class TuioCommunicatorHack
- {
-
-
-
- private TuioServer server;
-
-
-
- private Dictionary<int, TuioCursor> tcursors;
-
-
-
- private Dictionary<int, List<TuioObject>> tobjects;
-
-
-
-
-
- public TuioCommunicatorHack(string host, int port)
- {
- server = new TuioServer(host, port);
- tcursors = new Dictionary<int, TuioCursor>();
- tobjects = new Dictionary<int, List<TuioObject>>();
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- public static bool tryParsePort(String portIn, out Int16 portOut)
- {
- return Int16.TryParse(portIn, out portOut);
- }
-
-
-
- public void close()
- {
- server.close();
- }
-
-
-
-
-
- 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)
- {
- Point p = getButton(te.Touch.RelativePosition.X, te.Touch.RelativePosition.Y);
- switch (te.Type)
- {
- case TouchEventType.Down:
- TuioCursor tcur = server.addTuioCursor(p.X, p.Y);
- tcursors.Add(te.Touch.TrackID, tcur);
- break;
- case TouchEventType.Move:
- server.updateTuioCursor(tcursors[te.Touch.TrackID], p.X, p.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))
- {
-
- List<TuioObject> palmTobjs = tobjects[palm.TrackID];
- Vector2D[] corners = palm.Quad.Corners;
- for (int i = 0; i < 4; i++)
- {
-
- int x0 = 0;
- int y0 = 0;
- int x1 = 0;
- int y1 = 0;
- if (i == 0)
- {
- x0 = (int)corners[0].X;
- y0 = (int)corners[0].Y;
- x1 = (int)corners[2].X;
- y1 = (int)corners[2].Y;
- }
- else if (i == 1)
- {
- x0 = (int)corners[1].X;
- y0 = (int)corners[1].Y;
- x1 = (int)corners[3].X;
- y1 = (int)corners[3].Y;
- }
- else if (i == 2)
- {
- x0 = (int)corners[2].X;
- y0 = (int)corners[2].Y;
- x1 = (int)corners[0].X;
- y1 = (int)corners[0].Y;
- }
- else if (i == 3)
- {
- x0 = (int)corners[3].X;
- y0 = (int)corners[3].Y;
- x1 = (int)corners[1].X;
- y1 = (int)corners[1].Y;
- }
-
- server.updateTuioObject(
- palmTobjs[i],
- corners[i].X,
- corners[i].Y,
- frameData.DepthImage.getDepthAtFixed(x0, y0, x1, y1));
-
- }
- updatedIDs.Add(palm.TrackID);
- }
- else
- {
-
- List<TuioObject> palmTobjs = new List<TuioObject>();
- Vector2D[] corners = palm.Quad.Corners;
- for (int i = 0; i < 4; i++)
- {
-
-
- int x0 = 0;
- int y0 = 0;
- int x1 = 0;
- int y1 = 0;
- if (i == 0)
- {
- x0 = (int)corners[0].X;
- y0 = (int)corners[0].Y;
- x1 = (int)corners[2].X;
- y1 = (int)corners[2].Y;
- }
- else if (i == 1)
- {
- x0 = (int)corners[1].X;
- y0 = (int)corners[1].Y;
- x1 = (int)corners[3].X;
- y1 = (int)corners[3].Y;
- }
- else if (i == 2)
- {
- x0 = (int)corners[2].X;
- y0 = (int)corners[2].Y;
- x1 = (int)corners[0].X;
- y1 = (int)corners[0].Y;
- }
- else if (i == 3)
- {
- x0 = (int)corners[3].X;
- y0 = (int)corners[3].Y;
- x1 = (int)corners[1].X;
- y1 = (int)corners[1].Y;
- }
-
- palmTobjs.Add(server.addTuioObject(
- corners[i].X,
- corners[i].Y,
- frameData.DepthImage.getDepthAtFixed(x0, y0, x1, y1)));
- }
- tobjects.Add(palm.TrackID, palmTobjs);
- updatedIDs.Add(palm.TrackID);
- }
- }
-
- 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();
- }
- private Point getButton(float rX , float rY)
- {
- int activeRow = (int)Math.Min(rY * Parameters.ActiveTouches.Count,
- Parameters.PalmGridDefaultNumRows - 1);
- int activeCol = (int)Math.Min(rX * Parameters.ActiveTouches.ElementAt(activeRow).Count,
- Parameters.ActiveTouches.ElementAt(activeRow).Count - 1);
- if (activeRow != Parameters.PalmSliderPos)
- return new Point(activeRow, activeCol);
- else
- return new Point(activeRow, Parameters.PalmSliderCurr);
- }
-
-
-
- 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();
- }
- }
- }
|