Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
aim_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>, 2>& v,
20 std::array<Eigen::Matrix3<T>, 2>& dfdv) const {
22
23 const AimDataT<T>& constr = this->constraints_[constrIndex];
24 v[0] = state.transform * constr.localPoint;
25 v[1] = state.rotation() * constr.localDir;
26 const Vector3<T> tgtVec = constr.globalTarget - v[0];
27 const T projLength = v[1].dot(tgtVec);
28
29 // f = (globalTarget - point).dot(srcDir) * srcDir - (globalTarget - point)
30 f = projLength * v[1] - tgtVec;
31 // df/d(point) = I - outterProd(srcDir, srcDir)
32 dfdv[0].setIdentity();
33 dfdv[0].noalias() -= v[1] * v[1].transpose();
34 // df/d(dir) = projLength * I + outterProd(srcDir, tgtVec)
35 dfdv[1].noalias() = v[1] * tgtVec.transpose();
36 dfdv[1].diagonal().array() += projLength;
37}
38
39template <typename T>
41 const size_t constrIndex,
42 const JointStateT<T>& state,
43 Vector3<T>& f,
44 std::array<Vector3<T>, 2>& v,
45 std::array<Eigen::Matrix3<T>, 2>& dfdv) const {
47
48 const AimDataT<T>& constr = this->constraints_[constrIndex];
49 v[0] = state.transform * constr.localPoint;
50 v[1] = state.rotation() * constr.localDir;
51 const Vector3<T> tgtVec = constr.globalTarget - v[0];
52 const T tgtNorm = tgtVec.norm();
54 if (tgtNorm > 1e-16) {
55 tgtDir = tgtVec / tgtNorm;
56 }
57
58 // f = srcDir - (globalTarget - point).normalize()
59 f = v[1] - tgtDir;
60 // df/d(point) = (I - outterProd(tgtDir, tgtDir)) / tgtNorm
61 dfdv[0].setZero();
62 if (tgtNorm > 1e-16) {
63 dfdv[0].noalias() -= (tgtDir * tgtDir.transpose()) / tgtNorm;
64 dfdv[0].diagonal().array() += T(1) / tgtNorm;
65 }
66 dfdv[1].setIdentity();
67}
68
69template class AimDistErrorFunctionT<float>;
71
72template class AimDirErrorFunctionT<float>;
73template class AimDirErrorFunctionT<double>;
74
75} // namespace momentum
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector3< T > &f, std::array< Vector3< T >, 2 > &v, std::array< Eigen::Matrix3< T >, 2 > &dfdv) const final
The AimDistErrorFunction computes the distance from the aim target point to an aim ray (an original a...
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector3< T > &f, std::array< Vector3< T >, 2 > &v, std::array< Eigen::Matrix3< T >, 2 > &dfdv) const final
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
Aim constraint data: a local ray (point, dir) passes a global point target.
Vector3< T > localDir
The direction of the local ray.
Vector3< T > localPoint
The origin of the local ray.
Vector3< T > globalTarget
The global aim target.
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
TransformT< T > transform
Global transformation in world space.
Definition joint_state.h:59
const Quaternion< T > & rotation() const
Access the joint's global rotation quaternion.