PointCorrector.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Linq;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /*
  6. *需要解决的问题:渲染的点过多时App会卡死
  7. *解决方案:因为点集合的最终形态是球体,所以可按角度划分区间来控制点的密度
  8. */
  9. public class PointCorrector {
  10. Ellipse ellipse;
  11. o0MagneticCalibraterEllipsoidFitting MagCalibrater;
  12. public static PointCorrector ins;
  13. public PointCorrector(
  14. Ellipse ellipse,
  15. o0MagneticCalibraterEllipsoidFitting MagCalibrater
  16. ) {
  17. this.ellipse = ellipse;
  18. this.MagCalibrater = MagCalibrater;
  19. }
  20. const int correctNeedPointCount = 3600; //需要收集多少点才开始进行剔除
  21. int angleDivisionValue = 1; //多少角度分割为1个区间
  22. const int angleDivisionValueMax = 6; //最大多少角度分割为1个区间
  23. const float angleDivisionValueUpThreshold = 0.9f; //一轮点的剔除后,剩余的点超过百分之几时就加大角度分隔值
  24. public void Update(Vector3 centerPoint) {
  25. List<Vector3> li = this.ellipse.arrayList;
  26. if (li.Count > correctNeedPointCount) {
  27. HashSet<string> angleSet = new HashSet<string>();
  28. li.RemoveAll(p => {
  29. Vector3 angle = Quaternion.LookRotation(p - centerPoint, Vector3.up).eulerAngles;
  30. string key = Mathf.FloorToInt(angle.x / angleDivisionValue) + "_" + Mathf.FloorToInt(angle.y / angleDivisionValue);
  31. if (angleSet.Contains(key)) {
  32. return true;
  33. } else {
  34. angleSet.Add(key);
  35. return false;
  36. }
  37. });
  38. Debug.LogWarning("剔除完成:剩余点数:" + li.Count);
  39. if (li.Count > correctNeedPointCount * angleDivisionValueUpThreshold) {
  40. angleDivisionValue = System.Math.Min(angleDivisionValue + 1, angleDivisionValueMax);
  41. Debug.LogWarning("切割值尝试提升,当前值为" + angleDivisionValue);
  42. }
  43. MagCalibrater.records = li.ToList<Vector3>();
  44. }
  45. }
  46. }