InfraredSpot.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. //PixelSpotArea verifyArea;
  43. int verifyFailLimit;
  44. List<PixelSpotArea> spots;
  45. SimpleLocationEstimation estimation;
  46. Vector2? ScreenLocation;
  47. ScreenMap screenMap;
  48. public InfraredSpot(ScreenMap screenMap, InfraredMatch match)
  49. {
  50. this.screenMap = screenMap;
  51. MinVerifyLength = screenMap.UVSize.y / 5;
  52. Match = match;
  53. spots = new List<PixelSpotArea>();
  54. Reset();
  55. }
  56. public void Reset() {
  57. spots.Clear();
  58. estimation = new SimpleLocationEstimation(0.5f);
  59. ScreenLocation = null;
  60. Predict = null;
  61. //verifyArea = null;
  62. verifyFailLimit = MaxVerifyFailLimit;
  63. }
  64. public bool Verify(List<PixelSpotArea> areas, Dictionary<InfraredMatch, PixelSpotArea> matchedArea)
  65. {
  66. if (Predict == null) // 未初始化
  67. return false;
  68. PixelSpotArea select = null;
  69. var minLength = float.MaxValue;
  70. var predict = Predict.Value;
  71. foreach (var i in areas)
  72. {
  73. var len = (i.Center - predict).magnitude;
  74. if ( len < MinVerifyLength)
  75. {
  76. if (len < minLength)
  77. {
  78. select = i;
  79. minLength = len;
  80. }
  81. }
  82. }
  83. if (select == null)
  84. {
  85. //Debug.Log(verifyFailLimit);
  86. verifyFailLimit--;
  87. if (verifyFailLimit == 0)
  88. Reset();
  89. return false;
  90. }
  91. //verifyArea = select;
  92. matchedArea[Match] = select;
  93. return true;
  94. }
  95. //public void UpdateByVerifyArea()
  96. //{
  97. // Update(verifyArea);
  98. //}
  99. public void UpdateByPredict()
  100. {
  101. if (spots.Count != 0)
  102. {
  103. if (Predict == null) // 还未初始化,直接Reset
  104. Reset();
  105. else
  106. {
  107. // 采用预测点,但超出屏幕则Reset
  108. if (screenMap.UVInScreen(Predict.Value))
  109. {
  110. ScreenLocation = Predict;
  111. var newP = estimation.Update(Predict.Value);
  112. Predict = newP;
  113. }
  114. else
  115. Reset();
  116. }
  117. }
  118. }
  119. // 为null时采用预测点
  120. public void Update(PixelSpotArea area)
  121. {
  122. if (area == null)
  123. throw new System.Exception("[InfraredSpot] Update Wrong");
  124. var predict = estimation.Update(area.Center);
  125. //ScreenLocation = area.Center;
  126. ScreenLocation = area.Center * 0.8f + predict * 0.2f;
  127. spots.Add(area);
  128. if (spots.Count > 15)
  129. {
  130. Predict = predict;
  131. spots.RemoveAt(0);
  132. }
  133. }
  134. }
  135. }