123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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, TouchEvent e);
-
- public class TrackedTouch : TrackedObject<Touch>
- {
- private Kalman2DPositionFilter absolutePositionKalman;
- public Vector2D AbsolutePositionPrediction { get { return absolutePositionKalman.getPrediction(); } }
- public Touch OptimizedTouch { get; private set; }
- public bool IsTouchActive { get; private set; }
- public int FingerID { get; private set; }
- public event TouchEventHandler TouchEvent;
- public TrackedTouch(int id, Touch detectedTouch, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
- : base(id, detectedTouch, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
- {
- absolutePositionKalman = new Kalman2DPositionFilter(Parameters.TouchmXX, Parameters.TouchmXY, Parameters.TouchmYY);
- absolutePositionKalman.setInitialPosition(detectedTouch.AbsolutePosition);
- FingerID = detectedTouch.Finger.TrackID;
- IsTouchActive = false;
- logStateChange();
- }
- public override void updateFrame(Touch detectedTouch)
- {
- base.updateFrame(detectedTouch);
- if (NumFramesInCurrentState == 1)
- logStateChange();
- if (detectedTouch != null)
- {
- absolutePositionKalman.getCorrectedPosition(detectedTouch.AbsolutePosition);
- updateOptimizedTouch(detectedTouch);
- if (CurrentState == TrackingState.Tracked)
- {
- if (!IsTouchActive)
- TriggerTouchDown();
- else
- TriggerTouchMove();
- }
- }
- else if (IsTouchActive && CurrentState == TrackingState.Deleted)
- TriggerTouchUp();
- }
- private void updateOptimizedTouch(Touch detectedTouch)
- {
- OptimizedTouch = new Touch(AbsolutePositionPrediction, detectedTouch.Finger, detectedTouch.Palm);
- }
- private void logStateChange()
- {
- String stateAsString = CurrentState.ToString().ToLower();
- Logger.log(String.Format("Touch #{0} {1}", this.ID, stateAsString), LogSubject.TouchTracker);
- }
- private void TriggerTouchDown()
- {
- if (TouchEvent != null)
- {
- IsTouchActive = true;
- TouchEvent(this, new TouchEvent(TouchEventType.Down, OptimizedTouch));
- }
- }
- private void TriggerTouchMove()
- {
- if (TouchEvent != null)
- {
- TouchEvent(this, new TouchEvent(TouchEventType.Move, OptimizedTouch));
- }
- }
- private void TriggerTouchUp()
- {
- if (TouchEvent != null)
- {
- IsTouchActive = false;
- TouchEvent(this, new TouchEvent(TouchEventType.Up, OptimizedTouch));
- }
- }
- }
- }
|