如何桥接JavaScript(ragged)数组和std :: vector< std :: vector< T>>目的? [英] How to bridge a JavaScript (ragged) array and an std::vector<std::vector<T>> object?
问题描述
在JavaScript中,我有一个行列表,每个行由不定数量的点组成,每个都有 [x,y]
。所以它是一个三维粗糙的数组。现在我需要将它传递给我的C ++代码与帮助emscripten( embind )。这里是C ++函数的声明:
Eigen :: MatrixXd f(const std :: vector< std :: vector& :vector< double>>>& lines);
我想得到一个列表列表( [[m11,调用
f
后在JavaScript中调用[m12],[m22,m22],...]如何在这种情况下编写绑定代码(例如 EMSCRIPTEN_BINDINGS
中的内容)?
UPDATE :我现在可以把JavaScript数组传递给C ++。绑定部分是
typedef std :: vector< double>点;
typedef std :: vector< Point>线;
EMSCRIPTEN_BINDINGS(module){
register_vector< Line>(LineArray);
register_vector< Point>(Line);
register_vector< double>(Point);
emscripten :: function(f_wrapper,& f_wrapper);
}
其中 f_wrapper
f
但返回向量< vector< double>>
而不是 MatrixXd
。现在的问题是,我只能得到一个空的JavaScript对象调用 f_wrapper
后。 JavaScript是
var Module = require('./ bind.js'); // em ++的输出
var cppAllLines = new Module.LineArray();
//一些初始化
var result = Module.f_wrapper(cppAllLines); //一个空的Line对象
任何想法?
std :: vector< std :: vector< double>> f_wrapper(...);
至Javascript
var result = Module.f_wrapper(...);
结果
对象不是一个Javascript数组它实现 length
属性或数组索引访问,因此如果使用这些访问它的数据,它可以显示为空。
但它会暴露 get
和 size
函数以访问向量:
console.log('Back from C ++',result.size(),result.get(0).get(1)
(double get
返回向量的向量)
为了完整性,查看返回的对象的原型,它似乎暴露了以下函数。
-
get
-
push_back
-
resize
-
设定
-
size
稍微不一致,它暴露 get
和 set
函数,而不是等价于C ++ 在
功能。我怀疑自 在
返回一个允许它作为setter使用的引用,这在Javascript中是不可能的。
In JavaScript I have a list of "lines", each of which consists of indefinite number of "points", each of which has a form of [x, y]
. So it is a 3D ragged array. Now I need to pass it to my C++ code with the help from emscripten (embind). Here's declaration of the C++ function:
Eigen::MatrixXd f(const std::vector<std::vector<std::vector<double>>>& lines);
And I would like to get a list of lists ([[m11, m12],[m22, m22],...]
) in JavaScript after calling f
. How to write the binding code in this case (the stuff inside EMSCRIPTEN_BINDINGS
, for example)?
UPDATE: I can now pass the JavaScript array to C++. The binding part is something like
typedef std::vector<double> Point;
typedef std::vector<Point> Line;
EMSCRIPTEN_BINDINGS(module) {
register_vector<Line>("LineArray");
register_vector<Point>("Line");
register_vector<double>("Point");
emscripten::function("f_wrapper", &f_wrapper);
}
where f_wrapper
calls f
but returns vector<vector<double>>
instead of MatrixXd
. Now the problem is that I can only get an empty JavaScript object after calling f_wrapper
. The JavaScript is
var Module = require('./bind.js'); // the output of em++
var cppAllLines = new Module.LineArray();
// some initialization
var result = Module.f_wrapper(cppAllLines); // an empty "Line" object
Any ideas?
When passing an embound vector from a C++ function, such as
std::vector<std::vector<double>> f_wrapper(...);
to Javascript
var result = Module.f_wrapper(...);
The result
object is not a Javascript array that implements length
property or array-indexed access, so it can appear "empty" if using these to access its data.
But it does expose get
and size
functions to access the vector:
console.log('Back from C++', result.size(), result.get(0).get(1));
(The double get
is because we're returning a vector of vectors)
For completeness, looking into the prototype of the object returned, it seems to expose the following functions.
get
push_back
resize
set
size
Slightly inconsistently it exposes get
and set
functions rather than an equivalent of the C++ at
function. I suspect that it's not possible for an exactly equivalent function since at
returns a reference which allows it to be used as a setter, which isn't possible in Javascript.
这篇关于如何桥接JavaScript(ragged)数组和std :: vector< std :: vector< T>>目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!