[vtkusers] VTK bindings to Node.js/V8/JavaScript

Axel Kittenberger axkibe at gmail.com
Thu Jan 28 04:56:45 EST 2016


Hello,

I created a wrapper library generator for VTK to Node.js. If you have all
needed packages installed ("node", "vtk6-dev", "qtbase5-dev") running
  npm install vtk
should do it and compile the pre-generated wrapper classes matching the VTK
library found on your system. Currently I included pre-generated wrappers
for VTK 6.1, 6.2 and 6.3 (on Linux).

The full source code: https://github.com/axkibe/node-vtk

Example code creating a cone:
--------------------------
'use strict';
var vtk, source, mapper, actor, renderer, renderWindow,
renderWindowInteractor;

vtk = require('vtk');

source = new vtk.ConeSource();
source.setResolution( 10 );

mapper = new vtk.PolyDataMapper();

mapper.setInputConnection( source.getOutputPort() );

actor = new vtk.Actor();
actor.setMapper( mapper );

renderer = new vtk.Renderer();
renderer.addActor( actor );
renderer.setBackground( 0.3, 0.2, 0.1 );
renderer.resetCamera();

renderWindow = new vtk.RenderWindow();
renderWindow.addRenderer( renderer );

renderWindowInteractor = new vtk.RenderWindowInteractor();
renderWindowInteractor.setRenderWindow( renderWindow );
renderWindowInteractor.setInteractorStyle( new
vtk.InteractorStyleTrackballCamera() );

renderWindow.render();

renderWindowInteractor.start();
--------------------------

I know there is Paraview having a JavaScript interface to interact with on
browser side, but this module directly includes VTK on Node.js server side
or simply for small rapid development scripts if someone like me just likes
JavaScript to prefer this for quick testing instead of other wrappers or
using C++ directly.

What I did is:

* Hijack the VTK wrapping tools build scripts to generate V8 wrapping
classes using the "Native Abstractions for Node" (nan) layer. This doesn't
build the binary libraries right away but stops after generating the
wrapper classes to be copied over the Node.js module build system.

* Figure out how to expose hierarchical native data as hierarchical
JavaScript objects creating a matching prototype chain. I didn't find
example code for this as most nan examples where simply object here <->
object there mappings.
  var vtk = require('vtk')
  var source = new vtk.ConeSource();
  console.log( source instanceof vtk.Algorithm, source instanceof
vtk.CubeSource );
correctly prints out "true false"

* Creating overload detection and figure out the correct native overload to
call.

* Make the wrapping as foolproof as I could think of. At least I don't know
how to create a Segfault from JavaScript side, everything wrong doing
should raise a JavaScript exception instead.

* Lazily initialize JavaScript prototypes only as they are needed. On
startup only getters for the VTK object are set, everything else is only
initialized when accessed. This improves startup time. After the first
start of node with VTK which takes a long time (10 seconds for me) for I
suppose the linker doing its stuff, subsequent calls start up the whole
thing in 1 second.

* Set references for different naming conventions what you prefer.
vtk.vtkConeSource (strictly correct and VTK case scheme, but kind of
repetitive saying "vtk" twice and violating JavaScript conventions of
constructor functions being uppercase) and vtk.ConeSource (shorted and fits
the convention of uppercase constructors) point to the same thing. Similar
source.SetResolution (strictly correct VTK name) and source.setResolution
(JavaScript convention for lowercase non-constructor functions) are the
same thing too.

What I did not:

* I skipped a lot of less used parameter types. Right now wrappers are only
created for functions having double, char, char (const) *, int and
vtkObject pointers as parameters and return values. Other primitives should
be easy to add, I just didn't yet need them.
Predefined length arrays as parameters might be a little more work. QObject
I didn't look at. Templates as parameters (as Tuples) I don't know how to
and I suppose PythonWrapping, which I used as guideline, doesn't either.
C++ Classes/Structs not being a child of vtkObjectBase are excluded as
vtk-node uses vtkSmartPointers for all its objects and these don't work
there.

* Create the .so libraries right from within the VTK build.

* Create altered WindowInteractor derivates which keep the node event loop
running. I looked a little into XWindowRenderInteractor and there it should
be very possible to have the X-event loop halt its select() and timer
calculations in a separate thread, but hand on any X message via semaphore
to be processed in the main thread.

* Detect other operating systems than Linux in the "npm install" script and
include/exclude the correct wrapper classes.

Kind regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160128/450ffe7d/attachment.html>


More information about the vtkusers mailing list