💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.9 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_ANIMATION_BONE_HPP
  20. #define ANTKEEPER_ANIMATION_BONE_HPP
  21. #include <cstdint>
  22. /// Mask to extract the index of a bone.
  23. constexpr std::uint16_t bone_index_mask = 0xFF;
  24. /**
  25. * Skeletal animation bone identifier, consisting of a bone index in the lower half, and a parent bone index in the upper half.
  26. */
  27. typedef std::uint16_t bone;
  28. /**
  29. * Bone index comparison function object.
  30. */
  31. struct bone_index_compare
  32. {
  33. /**
  34. * Compares the indices of two bones.
  35. *
  36. * @param lhs First bone.
  37. * @param rhs Second bone.
  38. * @return Comparison result.
  39. */
  40. bool operator()(const bone& lhs, const bone& rhs) const;
  41. };
  42. /**
  43. * Constructs a bone identifier.
  44. *
  45. * @param index Index of the bone.
  46. * @param parent_index Index of the parent bone.
  47. * @return Bone identifier.
  48. */
  49. bone make_bone(std::uint8_t index, std::uint8_t parent_index);
  50. /**
  51. * Constructs an orphan bone identifier.
  52. *
  53. * @param index Index of the orphan bone.
  54. * @return Orphan bone identifier.
  55. */
  56. bone make_bone(std::uint8_t index);
  57. /**
  58. * Returns the index of a bone.
  59. *
  60. * @param x Bone identifier.
  61. * @return Index of the bone.
  62. */
  63. std::uint8_t bone_index(bone x);
  64. /**
  65. * Returns the parent index of a bone.
  66. *
  67. * @param x Bone identifier.
  68. * @return Index of the parent bone.
  69. */
  70. std::uint8_t bone_parent_index(bone x);
  71. /**
  72. * Returns `true` if a bone has a parent, `false` otherwise.
  73. *
  74. * @param x Bone identifier.
  75. * @return Bone parent status.
  76. */
  77. bool bone_has_parent(bone x);
  78. inline bool bone_index_compare::operator()(const bone& lhs, const bone& rhs) const
  79. {
  80. return (lhs & bone_index_mask) < (rhs & bone_index_mask);
  81. }
  82. inline bone make_bone(std::uint8_t index, std::uint8_t parent_index)
  83. {
  84. return (static_cast<std::uint16_t>(parent_index) << 8) | index;
  85. }
  86. inline bone make_bone(std::uint8_t index)
  87. {
  88. return make_bone(index, index);
  89. }
  90. inline std::uint8_t bone_index(bone x)
  91. {
  92. return static_cast<std::uint8_t>(x & bone_index_mask);
  93. }
  94. inline std::uint8_t bone_parent_index(bone x)
  95. {
  96. return static_cast<std::uint8_t>(x >> 8);
  97. }
  98. inline bool bone_has_parent(bone x)
  99. {
  100. return (x & bone_index_mask) != (x >> 8);
  101. }
  102. #endif // ANTKEEPER_ANIMATION_BONE_HPP