Path: blob/master/thirdparty/jolt_physics/Jolt/Skeleton/SkeletonMapper.cpp
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Skeleton/SkeletonMapper.h>78JPH_NAMESPACE_BEGIN910void SkeletonMapper::Initialize(const Skeleton *inSkeleton1, const Mat44 *inNeutralPose1, const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2, const CanMapJoint &inCanMapJoint)11{12JPH_ASSERT(mMappings.empty() && mChains.empty() && mUnmapped.empty()); // Should not be initialized yet1314// Count joints15int n1 = inSkeleton1->GetJointCount();16int n2 = inSkeleton2->GetJointCount();17JPH_ASSERT(n1 <= n2, "Skeleton 1 should be the low detail skeleton!");1819// Keep track of mapped joints (initialize to false)20Array<bool> mapped1(n1, false);21Array<bool> mapped2(n2, false);2223// Find joints that can be mapped directly24for (int j1 = 0; j1 < n1; ++j1)25for (int j2 = 0; j2 < n2; ++j2)26if (inCanMapJoint(inSkeleton1, j1, inSkeleton2, j2))27{28// Calculate the transform that takes this joint from skeleton 1 to 229Mat44 joint_1_to_2 = inNeutralPose1[j1].Inversed() * inNeutralPose2[j2];3031// Ensure bottom right element is 1 (numerical imprecision in the inverse can make this not so)32joint_1_to_2(3, 3) = 1.0f;3334mMappings.emplace_back(j1, j2, joint_1_to_2);35mapped1[j1] = true;36mapped2[j2] = true;37break;38}3940Array<int> cur_chain; // Taken out of the loop to minimize amount of allocations4142// Find joint chains43for (int m1 = 0; m1 < (int)mMappings.size(); ++m1)44{45Array<int> chain2;46int chain2_m = -1;4748for (int m2 = m1 + 1; m2 < (int)mMappings.size(); ++m2)49{50// Find the chain from back from m2 to m151int start = mMappings[m1].mJointIdx2;52int end = mMappings[m2].mJointIdx2;53int cur = end;54cur_chain.clear(); // Should preserve memory55do56{57cur_chain.push_back(cur);58cur = inSkeleton2->GetJoint(cur).mParentJointIndex;59}60while (cur >= 0 && cur != start && !mapped2[cur]);61cur_chain.push_back(start);6263if (cur == start // This should be the correct chain64&& cur_chain.size() > 2 // It should have joints between the mapped joints65&& cur_chain.size() > chain2.size()) // And it should be the longest so far66{67chain2.swap(cur_chain);68chain2_m = m2;69}70}7172if (!chain2.empty())73{74// Get the chain for 175Array<int> chain1;76int start = mMappings[m1].mJointIdx1;77int cur = mMappings[chain2_m].mJointIdx1;78do79{80chain1.push_back(cur);81cur = inSkeleton1->GetJoint(cur).mParentJointIndex;82}83while (cur >= 0 && cur != start && !mapped1[cur]);84chain1.push_back(start);8586// If the chain exists in 1 too87if (cur == start)88{89// Reverse the chains90std::reverse(chain1.begin(), chain1.end());91std::reverse(chain2.begin(), chain2.end());9293// Mark elements mapped94for (int j1 : chain1)95mapped1[j1] = true;96for (int j2 : chain2)97mapped2[j2] = true;9899// Insert the chain100mChains.emplace_back(std::move(chain1), std::move(chain2));101}102}103}104105// Collect unmapped joints from 2106for (int j2 = 0; j2 < n2; ++j2)107if (!mapped2[j2])108mUnmapped.emplace_back(j2, inSkeleton2->GetJoint(j2).mParentJointIndex);109}110111void SkeletonMapper::LockTranslations(const Skeleton *inSkeleton2, const bool *inLockedTranslations, const Mat44 *inNeutralPose2)112{113JPH_ASSERT(inSkeleton2->AreJointsCorrectlyOrdered());114115int n = inSkeleton2->GetJointCount();116117// Copy locked joints to array but don't actually include the first joint (this is physics driven)118for (int i = 0; i < n; ++i)119if (inLockedTranslations[i])120{121Locked l;122l.mJointIdx = i;123l.mParentJointIdx = inSkeleton2->GetJoint(i).mParentJointIndex;124if (l.mParentJointIdx >= 0)125l.mTranslation = inNeutralPose2[l.mParentJointIdx].Inversed() * inNeutralPose2[i].GetTranslation();126else127l.mTranslation = inNeutralPose2[i].GetTranslation();128mLockedTranslations.push_back(l);129}130}131132void SkeletonMapper::LockAllTranslations(const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2)133{134JPH_ASSERT(!mMappings.empty(), "Call Initialize first!");135JPH_ASSERT(inSkeleton2->AreJointsCorrectlyOrdered());136137// The first mapping is the top most one (remember that joints should be ordered so that parents go before children).138// Because we created the mappings from the lowest joint first, this should contain the first mappable joint.139int root_idx = mMappings[0].mJointIdx2;140141// Create temp array to hold locked joints142int n = inSkeleton2->GetJointCount();143bool *locked_translations = (bool *)JPH_STACK_ALLOC(n * sizeof(bool));144memset(locked_translations, 0, n * sizeof(bool));145146// Mark root as locked147locked_translations[root_idx] = true;148149// Loop over all joints and propagate the locked flag to all children150for (int i = root_idx + 1; i < n; ++i)151{152int parent_idx = inSkeleton2->GetJoint(i).mParentJointIndex;153if (parent_idx >= 0)154locked_translations[i] = locked_translations[parent_idx];155}156157// Unmark root because we don't actually want to include this (this determines the position of the entire ragdoll)158locked_translations[root_idx] = false;159160// Call the generic function161LockTranslations(inSkeleton2, locked_translations, inNeutralPose2);162}163164void SkeletonMapper::Map(const Mat44 *inPose1ModelSpace, const Mat44 *inPose2LocalSpace, Mat44 *outPose2ModelSpace) const165{166// Apply direct mappings167for (const Mapping &m : mMappings)168outPose2ModelSpace[m.mJointIdx2] = inPose1ModelSpace[m.mJointIdx1] * m.mJoint1To2;169170// Apply chain mappings171for (const Chain &c : mChains)172{173// Calculate end of chain given local space transforms of the joints of the chain174Mat44 &chain_start = outPose2ModelSpace[c.mJointIndices2.front()];175Mat44 chain_end = chain_start;176for (int j = 1; j < (int)c.mJointIndices2.size(); ++j)177chain_end = chain_end * inPose2LocalSpace[c.mJointIndices2[j]];178179// Calculate the direction in world space for skeleton 1 and skeleton 2 and the rotation between them180Vec3 actual = chain_end.GetTranslation() - chain_start.GetTranslation();181Vec3 desired = inPose1ModelSpace[c.mJointIndices1.back()].GetTranslation() - inPose1ModelSpace[c.mJointIndices1.front()].GetTranslation();182Quat rotation = Quat::sFromTo(actual, desired);183184// Rotate the start of the chain185chain_start.SetRotation(Mat44::sRotation(rotation) * chain_start.GetRotation());186187// Update all joints but the first and the last joint using their local space transforms188for (int j = 1; j < (int)c.mJointIndices2.size() - 1; ++j)189{190int parent = c.mJointIndices2[j - 1];191int child = c.mJointIndices2[j];192outPose2ModelSpace[child] = outPose2ModelSpace[parent] * inPose2LocalSpace[child];193}194}195196// All unmapped joints take the local pose and convert it to model space197for (const Unmapped &u : mUnmapped)198if (u.mParentJointIdx >= 0)199{200JPH_ASSERT(u.mParentJointIdx < u.mJointIdx, "Joints must be ordered: parents first");201outPose2ModelSpace[u.mJointIdx] = outPose2ModelSpace[u.mParentJointIdx] * inPose2LocalSpace[u.mJointIdx];202}203else204outPose2ModelSpace[u.mJointIdx] = inPose2LocalSpace[u.mJointIdx];205206// Update all locked joint translations207for (const Locked &l : mLockedTranslations)208outPose2ModelSpace[l.mJointIdx].SetTranslation(outPose2ModelSpace[l.mParentJointIdx] * l.mTranslation);209}210211void SkeletonMapper::MapReverse(const Mat44 *inPose2ModelSpace, Mat44 *outPose1ModelSpace) const212{213// Normally each joint in skeleton 1 should be present in the mapping, so we only need to apply the direct mappings214for (const Mapping &m : mMappings)215outPose1ModelSpace[m.mJointIdx1] = inPose2ModelSpace[m.mJointIdx2] * m.mJoint2To1;216}217218int SkeletonMapper::GetMappedJointIdx(int inJoint1Idx) const219{220for (const Mapping &m : mMappings)221if (m.mJointIdx1 == inJoint1Idx)222return m.mJointIdx2;223224return -1;225}226227bool SkeletonMapper::IsJointTranslationLocked(int inJoint2Idx) const228{229for (const Locked &l : mLockedTranslations)230if (l.mJointIdx == inJoint2Idx)231return true;232233return false;234}235236JPH_NAMESPACE_END237238239