Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
-
Bernardo Costa
- Posts: 12
- Joined: Tue Jul 16, 2019 11:56 am
Post
by Bernardo Costa » Mon Jan 13, 2020 5:02 am
I was trying to translate the above code from python to cpp, but it segfaults while calling the getValue function below. I don't get why. What is wrong in here?
Code: Select all
SimTK::State wstate = model.getWorkingState();
OpenSim::CoordinateSet cdset = model.getCoordinateSet();
for (unsigned int i=0; i < cdset.getSize(); ++i) {
std::cout << "i = " << i << " coordinate = " << cdset.get(i).getValue(wstate) << std::endl;
}
Tags:
-
Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Post
by Ayman Habib » Mon Jan 13, 2020 11:08 am
Hello,
You can call the following methods from the scripting shell
Code: Select all
setCoordinateValue(Coordinate coordinate, double newValue) or setCoordinateValueDegrees(Coordinate coordinate, double newValue)
and the visualization will update, however these calls are not part of the API and are not recommended if you're doing multiple coordinate changes since the GUI update would be expensive and unnecessary.
Using the API visualizer maybe more appropriate in this case.
-Ayman
-
Bernardo Costa
- Posts: 12
- Joined: Tue Jul 16, 2019 11:56 am
Post
by Bernardo Costa » Mon Jan 13, 2020 12:40 pm
Well, I might need to change more than one coordinate. From your words, using the Visualizer API seems the correct way of doing it. I also believe that if I code in cpp it will run faster than in python. However, my cpp code segfaults while the equivalent python code runs fine. I can't see a reason for this.
Code: Select all
state = model.getWorkingState()
cdset = model.getCoordinateSet()
for i in range(cdset.getSize()):
print('coordinate name = ',cdset.get(i).toString())
print('coordinate = ',cdset.get(i).getValue(state))
Code: Select all
SimTK::State state = model.getWorkingState();
OpenSim::CoordinateSet cdset = model.getCoordinateSet();
OpenSim::Coordinate coord;
for (int i=0; i < cdset.getSize(); ++i) {
coord = cdset.get(i);
std::cout << "i = " << i << " coordinate name = " << coord.toString() << std::endl;
std::cout << "i = " << i << " coordinate = " << coord.getValue(state) << std::endl; // segfaults here
}
-
Ayman Habib
- Posts: 2248
- Joined: Fri Apr 01, 2005 12:24 pm
Post
by Ayman Habib » Mon Jan 13, 2020 1:21 pm
Hello,
Most of the C++ methods return references, if you don't use the correct syntax (e.g. OpenSim::CoordinateSet&) then C++ creates a copy, and that will cause crashes downstream since copies are dissociated from the model and are not the same as references. In Python, Matlab and other scripting languages, objects are typically just references.
Please consult the API guide or your favorite C++ "reference" to use the API as intended.
Hope this helps,
-Ayman