-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement AbstractPath
and related API changes to support multiple path types
#3535
Changes from 16 commits
34daa7a
3d5c2ee
e0f19be
81d1aee
b4f11fd
b70b2fa
5e98a45
4f81105
016d8bd
bb4b8be
5a74515
ce3b0c4
c383f64
e57c1f6
e53ea0b
c3ac610
df76b0a
f64e3cd
f6e7208
2940c77
25ed001
e9e0008
507e4e1
83577ea
83959bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
#include <OpenSim/Simulation/SimbodyEngine/PinJoint.h> | ||
#include <OpenSim/Simulation/SimbodyEngine/SliderJoint.h> | ||
#include <OpenSim/Simulation/SimbodyEngine/WeldJoint.h> | ||
#include <OpenSim/Simulation/Model/GeometryPath.h> | ||
#include <OpenSim/Common/CommonUtilities.h> | ||
|
||
using namespace OpenSim; | ||
|
@@ -171,35 +172,44 @@ void ModelFactory::replaceMusclesWithPathActuators(OpenSim::Model &model) { | |
|
||
model.addForce(actu); | ||
|
||
// For the connectee names in the PathPoints to be correct, we must add | ||
// the path points after adding the PathActuator to the model. | ||
const auto& pathPointSet = musc.getGeometryPath().getPathPointSet(); | ||
auto& geomPath = actu->updGeometryPath(); | ||
for (int ip = 0; ip < pathPointSet.getSize(); ++ip) { | ||
auto* pathPoint = pathPointSet.get(ip).clone(); | ||
const auto& socketNames = pathPoint->getSocketNames(); | ||
for (const auto& socketName : socketNames) { | ||
pathPoint->updSocket(socketName) | ||
.connect(pathPointSet.get(ip) | ||
.getSocket(socketName) | ||
.getConnecteeAsObject()); | ||
// Copy the muscle's path. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code's behavior/intent changes with the change. The function is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do this. it's a bad idea for a large C++ codebase to start having many Either:
Then you can do what you're doing within the framework of those solutions, but only if the concrete implementation has no feasible way of working without points (e.g. the visitor would throw for non-point-based paths with "there's no way of doing this operation with a function-based path - sorry not sorry" or similar). |
||
if (auto* pGeometryPath = musc.tryUpdPath<GeometryPath>()) { | ||
// For the connectee names in the PathPoints to be correct, we must | ||
// add the path points after adding the PathActuator to the model. | ||
const auto& pathPointSet = pGeometryPath->getPathPointSet(); | ||
for (int ip = 0; ip < pathPointSet.getSize(); ++ip) { | ||
auto* pathPoint = pathPointSet.get(ip).clone(); | ||
const auto& socketNames = pathPoint->getSocketNames(); | ||
for (const auto& socketName : socketNames) { | ||
pathPoint->updSocket(socketName) | ||
.connect(pathPointSet.get(ip) | ||
.getSocket(socketName) | ||
.getConnecteeAsObject()); | ||
} | ||
actu->updGeometryPath().updPathPointSet() | ||
.adoptAndAppend(pathPoint); | ||
} | ||
geomPath.updPathPointSet().adoptAndAppend(pathPoint); | ||
} | ||
|
||
// For the connectee names in the PathWraps to be correct, we must add | ||
// the path wraps after adding the PathActuator to the model. | ||
const auto& pathWrapSet = musc.getGeometryPath().getWrapSet(); | ||
for (int ipw = 0; ipw < pathWrapSet.getSize(); ++ipw) { | ||
auto* pathWrap = pathWrapSet.get(ipw).clone(); | ||
const auto& socketNames = pathWrap->getSocketNames(); | ||
for (const auto& socketName : socketNames) { | ||
pathWrap->updSocket(socketName) | ||
.connect(pathWrapSet.get(ipw) | ||
.getSocket(socketName) | ||
.getConnecteeAsObject()); | ||
// For the connectee names in the PathWraps to be correct, we must | ||
// add the path wraps after adding the PathActuator to the model. | ||
const auto& pathWrapSet = pGeometryPath->getWrapSet(); | ||
for (int ipw = 0; ipw < pathWrapSet.getSize(); ++ipw) { | ||
auto* pathWrap = pathWrapSet.get(ipw).clone(); | ||
const auto& socketNames = pathWrap->getSocketNames(); | ||
for (const auto& socketName : socketNames) { | ||
pathWrap->updSocket(socketName) | ||
.connect(pathWrapSet.get(ipw) | ||
.getSocket(socketName) | ||
.getConnecteeAsObject()); | ||
} | ||
actu->updGeometryPath().updWrapSet() | ||
.adoptAndAppend(pathWrap); | ||
} | ||
geomPath.updWrapSet().adoptAndAppend(pathWrap); | ||
} else { | ||
OPENSIM_THROW(Exception, | ||
"Muscle '{}' contains supported path type {}.", | ||
musc.getName(), | ||
musc.getPath().getConcreteClassName()) | ||
} | ||
|
||
musclesToDelete.push_back(&musc); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* -------------------------------------------------------------------------- * | ||
* OpenSim: AbstractPath.cpp * | ||
* -------------------------------------------------------------------------- * | ||
* The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * | ||
* See http://opensim.stanford.edu and the NOTICE file for more information. * | ||
* OpenSim is developed at Stanford University and supported by the US * | ||
* National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * | ||
* through the Warrior Web program. * | ||
* * | ||
* Copyright (c) 2005-2023 Stanford University and the Authors * | ||
* Author(s): Nicholas Bianco, Joris Verhagen, Adam Kewley * | ||
* * | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may * | ||
* not use this file except in compliance with the License. You may obtain a * | ||
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, software * | ||
* distributed under the License is distributed on an "AS IS" BASIS, * | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
* See the License for the specific language governing permissions and * | ||
* limitations under the License. * | ||
* -------------------------------------------------------------------------- */ | ||
|
||
#include "AbstractPath.h" | ||
#include "Appearance.h" | ||
|
||
using namespace OpenSim; | ||
|
||
AbstractPath::AbstractPath() : ModelComponent() { | ||
setAuthors("Nicholas Bianco, Joris Verhagen, Adam Kewley"); | ||
|
||
Appearance appearance; | ||
appearance.set_color(SimTK::Gray); | ||
constructProperty_Appearance(appearance); | ||
} | ||
|
||
AbstractPath::AbstractPath(AbstractPath const&) = default; | ||
|
||
AbstractPath::~AbstractPath() noexcept = default; | ||
|
||
AbstractPath& AbstractPath::operator=(const AbstractPath&) = default; | ||
nickbianco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
AbstractPath::AbstractPath(AbstractPath&& other) noexcept = default; | ||
|
||
AbstractPath& AbstractPath::operator=(AbstractPath&& other) noexcept = default; | ||
|
||
// DEFAULTED METHODS | ||
const SimTK::Vec3& AbstractPath::getDefaultColor() const | ||
{ | ||
return get_Appearance().get_color(); | ||
} | ||
|
||
void AbstractPath::setDefaultColor(const SimTK::Vec3& color) | ||
{ | ||
updProperty_Appearance().setValueIsDefault(false); | ||
upd_Appearance().set_color(color); | ||
} | ||
|
||
double AbstractPath::getPreScaleLength(const SimTK::State&) const | ||
{ | ||
return _preScaleLength; | ||
} | ||
|
||
void AbstractPath::setPreScaleLength(const SimTK::State&, | ||
double preScaleLength) | ||
{ | ||
_preScaleLength = preScaleLength; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes behavior/intent
The function is
DeGrooteFregly2016Muscle::replaceMuscles
. Apart from that function,DeGrooteFregly2016Muscle
doesn't appear to rely on "point-based paths" directly, so it should work with function-based paths also? The refactor effectively breaksDeGrootFregly2016Muscle
if it is given a non-point-based path because it makes no attempt to handle theelse
part ofif (auto* pGeometryPath = dynamic_cast<GeometryPath*>(&path))
If that's the case, I'd recommend writing a generic
CopyPath
/CopyMuscle
functions that can be used in cases where muscles want to copy this information around.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as below, I've added an
else
branch to catch unsupported path types. As we add new path types, we can add newelseif
branches to included supported path types.The generic functions for copying muscle information could be implemented and should probably live somewhere else (@aymanhab has suggested this before). I think that could be addressed in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd tackle copying centrally in this PR rather than leaving an
else
as a future exercise: there's a high chance it'll be forgotten and undetected until a user runs into it at runtime (e.g. by usingDeGrooteFregly2016Muscle
with a non-GeometryPath
)Plus, if copy assignment of paths is something that's in downstream code (e.g. as it is here) then, architecturally speaking, it makes sense that copying is tackled centrally and code is ported accordingly.