OpenSim API: set-translation/rotation for wrapping objects

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Mojtaba Mirakhorlo
Posts: 10
Joined: Tue Nov 25, 2014 3:28 am

OpenSim API: set-translation/rotation for wrapping objects

Post by Mojtaba Mirakhorlo » Wed Nov 18, 2015 1:14 am

Hi,

Is it possible to set the properties "translation" and "xyz_body_rotation"for WrapCylinder in opensim API?

Yours,
Mojtaba

User avatar
jimmy d
Posts: 1375
Joined: Thu Oct 04, 2007 11:51 pm

Re: OpenSim API: set-translation/rotation for wrapping obje

Post by jimmy d » Fri Nov 20, 2015 2:24 pm

Hi,

Yes, it is possible. But it is a hack that may have unpredictable effects if not done correctly. We will be building methods into future versions that can help edit properties and make sure that the model is valid, but for now, proceed at your own risk! ;)

This is how to do it in MATLAB:

Code: Select all

% Access a property named 'factor' in an OpenSim::Object 
% it is equivalent to having the XML tag name <factor> in an .osim file
factorProp  = objectWithFactor.getPropertyByName('factor');
% get its value
factor = PropertyHelper.getValueDouble(factorProp);
% double the factor and set it on the property
PropertyHelper.setValueDouble(2*factor, factorProp);

% Access a property named point, which is of type SimTK::Vec3
pointVec3Prop = objectWithPoint.getPropertyByName('point');
point(1) = PropertyHelper.getValueVec3(pointVec3Prop , 0); % x component
point(2) = PropertyHelper.getValueVec3(pointVec3Prop , 1); % y component 
point(3) = PropertyHelper.getValueVec3(pointVec3Prop , 2); % z component
NOTE: All property editing should be complete before the Model::finalizeFromProperties() method is invoked, which gets called at the beginning of Model::initSystem(). Any downstream edits on properties will have no (or in some cases unpredictable) effects on the system.

Let me know if this works
-James

User avatar
Mojtaba Mirakhorlo
Posts: 10
Joined: Tue Nov 25, 2014 3:28 am

Re: OpenSim API: set-translation/rotation for wrapping obje

Post by Mojtaba Mirakhorlo » Mon Nov 23, 2015 7:59 am

thanks for your consideration,
I tried to do that in this way :
wrap=WrapCylinder();
wrap.setName(name);
pointVec3Prop = wrap.getPropertyByName('translation');
PropertyHelper.setValueVec3(0.01,pointVec3Prop , 0)

but I ran into this error :

"Java exception occurred:
java.lang.RuntimeException: Bad dynamic_cast!

at org.opensim.modeling.opensimModelJNI.PropertyHelper_setValueVec3(Native
Method)

at org.opensim.modeling.PropertyHelper.setValueVec3(PropertyHelper.java:131)
"
btw , trying to run this command "PropertyHelper.getValueVec3(pointVec3Prop , 0)" is also lead to same error.
can you help to solve it?

User avatar
jimmy d
Posts: 1375
Joined: Thu Oct 04, 2007 11:51 pm

Re: OpenSim API: set-translation/rotation for wrapping obje

Post by jimmy d » Tue Nov 24, 2015 12:20 pm

Hi

You trying to access a Vec3 and not a Double. Let me show a 'real' example;

Code: Select all

## this is written in python. Just get rid of the 'osm.' parts and it should work in ## matlab

import opensim as osm 

# load a model
model = osm.Model('/home/support/Arnoldetal2010_2Legs_50pct_v2.1.osim')

# get a handle to a wrap object
wrap = model.getBodySet().get('pelvis').getWrapObjectSet().get(1)

# get handle to the property
prop = wrap.getPropertyByName('translation')

# get the x translation value
x_translation = osm.PropertyHelper.getValueDouble(prop , 0)

# set a new x tranlsation value
osm.PropertyHelper.setValueDouble(x_translation + 1 ,prop, 0)

# re-initialize the system to validate the 'new' model.
model.initSystem()
There is some documentation for the PropertyHelper, here. This is old (OpenSIm 3.0) and we don't provide documentation for it now because -as I mentioned earlier- in general we do not want people using this hack to change the model. If you call the wrong method, it will most likely crash the system you are using (either matlab or python), so backup your code trying to mess around with it.

Goodluck,
-James

User avatar
Vinh Nguyen
Posts: 21
Joined: Fri Sep 26, 2014 8:53 pm

Re: OpenSim API: set-translation/rotation for wrapping objects

Post by Vinh Nguyen » Mon Nov 07, 2016 4:42 pm

Hi,

I am using OpenSim 3.3, and it seems that it does not have PropertyHelper class.
Do you have any suggestions for this?

Thanks.

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

Re: OpenSim API: set-translation/rotation for wrapping objects

Post by Ayman Habib » Mon Nov 07, 2016 4:47 pm

Hi Vinh,

OpenSim 3.3 does have this class indeed (in fact it is used by the OpenSim application) though the documentation maybe lacking or missing. Why do you think the class is missing? Can you provide code snippet?

Best regards,
-Ayman

User avatar
Vinh Nguyen
Posts: 21
Joined: Fri Sep 26, 2014 8:53 pm

Re: OpenSim API: set-translation/rotation for wrapping objects

Post by Vinh Nguyen » Mon Nov 07, 2016 9:09 pm

Hi Ayman,

I tried to find "PropertyHelper" in OpenSim 3.3 doxygen (https://simtk.org/api_docs/opensim/api_docs/index.html), and
source code 3.3, but I could not find it.
Maybe I missed somethings here? Any suggestions?

Best regards,
Vinh

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

Re: OpenSim API: set-translation/rotation for wrapping objects

Post by Ayman Habib » Thu Nov 10, 2016 2:22 pm

Vinh,

PropertyHelper class is used only when creating bindings to call OpenSim from Python or Matlab, (primarily because these languages do not support templates, and the Property classes are all templatized). If you're coding on the C++ side then you should use the templatized version of the methods. The Doxygen documentation does not include the PropertyHelper class for that reason (not available in C++).

Please indicate your context (language/environment), what you're trying to accomplish & what error you're getting and we'll try to help you out.

Best regards,
-Ayman

User avatar
Vinh Nguyen
Posts: 21
Joined: Fri Sep 26, 2014 8:53 pm

Re: OpenSim API: set-translation/rotation for wrapping objects

Post by Vinh Nguyen » Thu Nov 10, 2016 3:12 pm

Thank you for your answers,

I am writing in VS C++. I would like to create a model which has a WrapCylinder().
The thing is when I add the wrapcylinder to the body, it is by default attached to the coordinate system of the body.
But I want to translate it to other position. This would be easy by modifying the .osim file. However, I would like to
get a way to do that with VS C++ so I don't need to switch to .osim file.

Best regards,

Vinh

User avatar
zanne cox
Posts: 5
Joined: Fri Sep 02, 2016 6:22 am

Re: OpenSim API: set-translation/rotation for wrapping objects

Post by zanne cox » Sat Nov 02, 2019 6:00 am

Thank you for this thread. Using the suggested approach, I was able to use MATLAB optimization tools to fit wrap surfaces to experimentally collected moment arm data. I'm using it to make subject specific models - but it would have been a lot easier to build the model in the first place without hand fitting wrap surfaces! This thread was specific to V3. Have there been any changes in 4.0 that would change the best way to do this?

POST REPLY