InfraredSpot.cs 4.4 KB

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