PointCloudAggregator.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Windows.Kinect;
  4. public class PointCloudAggregator : MonoBehaviour {
  5. int minY = 0;
  6. int maxY = 0;
  7. public float accuracy = 1000f;
  8. public float maxDistance = 15f;
  9. public ParticleSystem _particleSystem;
  10. private ParticleSystem.Particle[] particles;
  11. // Kinectsensor foo
  12. private MultiSourceManager _MultiManager;
  13. private KinectSensor _Sensor;
  14. private CoordinateMapper _Mapper;
  15. private int depthWidth;
  16. private int depthHeight;
  17. private FrameDescription depthFrameDesc;
  18. private CameraSpacePoint[] cameraSpacePoints;
  19. private ColorSpacePoint[] colorSpacePoints;
  20. public Texture2D cameraTexture;
  21. private Color[] cameraColor;
  22. Dictionary<Vector2Int, ColorHeight> data = new Dictionary<Vector2Int, ColorHeight>();
  23. Dictionary<Vector3, Color> data2 = new Dictionary<Vector3, Color>();
  24. public struct ColorHeight
  25. {
  26. public float height;
  27. public Color color;
  28. float avgAbout;
  29. public ColorHeight(float inHeight, Color inColor)
  30. {
  31. height = inHeight;
  32. color = inColor;
  33. avgAbout = 1;
  34. }
  35. public void UpdateInfo(float newHeight, Color newColor)
  36. {
  37. color = (color * avgAbout / (avgAbout + 1)) + newColor / avgAbout;
  38. height = (height * avgAbout / (avgAbout + 1)) + newHeight / avgAbout;
  39. avgAbout++;
  40. }
  41. }
  42. void Start()
  43. {
  44. _Sensor = KinectSensor.GetDefault();
  45. if (_Sensor != null)
  46. {
  47. _MultiManager = GetComponent<MultiSourceManager>();
  48. _Mapper = _Sensor.CoordinateMapper;
  49. depthFrameDesc = _Sensor.DepthFrameSource.FrameDescription;
  50. depthWidth = depthFrameDesc.Width;
  51. depthHeight = depthFrameDesc.Height;
  52. if (!_Sensor.IsOpen)
  53. {
  54. _Sensor.Open();
  55. }
  56. cameraSpacePoints = new CameraSpacePoint[depthWidth * depthHeight];
  57. colorSpacePoints = new ColorSpacePoint[depthWidth * depthHeight];
  58. cameraColor = new Color[depthWidth * depthHeight];
  59. }
  60. }
  61. // Update is called once per frame
  62. void Update () {
  63. if (Input.GetKeyDown(KeyCode.Space))
  64. {
  65. Debug.Log("Snapshot");
  66. NewSnapshot();
  67. Debug.Log("Data set not has: " + data.Count + " entries");
  68. }
  69. if (Input.GetKeyDown(KeyCode.C))
  70. {
  71. Debug.Log("Cloud!");
  72. Debug.Log(data.Count);
  73. ShowAsPointCloud();
  74. }
  75. if (Input.GetKeyDown(KeyCode.A))
  76. {
  77. Debug.Log("Cloud2!");
  78. Debug.Log(data.Count);
  79. ShowAsPointCloud2();
  80. }
  81. if (Input.GetKeyDown(KeyCode.R))
  82. {
  83. Debug.Log("Cleared!");
  84. data = new Dictionary<Vector2Int, ColorHeight>();
  85. data2 = new Dictionary<Vector3, Color>();
  86. }
  87. }
  88. void NewSnapshot()
  89. {
  90. Vector3 currentPoint;
  91. Vector2Int floorPosition;
  92. ushort[] rawdata = _MultiManager.GetDepthData();
  93. Color? tempCol;
  94. Color validCol;
  95. cameraTexture = _MultiManager.GetColorTexture();
  96. _Mapper.MapDepthFrameToCameraSpace(rawdata, cameraSpacePoints);
  97. _Mapper.MapDepthFrameToColorSpace(rawdata, colorSpacePoints);
  98. for(int i = 0; i < cameraSpacePoints.Length; i++)
  99. {
  100. currentPoint = KinectPointToVector3(cameraSpacePoints[i]);
  101. currentPoint = transform.TransformPoint(currentPoint);
  102. float x = (Mathf.Clamp(currentPoint.x, -maxDistance, maxDistance ));
  103. float y = (Mathf.Clamp(currentPoint.z, -maxDistance, maxDistance ));
  104. x *= accuracy;
  105. y *= accuracy;
  106. if(float.IsNaN(x) || float.IsNaN(y))
  107. {
  108. continue;
  109. }
  110. floorPosition = new Vector2Int(Mathf.RoundToInt(x),Mathf.RoundToInt(y));
  111. tempCol = GetColorAtIndex(i);
  112. if (tempCol == null)
  113. {
  114. continue;
  115. }
  116. else {
  117. validCol = (Color)tempCol;
  118. }
  119. data2[currentPoint] = validCol;
  120. if (data.ContainsKey(floorPosition))
  121. {
  122. ColorHeight values = data[floorPosition];
  123. values.UpdateInfo(currentPoint.y, validCol);
  124. data[floorPosition] = values;
  125. }
  126. else
  127. {
  128. data[floorPosition] = new ColorHeight(currentPoint.y, validCol);
  129. }
  130. }
  131. }
  132. Vector3 KinectPointToVector3(CameraSpacePoint point)
  133. {
  134. return new Vector3(point.X, point.Y, point.Z);
  135. }
  136. Color? GetColorAtIndex(int index)
  137. {
  138. int x = Mathf.RoundToInt(colorSpacePoints[index].X);
  139. int y = Mathf.RoundToInt(colorSpacePoints[index].Y);
  140. if(y < 0 || y > cameraTexture.height)
  141. {
  142. return null;
  143. }
  144. Color result = cameraTexture.GetPixel(x,y);
  145. return result;
  146. }
  147. void ShowAsPointCloud()
  148. {
  149. ColorHeight colorHeight;
  150. particles = new ParticleSystem.Particle[data.Count];
  151. int i = 0;
  152. foreach(Vector2Int floorPos in data.Keys)
  153. {
  154. colorHeight = data[floorPos];
  155. particles[i].position = new Vector3((float)floorPos.x/accuracy, (float)colorHeight.height, (float)floorPos.y/ accuracy);
  156. particles[i].startColor = colorHeight.color;
  157. particles[i].startSize = 0.02f;
  158. i++;
  159. }
  160. _particleSystem.SetParticles(particles, particles.Length);
  161. }
  162. void ShowAsPointCloud2()
  163. {
  164. particles = new ParticleSystem.Particle[data2.Count];
  165. int i = 0;
  166. foreach (Vector3 pos in data2.Keys)
  167. {
  168. particles[i].position = pos;
  169. particles[i].startColor = data2[pos];
  170. particles[i].startSize = 0.02f;
  171. i++;
  172. }
  173. _particleSystem.SetParticles(particles, particles.Length);
  174. }
  175. }