-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into prep-release-0.2.0
- Loading branch information
Showing
16 changed files
with
315 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* -------------------------------------------------------------------------- * | ||
* OpenSim Moco: MocoFrameDistanceConstraint.cpp * | ||
* -------------------------------------------------------------------------- * | ||
* Copyright (c) 2019 Stanford University and the Authors * | ||
* * | ||
* Author(s): Nicholas Bianco * | ||
* * | ||
* 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 "MocoFrameDistanceConstraint.h" | ||
|
||
using namespace OpenSim; | ||
|
||
//============================================================================= | ||
// MocoFrameDistanceConstraintPair | ||
//============================================================================= | ||
|
||
MocoFrameDistanceConstraintPair::MocoFrameDistanceConstraintPair() { | ||
constructProperties(); | ||
} | ||
|
||
MocoFrameDistanceConstraintPair::MocoFrameDistanceConstraintPair( | ||
std::string frame1Path, std::string frame2Path, | ||
double minimum_distance, double maximum_distance) { | ||
constructProperties(); | ||
set_frame1_path(frame1Path); | ||
set_frame2_path(frame2Path); | ||
set_minimum_distance(minimum_distance); | ||
set_maximum_distance(maximum_distance); | ||
} | ||
|
||
void MocoFrameDistanceConstraintPair::constructProperties() { | ||
constructProperty_frame1_path(""); | ||
constructProperty_frame2_path(""); | ||
constructProperty_minimum_distance(0); | ||
constructProperty_maximum_distance(SimTK::Infinity); | ||
} | ||
|
||
//============================================================================= | ||
// MocoFrameDistanceConstraint | ||
//============================================================================= | ||
|
||
MocoFrameDistanceConstraint::MocoFrameDistanceConstraint(){ | ||
constructProperties(); | ||
} | ||
|
||
void MocoFrameDistanceConstraint::initializeOnModelImpl( | ||
const Model& model, const MocoProblemInfo&) const { | ||
int nFramePairs = getProperty_frame_pairs().size(); | ||
|
||
// TODO: setConstraintInfo() is not really intended for use here. | ||
MocoConstraintInfo info; | ||
std::vector<MocoBounds> bounds; | ||
for (int i = 0; i < nFramePairs; ++i) { | ||
const auto frame1_path = get_frame_pairs(i).get_frame1_path(); | ||
OPENSIM_THROW_IF(!model.hasComponent<Frame>(frame1_path), Exception, | ||
format("Could not find frame '%s'.", frame1_path)); | ||
auto& frame1 = model.getComponent<Frame>(frame1_path); | ||
const auto frame2_path = get_frame_pairs(i).get_frame2_path(); | ||
OPENSIM_THROW_IF(!model.hasComponent<Frame>(frame2_path), Exception, | ||
format("Could not find frame '%s'.", frame2_path)); | ||
auto& frame2 = model.getComponent<Frame>(frame2_path); | ||
m_frame_pairs.emplace_back(&frame1, &frame2); | ||
|
||
const double& minimum = get_frame_pairs(i).get_minimum_distance(); | ||
const double& maximum = get_frame_pairs(i).get_maximum_distance(); | ||
OPENSIM_THROW_IF(minimum < 0, Exception, | ||
format("Expected the minimum distance for this frame pair to " | ||
"non-negative, but it is %d.", minimum)); | ||
OPENSIM_THROW_IF(maximum < 0, Exception, | ||
format("Expected the maximum distance for this frame pair to " | ||
"non-negative, but it is %d.", maximum)); | ||
OPENSIM_THROW_IF(minimum > maximum, Exception, | ||
format("Expected the minimum distance for this frame pair to " | ||
"be less than or equal to the maximum distance, but " | ||
"they are %d and %d, respectively.", minimum, maximum)); | ||
bounds.emplace_back(SimTK::square(minimum), SimTK::square(maximum)); | ||
} | ||
|
||
setNumEquations(nFramePairs); | ||
info.setBounds(bounds); | ||
const_cast<MocoFrameDistanceConstraint*>(this)->setConstraintInfo(info); | ||
} | ||
|
||
void MocoFrameDistanceConstraint::calcPathConstraintErrorsImpl( | ||
const SimTK::State& state, SimTK::Vector& errors) const { | ||
getModel().realizePosition(state); | ||
int iconstr = 0; | ||
SimTK::Vec3 relative_position; | ||
for (const auto& frame_pair : m_frame_pairs) { | ||
const auto& frame1_pos = frame_pair.first->getPositionInGround(state); | ||
const auto& frame2_pos = frame_pair.second->getPositionInGround(state); | ||
relative_position = frame2_pos - frame1_pos; | ||
errors[iconstr++] = relative_position.normSqr(); | ||
} | ||
} | ||
|
||
void MocoFrameDistanceConstraint::constructProperties() { | ||
constructProperty_frame_pairs(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#ifndef MOCO_MOCOFRAMEDISTANCECONSTRAINT_H | ||
#define MOCO_MOCOFRAMEDISTANCECONSTRAINT_H | ||
/* -------------------------------------------------------------------------- * | ||
* OpenSim Moco: MocoFrameDistanceConstraint.h * | ||
* -------------------------------------------------------------------------- * | ||
* Copyright (c) 2019 Stanford University and the Authors * | ||
* * | ||
* Author(s): Nicholas Bianco * | ||
* * | ||
* 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 "MocoConstraint.h" | ||
#include "osimMocoDLL.h" | ||
|
||
namespace OpenSim { | ||
|
||
class OSIMMOCO_API MocoFrameDistanceConstraintPair : public Object { | ||
OpenSim_DECLARE_CONCRETE_OBJECT(MocoFrameDistanceConstraintPair, Object); | ||
|
||
public: | ||
OpenSim_DECLARE_PROPERTY(frame1_path, std::string, | ||
"The first model frame path of the pair."); | ||
OpenSim_DECLARE_PROPERTY(frame2_path, std::string, | ||
"The second model frame path of the pair."); | ||
OpenSim_DECLARE_PROPERTY(minimum_distance, double, | ||
"The minimum distance apart that the two frame origins can be."); | ||
OpenSim_DECLARE_PROPERTY(maximum_distance, double, | ||
"The maximum distance apart that the two frame origins can be.") | ||
|
||
MocoFrameDistanceConstraintPair(); | ||
MocoFrameDistanceConstraintPair(std::string firstFramePath, | ||
std::string secondFramePath, double minimum_distance, | ||
double maximum_distance); | ||
|
||
private: | ||
void constructProperties(); | ||
}; | ||
|
||
/// This path constraint enforces that the distance between the origins of pairs | ||
/// of model frames is kept between minimum and maximum bounds. Frame pairs and | ||
/// their bounds are specified via a MocoFrameDistancConstraintPair. | ||
/// Any model component derived from Frame is valid to be included in a frame | ||
/// pair, and any number of frame pairs may be append to this constraint via | ||
/// addFramePair(). | ||
/// | ||
/// This constraint can be used as a simple method for preventing bodies in your | ||
/// model from intersecting during an optimization. For example, the | ||
/// following prevents feet from intersecting during a walking optimization: | ||
/// @code | ||
/// distance = problem.addPathConstraint<MocoFrameDistanceConstraint>(); | ||
/// distance.setName("minimum_distance"); | ||
/// SimTK::Real inf = SimTK::Infinity; | ||
/// distance.addFramePair('/bodyset/calcn_l', '/bodyset/calcn_r', 0.1, inf); | ||
/// distance.addFramePair('/bodyset/toes_l', '/bodyset/toes_r', 0.1, inf); | ||
/// distance.addFramePair('/bodyset/calcn_l', '/bodyset/toes_r', 0.1, inf); | ||
/// distance.addFramePair('/bodyset/toes_l', '/bodyset/calcn_r', 0.1, inf); | ||
/// @endcode | ||
/// | ||
/// @note This class represents a path constraint, *not* a model kinematic | ||
/// constraint. Therefore, there are no Lagrange multipliers or constraint | ||
/// forces associated with this constraint. The model's force elements | ||
/// (including actuators) must generate the forces necessary for satisfying this | ||
/// constraint. | ||
/// | ||
/// @ingroup mocopathcon | ||
class OSIMMOCO_API MocoFrameDistanceConstraint : public MocoPathConstraint { | ||
OpenSim_DECLARE_CONCRETE_OBJECT( | ||
MocoFrameDistanceConstraint, MocoPathConstraint); | ||
|
||
public: | ||
MocoFrameDistanceConstraint(); | ||
|
||
void addFramePair(MocoFrameDistanceConstraintPair pair) { | ||
append_frame_pairs(std::move(pair)); | ||
} | ||
void addFramePair(const std::string& frame1_path, | ||
const std::string& frame2_path, double minimum_distance, | ||
double maximum_distance) { | ||
append_frame_pairs(MocoFrameDistanceConstraintPair(frame1_path, | ||
frame2_path, minimum_distance, maximum_distance)); | ||
} | ||
|
||
protected: | ||
void initializeOnModelImpl( | ||
const Model& model, const MocoProblemInfo&) const override; | ||
void calcPathConstraintErrorsImpl( | ||
const SimTK::State& state, SimTK::Vector& errors) const override; | ||
|
||
private: | ||
OpenSim_DECLARE_LIST_PROPERTY(frame_pairs, | ||
MocoFrameDistanceConstraintPair, | ||
"Pairs of frames whose origins are constrained to be within minimum " | ||
"and maximum bounds."); | ||
|
||
void constructProperties(); | ||
mutable std::vector<std::pair<SimTK::ReferencePtr<const Frame>, | ||
SimTK::ReferencePtr<const Frame>>> m_frame_pairs; | ||
}; | ||
|
||
} // namespace OpenSim | ||
|
||
#endif // MOCO_MOCOFRAMEDISTANCECONSTRAINT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.