ArmaturePopulate.cpp 9.1 KB

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