Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skeleton.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
12
13namespace momentum {
14
15template <typename T>
16SkeletonT<T>::SkeletonT(JointList joints_in) : joints(std::move(joints_in)) {
17 // Invariant: parents must precede children in the joint list. Many algorithms
18 // (e.g. forward kinematics, getChildrenJoints) rely on this topological order.
19 for (size_t iJoint = 0; iJoint < joints.size(); ++iJoint) {
20 MT_CHECK(joints[iJoint].parent == kInvalidIndex || joints[iJoint].parent < iJoint);
21 }
22}
23
24template <typename T>
25size_t SkeletonT<T>::getJointIdByName(const std::string_view name) const {
26 for (size_t i = 0; i < joints.size(); i++) {
27 if (joints[i].name == name) {
28 return i;
29 }
30 }
31 return kInvalidIndex;
32}
33
34template <typename T>
35std::vector<std::string> SkeletonT<T>::getJointNames() const {
36 std::vector<std::string> result;
37 result.reserve(joints.size());
38 for (const auto& j : joints) {
39 result.emplace_back(j.name);
40 }
41 return result;
42}
43
44template <typename T>
45std::vector<size_t> SkeletonT<T>::getChildrenJoints(const size_t jointId, const bool recursive)
46 const {
48 jointId >= joints.size(),
49 std::out_of_range,
50 "Out of bounds getChildrenJoints query. Requested index: {}. Number of joints: {}",
51 jointId,
52 joints.size());
53
54 std::vector<size_t> childrenJoints;
55 std::vector<int> jointDistance(joints.size(), -1);
56 jointDistance[jointId] = 0;
57
58 // Single forward pass works because the constructor guarantees parent index <
59 // child index, so every descendant of jointId has a higher index.
60 for (size_t jointIter = jointId + 1; jointIter < joints.size(); ++jointIter) {
61 const auto jointParent = joints[jointIter].parent;
62 const int distParent = (jointParent == kInvalidIndex) ? -1 : jointDistance[jointParent];
63 if ((recursive && distParent >= 0) || (!recursive && distParent == 0)) {
64 jointDistance[jointIter] = distParent + 1;
65 childrenJoints.push_back(jointIter);
66 }
67 }
68
69 return childrenJoints;
70}
71
72template <typename T>
73bool SkeletonT<T>::isAncestor(size_t jointId, size_t ancestorJointId) const {
74 MT_CHECK(jointId < joints.size(), "{} vs {}", jointId, joints.size());
75 MT_CHECK(ancestorJointId < joints.size(), "{} vs {}", ancestorJointId, joints.size());
76 while (jointId != kInvalidIndex) {
77 if (jointId == ancestorJointId) {
78 return true;
79 }
80 jointId = joints[jointId].parent;
81 }
82
83 return false;
84}
85
86template <typename T>
87bool SkeletonT<T>::isSameOrAdjacentJoints(size_t joint1, size_t joint2) const {
88 if (joint1 == kInvalidIndex || joint2 == kInvalidIndex) {
89 return false;
90 }
91 MT_CHECK(joint1 < joints.size(), "{} vs {}", joint1, joints.size());
92 MT_CHECK(joint2 < joints.size(), "{} vs {}", joint2, joints.size());
93 return joint1 == joint2 || joints[joint1].parent == joint2 || joints[joint2].parent == joint1;
94}
95
96template <typename T>
97size_t SkeletonT<T>::commonAncestor(size_t joint1, size_t joint2) const {
98 MT_CHECK(joint1 == kInvalidIndex || joint1 < joints.size());
99 MT_CHECK(joint2 == kInvalidIndex || joint2 < joints.size());
100
101 while (joint1 != joint2) {
102 // If either joint is invalid, there is no common ancestor.
103 if (joint1 == kInvalidIndex || joint2 == kInvalidIndex) {
104 return kInvalidIndex;
105 } else if (joint1 < joint2) {
106 joint2 = joints[joint2].parent;
107 } else {
108 joint1 = joints[joint1].parent;
109 }
110 }
111
112 return joint1;
113}
114
115template struct SkeletonT<float>;
116template struct SkeletonT<double>;
117
118} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_THROW_IF_T(Condition, Exception,...)
Conditionally throws an exception of a specified type with a formatted message if the condition is tr...
Definition exception.h:42
JointListT< float > JointList
Definition joint.h:81
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
The skeletal structure of a momentum Character.
Definition skeleton.h:22
size_t commonAncestor(size_t joint1, size_t joint2) const
Returns the lowest common ancestor of two joints in the hierarchy, or kInvalidIndex if either joint i...
Definition skeleton.cpp:97
size_t getJointIdByName(std::string_view name) const
Returns the index of a joint with the given name, or kInvalidIndex if not found.
Definition skeleton.cpp:25
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
Definition skeleton.h:25
std::vector< size_t > getChildrenJoints(size_t jointId, bool recursive=true) const
Returns indices of child joints of the specified joint.
Definition skeleton.cpp:45
std::vector< std::string > getJointNames() const
Definition skeleton.cpp:35
bool isAncestor(size_t jointId, size_t ancestorJointId) const
Returns true if ancestorJointId is an ancestor of jointId.
Definition skeleton.cpp:73
bool isSameOrAdjacentJoints(size_t joint1, size_t joint2) const
Returns true if the two joints are the same, or one is the direct parent of the other.
Definition skeleton.cpp:87