using BBIWARG.Input.InputHandling;
using BBIWARG.Recognition.Tracking;
using BBIWARG.Utility;
using System.Collections.Generic;
namespace BBIWARG.Recognition.TouchRecognition
{
///
/// Keeps track of touches over a period of time and generates touch events
///
internal class TouchTracker : Tracker
{
///
/// the touch events in the current frame
///
private List touchEvents;
///
/// Initializes a new instance of the TouchTracker class.
///
/// Size of the input image.
public TouchTracker(ImageSize imageSize)
: base(imageSize)
{
touchEvents = new List();
}
///
/// Calculates the similarity [0-1] of a tracked touch and a detected touch.
///
/// the tracked touch
/// the detected touch
/// the similarity
public override float calculateSimilarity(TrackedTouch trackedTouch, Touch detectedTouch)
{
return (trackedTouch.FingerID == detectedTouch.Finger.TrackID) ? 1 : 0;
}
///
/// Gets all accumulated TouchEvents since the last flush.
///
/// all accumulated touchEvents
public List flushTouchEvents()
{
List flushedTouchEvents = touchEvents;
touchEvents = new List();
return flushedTouchEvents;
}
///
/// Updates the tracked touches, and stores the (optimized) touches in frameData.trackedTouches and the touch events in frameData.touchEvents.
///
/// the current frame
public void trackTouches(FrameData frameData)
{
trackObjects(frameData.DetectedTouches);
frameData.TrackedTouches = getOptimizedTouches();
frameData.TouchEvents = flushTouchEvents();
}
///
/// Creates a new TrackedTouch
///
/// the detected touch
/// a new TrackedTouch
protected override TrackedTouch createTrackedObject(Touch detectedObject)
{
TrackedTouch tt = new TrackedTouch(idPool.getNextUnusedID(), detectedObject, Parameters.TouchTrackerNumFramesDetectedUntilTracked, Parameters.TouchTrackerNumFramesLostUntilDeleted);
tt.TouchEvent += handleTouchEvent;
return tt;
}
///
/// Get all optimized representations of all tracked touches
///
/// all optimized tracked touches
private List getOptimizedTouches()
{
List optimizedTouchs = new List();
foreach (TrackedTouch tp in TrackedObjects)
{
if (tp.IsTouchActive)
optimizedTouchs.Add(tp.OptimizedTouch);
}
return optimizedTouchs;
}
///
/// Handles a new TouchEvent
///
/// the sender of the event
/// the touch event
private void handleTouchEvent(object sender, TouchEvent e)
{
touchEvents.Add(e);
}
}
}