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/wikiThe compilation command line is:
emcc solve.cpp -O2 -o solve.js -I /hack/eigen -std=c++11 -DEIGEN_ASM_COMMENT -s EXPORTED_FUNCTIONS="['_solve']"
(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