ArmaturePopulate.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, 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. bool ArmaturePopulate::IsActive(unsigned int pFlags) const {
  40. return (pFlags & aiProcess_PopulateArmatureData) != 0;
  41. }
  42. void ArmaturePopulate::SetupProperties(const Importer *pImp) {
  43. // do nothing
  44. }
  45. void ArmaturePopulate::Execute(aiScene *out) {
  46. // Now convert all bone positions to the correct mOffsetMatrix
  47. std::vector<aiBone *> bones;
  48. std::vector<aiNode *> nodes;
  49. std::map<aiBone *, aiNode *> bone_stack;
  50. BuildBoneList(out->mRootNode, out->mRootNode, out, bones);
  51. BuildNodeList(out->mRootNode, nodes);
  52. BuildBoneStack(out->mRootNode, out->mRootNode, out, bones, bone_stack, nodes);
  53. ASSIMP_LOG_DEBUG_F("Bone stack size: %ld\n", bone_stack.size());
  54. for (std::pair<aiBone *, aiNode *> kvp : bone_stack) {
  55. aiBone *bone = kvp.first;
  56. aiNode *bone_node = kvp.second;
  57. ASSIMP_LOG_DEBUG_F("active node lookup: %s\n", bone->mName.C_Str());
  58. // lcl transform grab - done in generate_nodes :)
  59. // bone->mOffsetMatrix = bone_node->mTransformation;
  60. aiNode *armature = GetArmatureRoot(bone_node, bones);
  61. ai_assert(armature);
  62. // set up bone armature id
  63. bone->mArmature = armature;
  64. // set this bone node to be referenced properly
  65. ai_assert(bone_node);
  66. bone->mNode = bone_node;
  67. }
  68. }
  69. /* Returns the armature root node */
  70. /* This is required to be detected for a bone initially, it will recurse up
  71. * until it cannot find another bone and return the node No known failure
  72. * points. (yet)
  73. */
  74. aiNode *ArmaturePopulate::GetArmatureRoot(aiNode *bone_node,
  75. std::vector<aiBone *> &bone_list) {
  76. while (bone_node) {
  77. if (!IsBoneNode(bone_node->mName, bone_list)) {
  78. ASSIMP_LOG_DEBUG_F("Found valid armature: %s\n", bone_node->mName.C_Str());
  79. return bone_node;
  80. }
  81. bone_node = bone_node->mParent;
  82. }
  83. ASSIMP_LOG_WARN_F("can't find armature! node: %s\n", bone_node->mName.C_Str());
  84. return NULL;
  85. }
  86. /* Simple IsBoneNode check if this could be a bone */
  87. bool ArmaturePopulate::IsBoneNode(const aiString &bone_name,
  88. std::vector<aiBone *> &bones) {
  89. for (aiBone *bone : bones) {
  90. if (bone->mName == bone_name) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /* Pop this node by name from the stack if found */
  97. /* Used in multiple armature situations with duplicate node / bone names */
  98. /* Known flaw: cannot have nodes with bone names, will be fixed in later release
  99. */
  100. /* (serious to be fixed) Known flaw: nodes which have more than one bone could
  101. * be prematurely dropped from stack */
  102. aiNode *ArmaturePopulate::GetNodeFromStack(const aiString &node_name,
  103. std::vector<aiNode *> &nodes) {
  104. std::vector<aiNode *>::iterator iter;
  105. aiNode *found = NULL;
  106. for (iter = nodes.begin(); iter < nodes.end(); ++iter) {
  107. aiNode *element = *iter;
  108. ai_assert(element);
  109. // node valid and node name matches
  110. if (element->mName == node_name) {
  111. found = element;
  112. break;
  113. }
  114. }
  115. if (found != NULL) {
  116. // now pop the element from the node list
  117. nodes.erase(iter);
  118. return found;
  119. }
  120. return NULL;
  121. }
  122. /* Prepare flat node list which can be used for non recursive lookups later */
  123. void ArmaturePopulate::BuildNodeList(const aiNode *current_node,
  124. std::vector<aiNode *> &nodes) {
  125. ai_assert(current_node);
  126. for (unsigned int nodeId = 0; nodeId < current_node->mNumChildren; ++nodeId) {
  127. aiNode *child = current_node->mChildren[nodeId];
  128. ai_assert(child);
  129. nodes.push_back(child);
  130. BuildNodeList(child, nodes);
  131. }
  132. }
  133. /* Reprocess all nodes to calculate bone transforms properly based on the REAL
  134. * mOffsetMatrix not the local. */
  135. /* Before this would use mesh transforms which is wrong for bone transforms */
  136. /* Before this would work for simple character skeletons but not complex meshes
  137. * with multiple origins */
  138. /* Source: sketch fab log cutter fbx */
  139. void ArmaturePopulate::BuildBoneList(aiNode *current_node,
  140. const aiNode *root_node,
  141. const aiScene *scene,
  142. std::vector<aiBone *> &bones) {
  143. ai_assert(scene);
  144. for (unsigned int nodeId = 0; nodeId < current_node->mNumChildren; ++nodeId) {
  145. aiNode *child = current_node->mChildren[nodeId];
  146. ai_assert(child);
  147. // check for bones
  148. for (unsigned int meshId = 0; meshId < child->mNumMeshes; ++meshId) {
  149. ai_assert(child->mMeshes);
  150. unsigned int mesh_index = child->mMeshes[meshId];
  151. aiMesh *mesh = scene->mMeshes[mesh_index];
  152. ai_assert(mesh);
  153. for (unsigned int boneId = 0; boneId < mesh->mNumBones; ++boneId) {
  154. aiBone *bone = mesh->mBones[boneId];
  155. ai_assert(bone);
  156. // duplicate meshes exist with the same bones sometimes :)
  157. // so this must be detected
  158. if (std::find(bones.begin(), bones.end(), bone) == bones.end()) {
  159. // add the element once
  160. bones.push_back(bone);
  161. }
  162. }
  163. // find mesh and get bones
  164. // then do recursive lookup for bones in root node hierarchy
  165. }
  166. BuildBoneList(child, root_node, scene, bones);
  167. }
  168. }
  169. /* A bone stack allows us to have multiple armatures, with the same bone names
  170. * A bone stack allows us also to retrieve bones true transform even with
  171. * duplicate names :)
  172. */
  173. void ArmaturePopulate::BuildBoneStack(aiNode *current_node,
  174. const aiNode *root_node,
  175. const aiScene *scene,
  176. const std::vector<aiBone *> &bones,
  177. std::map<aiBone *, aiNode *> &bone_stack,
  178. std::vector<aiNode *> &node_stack) {
  179. ai_assert(scene);
  180. ai_assert(root_node);
  181. ai_assert(!node_stack.empty());
  182. for (aiBone *bone : bones) {
  183. ai_assert(bone);
  184. aiNode *node = GetNodeFromStack(bone->mName, node_stack);
  185. if (node == NULL) {
  186. node_stack.clear();
  187. BuildNodeList(root_node, node_stack);
  188. ASSIMP_LOG_DEBUG_F("Resetting bone stack: null element %s\n", bone->mName.C_Str());
  189. node = GetNodeFromStack(bone->mName, node_stack);
  190. if (!node) {
  191. ASSIMP_LOG_ERROR("serious import issue armature failed to be detected");
  192. continue;
  193. }
  194. }
  195. ASSIMP_LOG_DEBUG_F("Successfully added bone to stack and have valid armature: %s\n", bone->mName.C_Str());
  196. bone_stack.insert(std::pair<aiBone *, aiNode *>(bone, node));
  197. }
  198. }
  199. } // Namespace Assimp