TrackableObjectHistory.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Utility;
  7. namespace bbiwarg.Recognition.Tracking
  8. {
  9. public delegate void TrackingStateChangedEventHandler(object sender, EventArgs e);
  10. class TrackableObjectHistory<T> where T : TrackableObject
  11. {
  12. private static List<int> usedIDs = new List<int>();
  13. public int ID { get; private set; }
  14. public List<T> TrackedObjects { get; private set; }
  15. public List<TrackingState> States { get; private set; }
  16. public T LastObject { get; private set; }
  17. public T CurrentObject { get { return TrackedObjects[TrackedObjects.Count - 1]; } }
  18. public TrackingState CurrentState { get { return States[States.Count - 1]; } }
  19. public int NumFramesInCurrentState { get; private set; }
  20. private int numFramesDetectedUntilTracked;
  21. private int numFramesLostUntilDeleted;
  22. private bool wasTrackedBefore;
  23. public event TrackingStateChangedEventHandler Detected;
  24. public event TrackingStateChangedEventHandler Tracked;
  25. public event TrackingStateChangedEventHandler Retracked;
  26. public event TrackingStateChangedEventHandler Lost;
  27. public event TrackingStateChangedEventHandler Deleted;
  28. protected virtual void OnDetect(EventArgs e) { if (Detected != null) Detected(this, e); }
  29. protected virtual void OnTrack(EventArgs e) { if (Tracked != null) Tracked(this, e); }
  30. protected virtual void OnRetrack(EventArgs e) { if (Retracked != null) Retracked(this, e); }
  31. protected virtual void OnLoose(EventArgs e) { if (Lost != null) Lost(this, e); }
  32. protected virtual void OnDelete(EventArgs e) { if (Deleted != null) Deleted(this, e); }
  33. public TrackableObjectHistory(int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  34. {
  35. ID = getNextUnusedID();
  36. TrackedObjects = new List<T>();
  37. TrackedObjects.Add(null);
  38. States = new List<TrackingState>();
  39. States.Add(TrackingState.None);
  40. NumFramesInCurrentState = 1;
  41. this.numFramesDetectedUntilTracked = numFramesDetectedUntilTracked;
  42. this.numFramesLostUntilDeleted = numFramesLostUntilDeleted;
  43. this.wasTrackedBefore = false;
  44. }
  45. ~TrackableObjectHistory()
  46. {
  47. setIDUnused(ID);
  48. }
  49. public void addObjectToHistory(T detectedObject)
  50. {
  51. TrackingState previousState = CurrentState;
  52. TrackingState newState = TrackingState.None;
  53. //get newState
  54. if (detectedObject != null)
  55. {
  56. if (previousState == TrackingState.None)
  57. newState = TrackingState.Detected;
  58. else if (previousState == TrackingState.Lost)
  59. {
  60. if (wasTrackedBefore)
  61. newState = TrackingState.Tracked;
  62. else
  63. newState = TrackingState.Detected;
  64. }
  65. else if (previousState == TrackingState.Tracked)
  66. newState = TrackingState.Tracked;
  67. else if (previousState == TrackingState.Detected)
  68. {
  69. if (NumFramesInCurrentState == numFramesDetectedUntilTracked)
  70. newState = TrackingState.Tracked;
  71. else
  72. newState = TrackingState.Detected;
  73. }
  74. }
  75. else if (previousState == TrackingState.Lost && NumFramesInCurrentState == numFramesLostUntilDeleted)
  76. newState = TrackingState.Delete;
  77. else
  78. newState = TrackingState.Lost;
  79. //set trackedBefore
  80. if (newState == TrackingState.Tracked)
  81. wasTrackedBefore = true;
  82. //update numFramesInCurrentState
  83. if (newState == previousState)
  84. NumFramesInCurrentState++;
  85. else
  86. NumFramesInCurrentState = 1;
  87. //update lastObject
  88. if (detectedObject != null)
  89. {
  90. LastObject = detectedObject;
  91. detectedObject.setTrackID(ID);
  92. }
  93. //add current state+object
  94. TrackedObjects.Add(detectedObject);
  95. States.Add(newState);
  96. //fire event
  97. switch (newState) {
  98. case TrackingState.Detected:
  99. OnDetect(EventArgs.Empty);
  100. break;
  101. case TrackingState.Tracked:
  102. if (previousState == TrackingState.Lost)
  103. OnRetrack(EventArgs.Empty);
  104. else
  105. OnTrack(EventArgs.Empty);
  106. break;
  107. case TrackingState.Lost:
  108. OnLoose(EventArgs.Empty);
  109. break;
  110. case TrackingState.Delete:
  111. OnDelete(EventArgs.Empty);
  112. break;
  113. }
  114. }
  115. public static int getNextUnusedID()
  116. {
  117. int id = 1;
  118. while (usedIDs.Contains(id))
  119. id++;
  120. usedIDs.Add(id);
  121. return id;
  122. }
  123. public static void setIDUnused(int id)
  124. {
  125. usedIDs.Remove(id);
  126. }
  127. }
  128. }