Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
parameter_limits.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
13
14namespace momentum {
15
16std::string_view toString(const LimitType type) {
17 switch (type) {
18 case MinMax:
19 return "MinMax";
20 case MinMaxJoint:
21 return "MinMaxJoint";
23 return "MinMaxJointPassive";
24 case Linear:
25 return "Linear";
26 case LinearJoint:
27 return "LinearJoint";
28 case Ellipsoid:
29 return "Ellipsoid";
30 case HalfPlane:
31 return "HalfPlane";
32 case LimitTypeCount:
33 // Sentinel value used to count enum entries; not a real limit type.
34 return "LimitTypeCount";
35 default:
36 return "Unknown";
37 }
38}
39
41 const ParameterLimits& limits,
42 const JointParameters& jointParams) {
43 JointParameters res = jointParams;
44 for (const auto& limit : limits) {
45 if (limit.type != MinMaxJointPassive) {
46 continue;
47 }
48
49 const auto& data = limit.data.minMaxJoint;
50 const size_t parameterIndex = data.jointIndex * kParametersPerJoint + data.jointParameter;
52 parameterIndex >= static_cast<size_t>(jointParams.size()),
53 "Parameter index {} out of bounds (size: {})",
54 parameterIndex,
55 jointParams.size());
56 if (res(parameterIndex) < data.limits[0]) {
57 res(parameterIndex) = data.limits[0];
58 }
59 if (res(parameterIndex) > data.limits[1]) {
60 res(parameterIndex) = data.limits[1];
61 }
62 }
63 return res;
64}
65
67 const std::string& name,
68 const ParameterTransform& pt,
69 const float weight) {
71
72 if (pt.poseConstraints.count(name) > 0) {
73 for (const auto& pc : pt.poseConstraints.at(name).parameterIdValue) {
75 p.type = MinMax;
76 p.data.minMax.parameterIndex = pc.first;
77 p.data.minMax.limits = Vector2f::Constant(pc.second);
78 p.weight = weight;
79 res.emplace_back(p);
80 }
81 }
82
83 return res;
84}
85
87 std::fill_n(rawData.begin(), rawData.size(), 0);
88}
89
91 std::copy_n(rhs.rawData.begin(), rhs.rawData.size(), rawData.begin());
92}
93
95 if (&rhs != this) {
96 std::copy_n(rhs.rawData.begin(), rhs.rawData.size(), rawData.begin());
97 }
98 return *this;
99}
100
101bool LimitData::operator==(const LimitData& limitData) const {
102 return (std::memcmp(this, &limitData, sizeof(LimitData)) == 0);
103}
104
105bool isInRange(const LimitLinear& limit, float value) {
106 // Treat a zero-zero range as "applies everywhere": LimitData's default constructor
107 // zero-fills rawData (see LimitData::LimitData above), so an unset range is (0, 0).
108 if (limit.rangeMin == 0 && limit.rangeMax == 0) {
109 return true;
110 }
111
112 return value >= limit.rangeMin && value < limit.rangeMax;
113}
114
115bool isInRange(const LimitLinearJoint& limit, float value) {
116 // Treat a zero-zero range as "applies everywhere": LimitData's default constructor
117 // zero-fills rawData (see LimitData::LimitData above), so an unset range is (0, 0).
118 if (limit.rangeMin == 0 && limit.rangeMax == 0) {
119 return true;
120 }
121
122 return value >= limit.rangeMin && value < limit.rangeMax;
123}
124
125} // namespace momentum
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
std::vector< ParameterLimit > ParameterLimits
JointParameters applyPassiveJointParameterLimits(const ParameterLimits &limits, const JointParameters &jointParams)
Only processes MinMaxJointPassive limits, clamping parameters to their ranges.
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
JointParametersT< float > JointParameters
Definition types.h:135
constexpr size_t kParametersPerJoint
Definition types.h:21
bool isInRange(const LimitLinear &limit, float value)
If rangeMin and rangeMax are both 0, the limit applies to all values.
ParameterLimits getPoseConstraintParameterLimits(const std::string &name, const ParameterTransform &pt, const float weight)
Creates MinMax limits from a pose constraint.
std::string_view toString(const LimitType type)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto size() const
Definition types.h:87
float rangeMax
Non-inclusive.
float rangeMin
Range where limit applies (in target parameter values).
Linear relationship between parameters: p_0 = s * p_1 - o.
float rangeMin
Range where limit applies (in target parameter values).
float rangeMax
Non-inclusive.
Vector2f limits
[min, max]
PoseConstraints poseConstraints
A set of predefined poses.
bool operator==(const LimitData &limitData) const
Compares raw memory.
LimitData & operator=(const LimitData &rhs)
LimitData()
Initializes all data to zero.
std::array< unsigned char, 512 > rawData
For memory operations.