OptimizeGraph.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file OptimizeGraph.cpp
  35. * @brief Implementation of the aiProcess_OptimizGraph step
  36. */
  37. #ifndef ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS
  38. #include "OptimizeGraph.h"
  39. #include "ProcessHelper.h"
  40. #include "SceneCombiner.h"
  41. #include "Exceptional.h"
  42. #include <stdio.h>
  43. using namespace Assimp;
  44. #define AI_RESERVED_NODE_NAME "$Reserved_And_Evil"
  45. /* AI_OG_USE_HASHING enables the use of hashing to speed-up std::set lookups.
  46. * The unhashed variant should be faster, except for *very* large data sets
  47. */
  48. #ifdef AI_OG_USE_HASHING
  49. // Use our standard hashing function to compute the hash
  50. # define AI_OG_GETKEY(str) SuperFastHash(str.data,str.length)
  51. #else
  52. // Otherwise hope that std::string will utilize a static buffer
  53. // for shorter node names. This would avoid endless heap copying.
  54. # define AI_OG_GETKEY(str) std::string(str.data)
  55. #endif
  56. // ------------------------------------------------------------------------------------------------
  57. // Constructor to be privately used by Importer
  58. OptimizeGraphProcess::OptimizeGraphProcess()
  59. : mScene()
  60. , nodes_in()
  61. , nodes_out()
  62. , count_merged()
  63. {}
  64. // ------------------------------------------------------------------------------------------------
  65. // Destructor, private as well
  66. OptimizeGraphProcess::~OptimizeGraphProcess()
  67. {}
  68. // ------------------------------------------------------------------------------------------------
  69. // Returns whether the processing step is present in the given flag field.
  70. bool OptimizeGraphProcess::IsActive( unsigned int pFlags) const
  71. {
  72. return (0 != (pFlags & aiProcess_OptimizeGraph));
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Setup properties for the postprocessing step
  76. void OptimizeGraphProcess::SetupProperties(const Importer* pImp)
  77. {
  78. // Get value of AI_CONFIG_PP_OG_EXCLUDE_LIST
  79. std::string tmp = pImp->GetPropertyString(AI_CONFIG_PP_OG_EXCLUDE_LIST,"");
  80. AddLockedNodeList(tmp);
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Collect new children
  84. void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& nodes)
  85. {
  86. nodes_in += nd->mNumChildren;
  87. // Process children
  88. std::list<aiNode*> child_nodes;
  89. for (unsigned int i = 0; i < nd->mNumChildren; ++i) {
  90. CollectNewChildren(nd->mChildren[i],child_nodes);
  91. nd->mChildren[i] = NULL;
  92. }
  93. // Check whether we need this node; if not we can replace it by our own children (warn, danger of incest).
  94. if (locked.find(AI_OG_GETKEY(nd->mName)) == locked.end() ) {
  95. for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end();) {
  96. if (locked.find(AI_OG_GETKEY((*it)->mName)) == locked.end()) {
  97. (*it)->mTransformation = nd->mTransformation * (*it)->mTransformation;
  98. nodes.push_back(*it);
  99. it = child_nodes.erase(it);
  100. continue;
  101. }
  102. ++it;
  103. }
  104. if (nd->mNumMeshes || !child_nodes.empty()) {
  105. nodes.push_back(nd);
  106. }
  107. else {
  108. delete nd; /* bye, node */
  109. return;
  110. }
  111. }
  112. else {
  113. // Retain our current position in the hierarchy
  114. nodes.push_back(nd);
  115. // Now check for possible optimizations in our list of child nodes. join as many as possible
  116. aiNode* join_master = NULL;
  117. aiMatrix4x4 inv;
  118. const LockedSetType::const_iterator end = locked.end();
  119. std::list<aiNode*> join;
  120. for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end();) {
  121. aiNode* child = *it;
  122. if (child->mNumChildren == 0 && locked.find(AI_OG_GETKEY(child->mName)) == end) {
  123. // There may be no instanced meshes
  124. unsigned int n = 0;
  125. for (; n < child->mNumMeshes;++n) {
  126. if (meshes[child->mMeshes[n]] > 1) {
  127. break;
  128. }
  129. }
  130. if (n == child->mNumMeshes) {
  131. if (!join_master) {
  132. join_master = child;
  133. inv = join_master->mTransformation;
  134. inv.Inverse();
  135. }
  136. else {
  137. child->mTransformation = inv * child->mTransformation ;
  138. join.push_back(child);
  139. it = child_nodes.erase(it);
  140. continue;
  141. }
  142. }
  143. }
  144. ++it;
  145. }
  146. if (join_master && !join.empty()) {
  147. join_master->mName.length = ::ai_snprintf(join_master->mName.data, MAXLEN, "$MergedNode_%i",count_merged++);
  148. unsigned int out_meshes = 0;
  149. for (std::list<aiNode*>::iterator it = join.begin(); it != join.end(); ++it) {
  150. out_meshes += (*it)->mNumMeshes;
  151. }
  152. // copy all mesh references in one array
  153. if (out_meshes) {
  154. unsigned int* meshes = new unsigned int[out_meshes+join_master->mNumMeshes], *tmp = meshes;
  155. for (unsigned int n = 0; n < join_master->mNumMeshes;++n) {
  156. *tmp++ = join_master->mMeshes[n];
  157. }
  158. for (std::list<aiNode*>::iterator it = join.begin(); it != join.end(); ++it) {
  159. for (unsigned int n = 0; n < (*it)->mNumMeshes; ++n) {
  160. *tmp = (*it)->mMeshes[n];
  161. aiMesh* mesh = mScene->mMeshes[*tmp++];
  162. // manually move the mesh into the right coordinate system
  163. const aiMatrix3x3 IT = aiMatrix3x3( (*it)->mTransformation ).Inverse().Transpose();
  164. for (unsigned int a = 0; a < mesh->mNumVertices; ++a) {
  165. mesh->mVertices[a] *= (*it)->mTransformation;
  166. if (mesh->HasNormals())
  167. mesh->mNormals[a] *= IT;
  168. if (mesh->HasTangentsAndBitangents()) {
  169. mesh->mTangents[a] *= IT;
  170. mesh->mBitangents[a] *= IT;
  171. }
  172. }
  173. }
  174. delete *it; // bye, node
  175. }
  176. delete[] join_master->mMeshes;
  177. join_master->mMeshes = meshes;
  178. join_master->mNumMeshes += out_meshes;
  179. }
  180. }
  181. }
  182. // reassign children if something changed
  183. if (child_nodes.empty() || child_nodes.size() > nd->mNumChildren) {
  184. delete[] nd->mChildren;
  185. if (!child_nodes.empty())
  186. nd->mChildren = new aiNode*[child_nodes.size()];
  187. else nd->mChildren = NULL;
  188. }
  189. nd->mNumChildren = static_cast<unsigned int>(child_nodes.size());
  190. aiNode** tmp = nd->mChildren;
  191. for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end(); ++it) {
  192. aiNode* node = *tmp++ = *it;
  193. node->mParent = nd;
  194. }
  195. nodes_out += static_cast<unsigned int>(child_nodes.size());
  196. }
  197. // ------------------------------------------------------------------------------------------------
  198. // Execute the postprocessing step on the given scene
  199. void OptimizeGraphProcess::Execute( aiScene* pScene)
  200. {
  201. DefaultLogger::get()->debug("OptimizeGraphProcess begin");
  202. nodes_in = nodes_out = count_merged = 0;
  203. mScene = pScene;
  204. meshes.resize(pScene->mNumMeshes,0);
  205. FindInstancedMeshes(pScene->mRootNode);
  206. // build a blacklist of identifiers. If the name of a node matches one of these, we won't touch it
  207. locked.clear();
  208. for (std::list<std::string>::const_iterator it = locked_nodes.begin(); it != locked_nodes.end(); ++it) {
  209. #ifdef AI_OG_USE_HASHING
  210. locked.insert(SuperFastHash((*it).c_str()));
  211. #else
  212. locked.insert(*it);
  213. #endif
  214. }
  215. for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {
  216. for (unsigned int a = 0; a < pScene->mAnimations[i]->mNumChannels; ++a) {
  217. aiNodeAnim* anim = pScene->mAnimations[i]->mChannels[a];
  218. locked.insert(AI_OG_GETKEY(anim->mNodeName));
  219. }
  220. }
  221. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  222. for (unsigned int a = 0; a < pScene->mMeshes[i]->mNumBones; ++a) {
  223. aiBone* bone = pScene->mMeshes[i]->mBones[a];
  224. locked.insert(AI_OG_GETKEY(bone->mName));
  225. // HACK: Meshes referencing bones may not be transformed; we need to look them.
  226. // The easiest way to do this is to increase their reference counters ...
  227. meshes[i] += 2;
  228. }
  229. }
  230. for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {
  231. aiCamera* cam = pScene->mCameras[i];
  232. locked.insert(AI_OG_GETKEY(cam->mName));
  233. }
  234. for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
  235. aiLight* lgh = pScene->mLights[i];
  236. locked.insert(AI_OG_GETKEY(lgh->mName));
  237. }
  238. // Insert a dummy master node and make it read-only
  239. aiNode* dummy_root = new aiNode(AI_RESERVED_NODE_NAME);
  240. locked.insert(AI_OG_GETKEY(dummy_root->mName));
  241. const aiString prev = pScene->mRootNode->mName;
  242. pScene->mRootNode->mParent = dummy_root;
  243. dummy_root->mChildren = new aiNode*[dummy_root->mNumChildren = 1];
  244. dummy_root->mChildren[0] = pScene->mRootNode;
  245. // Do our recursive processing of scenegraph nodes. For each node collect
  246. // a fully new list of children and allow their children to place themselves
  247. // on the same hierarchy layer as their parents.
  248. std::list<aiNode*> nodes;
  249. CollectNewChildren (dummy_root,nodes);
  250. ai_assert(nodes.size() == 1);
  251. if (dummy_root->mNumChildren == 0) {
  252. pScene->mRootNode = NULL;
  253. throw DeadlyImportError("After optimizing the scene graph, no data remains");
  254. }
  255. if (dummy_root->mNumChildren > 1) {
  256. pScene->mRootNode = dummy_root;
  257. // Keep the dummy node but assign the name of the old root node to it
  258. pScene->mRootNode->mName = prev;
  259. }
  260. else {
  261. // Remove the dummy root node again.
  262. pScene->mRootNode = dummy_root->mChildren[0];
  263. dummy_root->mChildren[0] = NULL;
  264. delete dummy_root;
  265. }
  266. pScene->mRootNode->mParent = NULL;
  267. if (!DefaultLogger::isNullLogger()) {
  268. if ( nodes_in != nodes_out) {
  269. char buf[512];
  270. ::ai_snprintf(buf,512,"OptimizeGraphProcess finished; Input nodes: %u, Output nodes: %u",nodes_in,nodes_out);
  271. DefaultLogger::get()->info(buf);
  272. }
  273. else DefaultLogger::get()->debug("OptimizeGraphProcess finished");
  274. }
  275. meshes.clear();
  276. locked.clear();
  277. }
  278. // ------------------------------------------------------------------------------------------------
  279. // Buidl a LUT of all instanced meshes
  280. void OptimizeGraphProcess::FindInstancedMeshes (aiNode* pNode)
  281. {
  282. for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
  283. ++meshes[pNode->mMeshes[i]];
  284. }
  285. for (unsigned int i = 0; i < pNode->mNumChildren; ++i)
  286. FindInstancedMeshes(pNode->mChildren[i]);
  287. }
  288. #endif // !! ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS