MovingAveragePosition.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // ReSharper disable ParameterHidesMember
  2. namespace ARLocation.Utils
  3. {
  4. public class MovingAveragePosition
  5. {
  6. public DVector3 CalculateAveragePosition()
  7. {
  8. return mPosition;
  9. }
  10. public double aMin = 2.0;
  11. public double aMax = 10.0;
  12. public double cutoff = 0.01;
  13. public double alpha = 0.25;
  14. private DVector3 mPosition;
  15. private bool first = true;
  16. private double Weight(double a, double aMin, double aMax, double cutoff = 0.01)
  17. {
  18. if (a <= aMin)
  19. {
  20. return 1.0;
  21. }
  22. if (a >= aMax)
  23. {
  24. return 0.0;
  25. }
  26. var lambda = System.Math.Log(1 / cutoff) / (aMax - aMin);
  27. return System.Math.Exp(-lambda * (a - aMin));
  28. }
  29. public void AddEntry(DVector3 position, double accuracy)
  30. {
  31. if (first)
  32. {
  33. mPosition = position;
  34. first = false;
  35. }
  36. else
  37. {
  38. var b = Weight(accuracy, aMin, aMax, cutoff);
  39. var a = alpha * b;
  40. mPosition = a * position + (1 - a) * mPosition;
  41. }
  42. }
  43. public void Rest()
  44. {
  45. first = true;
  46. mPosition = new DVector3();
  47. }
  48. }
  49. }