Heatmap.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.IO;
  2. using UnityEngine;
  3. public class Heatmap : MonoBehaviour
  4. {
  5. [Header("Read and Save Settings:")]
  6. public int readIndex;
  7. public int saveIndex = 2019;
  8. [Header("Grid Settings:")]
  9. public Vector2 gridSize = new Vector2(70, 35);
  10. public int scaleHeatmap = 8;
  11. [Header("Set Scene Obstacles:")]
  12. public GameObject[] obstacles;
  13. private void Start()
  14. {
  15. GridArray grid = new GridArray((int)gridSize.x, (int)gridSize.y) ;
  16. // Read from CSV file and save time, position, rotation in matrix
  17. string dir = Directory.GetCurrentDirectory();
  18. string reference = @"\Assets\Data_position\Walk" + readIndex + ".csv";
  19. var timePosRotList = gameObject.GetComponent<ReadFromCSVNew>().ReadFromCSVFile(dir + reference);
  20. // increment grid at the positions where the people are located
  21. for(int i = 0; i < timePosRotList.Length; ++i) // groups
  22. {
  23. for(int j = 0; j < timePosRotList[i].Length; ++j) // humans
  24. {
  25. int currentIndex = 0;
  26. // increment grid for one Human completly
  27. while(currentIndex < timePosRotList[i][j].Item2.Count)
  28. {
  29. int width = (int)timePosRotList[i][j].Item2[currentIndex].x;
  30. int height = (int)timePosRotList[i][j].Item2[currentIndex].z;
  31. grid.IncrementGridArray(width, height);
  32. currentIndex++;
  33. }
  34. }
  35. }
  36. var normalizedHeatmap = grid.NormalizeArray();
  37. //add obstacles into heatmap
  38. foreach(var obstacle in obstacles)
  39. normalizedHeatmap = grid.SetObstacles(normalizedHeatmap, obstacle);
  40. string fileName = @"\Assets\Data_image\Heatmap" + saveIndex + ".png";
  41. float[,] scaledHeatmap = grid.ScaleArray(normalizedHeatmap, scaleHeatmap);
  42. scaledHeatmap = grid.SetBorder(scaledHeatmap, obstacles[1], scaleHeatmap);
  43. grid.SaveGrid(scaledHeatmap, dir + fileName);
  44. }
  45. }