Skeleton.h 2.6 KB

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