InfraredSpot.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using ZIM.Unity;
  4. using Color = UnityEngine.Color;
  5. namespace ZIM
  6. {
  7. public class InfraredSpot
  8. {
  9. // 达到该数量才完成初始化
  10. //static int InitCount = 5;
  11. static int MaxVerifyFailLimit = 20;
  12. public static float MinVerifyLength = 240;
  13. // 返回null代表没有点,范围在[0, 1]内
  14. public Vector2? ScreenUV
  15. {
  16. get
  17. {
  18. if (ScreenLocation.HasValue)
  19. {
  20. return screenMap.UVNormalize(ScreenLocation.Value);
  21. }
  22. else
  23. {
  24. return null;
  25. }
  26. }
  27. }
  28. // 范围在摄像机的Size内
  29. public Vector2? CameraLocation {
  30. get
  31. {
  32. if (ScreenLocation.HasValue)
  33. {
  34. return screenMap.TransformToCamera(ScreenLocation.Value);
  35. }
  36. else
  37. return null;
  38. }
  39. }
  40. public Vector2? Predict { get; set; } // 为null时代表未初始化
  41. public InfraredMatch Match { get; private set; }
  42. public bool Disappear;
  43. //PixelSpotArea verifyArea;
  44. int verifyFailLimit;
  45. List<PixelSpotArea> spots;
  46. SimpleLocationEstimation estimation;
  47. Vector2? ScreenLocation;
  48. ScreenMap screenMap;
  49. public InfraredSpot(ScreenMap screenMap, InfraredMatch match)
  50. {
  51. this.screenMap = screenMap;
  52. MinVerifyLength = screenMap.UVSize.y / 5;
  53. Match = match;
  54. spots = new List<PixelSpotArea>();
  55. Reset();
  56. }
  57. public void Reset() {
  58. spots.Clear();
  59. estimation = new SimpleLocationEstimation(0.5f);
  60. ScreenLocation = null;
  61. Predict = null;
  62. Disappear = false;
  63. //verifyArea = null;
  64. verifyFailLimit = MaxVerifyFailLimit;
  65. }
  66. public bool Verify(List<PixelSpotArea> areas, Dictionary<InfraredMatch, PixelSpotArea> matchedArea)
  67. {
  68. if (Predict == null) // 未初始化
  69. return false;
  70. PixelSpotArea select = null;
  71. var minLength = float.MaxValue;
  72. var predict = Predict.Value;
  73. foreach (var i in areas)
  74. {
  75. var len = (i.Center - predict).magnitude;
  76. if ( len < MinVerifyLength)
  77. {
  78. if (len < minLength)
  79. {
  80. select = i;
  81. minLength = len;
  82. }
  83. }
  84. }
  85. if (select == null)
  86. {
  87. //Debug.Log(verifyFailLimit);
  88. verifyFailLimit--;
  89. if (verifyFailLimit == 0)
  90. Reset();
  91. return false;
  92. }
  93. //verifyArea = select;
  94. matchedArea[Match] = select;
  95. return true;
  96. }
  97. //public void UpdateByVerifyArea()
  98. //{
  99. // Update(verifyArea);
  100. //}
  101. // 为null时采用预测点
  102. public void Update(PixelSpotArea area)
  103. {
  104. if (area == null)
  105. {
  106. //throw new System.Exception("[InfraredSpot] Update Wrong");
  107. UpdateByPredict();
  108. return;
  109. }
  110. var predict = estimation.Update(area.Center);
  111. //ScreenLocation = area.Center;
  112. ScreenLocation = area.Center * 0.8f + predict * 0.2f;
  113. spots.Add(area);
  114. if (spots.Count > 15)
  115. {
  116. Predict = predict;
  117. spots.RemoveAt(0);
  118. }
  119. }
  120. void UpdateByPredict()
  121. {
  122. if (spots.Count != 0)
  123. {
  124. if (Predict == null) // 还未初始化,直接Reset
  125. Reset();
  126. else
  127. {
  128. // 采用预测点,但超出屏幕则Reset
  129. if (screenMap.UVInScreen(Predict.Value))
  130. {
  131. ScreenLocation = Predict;
  132. var newP = estimation.Update(Predict.Value);
  133. Predict = newP;
  134. }
  135. else
  136. Reset();
  137. }
  138. }
  139. }
  140. }
  141. }