diff --git a/wrappers/nodejs/index.js b/wrappers/nodejs/index.js index 84e83fde5d..c73a068a9c 100644 --- a/wrappers/nodejs/index.js +++ b/wrappers/nodejs/index.js @@ -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 diff --git a/wrappers/nodejs/src/addon.cpp b/wrappers/nodejs/src/addon.cpp index cd667035b5..a65f17b90f 100644 --- a/wrappers/nodejs/src/addon.cpp +++ b/wrappers/nodejs/src/addon.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include class MainThreadCallbackInfo { @@ -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); @@ -935,6 +937,19 @@ class RSFrame : public Nan::ObjectWrap { info.GetReturnValue().Set(Nan::Undefined()); } + static NAN_METHOD(ExportToPly) { + auto me = Nan::ObjectWrap::Unwrap(info.Holder()); + v8::String::Utf8Value str(info[0]); + std::string file = std::string(*str); + auto texture = Nan::ObjectWrap::Unwrap(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(info.Holder()); if (me) { diff --git a/wrappers/nodejs/test/test-functional-online.js b/wrappers/nodejs/test/test-functional-online.js index 3b37c9df0a..76d14c75da 100644 --- a/wrappers/nodejs/test/test-functional-online.js +++ b/wrappers/nodejs/test/test-functional-online.js @@ -6,6 +6,7 @@ /* global describe, it, before, after */ const assert = require('assert'); +const fs = require('fs'); let rs2; try { rs2 = require('node-librealsense'); @@ -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(); + }); +});