PersonManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Assets.StreetLight.Adapters;
  2. using Assets.StreetLight.Interfaces;
  3. using Assets.StreetLight.Poco;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using UnityEngine;
  11. namespace Assets.StreetLight.Scripts
  12. {
  13. public class PersonManager : MonoBehaviour
  14. {
  15. public ObservableCollection<Person> Persons { get; private set; }
  16. private PositionCalculator PositionCalculator;
  17. private IPersonDetector personDetector;
  18. public event EventHandler DetectionReady;
  19. /// <summary>
  20. /// Occurs when a new person has been detected that was not present before.
  21. /// </summary>
  22. public event EventHandler<Person> PersonAppeared;
  23. /// <summary>
  24. /// Occurs when a person can no longer be detected.
  25. /// </summary>
  26. public event EventHandler<Person> PersonDisappeared;
  27. void Start()
  28. {
  29. try
  30. {
  31. var homographyArray = GetHomographyArrayFromFile();
  32. PositionCalculator = new PositionCalculator(homographyArray);
  33. }
  34. catch (Exception ex)
  35. {
  36. Debug.LogWarning($"Could not load homography: {ex.Message} Calculating the unity position of a person will not be possible. (This is expected during calibration.)");
  37. }
  38. Persons = new ObservableCollection<Person>();
  39. personDetector = new ZedPersonDetector(FindObjectOfType<ZEDManager>());
  40. personDetector.PersonsDetected += InvokeDetectionReady;
  41. personDetector.PersonsDetected += PersonDetector_PersonsDetected;
  42. }
  43. private double[,] GetHomographyArrayFromFile()
  44. {
  45. var homographyString = File.ReadAllLines(Configuration.Instance.HomographyFilePath);
  46. var values = homographyString.Select(l => l.Split(' ').Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray()).ToArray();
  47. double[,] array = new double[3, 3];
  48. for (int i = 0; i < 3; i++)
  49. {
  50. for (int j = 0; j < 3; j++)
  51. {
  52. array[i, j] = values[i][j];
  53. }
  54. }
  55. return array;
  56. }
  57. private void InvokeDetectionReady(object sender, IEnumerable<Person> e)
  58. {
  59. DetectionReady?.Invoke(this, EventArgs.Empty);
  60. personDetector.PersonsDetected -= InvokeDetectionReady;
  61. }
  62. private void PersonDetector_PersonsDetected(object sender, IEnumerable<Person> e)
  63. {
  64. var detectedIds = new HashSet<int>();
  65. foreach (var detectedPerson in e)
  66. {
  67. var person = Persons.SingleOrDefault(p => p.Id == detectedPerson.Id);
  68. if (person == null)
  69. {
  70. person = new Person(detectedPerson.Id, PositionCalculator);
  71. Persons.Add(person);
  72. PersonAppeared?.Invoke(this, person);
  73. }
  74. person.WorldPosition = detectedPerson.WorldPosition;
  75. detectedIds.Add(detectedPerson.Id);
  76. }
  77. var personsToBeRemoved = Persons.Where(p => !detectedIds.Contains(p.Id)).ToArray();
  78. foreach (var person in personsToBeRemoved)
  79. {
  80. Persons.Remove(person);
  81. PersonDisappeared?.Invoke(this, person);
  82. }
  83. }
  84. void Update()
  85. {
  86. }
  87. }
  88. }