|
@@ -6,73 +6,79 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using bbiwarg.Images;
|
|
|
using bbiwarg.Graphics;
|
|
|
+using bbiwarg.Recognition.Tracking;
|
|
|
+using bbiwarg.Utility;
|
|
|
+using TUIO;
|
|
|
|
|
|
namespace bbiwarg.Recognition.TouchRecognition
|
|
|
{
|
|
|
- class TouchTracker
|
|
|
+ class TouchTracker : Tracker<TouchEvent>
|
|
|
{
|
|
|
- private OutputImage outputImage;
|
|
|
- private List<TouchEvent>[] detectedTouchEvents;
|
|
|
- private int framesUntilTracked;
|
|
|
- public List<TouchEvent> TrackedTouchEvents;
|
|
|
+ public List<TouchEvent> TouchEvents { get { return TrackedObjects; } }
|
|
|
+ private TuioServer tuioServer;
|
|
|
+ private Dictionary<int, TuioCursor> tuioCursors;
|
|
|
+ private TouchEventVisualizer touchEventVisualizer;
|
|
|
|
|
|
- public TouchTracker()
|
|
|
+ public TouchTracker(TuioServer tuioServer, TouchEventVisualizer touchEventVisualizer)
|
|
|
+ : base(Constants.TouchEventNumFramesDetectedUntilTracked, Constants.TouchEventNumFramesLostUntilDeleted, Constants.TouchEventMinSimilarityForTracking)
|
|
|
{
|
|
|
- reset();
|
|
|
+ this.tuioServer = tuioServer;
|
|
|
+ this.tuioCursors = new Dictionary<int, TuioCursor>();
|
|
|
+ this.touchEventVisualizer = touchEventVisualizer;
|
|
|
}
|
|
|
|
|
|
- public void reset() {
|
|
|
- framesUntilTracked = Constants.TouchEventNumFramesUntilTracked;
|
|
|
- detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
|
|
|
-
|
|
|
- for (int i = 0; i < framesUntilTracked; i++)
|
|
|
- {
|
|
|
- detectedTouchEvents[i] = new List<TouchEvent>();
|
|
|
- }
|
|
|
+ public new void reset() {
|
|
|
+ tuioCursors.Clear();
|
|
|
+ touchEventVisualizer.Reset();
|
|
|
+ base.reset();
|
|
|
}
|
|
|
|
|
|
- public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame, OutputImage outputImage)
|
|
|
+ public new void updateFrame(List<TouchEvent> detectedObjects)
|
|
|
{
|
|
|
- this.outputImage = outputImage;
|
|
|
-
|
|
|
- for (int i = (framesUntilTracked - 1); i > 0; i--)
|
|
|
- {
|
|
|
- detectedTouchEvents[i] = detectedTouchEvents[i - 1];
|
|
|
- }
|
|
|
- detectedTouchEvents[0] = touchEventsThisFrame;
|
|
|
-
|
|
|
-
|
|
|
- findTrackedTouches();
|
|
|
+ tuioServer.initFrame();
|
|
|
+ base.updateFrame(detectedObjects);
|
|
|
+ tuioServer.commitFrame();
|
|
|
+ touchEventVisualizer.updateImage();
|
|
|
}
|
|
|
|
|
|
- private void findTrackedTouches()
|
|
|
+ protected override void onStateChanged(object sender, TrackingStateChangeEventArgs e)
|
|
|
{
|
|
|
- TrackedTouchEvents = new List<TouchEvent>();
|
|
|
+ TrackableObjectHistory<TouchEvent> history = (TrackableObjectHistory<TouchEvent>)sender;
|
|
|
|
|
|
- foreach (TouchEvent te in detectedTouchEvents[0])
|
|
|
+ TouchEvent currentTouchEvent = history.LastObject;
|
|
|
+ if (currentTouchEvent is PalmTouchEvent)
|
|
|
{
|
|
|
- bool tracked = true;
|
|
|
- for (int i = 1; i < framesUntilTracked; i++)
|
|
|
+ PalmTouchEvent currentPalmTouchEvent = (PalmTouchEvent)currentTouchEvent;
|
|
|
+ Vector2D currentRelativePosition = currentPalmTouchEvent.RelativePalmPosition;
|
|
|
+ TuioCursor tuioCursor;
|
|
|
+ switch (e.NextState)
|
|
|
{
|
|
|
- if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
|
|
|
+ case TrackingState.Tracked:
|
|
|
+ if (tuioCursors.TryGetValue(history.ID, out tuioCursor))
|
|
|
+ {
|
|
|
+ tuioServer.updateTuioCursor(tuioCursor, currentRelativePosition.X, currentRelativePosition.Y);
|
|
|
+ touchEventVisualizer.updateTouchEvent(history.ID, currentRelativePosition);
|
|
|
+ Logger.log("TouchEvent #" + history.ID.ToString() + " touchMove to (" + currentRelativePosition.X + ", " + currentRelativePosition.Y + ")", LogSubject.TouchTracker);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TuioCursor newTuioCursor = tuioServer.addTuioCursor(currentRelativePosition.X, currentRelativePosition.Y);
|
|
|
+ tuioCursors.Add(history.ID, newTuioCursor);
|
|
|
+ touchEventVisualizer.addTouchEvent(history.ID, currentRelativePosition);
|
|
|
+ Logger.log("TouchEvent #" + history.ID.ToString() + " touchDown at (" + currentRelativePosition.X + ", " + currentRelativePosition.Y + ")", LogSubject.TouchTracker);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TrackingState.Delete:
|
|
|
+ if (tuioCursors.TryGetValue(history.ID, out tuioCursor))
|
|
|
+ {
|
|
|
+ tuioServer.removeTuioCursor(tuioCursor);
|
|
|
+ tuioCursors.Remove(history.ID);
|
|
|
+ touchEventVisualizer.removeTouchEvent(history.ID);
|
|
|
+ Logger.log("TouchEvent #" + history.ID.ToString() + " touchUp at (" + currentRelativePosition.X + ", " + currentRelativePosition.Y + ")", LogSubject.TouchTracker);
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
- if (tracked)
|
|
|
- {
|
|
|
- outputImage.fillCircle(te.Position.IntX, te.Position.IntY, 5, Color.Yellow);
|
|
|
- TrackedTouchEvents.Add(te);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private bool hasSimilarTouchEventInFrame(TouchEvent touchEvent, int frame)
|
|
|
- {
|
|
|
- foreach (TouchEvent te in detectedTouchEvents[frame])
|
|
|
- {
|
|
|
- if (touchEvent.isSimilarTo(te))
|
|
|
- return true;
|
|
|
}
|
|
|
-
|
|
|
- return false;
|
|
|
}
|
|
|
}
|
|
|
}
|