Skeleton.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Skeleton/Skeleton.h>
  6. #include <Jolt/ObjectStream/TypeDeclarations.h>
  7. #include <Jolt/Core/StreamIn.h>
  8. #include <Jolt/Core/StreamOut.h>
  9. JPH_NAMESPACE_BEGIN
  10. JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(Skeleton::Joint)
  11. {
  12. JPH_ADD_ATTRIBUTE(Joint, mName)
  13. JPH_ADD_ATTRIBUTE(Joint, mParentName)
  14. }
  15. JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(Skeleton)
  16. {
  17. JPH_ADD_ATTRIBUTE(Skeleton, mJoints)
  18. }
  19. int Skeleton::GetJointIndex(const string_view &inName) const
  20. {
  21. for (int i = 0; i < (int)mJoints.size(); ++i)
  22. if (mJoints[i].mName == inName)
  23. return i;
  24. return -1;
  25. }
  26. void Skeleton::CalculateParentJointIndices()
  27. {
  28. for (Joint &j : mJoints)
  29. j.mParentJointIndex = GetJointIndex(j.mParentName);
  30. }
  31. bool Skeleton::AreJointsCorrectlyOrdered() const
  32. {
  33. for (int i = 0; i < (int)mJoints.size(); ++i)
  34. if (mJoints[i].mParentJointIndex >= i)
  35. return false;
  36. return true;
  37. }
  38. void Skeleton::SaveBinaryState(StreamOut &inStream) const
  39. {
  40. inStream.Write((uint32)mJoints.size());
  41. for (const Joint &j : mJoints)
  42. {
  43. inStream.Write(j.mName);
  44. inStream.Write(j.mParentJointIndex);
  45. inStream.Write(j.mParentName);
  46. }
  47. }
  48. Skeleton::SkeletonResult Skeleton::sRestoreFromBinaryState(StreamIn &inStream)
  49. {
  50. Ref<Skeleton> skeleton = new Skeleton;
  51. uint32 len = 0;
  52. inStream.Read(len);
  53. skeleton->mJoints.resize(len);
  54. for (Joint &j : skeleton->mJoints)
  55. {
  56. inStream.Read(j.mName);
  57. inStream.Read(j.mParentJointIndex);
  58. inStream.Read(j.mParentName);
  59. }
  60. SkeletonResult result;
  61. if (inStream.IsEOF() || inStream.IsFailed())
  62. result.SetError("Failed to read skeleton from stream");
  63. else
  64. result.Set(skeleton);
  65. return result;
  66. }
  67. JPH_NAMESPACE_END