Skip to content

Commit

Permalink
Fix PointCloud2 buffers (#244)
Browse files Browse the repository at this point in the history
* Fix PointCloud2 buffers

Move the decoding buffer out of Points, since it's only needed during base64 decode.

* Clamp n in PointCloud2 binary path

Thanks @jubeira for this change.

Co-Authored-By: mvollrath <matt@endpoint.com>
  • Loading branch information
mvollrath authored Jan 16, 2019
1 parent e3fb0ad commit 1d2e041
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/sensors/PointCloud2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
n = msg.height*msg.width / pointRatio;
this.buffer = msg.data.slice(0, Math.min(msg.data.byteLength, bufSz));
n = Math.min(msg.height*msg.width / pointRatio, this.points.positions.array.length / 3);
} 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;
Expand Down
5 changes: 0 additions & 5 deletions src/sensors/Points.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@ ROS3D.Points = function(options) {
}

this.sn = null;
this.buffer = null;
};

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 = {};
Expand Down

0 comments on commit 1d2e041

Please sign in to comment.