23#include <Eigen/Geometry>
26#include <unordered_map>
34 const std::array<GloveOffset, 2>& offsets,
35 const std::string& prefix) {
36 MT_THROW_IF(prefix.empty(),
"addGloveBones: prefix must not be empty.");
41 for (
size_t hand = 0; hand < 2; ++hand) {
44 wristName.empty(),
"addGloveBones: wrist joint name for hand {} must not be empty.", hand);
46 const size_t wristIdx = newSkel.getJointIdByName(wristName);
48 MT_LOGW(
"addGloveBones: wrist joint '{}' not found, skipping", wristName);
53 const std::string gloveBoneName = prefix + wristName;
54 if (newSkel.getJointIdByName(gloveBoneName) !=
kInvalidIndex) {
60 joint.
name = gloveBoneName;
66 newSkel.joints.push_back(joint);
67 newInvBindPose.push_back(Affine3f::Identity());
71 newTransform.activeJointParams.conservativeResize(
74 j <
static_cast<size_t>(newTransform.offsets.size());
76 newTransform.offsets(j) = 0.0f;
79 j <
static_cast<size_t>(newTransform.activeJointParams.size());
81 newTransform.activeJointParams(j) =
true;
86 const int curCols =
static_cast<int>(newTransform.name.size());
87 newTransform.transform.conservativeResize(newRows, curCols);
96 const std::string& prefix) {
97 MT_THROW_IF(prefix.empty(),
"addGloveCalibrationParameters: prefix must not be empty.");
102 std::vector<Eigen::Triplet<float>> triplets;
105 static const std::array<std::string, 6> dofNames{
"_tx",
"_ty",
"_tz",
"_rx",
"_ry",
"_rz"};
107 static const std::array<size_t, 6> dofIndices{
TX,
TY,
TZ,
RX,
RY,
RZ};
109 for (
size_t hand = 0; hand < 2; ++hand) {
112 "addGloveCalibrationParameters: wrist joint name for hand {} must not be empty.",
115 const size_t jointIdx = newSkel.getJointIdByName(gloveBoneName);
117 MT_LOGW(
"addGloveCalibrationParameters: glove bone '{}' not found, skipping", gloveBoneName);
122 for (
size_t d = 0; d < 6; ++d) {
123 const std::string paramName = gloveBoneName + dofNames[d];
125 const auto col =
static_cast<int>(newTransform.name.size());
127 triplets.emplace_back(row, col, 1.0f);
131 "Number of model parameters reached the {} limit.",
133 gloveSet.set(newTransform.name.size());
134 newTransform.name.push_back(paramName);
138 newTransform.parameterSets[
"gloves"] = gloveSet;
142 const int newCols =
static_cast<int>(newTransform.name.size());
143 newTransform.transform.conservativeResize(newRows, newCols);
145 additionalTransforms.setFromTriplets(triplets.begin(), triplets.end());
146 newTransform.transform += additionalTransforms;
160 std::array<GloveOffset, 2> result;
165 for (
size_t hand = 0; hand < 2; ++hand) {
166 const std::string gloveBoneName =
"glove_" + cfg.
wristJointNames[hand];
173 result[hand].translation =
174 Vector3f(jointParams.
v(base +
TX), jointParams.
v(base +
TY), jointParams.
v(base +
TZ));
175 result[hand].rotationEulerXYZ =
176 Vector3f(jointParams.
v(base +
RX), jointParams.
v(base +
RY), jointParams.
v(base +
RZ));
186 const std::optional<GloveConfig>& cfg) {
192 params.
pose = solvedParams;
197 for (
size_t hand = 0; hand < 2; ++hand) {
199 "Baking glove offset for '{}': translation=({:.4f}, {:.4f}, {:.4f}), "
200 "rotation_xyz=({:.4f}, {:.4f}, {:.4f})",
201 cfg->wristJointNames[hand],
202 offsets[hand].translation.x(),
203 offsets[hand].translation.y(),
204 offsets[hand].translation.z(),
205 offsets[hand].rotationEulerXYZ.x(),
206 offsets[hand].rotationEulerXYZ.y(),
207 offsets[hand].rotationEulerXYZ.z());
215 std::span<const GloveFrameData> gloveData,
219 if (gloveData.empty() || handIndex >= 2) {
223 std::vector<std::vector<JointToJointPositionDataT<float>>> results(gloveData.size());
226 const std::string gloveBoneName =
"glove_" + cfg.
wristJointNames[handIndex];
236 std::unordered_map<std::string, size_t> jointLookup;
242 for (
size_t iFrame = 0; iFrame < results.size(); ++iFrame) {
243 results[iFrame].reserve(gloveData[iFrame].size());
244 for (
const auto& obs : gloveData[iFrame]) {
249 auto it = jointLookup.find(obs.jointName);
250 if (it == jointLookup.end()) {
259 constraint.
target = obs.position;
261 constraint.
name = obs.jointName;
262 results[iFrame].push_back(std::move(constraint));
270 std::span<const GloveFrameData> gloveData,
274 if (gloveData.empty() || handIndex >= 2) {
278 std::vector<std::vector<JointToJointOrientationDataT<float>>> results(gloveData.size());
281 const std::string gloveBoneName =
"glove_" + cfg.
wristJointNames[handIndex];
291 std::unordered_map<std::string, size_t> jointLookup;
297 for (
size_t iFrame = 0; iFrame < results.size(); ++iFrame) {
298 results[iFrame].reserve(gloveData[iFrame].size());
299 for (
const auto& obs : gloveData[iFrame]) {
304 auto it = jointLookup.find(obs.jointName);
305 if (it == jointLookup.end()) {
312 constraint.
target = obs.orientation;
314 constraint.
name = obs.jointName;
315 results[iFrame].push_back(std::move(constraint));
332 if (iFrame < posData.size() && !posData[iFrame].empty()) {
333 auto posFunc = std::make_shared<JointToJointPositionErrorFunctionT<float>>(character);
334 for (
const auto& c : posData[iFrame]) {
335 posFunc->addConstraint(c);
342 if (iFrame < oriData.size() && !oriData[iFrame].empty()) {
343 auto oriFunc = std::make_shared<JointToJointOrientationErrorFunctionT<float>>(character);
344 for (
const auto& c : oriData[iFrame]) {
345 oriFunc->addConstraint(c);
355 std::span<const GloveFrameData> gloveData,
358 if (gloveData.empty()) {
363 if (handIndex >= 2) {
366 const std::string gloveBoneName =
"glove_" + cfg.
wristJointNames[handIndex];
382 funcs.
posFunc = std::make_shared<JointToJointPositionErrorFunctionT<float>>(character);
386 funcs.
oriFunc = std::make_shared<JointToJointOrientationErrorFunctionT<float>>(character);
395 funcs.
posFunc->clearConstraints();
396 if (iFrame < funcs.
posData.size()) {
397 for (
const auto& c : funcs.
posData[iFrame]) {
398 funcs.
posFunc->addConstraint(c);
403 funcs.
oriFunc->clearConstraints();
404 if (iFrame < funcs.
oriData.size()) {
405 for (
const auto& c : funcs.
oriData[iFrame]) {
406 funcs.
oriFunc->addConstraint(c);
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
static constexpr float kLegacyWeight
static constexpr float kLegacyWeight
void addErrorFunction(size_t frame, std::shared_ptr< SkeletonErrorFunctionT< T > > errorFunction)
void addErrorFunction(std::shared_ptr< SkeletonErrorFunctionT< T > > solvable)
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
void bakeGloveOffsetsFromParams(Character &character, const ModelParameters &solvedParams, const Character &solvingCharacter, const std::optional< GloveConfig > &cfg)
Bake solved glove offsets from calibration parameters into the skeleton.
Character addGloveBones(Character character, const GloveConfig &cfg, const std::array< GloveOffset, 2 > &offsets, const std::string &prefix)
Add glove joints to the skeleton with given offsets but no model parameters.
constexpr size_t kMaxModelParams
Maximum number of model parameters supported per frame across any character.
void addGloveConstraintsToSequenceSolver(SequenceSolverFunctionT< float > &solverFunc, size_t solverFrame, const Character &character, const std::vector< std::vector< JointToJointPositionDataT< float > > > &posData, const std::vector< std::vector< JointToJointOrientationDataT< float > > > &oriData, size_t iFrame, float posWeight, float oriWeight)
Add glove error functions to a SequenceSolverFunction for one frame.
@ Intrinsic
The intrinsic convention.
std::vector< std::vector< JointToJointOrientationDataT< float > > > createGloveOrientationConstraintData(std::span< const GloveFrameData > gloveData, const Character &character, const GloveConfig &cfg, size_t handIndex)
Convert per-frame glove observations to JointToJoint orientation constraints.
JointParametersT< float > JointParameters
Character createGloveCharacter(const Character &src, const GloveConfig &cfg, const std::string &prefix)
Add glove bones with 6-DOF model parameters (convenience wrapper).
CharacterParametersT< float > CharacterParameters
constexpr size_t kParametersPerJoint
std::array< GloveOffset, 2 > extractGloveOffsetsFromCharacter(const Character &gloveChar, const CharacterParameters ¶ms, const GloveConfig &cfg)
Extract calibrated glove offsets from solved parameters.
Quaternion< T > eulerToQuaternion(const Vector3< T > &angles, int axis0, int axis1, int axis2, EulerConvention convention)
Converts Euler angles to quaternion.
ModelParametersT< float > ModelParameters
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
void updateGloveConstraintsForFrame(GloveErrorFunctions &funcs, size_t iFrame)
Swap per-frame glove constraint data on already-registered error functions.
Vector3< float > Vector3f
std::optional< GloveErrorFunctions > setupGloveErrorFunctions(SkeletonSolverFunctionT< float > &solverFunc, const Character &character, std::span< const GloveFrameData > gloveData, const GloveConfig &cfg, size_t handIndex)
Create and register glove error functions for the per-frame solver.
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
SparseRowMatrix< float > SparseRowMatrixf
std::vector< std::vector< JointToJointPositionDataT< float > > > createGlovePositionConstraintData(std::span< const GloveFrameData > gloveData, const Character &character, const GloveConfig &cfg, size_t handIndex)
Convert per-frame glove observations to JointToJoint position constraints.
Character addGloveCalibrationParameters(Character character, const GloveConfig &cfg, const std::string &prefix)
Add 6-DOF model parameters for existing glove bones.
JointParametersT< T > offsets
The joint parameter vector representing the unique bone lengths of the character, defining the charac...
ModelParametersT< T > pose
The model parameter vector representing the pose of the character.
A character model with skeletal structure, mesh, and optional components.
TransformationList inverseBindPose
Inverse of the bind pose transformations for each joint.
Skeleton skeleton
Skeletal structure defining the character's joints and hierarchy.
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
static EIGEN_DEVICE_FUNC Derived< Scalar > Zero(::Eigen::Index size)
Configuration for glove constraints.
std::array< std::string, 2 > wristJointNames
Names of the left and right wrist joints in the skeleton.
float positionWeight
Weight for position constraints between glove and finger joints.
float orientationWeight
Weight for orientation constraints between glove and finger joints.
Holds per-frame glove error functions for the single-frame solver.
std::shared_ptr< JointToJointPositionErrorFunctionT< float > > posFunc
Position error function registered with the solver.
std::vector< std::vector< JointToJointOrientationDataT< float > > > oriData
Pre-built per-frame orientation constraint data.
std::shared_ptr< JointToJointOrientationErrorFunctionT< float > > oriFunc
Orientation error function registered with the solver.
std::vector< std::vector< JointToJointPositionDataT< float > > > posData
Pre-built per-frame position constraint data.
size_t parent
Index of the parent joint in the skeleton hierarchy, or kInvalidIndex for the root.
Quaternion< T > preRotation
Pre-rotation from the parent joint's axes to this joint's local axes.
Vector3< T > translationOffset
Constant translation offset from the parent joint's origin to this joint's origin,...
Constraint data for joint-to-joint orientation error between two joints.
T weight
Weight for this constraint.
std::string name
Name of the constraint (for debugging).
Eigen::Quaternion< T > target
Target relative orientation as a quaternion (R_ref^T * R_src should match this).
size_t sourceJoint
Index of the source joint (the joint whose orientation we want to constrain).
size_t referenceJoint
Index of the reference joint (the joint whose coordinate frame we use).
Constraint data for joint-to-joint position error between two joints.
size_t referenceJoint
Index of the reference joint (the joint whose coordinate frame we use).
Vector3< T > referenceOffset
Offset from referenceJoint in the local coordinate system of referenceJoint.
size_t sourceJoint
Index of the source joint (the joint whose position we want to constrain).
std::string name
Name of the constraint (for debugging).
Vector3< T > target
Target position in the reference joint's coordinate frame.
T weight
Weight for this constraint.
Vector3< T > sourceOffset
Offset from sourceJoint in the local coordinate system of sourceJoint.
size_t getJointIdByName(std::string_view name) const
Returns the index of a joint with the given name, or kInvalidIndex if not found.
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...