Closed chrisdembia closed 4 years ago
This is very cool. I can prototype it if that's okay?
I can prototype it if that's okay?
Yay! We might need to discuss with the group about what interface would be best, etc., but prototyping is definitely in order.
We could also add a similar getAsJavaArray()
for Vector
(or VectorBase
or something) in the SWIG interface files.
It's also possible that MATLAB has even nicer conversion functions than having to call both cell2mat()
and cell()
. Maybe we should ask on the MATLAB forums.
By the way, going from a MATLAB matrix to an OpenSim Vector should be easier, based on those links above; we simply need to add a constructor to Vector that takes a java array (in the SWIG interface), and MATLAB will automatically convert a MATLAB matrix to a java array for us. That means it should be possible to do:
v = Vector(matlabMatrix)
I don't see a Vec3::getAsJavaArray()
method in OpenSim 3.3, but this hack works:
v = Vec3(1,2,3);
matlabVec = eval(v.toString().substring(1));
@aseth1 Allow getting a single table column as a Matlab matrix.
Check out section 25.8.3 at the following link (Wrapping C arrays with Java arrays): http://www.swig.org/Doc3.0/Java.html#Java_typemaps_c_to_java_types
I played around with this a little more and got the following to work (based on https://stackoverflow.com/questions/1610045/how-to-return-an-array-from-jni-to-java):
(java code that passes native java arrays to C functions populate()
and set_from_java()
)
int n = 10;
double[] arr = new double[n];
example.populate(arr);
for (int i = 0; i < arr.length; ++i) {
System.out.println(arr[i]);
}
myvec v = new myvec();
v.set_from_java(arr);
See attached.
This link should be helpful for going from C++ to Java: https://stackoverflow.com/questions/1610045/how-to-return-an-array-from-jni-to-java
Converting OpenSim Vectors into MATLAB matrices is challenging but is a common task.
MATLAB supports converting java arrays to matrices:
We already have a converter from
Vec3
to a java array here: https://github.com/opensim-org/opensim-core/blob/master/Bindings/Java/swig/java_simbody.i#L20.This means that one can do the following right now in MATLAB:
This is still very verbose, but perhaps we could create a utility function
osimMakeMatrix()
or something like that.@tkuchida and I just looked into this together.
Links: