opensim-org / opensim-core

SimTK OpenSim C++ libraries and command-line applications, and Java/Python wrapping.
https://opensim.stanford.edu
Apache License 2.0
796 stars 319 forks source link

Convert OpenSim numeric types to MATLAB matrices #1882

Closed chrisdembia closed 4 years ago

chrisdembia commented 7 years ago

Converting OpenSim Vectors into MATLAB matrices is challenging but is a common task.

MATLAB supports converting java arrays to matrices:

cell2mat(cell(javaArray))

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:

cell2mat(cell(vec3.getAsJavaArray()))

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:

jimmyDunne commented 7 years ago

This is very cool. I can prototype it if that's okay?

chrisdembia commented 7 years ago

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.

chrisdembia commented 7 years ago

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)
tkuchida commented 7 years ago

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));
scrum2a commented 6 years ago

@aseth1 Allow getting a single table column as a Matlab matrix.

carmichaelong commented 6 years ago

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

chrisdembia commented 6 years ago

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.

java_arrays.zip

chrisdembia commented 6 years ago

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