PointCloudAggregator.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Windows.Kinect;
  4. public class PointCloudAggregator : MonoBehaviour {
  5. public float accuracy = 1000f;
  6. public float maxDistance = 15f;
  7. public ParticleSystem _particleSystem;
  8. private ParticleSystem.Particle[] particles;
  9. // Kinectsensor foo
  10. private MultiSourceManager _MultiManager;
  11. private KinectSensor _Sensor;
  12. private CoordinateMapper _Mapper;
  13. private int depthWidth;
  14. private int depthHeight;
  15. private FrameDescription depthFrameDesc;
  16. private CameraSpacePoint[] cameraSpacePoints;
  17. private ColorSpacePoint[] colorSpacePoints;
  18. private Texture2D cameraTexture;
  19. private Color[] cameraColor;
  20. private Vector2 maxVals;
  21. private Vector2 minVals;
  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. maxVals = Vector2.negativeInfinity;
  61. minVals = Vector2.positiveInfinity;
  62. }
  63. // Update is called once per frame
  64. void Update () {
  65. if (Input.GetKeyDown(KeyCode.Space))
  66. {
  67. Debug.Log("Snapshot");
  68. NewSnapshot(true);
  69. Debug.Log("Data set not has: " + data.Count + " entries");
  70. }
  71. if (Input.GetKeyDown(KeyCode.Y))
  72. {
  73. Debug.Log("Snapshot");
  74. NewSnapshot(false);
  75. Debug.Log("Data set not has: " + data.Count + " entries");
  76. }
  77. if (Input.GetKeyDown(KeyCode.C))
  78. {
  79. Debug.Log("Cloud!");
  80. Debug.Log(data.Count);
  81. ShowAsPointCloud();
  82. }
  83. if (Input.GetKeyDown(KeyCode.A))
  84. {
  85. Debug.Log("Cloud2!");
  86. Debug.Log(data.Count);
  87. ShowAsPointCloud2();
  88. }
  89. }
  90. void NewSnapshot(bool normal)
  91. {
  92. Vector3 currentPoint;
  93. Vector2Int floorPosition;
  94. ushort[] rawdata = _MultiManager.GetDepthData();
  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.InverseTransformPoint(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. minVals = Vector2.Min(floorPosition, minVals);
  112. maxVals = Vector2.Max(floorPosition, maxVals);
  113. if (!normal)
  114. {
  115. data2[currentPoint] = GetColorAtIndex(i);
  116. }
  117. else
  118. {
  119. if (data.ContainsKey(floorPosition))
  120. {
  121. ColorHeight values = data[floorPosition];
  122. values.UpdateInfo(currentPoint.y, GetColorAtIndex(i));
  123. data[floorPosition] = values;
  124. }
  125. else
  126. {
  127. data[floorPosition] = new ColorHeight(currentPoint.y, GetColorAtIndex(i));
  128. }
  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. Color result = cameraTexture.GetPixel(Mathf.RoundToInt(colorSpacePoints[index].X), Mathf.RoundToInt(colorSpacePoints[index].Y));
  139. return result;
  140. }
  141. void ShowAsPointCloud()
  142. {
  143. ColorHeight colorHeight;
  144. particles = new ParticleSystem.Particle[data.Count];
  145. int i = 0;
  146. foreach(Vector2Int floorPos in data.Keys)
  147. {
  148. colorHeight = data[floorPos];
  149. particles[i].position = new Vector3((float)floorPos.x/accuracy, (float)colorHeight.height, (float)floorPos.y/ accuracy);
  150. particles[i].startColor = colorHeight.color;
  151. particles[i].startSize = 0.02f;
  152. i++;
  153. }
  154. _particleSystem.SetParticles(particles, particles.Length);
  155. }
  156. void ShowAsPointCloud2()
  157. {
  158. particles = new ParticleSystem.Particle[data2.Count];
  159. int i = 0;
  160. foreach (Vector3 pos in data2.Keys)
  161. {
  162. particles[i].position = pos;
  163. particles[i].startColor = data2[pos];
  164. particles[i].startSize = 0.02f;
  165. i++;
  166. }
  167. _particleSystem.SetParticles(particles, particles.Length);
  168. }
  169. }