Skeleton.h 2.7 KB

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