PointCloudSubscriber.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Author: inmo-jang https://github.com/inmo-jang/unity_assets/tree/master/PointCloudStreaming
  2. * Modifications: Added queue compatibility by Trung-Hoa Ha
  3. */
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using RosSharp.RosBridgeClient.MessageTypes.Sensor;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using System.Threading;
  11. // Attached to RosBridge object.
  12. namespace RosSharp.RosBridgeClient
  13. {
  14. [RequireComponent(typeof(RosConnector))]
  15. public class PointCloudSubscriber : UnitySubscriber<MessageTypes.Sensor.PointCloud2>
  16. {
  17. public int MaxQueueSize;
  18. private byte[] byteArray;
  19. private bool isMessageReceived = false;
  20. private int messageSize;
  21. private int receivedMessages = 0;
  22. private int lastmessageSize;
  23. private Vector3[] pcl;
  24. private Color[] pcl_color;
  25. private Queue<Vector3> pclQueue = new Queue<Vector3>();
  26. private Queue<Color> pcl_colorQueue = new Queue<Color>();
  27. int width;
  28. int height;
  29. int row_step;
  30. int point_step;
  31. protected override void Start()
  32. {
  33. Debug.Log("PointCloudSubscriber: Max Queue Size is set to " + MaxQueueSize);
  34. base.Start();
  35. }
  36. // Check if new message received.
  37. public void Update()
  38. {
  39. if (isMessageReceived)
  40. {
  41. PointCloudRendering();
  42. isMessageReceived = false;
  43. }
  44. }
  45. // Called by subscriber to send message.
  46. protected override void ReceiveMessage(PointCloud2 message)
  47. {
  48. messageSize = message.data.GetLength(0);
  49. byteArray = new byte[messageSize];
  50. byteArray = message.data;
  51. width = (int)message.width;
  52. height = (int)message.height;
  53. row_step = (int)message.row_step;
  54. point_step = (int)message.point_step;
  55. messageSize = messageSize / point_step;
  56. isMessageReceived = true;
  57. }
  58. // Change points of the point cloud.
  59. void PointCloudRendering()
  60. {
  61. int x_posi;
  62. int y_posi;
  63. int z_posi;
  64. float x;
  65. float y;
  66. float z;
  67. int rgb_posi;
  68. var rgb_max = 255;
  69. float r;
  70. float g;
  71. float b;
  72. // Create every vector, convert byte type to float in this part.
  73. for (var n = 0; n < messageSize; n++)
  74. {
  75. x_posi = n * point_step + 0;
  76. y_posi = n * point_step + 4;
  77. z_posi = n * point_step + 8;
  78. x = BitConverter.ToSingle(byteArray, x_posi);
  79. y = BitConverter.ToSingle(byteArray, y_posi);
  80. z = BitConverter.ToSingle(byteArray, z_posi);
  81. rgb_posi = n * point_step + 16;
  82. b = byteArray[rgb_posi + 0];
  83. g = byteArray[rgb_posi + 1];
  84. r = byteArray[rgb_posi + 2];
  85. r /= rgb_max;
  86. g /= rgb_max;
  87. b /= rgb_max;
  88. if (receivedMessages > MaxQueueSize){
  89. pclQueue.Dequeue();
  90. pcl_colorQueue.Dequeue();
  91. }
  92. pclQueue.Enqueue(new Vector3(x, z, y));
  93. pcl_colorQueue.Enqueue(new Color(r, g, b));
  94. }
  95. receivedMessages += 1;
  96. }
  97. public Vector3[] GetPCL()
  98. {
  99. return pclQueue.ToArray();
  100. }
  101. public Color[] GetPCLColor()
  102. {
  103. return pcl_colorQueue.ToArray();
  104. }
  105. }
  106. }