Skip to content

Commit

Permalink
Backwards compatibility for deserializing GeometryPath
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbianco committed Aug 30, 2023
1 parent e53ea0b commit c3ac610
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion OpenSim/Actuators/ModelFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class OSIMACTUATORS_API ModelFactory {
/// @name Modify a Model
/// @{

/// Replace muscles in a model with a PathActuator of the same GeometryPath,
/// Replace muscles in a model with a PathActuator of the same path,
/// optimal force, and min/max control defaults.
/// @note This only replaces muscles within the model's ForceSet.
static void replaceMusclesWithPathActuators(Model& model);
Expand Down
4 changes: 3 additions & 1 deletion OpenSim/Common/XMLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ using namespace std;
// 30516 for GeometryPath default_color -> Appearance
// 30517 for removal of _connectee_name suffix to shorten XML for socket, input
// 40000 for OpenSim 4.0 release 40000
// 40500 for OpenSim 4.5 release, moving 'GeometryPath' nodes under the 'path'
// node for supporting generic path types.

const int XMLDocument::LatestVersion = 40000;
const int XMLDocument::LatestVersion = 40500;
//=============================================================================
// DESTRUCTOR AND CONSTRUCTOR(S)
//=============================================================================
Expand Down
2 changes: 1 addition & 1 deletion OpenSim/Simulation/Model/AbstractPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ OpenSim_DECLARE_ABSTRACT_OBJECT(AbstractPath, ModelComponent);
* @param[in,out] mobilityForces Vector of generalized forces
*/
virtual void addInEquivalentForces(const SimTK::State& state,
double tension,
const double& tension,
SimTK::Vector_<SimTK::SpatialVec>& bodyForces,
SimTK::Vector& mobilityForces) const = 0;

Expand Down
11 changes: 2 additions & 9 deletions OpenSim/Simulation/Model/GeometryPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static void PopulatePathElementLookup(
/*
* Default constructor.
*/
GeometryPath::GeometryPath() : AbstractPath(), _preScaleLength(0.0)
GeometryPath::GeometryPath() : AbstractPath()
{
setAuthors("Peter Loan");
constructProperties();
Expand Down Expand Up @@ -382,7 +382,7 @@ getPointForceDirections(const SimTK::State& s,
/* add in the equivalent spatial forces on bodies for an applied tension
along the GeometryPath to a set of bodyForces */
void GeometryPath::addInEquivalentForces(const SimTK::State& s,
double tension,
const double& tension,
SimTK::Vector_<SimTK::SpatialVec>& bodyForces,
SimTK::Vector& mobilityForces) const
{
Expand Down Expand Up @@ -564,13 +564,6 @@ void GeometryPath::setLengtheningSpeed( const SimTK::State& s, double speed ) co
setCacheVariableValue(s, _speedCV, speed);
}

void GeometryPath::setPreScaleLength( const SimTK::State& s, double length ) {
_preScaleLength = length;
}
double GeometryPath::getPreScaleLength( const SimTK::State& s) const {
return _preScaleLength;
}

//=============================================================================
// UTILITY
//=============================================================================
Expand Down
7 changes: 1 addition & 6 deletions OpenSim/Simulation/Model/GeometryPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ OpenSim_DECLARE_CONCRETE_OBJECT(GeometryPath, AbstractPath);
OpenSim_DECLARE_UNNAMED_PROPERTY(PathWrapSet,
"The wrap objects that are associated with this path");

// used for scaling tendon and fiber lengths
double _preScaleLength;

// Solver used to compute moment-arms. The GeometryPath owns this object,
// but we cannot simply use a unique_ptr because we want the pointer to be
// cleared on copy.
Expand Down Expand Up @@ -143,8 +140,6 @@ OpenSim_DECLARE_CONCRETE_OBJECT(GeometryPath, AbstractPath);

double getLength( const SimTK::State& s) const;
void setLength( const SimTK::State& s, double length) const;
double getPreScaleLength( const SimTK::State& s) const;
void setPreScaleLength( const SimTK::State& s, double preScaleLength);
const Array<AbstractPathPoint*>& getCurrentPath( const SimTK::State& s) const;

double getLengtheningSpeed(const SimTK::State& s) const;
Expand All @@ -163,7 +158,7 @@ OpenSim_DECLARE_CONCRETE_OBJECT(GeometryPath, AbstractPath);
@param[in,out] mobilityForces Vector of generalized forces, one per mobility
*/
void addInEquivalentForces(const SimTK::State& state,
double tension,
const double& tension,
SimTK::Vector_<SimTK::SpatialVec>& bodyForces,
SimTK::Vector& mobilityForces) const override;

Expand Down
48 changes: 48 additions & 0 deletions OpenSim/Simulation/Model/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,54 @@ void Model::updateFromXMLNode(SimTK::Xml::Element& aNode, int versionNumber)
framesNode->getParentElement().eraseNode(framesNode);
}
}
if (versionNumber < 40500) {
// In version 40500, the XML syntax for components that own
// GeometryPath objects (PathActuator, PathSpring, Ligament,
// and Blankevoort1991Ligament) changed: the 'GeometryPath` unnamed
// property was replaced with the named property 'path', which is of
// type 'AbstractPath'. Since 'path' is still a one object property,
// the property will still be serialized using the concrete type
// (e.g., 'GeometryPath') and with name attribute set to 'path'.
// Therefore, we can simply update the name attribute of any
// existing 'GeometryPath' nodes to 'path'.

// Components that own paths can be in the model's ForceSet or
// the components list.
SimTK::Xml::element_iterator forceSetNode =
aNode.element_begin("ForceSet");
SimTK::Xml::element_iterator componentsNode =
aNode.element_begin("components");

// Loop through the ForceSet and update any GeometryPath nodes.
if (forceSetNode != aNode.element_end()) {
SimTK::Xml::element_iterator objects_node =
forceSetNode->element_begin("objects");
SimTK::Xml::element_iterator forceIter =
objects_node->element_begin();
for (; forceIter != objects_node->element_end(); ++forceIter) {
SimTK::Xml::element_iterator geometryPathNode =
forceIter->element_begin("GeometryPath");
if (geometryPathNode != forceIter->element_end()) {
geometryPathNode->setAttributeValue("name", "path");
}
}
}

// Loop through the components list and update any GeometryPath
// nodes.
if (componentsNode != aNode.element_end()) {
SimTK::Xml::element_iterator forceIter =
componentsNode->element_begin();
for (; forceIter != componentsNode->element_end(); ++forceIter) {
SimTK::Xml::element_iterator geometryPathNode =
forceIter->element_begin("GeometryPath");
if (geometryPathNode != forceIter->element_end()) {
geometryPathNode->setAttributeValue("name", "path");
}
}
}

}
}
// Call base class now assuming _node has been corrected for current version
Super::updateFromXMLNode(aNode, versionNumber);
Expand Down

0 comments on commit c3ac610

Please sign in to comment.