Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FindInstancesProcess.cpp 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 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. : configSpeedFlag (false)
  44. {}
  45. // ------------------------------------------------------------------------------------------------
  46. // Destructor, private as well
  47. FindInstancesProcess::~FindInstancesProcess()
  48. {}
  49. // ------------------------------------------------------------------------------------------------
  50. // Returns whether the processing step is present in the given flag field.
  51. bool FindInstancesProcess::IsActive( unsigned int pFlags) const
  52. {
  53. // FindInstances makes absolutely no sense together with PreTransformVertices
  54. // fixme: spawn error message somewhere else?
  55. return 0 != (pFlags & aiProcess_FindInstances) && 0 == (pFlags & aiProcess_PreTransformVertices);
  56. }
  57. // ------------------------------------------------------------------------------------------------
  58. // Setup properties for the step
  59. void FindInstancesProcess::SetupProperties(const Importer* pImp)
  60. {
  61. // AI_CONFIG_FAVOUR_SPEED
  62. configSpeedFlag = (0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED,0));
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Compare the bones of two meshes
  66. bool CompareBones(const aiMesh* orig, const aiMesh* inst)
  67. {
  68. for (unsigned int i = 0; i < orig->mNumBones;++i) {
  69. aiBone* aha = orig->mBones[i];
  70. aiBone* oha = inst->mBones[i];
  71. if (aha->mNumWeights != oha->mNumWeights ||
  72. aha->mOffsetMatrix != oha->mOffsetMatrix ||
  73. aha->mNumWeights != oha->mNumWeights) {
  74. return false;
  75. }
  76. // compare weight per weight ---
  77. for (unsigned int n = 0; n < aha->mNumWeights;++n) {
  78. if (aha->mWeights[n].mVertexId != oha->mWeights[n].mVertexId ||
  79. (aha->mWeights[n].mWeight - oha->mWeights[n].mWeight) < 10e-3f) {
  80. return false;
  81. }
  82. }
  83. }
  84. return true;
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. // Update mesh indices in the node graph
  88. void UpdateMeshIndices(aiNode* node, unsigned int* lookup)
  89. {
  90. for (unsigned int n = 0; n < node->mNumMeshes;++n)
  91. node->mMeshes[n] = lookup[node->mMeshes[n]];
  92. for (unsigned int n = 0; n < node->mNumChildren;++n)
  93. UpdateMeshIndices(node->mChildren[n],lookup);
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Executes the post processing step on the given imported data.
  97. void FindInstancesProcess::Execute( aiScene* pScene)
  98. {
  99. DefaultLogger::get()->debug("FindInstancesProcess begin");
  100. if (pScene->mNumMeshes) {
  101. // use a pseudo hash for all meshes in the scene to quickly find
  102. // the ones which are possibly equal. This step is executed early
  103. // in the pipeline, so we could, depending on the file format,
  104. // have several thousand small meshes. That's too much for a brute
  105. // everyone-against-everyone check involving up to 10 comparisons
  106. // each.
  107. boost::scoped_array<uint64_t> hashes (new uint64_t[pScene->mNumMeshes]);
  108. boost::scoped_array<unsigned int> remapping (new unsigned int[pScene->mNumMeshes]);
  109. unsigned int numMeshesOut = 0;
  110. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  111. aiMesh* inst = pScene->mMeshes[i];
  112. hashes[i] = GetMeshHash(inst);
  113. for (int a = i-1; a >= 0; --a) {
  114. if (hashes[i] == hashes[a])
  115. {
  116. aiMesh* orig = pScene->mMeshes[a];
  117. if (!orig)
  118. continue;
  119. // check for hash collision .. we needn't check
  120. // the vertex format, it *must* match due to the
  121. // (brilliant) construction of the hash
  122. if (orig->mNumBones != inst->mNumBones ||
  123. orig->mNumFaces != inst->mNumFaces ||
  124. orig->mNumVertices != inst->mNumVertices ||
  125. orig->mMaterialIndex != inst->mMaterialIndex ||
  126. orig->mPrimitiveTypes != inst->mPrimitiveTypes)
  127. continue;
  128. // up to now the meshes are equal. find an appropriate
  129. // epsilon to compare position differences against
  130. float epsilon = ComputePositionEpsilon(inst);
  131. epsilon *= epsilon;
  132. // now compare vertex positions, normals,
  133. // tangents and bitangents using this epsilon.
  134. if (orig->HasPositions()) {
  135. if(!CompareArrays(orig->mVertices,inst->mVertices,orig->mNumVertices,epsilon))
  136. continue;
  137. }
  138. if (orig->HasNormals()) {
  139. if(!CompareArrays(orig->mNormals,inst->mNormals,orig->mNumVertices,epsilon))
  140. continue;
  141. }
  142. if (orig->HasTangentsAndBitangents()) {
  143. if (!CompareArrays(orig->mTangents,inst->mTangents,orig->mNumVertices,epsilon) ||
  144. !CompareArrays(orig->mBitangents,inst->mBitangents,orig->mNumVertices,epsilon))
  145. continue;
  146. }
  147. // use a constant epsilon for colors and UV coordinates
  148. static const float uvEpsilon = 10e-4f;
  149. {
  150. unsigned int i, end = orig->GetNumUVChannels();
  151. for(i = 0; i < end; ++i) {
  152. if (!orig->mTextureCoords[i]) {
  153. continue;
  154. }
  155. if(!CompareArrays(orig->mTextureCoords[i],inst->mTextureCoords[i],orig->mNumVertices,uvEpsilon)) {
  156. break;
  157. }
  158. }
  159. if (i != end) {
  160. continue;
  161. }
  162. }
  163. {
  164. unsigned int i, end = orig->GetNumColorChannels();
  165. for(i = 0; i < end; ++i) {
  166. if (!orig->mColors[i]) {
  167. continue;
  168. }
  169. if(!CompareArrays(orig->mColors[i],inst->mColors[i],orig->mNumVertices,uvEpsilon)) {
  170. break;
  171. }
  172. }
  173. if (i != end) {
  174. continue;
  175. }
  176. }
  177. // These two checks are actually quite expensive and almost *never* required.
  178. // Almost. That's why they're still here. But there's no reason to do them
  179. // in speed-targeted imports.
  180. if (!configSpeedFlag) {
  181. // It seems to be strange, but we really need to check whether the
  182. // bones are identical too. Although it's extremely unprobable
  183. // that they're not if control reaches here, we need to deal
  184. // with unprobable cases, too. It could still be that there are
  185. // equal shapes which are deformed differently.
  186. if (!CompareBones(orig,inst))
  187. continue;
  188. // For completeness ... compare even the index buffers for equality
  189. // face order & winding order doesn't care. Input data is in verbose format.
  190. boost::scoped_array<unsigned int> ftbl_orig(new unsigned int[orig->mNumVertices]);
  191. boost::scoped_array<unsigned int> ftbl_inst(new unsigned int[orig->mNumVertices]);
  192. for (unsigned int tt = 0; tt < orig->mNumFaces;++tt) {
  193. aiFace& f = orig->mFaces[tt];
  194. for (unsigned int nn = 0; nn < f.mNumIndices;++nn)
  195. ftbl_orig[f.mIndices[nn]] = tt;
  196. aiFace& f2 = inst->mFaces[tt];
  197. for (unsigned int nn = 0; nn < f2.mNumIndices;++nn)
  198. ftbl_inst[f2.mIndices[nn]] = tt;
  199. }
  200. if (0 != ::memcmp(ftbl_inst.get(),ftbl_orig.get(),orig->mNumVertices*sizeof(unsigned int)))
  201. continue;
  202. }
  203. // We're still here. Or in other words: 'inst' is an instance of 'orig'.
  204. // Place a marker in our list that we can easily update mesh indices.
  205. remapping[i] = remapping[a];
  206. // Delete the instanced mesh, we don't need it anymore
  207. delete inst;
  208. pScene->mMeshes[i] = NULL;
  209. break;
  210. }
  211. }
  212. // If we didn't find a match for the current mesh: keep it
  213. if (pScene->mMeshes[i]) {
  214. remapping[i] = numMeshesOut++;
  215. }
  216. }
  217. ai_assert(0 != numMeshesOut);
  218. if (numMeshesOut != pScene->mNumMeshes) {
  219. // Collapse the meshes array by removing all NULL entries
  220. for (unsigned int real = 0, i = 0; real < numMeshesOut; ++i) {
  221. if (pScene->mMeshes[i])
  222. pScene->mMeshes[real++] = pScene->mMeshes[i];
  223. }
  224. // And update the nodegraph with our nice lookup table
  225. UpdateMeshIndices(pScene->mRootNode,remapping.get());
  226. // write to log
  227. if (!DefaultLogger::isNullLogger()) {
  228. char buffer[512];
  229. ::sprintf(buffer,"FindInstancesProcess finished. Found %i instances",pScene->mNumMeshes-numMeshesOut);
  230. DefaultLogger::get()->info(buffer);
  231. }
  232. pScene->mNumMeshes = numMeshesOut;
  233. }
  234. else DefaultLogger::get()->debug("FindInstancesProcess finished. No instanced meshes found");
  235. }
  236. }