ArmaturePopulate.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "ArmaturePopulate.h"
  34. #include <assimp/BaseImporter.h>
  35. #include <assimp/DefaultLogger.hpp>
  36. #include <assimp/postprocess.h>
  37. #include <assimp/scene.h>
  38. namespace Assimp {
  39. static bool IsBoneNode(const aiString &bone_name, std::vector<aiBone *> &bones) {
  40. for (aiBone *bone : bones) {
  41. if (bone->mName == bone_name) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. bool ArmaturePopulate::IsActive(unsigned int pFlags) const {
  48. return (pFlags & aiProcess_PopulateArmatureData) != 0;
  49. }
  50. void ArmaturePopulate::SetupProperties(const Importer *) {
  51. // do nothing
  52. }
  53. void ArmaturePopulate::Execute(aiScene *out) {
  54. // Now convert all bone positions to the correct mOffsetMatrix
  55. std::vector<aiBone *> bones;
  56. std::vector<aiNode *> nodes;
  57. std::map<aiBone *, aiNode *> bone_stack;
  58. BuildBoneList(out->mRootNode, out->mRootNode, out, bones);
  59. BuildNodeList(out->mRootNode, nodes);
  60. BuildBoneStack(out->mRootNode, out, bones, bone_stack, nodes);
  61. ASSIMP_LOG_DEBUG("Bone stack size: ", bone_stack.size());
  62. for (std::pair<aiBone *, aiNode *> kvp : bone_stack) {
  63. aiBone *bone = kvp.first;
  64. aiNode *bone_node = kvp.second;
  65. ASSIMP_LOG_VERBOSE_DEBUG("active node lookup: ", bone->mName.C_Str());
  66. // lcl transform grab - done in generate_nodes :)
  67. aiNode *armature = GetArmatureRoot(bone_node, bones);
  68. ai_assert(armature);
  69. #ifndef ASSIMP_BUILD_NO_ARMATUREPOPULATE_PROCESS
  70. // set up bone armature id
  71. bone->mArmature = armature;
  72. // set this bone node to be referenced properly
  73. ai_assert(bone_node);
  74. bone->mNode = bone_node;
  75. #endif
  76. }
  77. }
  78. // Reprocess all nodes to calculate bone transforms properly based on the REAL
  79. // mOffsetMatrix not the local.
  80. // Before this would use mesh transforms which is wrong for bone transforms
  81. // Before this would work for simple character skeletons but not complex meshes
  82. // with multiple origins
  83. // Source: sketch fab log cutter fbx
  84. void ArmaturePopulate::BuildBoneList(aiNode *current_node,
  85. const aiNode *root_node,
  86. const aiScene *scene,
  87. std::vector<aiBone *> &bones) {
  88. ai_assert(scene);
  89. for (unsigned int nodeId = 0; nodeId < current_node->mNumChildren; ++nodeId) {
  90. aiNode *child = current_node->mChildren[nodeId];
  91. ai_assert(child);
  92. // check for bones
  93. for (unsigned int meshId = 0; meshId < child->mNumMeshes; ++meshId) {
  94. ai_assert(child->mMeshes);
  95. unsigned int mesh_index = child->mMeshes[meshId];
  96. aiMesh *mesh = scene->mMeshes[mesh_index];
  97. ai_assert(mesh);
  98. for (unsigned int boneId = 0; boneId < mesh->mNumBones; ++boneId) {
  99. aiBone *bone = mesh->mBones[boneId];
  100. ai_assert(nullptr != bone);
  101. // duplicate mehes exist with the same bones sometimes :)
  102. // so this must be detected
  103. if (std::find(bones.begin(), bones.end(), bone) == bones.end()) {
  104. // add the element once
  105. bones.emplace_back(bone);
  106. }
  107. }
  108. // find mesh and get bones
  109. // then do recursive lookup for bones in root node hierarchy
  110. }
  111. BuildBoneList(child, root_node, scene, bones);
  112. }
  113. }
  114. // Prepare flat node list which can be used for non recursive lookups later
  115. void ArmaturePopulate::BuildNodeList(const aiNode *current_node,
  116. std::vector<aiNode *> &nodes) {
  117. ai_assert(nullptr != current_node);
  118. for (unsigned int nodeId = 0; nodeId < current_node->mNumChildren; ++nodeId) {
  119. aiNode *child = current_node->mChildren[nodeId];
  120. ai_assert(child);
  121. if (child->mNumMeshes == 0) {
  122. nodes.emplace_back(child);
  123. }
  124. BuildNodeList(child, nodes);
  125. }
  126. }
  127. // A bone stack allows us to have multiple armatures, with the same bone names
  128. // A bone stack allows us also to retrieve bones true transform even with
  129. // duplicate names :)
  130. void ArmaturePopulate::BuildBoneStack(const aiNode *root_node,
  131. const aiScene*,
  132. const std::vector<aiBone *> &bones,
  133. std::map<aiBone *, aiNode *> &bone_stack,
  134. std::vector<aiNode *> &node_stack) {
  135. if (node_stack.empty()) {
  136. return;
  137. }
  138. ai_assert(nullptr != root_node);
  139. for (aiBone *bone : bones) {
  140. ai_assert(bone);
  141. aiNode *node = GetNodeFromStack(bone->mName, node_stack);
  142. if (node == nullptr) {
  143. node_stack.clear();
  144. BuildNodeList(root_node, node_stack);
  145. ASSIMP_LOG_VERBOSE_DEBUG("Resetting bone stack: nullptr element ", bone->mName.C_Str());
  146. node = GetNodeFromStack(bone->mName, node_stack);
  147. if (nullptr == node) {
  148. ASSIMP_LOG_ERROR("serious import issue node for bone was not detected");
  149. continue;
  150. }
  151. }
  152. ASSIMP_LOG_VERBOSE_DEBUG("Successfully added bone[", bone->mName.C_Str(), "] to stack and bone node is: ", node->mName.C_Str());
  153. bone_stack.insert(std::pair<aiBone *, aiNode *>(bone, node));
  154. }
  155. }
  156. // Returns the armature root node
  157. // This is required to be detected for a bone initially, it will recurse up
  158. // until it cannot find another bone and return the node No known failure
  159. // points. (yet)
  160. aiNode *ArmaturePopulate::GetArmatureRoot(aiNode *bone_node, std::vector<aiBone *> &bone_list) {
  161. while (nullptr != bone_node) {
  162. if (!IsBoneNode(bone_node->mName, bone_list)) {
  163. ASSIMP_LOG_VERBOSE_DEBUG("GetArmatureRoot() Found valid armature: ", bone_node->mName.C_Str());
  164. return bone_node;
  165. }
  166. bone_node = bone_node->mParent;
  167. }
  168. ASSIMP_LOG_ERROR("GetArmatureRoot() can't find armature!");
  169. return nullptr;
  170. }
  171. // Pop this node by name from the stack if found
  172. // Used in multiple armature situations with duplicate node / bone names
  173. // Known flaw: cannot have nodes with bone names, will be fixed in later release
  174. // (serious to be fixed) Known flaw: nodes which have more than one bone could
  175. // be prematurely dropped from stack
  176. aiNode *ArmaturePopulate::GetNodeFromStack(const aiString &node_name,
  177. std::vector<aiNode *> &nodes) {
  178. std::vector<aiNode *>::iterator iter;
  179. aiNode *found = nullptr;
  180. for (iter = nodes.begin(); iter < nodes.end(); ++iter) {
  181. aiNode *element = *iter;
  182. ai_assert(nullptr != element);
  183. // node valid and node name matches
  184. if (element->mName == node_name) {
  185. found = element;
  186. break;
  187. }
  188. }
  189. if (found != nullptr) {
  190. ASSIMP_LOG_INFO("Removed node from stack: ", found->mName.C_Str());
  191. // now pop the element from the node list
  192. nodes.erase(iter);
  193. return found;
  194. }
  195. // unique names can cause this problem
  196. ASSIMP_LOG_ERROR("[Serious] GetNodeFromStack() can't find node from stack!");
  197. return nullptr;
  198. }
  199. } // Namespace Assimp