Page 1 of 1

Iteratively Changing a Point in a Geometry Path

Posted: Wed Jan 18, 2023 12:01 pm
by finn.eagen
Hello,

I am working with a path spring, and I want to see how its behavior changes as I move the origin across a series of points. My plan was to do this in MatLab by retrieving its geometry path, adding a new point, and deleting the old origin. However, I've ran into a few issues.

First, what is the intended use of .appendNewPathPoint versus .addPathPoint? Based on the inputs I assume in this case I should be using .appendNewPathPoint as it lets me specify the position, but just wanted to check.

More importantly, once I have edited the path, and I try to print the model to a new .osim file, or do any further analysis on the model, MatLab crashes. I'm unsure as to why.

Is there a known, stable way to do this sort of edit?

Any advice is appreciated. Thank you,
Finn

Re: Iteratively Changing a Point in a Geometry Path

Posted: Thu Jan 19, 2023 7:48 am
by tkuchida
what is the intended use of .appendNewPathPoint versus .addPathPoint? Based on the inputs I assume in this case I should be using .appendNewPathPoint as it lets me specify the position, but just wanted to check.
TL;DR: I believe the method to use is addNewPathPoint() (https://simtk.org/api_docs/opensim/api_ ... 1d5f10ceda). The example here might be helpful: https://github.com/opensim-org/opensim- ... ice.m#L131. If you need to change the location of the point, you can use the PathPoint's setLocation() method (https://simtk.org/api_docs/opensim/api_ ... 0eb91fc2d8).

With some of these Set-related methods (e.g., adding a new item to the PathPointSet, https://simtk.org/api_docs/opensim/api_ ... ntSet.html), special care is required to ensure the Model "owns" the new object (in this case, a PathPoint). A common issue in Matlab and Python scripts is creating a new object in a loop or function and "adding" it to the Model without the Model taking ownership of the new object; when the loop ends or the function is exited, the new object goes out of scope and is garbage collected---meaning that the memory location it occupied is freed up by the operating system. When the Model then tries to access the new information, a memory access violation exception is thrown: the operating system does not permit a program to read parts of memory that have not been allocated to it. In OpenSim, there are methods available to avoid this issue (such as addNewPathPoint()) or you use adoptAndAppend() which appends the new object to the Set but also "adopts" it or transfers ownership so that the memory is not released when the loop ends or the function is exited.

Re: Iteratively Changing a Point in a Geometry Path

Posted: Thu Jan 19, 2023 3:37 pm
by aymanh
Thanks Tom for the explanation and suggestions.

Indeed, typically we recommend you don't modify the structure of the Path by adding or removing points when in fact you only want to change the location of the attachment points since it is a fundamentally simpler task. Note also that the Set of PathPoints is ordered, so appending typically adds to the end of the list rather than insert into other positions, which can possibly cause other problems downstream.

Please let us know if you continue to have problems with this approach,

Best regards,
-Ayman

Re: Iteratively Changing a Point in a Geometry Path

Posted: Wed Feb 01, 2023 11:35 am
by finn.eagen
I appreciate the replies, it is very interesting to learn how OpenSim is handling the back end. As far as object "ownership" and all.

I was also able to figure out how to update just the location of the insertion. I had been missing the .safeDownCast that let me convert the abstract path point into a path point who's location I could update. But it makes sense now, and thank you again for your help.

Finn