Skip to content

Commit

Permalink
Merge pull request IntelRealSense#1073 from tingshao/add_ply_new
Browse files Browse the repository at this point in the history
[Node.js] Add exportToPly method to Points class
  • Loading branch information
Halton Huo authored Jan 30, 2018
2 parents 18e5bc5 + ee20a2a commit 06c937d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions wrappers/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,22 @@ class Points extends Frame {
this.cxxFrame = undefined;
}

/**
* Creates a ply file of the model with the given file name.
* @param {String} fileName name of the ply file
* @param {VideoFrame} texture texture frame
* @return {undefined}
*/
exportToPly(fileName, texture) {
const funcName = 'Points.exportToPly()';
checkArgumentLength(2, 2, arguments.length, funcName);
checkArgumentType(arguments, 'string', 0, funcName);
checkArgumentType(arguments, VideoFrame, 1, funcName);
if (this.cxxFrame) {
this.cxxFrame.exportToPly(fileName, texture.cxxFrame);
}
}

/**
* Get an array of texture coordinates per vertex
* Each coordinate represent a (u,v) pair within [0,1] range, to be mapped to texture image
Expand Down
15 changes: 15 additions & 0 deletions wrappers/nodejs/src/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

class MainThreadCallbackInfo {
Expand Down Expand Up @@ -519,6 +520,7 @@ class RSFrame : public Nan::ObjectWrap {
Nan::SetPrototypeMethod(tpl, "writeTextureCoordinates",
WriteTextureCoordinates);
Nan::SetPrototypeMethod(tpl, "getPointsCount", GetPointsCount);
Nan::SetPrototypeMethod(tpl, "exportToPly", ExportToPly);
Nan::SetPrototypeMethod(tpl, "isValid", IsValid);
Nan::SetPrototypeMethod(tpl, "getDistance", GetDistance);
Nan::SetPrototypeMethod(tpl, "getBaseLine", GetBaseLine);
Expand Down Expand Up @@ -935,6 +937,19 @@ class RSFrame : public Nan::ObjectWrap {
info.GetReturnValue().Set(Nan::Undefined());
}

static NAN_METHOD(ExportToPly) {
auto me = Nan::ObjectWrap::Unwrap<RSFrame>(info.Holder());
v8::String::Utf8Value str(info[0]);
std::string file = std::string(*str);
auto texture = Nan::ObjectWrap::Unwrap<RSFrame>(info[1]->ToObject());
info.GetReturnValue().Set(Nan::Undefined());
if (!me || !texture) return;

rs2_frame* ptr = nullptr;
std::swap(texture->frame_, ptr);
rs2_export_to_ply(me->frame_, file.c_str(), ptr, &me->error_);
}

static NAN_METHOD(IsValid) {
auto me = Nan::ObjectWrap::Unwrap<RSFrame>(info.Holder());
if (me) {
Expand Down
17 changes: 17 additions & 0 deletions wrappers/nodejs/test/test-functional-online.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/* global describe, it, before, after */
const assert = require('assert');
const fs = require('fs');
let rs2;
try {
rs2 = require('node-librealsense');
Expand Down Expand Up @@ -46,3 +47,19 @@ describe('Disparity transform tests', function() {
pipe.stop();
});
});

describe('Points.exportToPly', function() {
it('exportToPly', () => {
const pc = new rs2.PointCloud();
const pipe = new rs2.Pipeline();
const file = 'points.ply';
pipe.start();
const frameset = pipe.waitForFrames();
const points = pc.calculate(frameset.depthFrame);
points.exportToPly(file, frameset.colorFrame);
assert.equal(fs.existsSync(file), true);
fs.unlinkSync(file);
pipe.stop();
rs2.cleanup();
});
});

0 comments on commit 06c937d

Please sign in to comment.