Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS
  39. using namespace Assimp;
  40. #include "OptimizeGraph.h"
  41. #include "ProcessHelper.h"
  42. #include "SceneCombiner.h"
  43. #define AI_RESERVED_NODE_NAME "$Reserved_And_Evil"
  44. /* AI_OG_USE_HASHING enables the use of hashing to speed-up std::set lookups.
  45. * The unhashed variant should be faster, except for *very* large data sets
  46. */
  47. #ifdef AI_OG_USE_HASHING
  48. // Use our standard hashing function to compute the hash
  49. # define AI_OG_GETKEY(str) SuperFastHash(str.data,str.length)
  50. #else
  51. // Otherwise hope that std::string will utilize a static buffer
  52. // for shorter node names. This would avoid endless heap copying.
  53. # define AI_OG_GETKEY(str) std::string(str.data)
  54. #endif
  55. // ------------------------------------------------------------------------------------------------
  56. // Constructor to be privately used by Importer
  57. OptimizeGraphProcess::OptimizeGraphProcess()
  58. {}
  59. // ------------------------------------------------------------------------------------------------
  60. // Destructor, private as well
  61. OptimizeGraphProcess::~OptimizeGraphProcess()
  62. {}
  63. // ------------------------------------------------------------------------------------------------
  64. // Returns whether the processing step is present in the given flag field.
  65. bool OptimizeGraphProcess::IsActive( unsigned int pFlags) const
  66. {
  67. return (0 != (pFlags & aiProcess_OptimizeGraph));
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Setup properties for the postprocessing step
  71. void OptimizeGraphProcess::SetupProperties(const Importer* pImp)
  72. {
  73. // Get value of AI_CONFIG_PP_OG_EXCLUDE_LIST
  74. std::string tmp = pImp->GetPropertyString(AI_CONFIG_PP_OG_EXCLUDE_LIST,"");
  75. AddLockedNodeList(tmp);
  76. }
  77. // ------------------------------------------------------------------------------------------------
  78. // Collect new children
  79. void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& nodes)
  80. {
  81. nodes_in += nd->mNumChildren;
  82. // Process children
  83. std::list<aiNode*> child_nodes;
  84. for (unsigned int i = 0; i < nd->mNumChildren; ++i) {
  85. CollectNewChildren(nd->mChildren[i],child_nodes);
  86. nd->mChildren[i] = NULL;
  87. }
  88. // Check whether we need this node; if not we can replace it by our own children (warn, danger of incest).
  89. if (locked.find(AI_OG_GETKEY(nd->mName)) == locked.end() ) {
  90. for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end();) {
  91. if (locked.find(AI_OG_GETKEY((*it)->mName)) == locked.end()) {
  92. (*it)->mTransformation = nd->mTransformation * (*it)->mTransformation;
  93. nodes.push_back(*it);
  94. it = child_nodes.erase(it);
  95. continue;
  96. }
  97. ++it;
  98. }
  99. if (nd->mNumMeshes || child_nodes.size()) {
  100. nodes.push_back(nd);
  101. }
  102. else {
  103. delete nd; /* bye, node */
  104. return;
  105. }
  106. }
  107. else {
  108. // Retain our current position in the hierarchy
  109. nodes.push_back(nd);
  110. // Now check for possible optimizations in our list of child nodes. join as many as possible
  111. aiNode* join_master = NULL;
  112. aiMatrix4x4 inv;
  113. const LockedSetType::const_iterator end = locked.end();
  114. std::list<aiNode*> join;
  115. for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end();) {
  116. aiNode* child = *it;
  117. if (child->mNumChildren == 0 && locked.find(AI_OG_GETKEY(child->mName)) == end) {
  118. // There may be no instanced meshes
  119. unsigned int n = 0;
  120. for (; n < child->mNumMeshes;++n) {
  121. if (meshes[child->mMeshes[n]] > 1) {
  122. break;
  123. }
  124. }
  125. if (n == child->mNumMeshes) {
  126. if (!join_master) {
  127. join_master = child;
  128. inv = join_master->mTransformation;
  129. inv.Inverse();
  130. }
  131. else {
  132. child->mTransformation = inv * child->mTransformation ;
  133. join.push_back(child);
  134. it = child_nodes.erase(it);
  135. continue;
  136. }
  137. }
  138. }
  139. ++it;
  140. }
  141. if (join_master && join.size()) {
  142. join_master->mName.length = sprintf(join_master->mName.data,"$MergedNode_%i",count_merged++);
  143. unsigned int out_meshes = 0;
  144. for (std::list<aiNode*>::iterator it = join.begin(); it != join.end(); ++it) {
  145. out_meshes += (*it)->mNumMeshes;
  146. }
  147. // copy all mesh references in one array
  148. if (out_meshes) {
  149. unsigned int* meshes = new unsigned int[out_meshes+join_master->mNumMeshes], *tmp = meshes;
  150. for (unsigned int n = 0; n < join_master->mNumMeshes;++n) {
  151. *tmp++ = join_master->mMeshes[n];
  152. }
  153. for (std::list<aiNode*>::iterator it = join.begin(); it != join.end(); ++it) {
  154. for (unsigned int n = 0; n < (*it)->mNumMeshes; ++n) {
  155. *tmp = (*it)->mMeshes[n];
  156. aiMesh* mesh = mScene->mMeshes[*tmp++];
  157. // manually move the mesh into the right coordinate system
  158. const aiMatrix3x3 IT = aiMatrix3x3( (*it)->mTransformation ).Inverse().Transpose();
  159. for (unsigned int a = 0; a < mesh->mNumVertices; ++a) {
  160. mesh->mVertices[a] *= (*it)->mTransformation;
  161. if (mesh->HasNormals())
  162. mesh->mNormals[a] *= IT;
  163. if (mesh->HasTangentsAndBitangents()) {
  164. mesh->mTangents[a] *= IT;
  165. mesh->mBitangents[a] *= IT;
  166. }
  167. }
  168. }
  169. delete *it; // bye, node
  170. }
  171. delete[] join_master->mMeshes;
  172. join_master->mMeshes = meshes;
  173. join_master->mNumMeshes += out_meshes;
  174. }
  175. }
  176. }
  177. // reassign children if something changed
  178. if (child_nodes.empty() || child_nodes.size() > nd->mNumChildren) {
  179. delete[] nd->mChildren;
  180. if (child_nodes.size())
  181. nd->mChildren = new aiNode*[child_nodes.size()];
  182. else nd->mChildren = NULL;
  183. }
  184. nd->mNumChildren = child_nodes.size();
  185. aiNode** tmp = nd->mChildren;
  186. for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end(); ++it) {
  187. aiNode* node = *tmp++ = *it;
  188. node->mParent = nd;
  189. }
  190. nodes_out += child_nodes.size();
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. // Execute the postprocessing step on the given scene
  194. void OptimizeGraphProcess::Execute( aiScene* pScene)
  195. {
  196. DefaultLogger::get()->debug("OptimizeGraphProcess begin");
  197. nodes_in = nodes_out = count_merged = 0;
  198. mScene = pScene;
  199. meshes.resize(pScene->mNumMeshes,0);
  200. FindInstancedMeshes(pScene->mRootNode);
  201. // build a blacklist of identifiers. If the name of a node matches one of these, we won't touch it
  202. locked.clear();
  203. for (std::list<std::string>::const_iterator it = locked_nodes.begin(); it != locked_nodes.end(); ++it) {
  204. #ifdef AI_OG_USE_HASHING
  205. locked.insert(SuperFastHash((*it).c_str()));
  206. #else
  207. locked.insert(*it);
  208. #endif
  209. }
  210. for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {
  211. for (unsigned int a = 0; a < pScene->mAnimations[i]->mNumChannels; ++a) {
  212. aiNodeAnim* anim = pScene->mAnimations[i]->mChannels[a];
  213. locked.insert(AI_OG_GETKEY(anim->mNodeName));
  214. }
  215. }
  216. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  217. for (unsigned int a = 0; a < pScene->mMeshes[i]->mNumBones; ++a) {
  218. aiBone* bone = pScene->mMeshes[i]->mBones[a];
  219. locked.insert(AI_OG_GETKEY(bone->mName));
  220. // HACK: Meshes referencing bones may not be transformed; we need to look them.
  221. // The easiest way to do this is to increase their reference counters ...
  222. meshes[i] += 2;
  223. }
  224. }
  225. for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {
  226. aiCamera* cam = pScene->mCameras[i];
  227. locked.insert(AI_OG_GETKEY(cam->mName));
  228. }
  229. for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
  230. aiLight* lgh = pScene->mLights[i];
  231. locked.insert(AI_OG_GETKEY(lgh->mName));
  232. }
  233. // Insert a dummy master node and make it read-only
  234. aiNode* dummy_root = new aiNode(AI_RESERVED_NODE_NAME);
  235. locked.insert(AI_OG_GETKEY(dummy_root->mName));
  236. const aiString prev = pScene->mRootNode->mName;
  237. pScene->mRootNode->mParent = dummy_root;
  238. dummy_root->mChildren = new aiNode*[dummy_root->mNumChildren = 1];
  239. dummy_root->mChildren[0] = pScene->mRootNode;
  240. // Do our recursive processing of scenegraph nodes. For each node collect
  241. // a fully new list of children and allow their children to place themselves
  242. // on the same hierarchy layer as their parents.
  243. std::list<aiNode*> nodes;
  244. CollectNewChildren (dummy_root,nodes);
  245. ai_assert(nodes.size() == 1);
  246. if (dummy_root->mNumChildren == 0) {
  247. pScene->mRootNode = NULL;
  248. throw DeadlyImportError("After optimizing the scene graph, no data remains");
  249. }
  250. if (dummy_root->mNumChildren > 1) {
  251. pScene->mRootNode = dummy_root;
  252. // Keep the dummy node but assign the name of the old root node to it
  253. pScene->mRootNode->mName = prev;
  254. }
  255. else {
  256. // Remove the dummy root node again.
  257. pScene->mRootNode = dummy_root->mChildren[0];
  258. dummy_root->mChildren[0] = NULL;
  259. delete dummy_root;
  260. }
  261. pScene->mRootNode->mParent = NULL;
  262. if (!DefaultLogger::isNullLogger()) {
  263. if ( nodes_in != nodes_out) {
  264. char buf[512];
  265. sprintf(buf,"OptimizeGraphProcess finished; Input nodes: %i, Output nodes: %i",nodes_in,nodes_out);
  266. DefaultLogger::get()->info(buf);
  267. }
  268. else DefaultLogger::get()->debug("OptimizeGraphProcess finished");
  269. }
  270. meshes.clear();
  271. locked.clear();
  272. }
  273. // ------------------------------------------------------------------------------------------------
  274. // Buidl a LUT of all instanced meshes
  275. void OptimizeGraphProcess::FindInstancedMeshes (aiNode* pNode)
  276. {
  277. for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
  278. ++meshes[pNode->mMeshes[i]];
  279. }
  280. for (unsigned int i = 0; i < pNode->mNumChildren; ++i)
  281. FindInstancedMeshes(pNode->mChildren[i]);
  282. }
  283. #endif // !! ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS