Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
orientation_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 Vector<T, 9>& f,
19 std::array<Vector3<T>, 3>& v,
20 std::array<Eigen::Matrix<T, 9, 3>, 3>& dfdv) const {
22
23 const OrientationDataT<T>& constr = this->constraints_[constrIndex];
24 const Matrix3<T> rotMat = constr.offset.toRotationMatrix();
25 v[0] = state.rotation() * rotMat.col(0);
26 v[1] = state.rotation() * rotMat.col(1);
27 v[2] = state.rotation() * rotMat.col(2);
28
29 Matrix3<T> vec;
30 vec.col(0) = v[0];
31 vec.col(1) = v[1];
32 vec.col(2) = v[2];
33 Matrix3<T> val = vec - constr.target.toRotationMatrix();
34 f = Eigen::Map<Vector<T, 9>>(val.data(), val.size());
35
36 for (size_t iVec = 0; iVec < 3; ++iVec) {
37 dfdv[iVec].setZero();
38 dfdv[iVec].template middleRows<3>(iVec * 3).setIdentity();
39 }
40}
41
42template <typename T>
44 const size_t constrIndex,
45 const JointStateT<T>& state,
46 Vector<T, 9>& f,
47 std::array<Vector3<T>, 3>& v,
48 std::array<Eigen::Matrix<T, 9, 3>, 3>& dfdv) const {
50
51 const OrientationDataT<T>& constr = this->constraints_[constrIndex];
52 const Matrix3<T> vec = state.rotation() * constr.offset.toRotationMatrix();
53 const Matrix3<T> invTarget = constr.target.toRotationMatrix().transpose();
54 Matrix3<T> val = invTarget * vec - Matrix3<T>::Identity();
55 f = Eigen::Map<Vector<T, 9>>(val.data(), val.size());
56
57 v[0] = vec.col(0);
58 v[1] = vec.col(1);
59 v[2] = vec.col(2);
60
61 for (size_t iVec = 0; iVec < 3; ++iVec) {
62 dfdv[iVec].setZero();
63 dfdv[iVec].template middleRows<3>(iVec * 3) = invTarget;
64 }
65}
66
69
72
73} // namespace momentum
The OrientationErrorFunction minimizes the F-norm of the element-wise difference of a 3x3 rotation ma...
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector< T, 9 > &f, std::array< Vector3< T >, 3 > &v, std::array< Eigen::Matrix< T, 9, 3 >, 3 > &dfdv) const final
The OrientationRotDiffErrorFunction minimizes the F-norm of (R_target^T * R_current - I),...
void evalFunction(size_t constrIndex, const JointStateT< T > &state, Vector< T, 9 > &f, std::array< Vector3< T >, 3 > &v, std::array< Eigen::Matrix< T, 9, 3 >, 3 > &dfdv) const final
Eigen::Matrix< T, 3, 3 > Matrix3
Definition types.h:96
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
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.
Constraint data on 3x3 rotation represented in quaternions.
Eigen::Quaternion< T > target
Target rotation in global space.
Eigen::Quaternion< T > offset
Rotation offset in the parent space.