From a0c60f833a1e6f24112755c18e25e31c6b3e7ace Mon Sep 17 00:00:00 2001 From: Matt Vollrath Date: Sat, 5 Jan 2019 22:17:41 -0500 Subject: [PATCH 1/2] Fix PointCloud2 buffers Move the decoding buffer out of Points, since it's only needed during base64 decode. --- src/sensors/PointCloud2.js | 12 +++++++++--- src/sensors/Points.js | 5 ----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/sensors/PointCloud2.js b/src/sensors/PointCloud2.js index 511560bf..c0616a9b 100644 --- a/src/sensors/PointCloud2.js +++ b/src/sensors/PointCloud2.js @@ -62,8 +62,10 @@ ROS3D.PointCloud2 = function(options) { this.ros = options.ros; this.topicName = options.topic || '/points'; this.compression = options.compression || 'cbor'; + this.max_pts = options.max_pts || 10000; this.points = new ROS3D.Points(options); this.rosTopic = undefined; + this.buffer = null; this.subscribe(); }; ROS3D.PointCloud2.prototype.__proto__ = THREE.Object3D.prototype; @@ -94,16 +96,20 @@ ROS3D.PointCloud2.prototype.processMessage = function(msg){ } var n, pointRatio = this.points.pointRatio; + var bufSz = this.max_pts * msg.point_step; if (msg.data.buffer) { - this.points.buffer.set(msg.data); + this.buffer = msg.data.slice(0, Math.min(msg.data.byteLength, bufSz)); n = msg.height*msg.width / pointRatio; } else { - n = decode64(msg.data, this.points.buffer, msg.point_step, pointRatio); + if (!this.buffer || this.buffer.byteLength < bufSz) { + this.buffer = new Uint8Array(bufSz); + } + n = decode64(msg.data, this.buffer, msg.point_step, pointRatio); pointRatio = 1; } - var dv = new DataView(this.points.buffer.buffer); + var dv = new DataView(this.buffer.buffer); var littleEndian = !msg.is_bigendian; var x = this.points.fields.x.offset; var y = this.points.fields.y.offset; diff --git a/src/sensors/Points.js b/src/sensors/Points.js index ed582628..ab3658cd 100644 --- a/src/sensors/Points.js +++ b/src/sensors/Points.js @@ -40,7 +40,6 @@ ROS3D.Points = function(options) { } this.sn = null; - this.buffer = null; }; ROS3D.Points.prototype.__proto__ = THREE.Object3D.prototype; @@ -48,10 +47,6 @@ ROS3D.Points.prototype.__proto__ = THREE.Object3D.prototype; ROS3D.Points.prototype.setup = function(frame, point_step, fields) { if(this.sn===null){ - // scratch space to decode base64 buffers - if(point_step) { - this.buffer = new Uint8Array( this.max_pts * point_step ); - } // turn fields to a map fields = fields || []; this.fields = {}; From 1122a910aa74c053d847a4aae17c5c53897da4eb Mon Sep 17 00:00:00 2001 From: Juan Ignacio Ubeira Date: Fri, 11 Jan 2019 15:42:47 -0500 Subject: [PATCH 2/2] Clamp n in PointCloud2 binary path Thanks @jubeira for this change. Co-Authored-By: mvollrath --- src/sensors/PointCloud2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sensors/PointCloud2.js b/src/sensors/PointCloud2.js index c0616a9b..d9fb1413 100644 --- a/src/sensors/PointCloud2.js +++ b/src/sensors/PointCloud2.js @@ -100,7 +100,7 @@ ROS3D.PointCloud2.prototype.processMessage = function(msg){ if (msg.data.buffer) { this.buffer = msg.data.slice(0, Math.min(msg.data.byteLength, bufSz)); - n = msg.height*msg.width / pointRatio; + n = Math.min(msg.height*msg.width / pointRatio, this.points.positions.array.length / 3); } else { if (!this.buffer || this.buffer.byteLength < bufSz) { this.buffer = new Uint8Array(bufSz);