PixelArea.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using o0;
  2. using o0InfraredLocate.ZIM;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using Unity.VisualScripting;
  7. using UnityEngine;
  8. using ZIM.Unity;
  9. using Color = UnityEngine.Color;
  10. namespace ZIM
  11. {
  12. internal class PixelArea
  13. {
  14. public static int gridLength = 16;
  15. //public static (int x, int y) gridSize;
  16. public static (int x, int y) GetGrid(Vector2 v)
  17. {
  18. var m = (int)(v.x / gridLength);
  19. var n = (int)(v.y / gridLength);
  20. return (m, n);
  21. }
  22. public static List<(double cos, double sin)> AngleMathList {
  23. get
  24. {
  25. if (angleMathList != null)
  26. return angleMathList;
  27. // 三角函数值记录在list中
  28. double step = angleStep * 0.017453292519943295;
  29. angleMathList = new List<(double cos, double sin)>();
  30. for (double i = 0; i < 6.2831853; i += step)
  31. {
  32. var cos = Math.Cos(i);
  33. var sin = Math.Sin(i);
  34. angleMathList.Add((cos, sin));
  35. }
  36. return angleMathList;
  37. }
  38. }
  39. static float angleStep = 11.25f;
  40. static List<(double cos, double sin)> angleMathList;
  41. public Vector2 Center;
  42. public float MaxRadius;
  43. public HashSet<Vector2> Pixels;
  44. HashSet<(int, int)> grids;
  45. public PixelArea(Vector2 location)
  46. {
  47. Center = location;
  48. Pixels = new HashSet<Vector2> { location };
  49. MaxRadius = 0;
  50. grids = new HashSet<(int, int)>();
  51. Add(location);
  52. }
  53. public PixelArea(IList<PixelArea> areas)
  54. {
  55. Center = Vector2.zero;
  56. Pixels = new HashSet<Vector2>();
  57. MaxRadius = 0;
  58. grids = new HashSet<(int, int)>();
  59. foreach (var a in areas)
  60. {
  61. Pixels.AddRange(a.Pixels);
  62. grids.AddRange(a.grids);
  63. }
  64. foreach (var p in Pixels)
  65. {
  66. Center += p;
  67. }
  68. Center /= Pixels.Count;
  69. foreach (var p in Pixels)
  70. {
  71. var radius = (p - Center).magnitude;
  72. if (radius > MaxRadius)
  73. MaxRadius = radius;
  74. }
  75. }
  76. public bool Include((int, int) grid) => grids.Contains(grid);
  77. public void Add(Vector2 point) => Add(point, GetGrid(point));
  78. public void Add(Vector2 point, (int x, int y) grid)
  79. {
  80. Center += (point - Center) / Pixels.Count;
  81. var radius = (point - Center).magnitude;
  82. if (radius > MaxRadius)
  83. MaxRadius = radius;
  84. Pixels.Add(point);
  85. grids.AddRange(new (int, int)[]
  86. {
  87. (grid.x + -1, grid.y + 0) , (grid.x + 1 , grid.y + 0),
  88. (grid.x + 1, grid.y + -1) , (grid.x + -1, grid.y + 1),
  89. (grid.x + 0, grid.y + -1) , (grid.x + 0 , grid.y + 1),
  90. (grid.x + -1, grid.y + -1) , (grid.x + 1 , grid.y + 1),
  91. grid
  92. });
  93. }
  94. public float TotalBrightness(Color[] pixels,Func<int,int,int> Vector2ToIndex, int outer_size = 3)
  95. {
  96. (int x, int y) rectMin = ((int)(Center.x - MaxRadius - outer_size), (int)(Center.y - MaxRadius - outer_size));
  97. (int x, int y) rectMax = ((int)(Center.x + MaxRadius + outer_size), (int)(Center.y + MaxRadius + outer_size));
  98. var total = 0f;
  99. Parallel.For(rectMin.x, rectMax.x, (i) =>
  100. {
  101. var t = 0;
  102. for (int j = rectMin.y; j < rectMax.y; j++)
  103. {
  104. var index = Vector2ToIndex(i, j);
  105. var b = pixels[index].Brightness(64);
  106. t += b;
  107. }
  108. lock (InfraredLocate.Locker)
  109. {
  110. total += t;
  111. }
  112. });
  113. //total /= (rectMax.y - rectMin.y) * (rectMax.x - rectMin.x);
  114. return total;
  115. }
  116. }
  117. }