[eigen] Compiling Eigen to the Web
• To: eigen <eigen@xxxxxxxxxxxxxxxxxxx>
• Subject: [eigen] Compiling Eigen to the Web
• From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
• Date: Thu, 7 Nov 2013 08:48:31 -0500
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=BpdInpyh63ivNyhWh7iek7rOwbDV7QP9sgpHuJHt0Yc=; b=W+OVSXHowe/vlI0AUxY7LtiokeolCWU86azQzLiiSEtZoxDZAQP3kgPLxqVcm82nli EnOmgmUAtemeQKyM0vCAG4mlwANNQGzD4xgInasdEeiDpkzgUl7s/+s65XOn01oHPplx I3fdE0QFc7gohWyfUhR+XalV97+PagquYQAGZIg9ASRRweGdjoP/1PIZgUk+r3bkNZWb BVFImhDdkPnzux/27Mhs+QFkTmSQ2GprEfNAYGIXw9r4CHRhv2IvYRb5Lh7hF6uKEL6y R+rLaabuggVEkp+0/fdH+mMPk5kYdxgvw0ci9HqFF0et6gkUojt0RVIUiNMmQR8Oggaj 3ooQ==

Hi,

This is to share a simple example of using Eigen in a Web page:

http://people.mozilla.org/~bjacob/bezier/bezier.html

Here, a linear solver from Eigen is used to build a small _javascript_ power series library, which is used to do some calculus to find a constant-speed parametrization of a Bezier curve.

The compiler used to compile Eigen to JS is Emscripten:

https://github.com/kripken/emscripten/wiki

The Eigen-using C++ code being compiled is:

http://people.mozilla.org/~bjacob/bezier/solve.cpp

The compilation command line is:

emcc solve.cpp -O2 -o solve.js -I /hack/eigen -std=c++11 -DEIGEN_ASM_COMMENT -s EXPORTED_FUNCTIONS="['_solve']"

This yields a JS file which you can see here:

http://people.mozilla.org/~bjacob/bezier/solve.js

(If you want it to be more readable, so you can see what Eigen is doing in JS, pass -g to the compiler)

To use that JS code, user code uses Emscripten's pseudo malloc() function and writes/reads directly on the Typed Array that serves as Emscripten's heap, see e.g. the code implementing power series division using Eigen's solver:

```function PowerSeriesDiv(a, b) {
var size = a.length;

var matrix_data = Module._malloc(4 * size * size);
var rhs_data = Module._malloc(4 * size);
var result_data = Module._malloc(4 * size);

for (var i = 0; i < size; i++) {
HEAPF32[(rhs_data>>2) + i] = a[i];
}

for (var col = 0; col < size; col++) {
for (var row = 0; row < size; row++) {
HEAPF32[(matrix_data>>2) + col * size + row] = row >= col ? b[row - col] : 0;
}
}

_solve(size, matrix_data, rhs_data, result_data);

var result = new Float32Array(size);
for (var i = 0; i < size; i++) {
result[i] = HEAPF32[(result_data>>2) + i];
}

return result;
}
```

Cheers,
Benoit

 Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/