123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Recognition.Tracking;
- using bbiwarg.Utility;
- namespace bbiwarg.Recognition.TouchRecognition
- {
- public delegate void TouchEventHandler(object sender, TouchEventArgs e);
- public class TouchEventArgs : EventArgs
- {
- public int TrackID { get; private set; }
- public Vector2D AbsolutePosition { get; private set; }
- public Vector2D RelativePosition { get; private set; }
- public TouchEventArgs(int trackID, Vector2D absolutePosition, Vector2D relativePosition)
- {
- TrackID = trackID;
- AbsolutePosition = absolutePosition;
- RelativePosition = relativePosition;
- }
- }
- class TrackedTouchEvent : TrackedObject<TouchEvent>
- {
- private Kalman2DPositionFilter absolutePositionKalman;
- private Kalman2DPositionFilter relativePositionKalman;
- private int fingerID;
- private bool isTouchActive;
- public Vector2D AbsolutePositionPrediction { get { return absolutePositionKalman.getPrediction(); } }
- public Vector2D RelativePositionPrediction { get { return relativePositionKalman.getPrediction(); } }
- public event TouchEventHandler TouchDown;
- public event TouchEventHandler TouchMove;
- public event TouchEventHandler TouchUp;
- public TrackedTouchEvent(int id, TouchEvent detectedTouchEvent, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
- : base(id, detectedTouchEvent, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
- {
- absolutePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
- absolutePositionKalman.setInitialPosition(detectedTouchEvent.AbsolutePosition);
- relativePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
- relativePositionKalman.setInitialPosition(detectedTouchEvent.RelativePosition);
- fingerID = detectedTouchEvent.Finger.TrackID;
- isTouchActive = false;
- logStateChange();
- }
- public override void updateFrame(TouchEvent detectedTouchEvent)
- {
- base.updateFrame(detectedTouchEvent);
- if (NumFramesInCurrentState == 1)
- logStateChange();
- if (detectedTouchEvent != null)
- {
- absolutePositionKalman.getCorrectedPosition(detectedTouchEvent.AbsolutePosition);
- relativePositionKalman.getCorrectedPosition(detectedTouchEvent.RelativePosition);
- if (CurrentState == TrackingState.Tracked)
- {
- if (!isTouchActive)
- TriggerTouchDown(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
- else
- TriggerTouchMove(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
- }
- }
- else if (isTouchActive && CurrentState == TrackingState.Deleted)
- TriggerTouchUp(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
- }
- public override float calculateSimilarity(TouchEvent detectedTouchEvent)
- {
- float fingerSimilarity = (fingerID == detectedTouchEvent.Finger.TrackID) ? 1 : 0;
- float absolutePositionSimilarity = getPositionSimilarity(AbsolutePositionPrediction, detectedTouchEvent.AbsolutePosition, Parameters.TouchTrackerMaxAbsolutePositionMove);
- float relativePositionSimilarity = getPositionSimilarity(RelativePositionPrediction, detectedTouchEvent.RelativePosition, Parameters.TouchTrackerMaxRelativePositionMove);
- float similarity = fingerSimilarity * absolutePositionSimilarity * relativePositionSimilarity;
- return similarity;
- }
- private void logStateChange()
- {
- String stateAsString = CurrentState.ToString().ToLower();
- Logger.log(String.Format("TouchEvent #{0} {1}", this.ID, stateAsString), LogSubject.TouchTracker);
- }
- private void TriggerTouchDown(TouchEventArgs e)
- {
- if (TouchDown != null)
- {
- isTouchActive = true;
- TouchDown(this, e);
- Logger.log(String.Format("TouchEvent #{0} TouchDown at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
- }
- }
- private void TriggerTouchMove(TouchEventArgs e)
- {
- if (TouchMove != null)
- {
- TouchMove(this, e);
- Logger.log(String.Format("TouchEvent #{0} TouchMove at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
- }
- }
- private void TriggerTouchUp(TouchEventArgs e)
- {
- if (TouchUp != null)
- {
- isTouchActive = false;
- TouchUp(this, e);
- Logger.log(String.Format("TouchEvent #{0} TouchUp at {1}", this.ID, e.RelativePosition), LogSubject.TouchEvents);
- }
- }
- }
- }
|