Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
ceres_utility.h
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
8#pragma once
9
12
13#include <ceres/jet.h>
14#include <Eigen/Core>
15
16namespace momentum {
17
18template <typename T, typename T2>
19Eigen::Vector3<T>
20getRotationDerivative(const JointStateT<T2>& js, const size_t index, const Eigen::Vector3<T>& ref) {
21 return js.rotationAxis.col(index).cross(ref);
22}
23
24template <typename T, typename T2>
25Eigen::Vector3<T> getScaleDerivative(const JointStateT<T2>& js, const Eigen::Vector3<T>& ref) {
26 (void)js;
27 return ref * ln2<T2>();
28}
29
30template <typename T, int N>
31Eigen::Matrix<ceres::Jet<T, N>, N, 1> buildJetVec(const Eigen::Matrix<T, N, 1>& v) {
32 Eigen::Matrix<ceres::Jet<T, N>, N, 1> result;
33 for (int k = 0; k < N; ++k) {
34 result(k).a = v(k);
35 result(k).v.setZero();
36 result(k).v(k) = 1;
37 }
38
39 return result;
40}
41
42template <typename T, int Rows, int Cols>
43Eigen::Matrix<ceres::Jet<T, Cols * Rows>, Rows, Cols> buildJetMat(
44 const Eigen::Matrix<T, Rows, Cols>& m) {
45 Eigen::Matrix<ceres::Jet<T, Cols * Rows>, Rows, Cols> result;
46 for (int iRow = 0; iRow < Rows; ++iRow) {
47 for (int jCol = 0; jCol < Cols; ++jCol) {
48 result(iRow, jCol).a = m(iRow, jCol);
49 result(iRow, jCol).v.setZero();
50 result(iRow, jCol).v(iRow * Cols + jCol) = 1;
51 }
52 }
53 return result;
54}
55
56template <typename T, typename T2>
58 const T& value,
59 const size_t jointParamIdx,
60 const ParameterTransform& parameterTransform,
61 Eigen::Ref<const Eigen::VectorX<T2>> v) {
62 T result = T();
63 for (auto index = parameterTransform.transform.outerIndexPtr()[jointParamIdx];
64 index < parameterTransform.transform.outerIndexPtr()[jointParamIdx + 1];
65 ++index) {
66 result += value * static_cast<T2>(parameterTransform.transform.valuePtr()[index]) *
67 v[parameterTransform.transform.innerIndexPtr()[index]];
68 }
69
70 return result;
71}
72
73} // namespace momentum
T times_parameterTransform_times_v(const T &value, const size_t jointParamIdx, const ParameterTransform &parameterTransform, Eigen::Ref< const Eigen::VectorX< T2 > > v)
Eigen::Vector3< T > getScaleDerivative(const JointStateT< T2 > &js, const Eigen::Vector3< T > &ref)
constexpr T ln2() noexcept
Returns the natural logarithm of 2 (log_e 2).
Definition constants.h:40
Eigen::Matrix< ceres::Jet< T, N >, N, 1 > buildJetVec(const Eigen::Matrix< T, N, 1 > &v)
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Eigen::Matrix< ceres::Jet< T, Cols *Rows >, Rows, Cols > buildJetMat(const Eigen::Matrix< T, Rows, Cols > &m)
Eigen::Vector3< T > getRotationDerivative(const JointStateT< T2 > &js, const size_t index, const Eigen::Vector3< T > &ref)
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
Matrix3< T > rotationAxis
Rotation axes in global space.
Definition joint_state.h:69
SparseRowMatrix< T > transform
The sparse mapping matrix that maps model parameters to joint parameters.