RavEngine
Loading...
Searching...
No Matches
skeleton_utils.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_ANIMATION_RUNTIME_SKELETON_UTILS_H_
29#define OZZ_OZZ_ANIMATION_RUNTIME_SKELETON_UTILS_H_
30
31#include <cassert>
32
33#include "ozz/animation/runtime/export.h"
34#include "ozz/animation/runtime/skeleton.h"
35#include "ozz/base/maths/transform.h"
36
37namespace ozz {
38namespace animation {
39
40// Get rest-pose of a skeleton joint.
41OZZ_ANIMATION_DLL ozz::math::Transform GetJointLocalRestPose(
42 const Skeleton& _skeleton, int _joint);
43
44// Test if a joint is a leaf. _joint number must be in range [0, num joints].
45// "_joint" is a leaf if it's the last joint, or next joint's parent isn't
46// "_joint".
47inline bool IsLeaf(const Skeleton& _skeleton, int _joint) {
48 const int num_joints = _skeleton.num_joints();
49 assert(_joint >= 0 && _joint < num_joints && "_joint index out of range");
50 const span<const int16_t>& parents = _skeleton.joint_parents();
51 const int next = _joint + 1;
52 return next == num_joints || parents[next] != _joint;
53}
54
55// Finds joint index by name. Uses a case sensitive comparison.
56OZZ_ANIMATION_DLL int FindJoint(const Skeleton& _skeleton, const char* _name);
57
58// Applies a specified functor to each joint in a depth-first order.
59// _Fct is of type void(int _current, int _parent) where the first argument
60// is the child of the second argument. _parent is kNoParent if the _current
61// joint is a root. _from indicates the joint from which the joint hierarchy
62// traversal begins. Use Skeleton::kNoParent to traverse the whole
63// hierarchy, in case there are multiple roots.
64template <typename _Fct>
65inline _Fct IterateJointsDF(const Skeleton& _skeleton, _Fct _fct,
66 int _from = Skeleton::kNoParent) {
67 const span<const int16_t>& parents = _skeleton.joint_parents();
68 const int num_joints = _skeleton.num_joints();
69 //
70 // parents[i] >= _from is true as long as "i" is a child of "_from".
71 static_assert(Skeleton::kNoParent < 0,
72 "Algorithm relies on kNoParent being negative");
73 for (int i = _from < 0 ? 0 : _from, process = i < num_joints; process;
74 ++i, process = i < num_joints && parents[i] >= _from) {
75 _fct(i, parents[i]);
76 }
77 return _fct;
78}
79
80// Applies a specified functor to each joint in a reverse (from leaves to root)
81// depth-first order. _Fct is of type void(int _current, int _parent) where the
82// first argument is the child of the second argument. _parent is kNoParent if
83// the _current joint is a root.
84template <typename _Fct>
85inline _Fct IterateJointsDFReverse(const Skeleton& _skeleton, _Fct _fct) {
86 const span<const int16_t>& parents = _skeleton.joint_parents();
87 for (int i = _skeleton.num_joints() - 1; i >= 0; --i) {
88 _fct(i, parents[i]);
89 }
90 return _fct;
91}
92} // namespace animation
93} // namespace ozz
94#endif // OZZ_OZZ_ANIMATION_RUNTIME_SKELETON_UTILS_H_
Definition transform.h:40