FindInstancesProcess.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 FindInstancesProcess.cpp
  35. * @brief Implementation of the aiProcess_FindInstances postprocessing step
  36. */
  37. #include "AssimpPCH.h"
  38. #include "FindInstancesProcess.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor to be privately used by Importer
  42. FindInstancesProcess::FindInstancesProcess()
  43. {}
  44. // ------------------------------------------------------------------------------------------------
  45. // Destructor, private as well
  46. FindInstancesProcess::~FindInstancesProcess()
  47. {}
  48. // ------------------------------------------------------------------------------------------------
  49. // Returns whether the processing step is present in the given flag field.
  50. bool FindInstancesProcess::IsActive( unsigned int pFlags) const
  51. {
  52. return 0 != (pFlags & aiProcess_FindInstances);
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Compare the bones of two meshes
  56. bool CompareBones(const aiMesh* orig, const aiMesh* inst)
  57. {
  58. for (unsigned int i = 0; i < orig->mNumBones;++i) {
  59. aiBone* aha = orig->mBones[i];
  60. aiBone* oha = inst->mBones[i];
  61. if (aha->mNumWeights != oha->mNumWeights ||
  62. aha->mOffsetMatrix != oha->mOffsetMatrix ||
  63. aha->mNumWeights != oha->mNumWeights) {
  64. return false;
  65. }
  66. // compare weight per weight ---
  67. for (unsigned int n = 0; n < aha->mNumWeights;++n) {
  68. if (aha->mWeights[n].mVertexId != oha->mWeights[n].mVertexId ||
  69. (aha->mWeights[n].mWeight - oha->mWeights[n].mWeight) < 10e-3f) {
  70. return false;
  71. }
  72. }
  73. }
  74. return true;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Update mesh indices in the node graph
  78. void UpdateMeshIndices(aiNode* node, unsigned int* lookup)
  79. {
  80. for (unsigned int n = 0; n < node->mNumMeshes;++n)
  81. node->mMeshes[n] = lookup[node->mMeshes[n]];
  82. for (unsigned int n = 0; n < node->mNumChildren;++n)
  83. UpdateMeshIndices(node->mChildren[n],lookup);
  84. }
  85. // ------------------------------------------------------------------------------------------------
  86. // Executes the post processing step on the given imported data.
  87. void FindInstancesProcess::Execute( aiScene* pScene)
  88. {
  89. DefaultLogger::get()->debug("FindInstancesProcess begin");
  90. if (pScene->mNumMeshes) {
  91. // use a pseudo hash for all meshes in the scene to quickly find
  92. // the ones which are possibly equal. This step is executed early
  93. // in the pipeline, so we could, depending on the file format,
  94. // have several thousand small meshes. That's too much for a brute
  95. // everyone-against-everyone check involving up to 25 comparisons
  96. // each.
  97. boost::scoped_array<uint64_t> hashes (new uint64_t[pScene->mNumMeshes]);
  98. boost::scoped_array<unsigned int> remapping (new unsigned int[pScene->mNumMeshes]);
  99. unsigned int numMeshesOut = 0;
  100. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  101. aiMesh* inst = pScene->mMeshes[i];
  102. hashes[i] = GetMeshHash(inst);
  103. for (int a = i-1; a > 0; --a) {
  104. if (hashes[i] == hashes[a])
  105. {
  106. aiMesh* orig = pScene->mMeshes[a];
  107. if (!orig)
  108. continue;
  109. // check for hash collision .. we needn't check
  110. // the vertex format, it *must* match due to the
  111. // (brilliant) construction of the hash
  112. if (orig->mNumBones != inst->mNumBones ||
  113. orig->mNumFaces != inst->mNumFaces ||
  114. orig->mNumVertices != inst->mNumVertices ||
  115. orig->mMaterialIndex != inst->mMaterialIndex ||
  116. orig->mPrimitiveTypes != inst->mPrimitiveTypes)
  117. continue;
  118. // up to now the meshes are equal. find an appropriate
  119. // epsilon to compare position differences against
  120. float epsilon = ComputePositionEpsilon(inst);
  121. epsilon *= epsilon;
  122. // now compare vertex positions, normals,
  123. // tangents and bitangents using this epsilon.
  124. if (orig->HasPositions()) {
  125. if(!CompareArrays(orig->mVertices,inst->mVertices,orig->mNumVertices,epsilon))
  126. continue;
  127. }
  128. if (orig->HasNormals()) {
  129. if(!CompareArrays(orig->mNormals,inst->mNormals,orig->mNumVertices,epsilon))
  130. continue;
  131. }
  132. if (orig->HasTangentsAndBitangents()) {
  133. if (!CompareArrays(orig->mTangents,inst->mTangents,orig->mNumVertices,epsilon) ||
  134. !CompareArrays(orig->mBitangents,inst->mBitangents,orig->mNumVertices,epsilon))
  135. continue;
  136. }
  137. // use a constant epsilon for colors and UV coordinates
  138. static const float uvEpsilon = 10e-4f;
  139. BOOST_STATIC_ASSERT(4 == AI_MAX_NUMBER_OF_COLOR_SETS);
  140. // as in JIV: manually unrolled as continue wouldn't work as desired in inner loops
  141. if (orig->mTextureCoords[0]) {
  142. if(!CompareArrays(orig->mTextureCoords[0],inst->mTextureCoords[0],orig->mNumVertices,uvEpsilon))
  143. continue;
  144. if (orig->mTextureCoords[1]) {
  145. if(!CompareArrays(orig->mTextureCoords[1],inst->mTextureCoords[1],orig->mNumVertices,uvEpsilon))
  146. continue;
  147. if (orig->mTextureCoords[2]) {
  148. if(!CompareArrays(orig->mTextureCoords[2],inst->mTextureCoords[2],orig->mNumVertices,uvEpsilon))
  149. continue;
  150. if (orig->mTextureCoords[3]) {
  151. if(!CompareArrays(orig->mTextureCoords[3],inst->mTextureCoords[3],orig->mNumVertices,uvEpsilon))
  152. continue;
  153. }
  154. }
  155. }
  156. }
  157. BOOST_STATIC_ASSERT(4 == AI_MAX_NUMBER_OF_COLOR_SETS);
  158. // and the same nasty stuff for vertex colors ...
  159. if (orig->mColors[0]) {
  160. if(!CompareArrays(orig->mColors[0],inst->mColors[0],orig->mNumVertices,uvEpsilon))
  161. continue;
  162. if (orig->mTextureCoords[1]) {
  163. if(!CompareArrays(orig->mColors[1],inst->mColors[1],orig->mNumVertices,uvEpsilon))
  164. continue;
  165. if (orig->mTextureCoords[2]) {
  166. if(!CompareArrays(orig->mColors[2],inst->mColors[2],orig->mNumVertices,uvEpsilon))
  167. continue;
  168. if (orig->mTextureCoords[3]) {
  169. if(!CompareArrays(orig->mColors[3],inst->mColors[3],orig->mNumVertices,uvEpsilon))
  170. continue;
  171. }
  172. }
  173. }
  174. }
  175. // It seems to be strange, but we really need to check whether the
  176. // bones are identical too. Although it's extremely unprobable
  177. // that they're not if control reaches here, but we need to deal
  178. // with unprobable cases, too.
  179. if (!CompareBones(orig,inst))
  180. continue;
  181. // FIXME: Ignore the faces for the moment ... ok!?
  182. // We're still here. Or in other words: 'inst' is an instance of 'orig'.
  183. // Place a marker in our list that we can easily update mesh indices.
  184. remapping[i] = a;
  185. // Delete the instanced mesh, we don't need it anymore
  186. delete inst;
  187. pScene->mMeshes[i] = NULL;
  188. }
  189. }
  190. // If we didn't find a match for the current mesh: keep it
  191. if (pScene->mMeshes[i]) {
  192. remapping[i] = numMeshesOut++;
  193. }
  194. }
  195. ai_assert(0 != numMeshesOut);
  196. if (numMeshesOut != pScene->mNumMeshes) {
  197. // Collapse the meshes array by removing all NULL entries
  198. for (unsigned int real = 0, i = 0; real < numMeshesOut; ++i) {
  199. if (pScene->mMeshes[i])
  200. pScene->mMeshes[real++] = pScene->mMeshes[i];
  201. }
  202. // And update the nodegraph with our nice lookup table
  203. UpdateMeshIndices(pScene->mRootNode,remapping.get());
  204. // write to log
  205. if (!DefaultLogger::isNullLogger()) {
  206. char buffer[512];
  207. ::sprintf(buffer,"FindInstancesProcess finished. Found %i instances",pScene->mNumMeshes-numMeshesOut);
  208. DefaultLogger::get()->info(buffer);
  209. }
  210. pScene->mNumMeshes = numMeshesOut;
  211. }
  212. else DefaultLogger::get()->debug("FindInstancesProcess finished. No instanced meshes found");
  213. }
  214. }