Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Skeleton/Skeleton.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Core/Reference.h>
8
#include <Jolt/Core/Result.h>
9
#include <Jolt/ObjectStream/SerializableObject.h>
10
11
JPH_NAMESPACE_BEGIN
12
13
class StreamIn;
14
class StreamOut;
15
16
/// Resource that contains the joint hierarchy for a skeleton
17
class JPH_EXPORT Skeleton : public RefTarget<Skeleton>
18
{
19
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Skeleton)
20
21
public:
22
using SkeletonResult = Result<Ref<Skeleton>>;
23
24
/// Declare internal structure for a joint
25
class Joint
26
{
27
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Joint)
28
29
public:
30
Joint() = default;
31
Joint(const string_view &inName, const string_view &inParentName, int inParentJointIndex) : mName(inName), mParentName(inParentName), mParentJointIndex(inParentJointIndex) { }
32
33
String mName; ///< Name of the joint
34
String mParentName; ///< Name of parent joint
35
int mParentJointIndex = -1; ///< Index of parent joint (in mJoints) or -1 if it has no parent
36
};
37
38
using JointVector = Array<Joint>;
39
40
///@name Access to the joints
41
///@{
42
const JointVector & GetJoints() const { return mJoints; }
43
JointVector & GetJoints() { return mJoints; }
44
int GetJointCount() const { return (int)mJoints.size(); }
45
const Joint & GetJoint(int inJoint) const { return mJoints[inJoint]; }
46
Joint & GetJoint(int inJoint) { return mJoints[inJoint]; }
47
uint AddJoint(const string_view &inName, const string_view &inParentName = string_view()) { mJoints.emplace_back(inName, inParentName, -1); return (uint)mJoints.size() - 1; }
48
uint AddJoint(const string_view &inName, int inParentIndex) { mJoints.emplace_back(inName, inParentIndex >= 0? mJoints[inParentIndex].mName : String(), inParentIndex); return (uint)mJoints.size() - 1; }
49
///@}
50
51
/// Find joint by name
52
int GetJointIndex(const string_view &inName) const;
53
54
/// Fill in parent joint indices based on name
55
void CalculateParentJointIndices();
56
57
/// Many of the algorithms that use the Skeleton class require that parent joints are in the mJoints array before their children.
58
/// This function returns true if this is the case, false if not.
59
bool AreJointsCorrectlyOrdered() const;
60
61
/// Saves the state of this object in binary form to inStream.
62
void SaveBinaryState(StreamOut &inStream) const;
63
64
/// Restore the state of this object from inStream.
65
static SkeletonResult sRestoreFromBinaryState(StreamIn &inStream);
66
67
private:
68
/// Joints
69
JointVector mJoints;
70
};
71
72
JPH_NAMESPACE_END
73
74