Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
fixed_axis_error_function.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
11
12namespace momentum {
13
14template <typename T>
16 const size_t constrIndex,
17 const JointStateT<T>& state,
18 Vector3<T>& f,
19 std::array<Vector3<T>, 1>& v,
20 std::array<Matrix3<T>, 1>& dfdv) const {
22
23 const FixedAxisDataT<T>& constr = this->constraints_[constrIndex];
24 v[0] = state.rotation() * constr.localAxis;
25 f = v[0] - constr.globalAxis;
26 dfdv[0].setIdentity();
27}
28
29template <typename T>
31 const size_t constrIndex,
32 const JointStateT<T>& state,
33 Vector<T, 1>& f,
34 std::array<Vector3<T>, 1>& v,
35 std::array<Eigen::Matrix<T, 1, 3>, 1>& dfdv) const {
37
38 const FixedAxisDataT<T>& constr = this->constraints_[constrIndex];
39 v[0] = state.rotation() * constr.localAxis;
40 f[0] = 1 - v[0].dot(constr.globalAxis);
41 dfdv[0] = -constr.globalAxis.transpose();
42}
43
44template <typename T>
46 const size_t constrIndex,
47 const JointStateT<T>& state,
48 Vector<T, 1>& f,
49 std::array<Vector3<T>, 1>& v,
50 std::array<Eigen::Matrix<T, 1, 3>, 1>& dfdv) const {
52
53 const FixedAxisDataT<T>& constr = this->constraints_[constrIndex];
54 v[0] = state.rotation() * constr.localAxis;
55 const T dot = v[0].dot(constr.globalAxis);
56 f[0] = std::acos(std::clamp(dot, -T(1), T(1)));
57 // The derivative of d[acos(x)]/dx = -1/sqrt(1-x^2), where x is the cosine of the angle.
58 // When the angle is 0 or 180, x=+/-1.0, and d[acos(x)] is infinity. But because dx=sine(angle)
59 // is also zero, the final derivative will be zero as well.
60 // Comparing to the Cos version, this Acos version scales up the jacobian by the inverse of
61 // sine.
62 const T sine = std::sqrt(1 - dot * dot);
63 if (sine > 1e-9) {
64 dfdv[0] = -constr.globalAxis.transpose() / sine;
65 } else {
66 dfdv[0].setZero();
67 }
68}
69
72
75
78
79} // namespace momentum
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector< T, 1 > &f, std::array< Vector3< T >, 1 > &v, std::array< Eigen::Matrix< T, 1, 3 >, 1 > &dfdv) const final
Cos: compute the dot product between the two axis - cosine of the angle between them.
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector< T, 1 > &f, std::array< Vector3< T >, 1 > &v, std::array< Eigen::Matrix< T, 1, 3 >, 1 > &dfdv) const final
Diff: compute the element wise differences between the two axis.
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector3< T > &f, std::array< Vector3< T >, 1 > &v, std::array< Matrix3< T >, 1 > &dfdv) const final
Eigen::Matrix< T, 3, 3 > Matrix3
Definition types.h:96
FloatP dot(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes dot product of Eigen::Vector3f and 3-vector of packets.
Definition simd.h:191
Eigen::Matrix< T, Dim, 1, Options > Vector
Column vector alias (an Nx1 Eigen matrix).
Definition types.h:203
Vector< T, 3 > Vector3
Definition types.h:236
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
Definition profile.h:110
Vector3< T > localAxis
axis in the parent's coordinate frame
Vector3< T > globalAxis
target axis in the global frame
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
const Quaternion< T > & rotation() const
Access the joint's global rotation quaternion.