SplitByBoneCountProcess.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2018, 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. /// @file SplitByBoneCountProcess.cpp
  34. /// Implementation of the SplitByBoneCount postprocessing step
  35. // internal headers of the post-processing framework
  36. #include "SplitByBoneCountProcess.h"
  37. #include <assimp/postprocess.h>
  38. #include <assimp/DefaultLogger.hpp>
  39. #include <limits>
  40. #include <assimp/TinyFormatter.h>
  41. using namespace Assimp;
  42. using namespace Assimp::Formatter;
  43. // ------------------------------------------------------------------------------------------------
  44. // Constructor
  45. SplitByBoneCountProcess::SplitByBoneCountProcess()
  46. {
  47. // set default, might be overridden by importer config
  48. mMaxBoneCount = AI_SBBC_DEFAULT_MAX_BONES;
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // Destructor
  52. SplitByBoneCountProcess::~SplitByBoneCountProcess()
  53. {
  54. // nothing to do here
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Returns whether the processing step is present in the given flag.
  58. bool SplitByBoneCountProcess::IsActive( unsigned int pFlags) const
  59. {
  60. return !!(pFlags & aiProcess_SplitByBoneCount);
  61. }
  62. // ------------------------------------------------------------------------------------------------
  63. // Updates internal properties
  64. void SplitByBoneCountProcess::SetupProperties(const Importer* pImp)
  65. {
  66. mMaxBoneCount = pImp->GetPropertyInteger(AI_CONFIG_PP_SBBC_MAX_BONES,AI_SBBC_DEFAULT_MAX_BONES);
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Executes the post processing step on the given imported data.
  70. void SplitByBoneCountProcess::Execute( aiScene* pScene)
  71. {
  72. ASSIMP_LOG_DEBUG("SplitByBoneCountProcess begin");
  73. // early out
  74. bool isNecessary = false;
  75. for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
  76. if( pScene->mMeshes[a]->mNumBones > mMaxBoneCount )
  77. isNecessary = true;
  78. if( !isNecessary )
  79. {
  80. ASSIMP_LOG_DEBUG( format() << "SplitByBoneCountProcess early-out: no meshes with more than " << mMaxBoneCount << " bones." );
  81. return;
  82. }
  83. // we need to do something. Let's go.
  84. mSubMeshIndices.clear();
  85. mSubMeshIndices.resize( pScene->mNumMeshes);
  86. // build a new array of meshes for the scene
  87. std::vector<aiMesh*> meshes;
  88. for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
  89. {
  90. aiMesh* srcMesh = pScene->mMeshes[a];
  91. std::vector<aiMesh*> newMeshes;
  92. SplitMesh( pScene->mMeshes[a], newMeshes);
  93. // mesh was split
  94. if( !newMeshes.empty() )
  95. {
  96. // store new meshes and indices of the new meshes
  97. for( unsigned int b = 0; b < newMeshes.size(); ++b)
  98. {
  99. mSubMeshIndices[a].push_back( static_cast<unsigned int>(meshes.size()));
  100. meshes.push_back( newMeshes[b]);
  101. }
  102. // and destroy the source mesh. It should be completely contained inside the new submeshes
  103. delete srcMesh;
  104. }
  105. else
  106. {
  107. // Mesh is kept unchanged - store it's new place in the mesh array
  108. mSubMeshIndices[a].push_back( static_cast<unsigned int>(meshes.size()));
  109. meshes.push_back( srcMesh);
  110. }
  111. }
  112. // rebuild the scene's mesh array
  113. pScene->mNumMeshes = static_cast<unsigned int>(meshes.size());
  114. delete [] pScene->mMeshes;
  115. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  116. std::copy( meshes.begin(), meshes.end(), pScene->mMeshes);
  117. // recurse through all nodes and translate the node's mesh indices to fit the new mesh array
  118. UpdateNode( pScene->mRootNode);
  119. ASSIMP_LOG_DEBUG( format() << "SplitByBoneCountProcess end: split " << mSubMeshIndices.size() << " meshes into " << meshes.size() << " submeshes." );
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. // Splits the given mesh by bone count.
  123. void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh*>& poNewMeshes) const
  124. {
  125. // skip if not necessary
  126. if( pMesh->mNumBones <= mMaxBoneCount )
  127. return;
  128. // necessary optimisation: build a list of all affecting bones for each vertex
  129. // TODO: (thom) maybe add a custom allocator here to avoid allocating tens of thousands of small arrays
  130. typedef std::pair<unsigned int, float> BoneWeight;
  131. std::vector< std::vector<BoneWeight> > vertexBones( pMesh->mNumVertices);
  132. for( unsigned int a = 0; a < pMesh->mNumBones; ++a)
  133. {
  134. const aiBone* bone = pMesh->mBones[a];
  135. for( unsigned int b = 0; b < bone->mNumWeights; ++b)
  136. vertexBones[ bone->mWeights[b].mVertexId ].push_back( BoneWeight( a, bone->mWeights[b].mWeight));
  137. }
  138. unsigned int numFacesHandled = 0;
  139. std::vector<bool> isFaceHandled( pMesh->mNumFaces, false);
  140. while( numFacesHandled < pMesh->mNumFaces )
  141. {
  142. // which bones are used in the current submesh
  143. unsigned int numBones = 0;
  144. std::vector<bool> isBoneUsed( pMesh->mNumBones, false);
  145. // indices of the faces which are going to go into this submesh
  146. std::vector<unsigned int> subMeshFaces;
  147. subMeshFaces.reserve( pMesh->mNumFaces);
  148. // accumulated vertex count of all the faces in this submesh
  149. unsigned int numSubMeshVertices = 0;
  150. // a small local array of new bones for the current face. State of all used bones for that face
  151. // can only be updated AFTER the face is completely analysed. Thanks to imre for the fix.
  152. std::vector<unsigned int> newBonesAtCurrentFace;
  153. // add faces to the new submesh as long as all bones affecting the faces' vertices fit in the limit
  154. for( unsigned int a = 0; a < pMesh->mNumFaces; ++a)
  155. {
  156. // skip if the face is already stored in a submesh
  157. if( isFaceHandled[a] )
  158. continue;
  159. const aiFace& face = pMesh->mFaces[a];
  160. // check every vertex if its bones would still fit into the current submesh
  161. for( unsigned int b = 0; b < face.mNumIndices; ++b )
  162. {
  163. const std::vector<BoneWeight>& vb = vertexBones[face.mIndices[b]];
  164. for( unsigned int c = 0; c < vb.size(); ++c)
  165. {
  166. unsigned int boneIndex = vb[c].first;
  167. // if the bone is already used in this submesh, it's ok
  168. if( isBoneUsed[boneIndex] )
  169. continue;
  170. // if it's not used, yet, we would need to add it. Store its bone index
  171. if( std::find( newBonesAtCurrentFace.begin(), newBonesAtCurrentFace.end(), boneIndex) == newBonesAtCurrentFace.end() )
  172. newBonesAtCurrentFace.push_back( boneIndex);
  173. }
  174. }
  175. // leave out the face if the new bones required for this face don't fit the bone count limit anymore
  176. if( numBones + newBonesAtCurrentFace.size() > mMaxBoneCount )
  177. continue;
  178. // mark all new bones as necessary
  179. while( !newBonesAtCurrentFace.empty() )
  180. {
  181. unsigned int newIndex = newBonesAtCurrentFace.back();
  182. newBonesAtCurrentFace.pop_back(); // this also avoids the deallocation which comes with a clear()
  183. if( isBoneUsed[newIndex] )
  184. continue;
  185. isBoneUsed[newIndex] = true;
  186. numBones++;
  187. }
  188. // store the face index and the vertex count
  189. subMeshFaces.push_back( a);
  190. numSubMeshVertices += face.mNumIndices;
  191. // remember that this face is handled
  192. isFaceHandled[a] = true;
  193. numFacesHandled++;
  194. }
  195. // create a new mesh to hold this subset of the source mesh
  196. aiMesh* newMesh = new aiMesh;
  197. if( pMesh->mName.length > 0 )
  198. newMesh->mName.Set( format() << pMesh->mName.data << "_sub" << poNewMeshes.size());
  199. newMesh->mMaterialIndex = pMesh->mMaterialIndex;
  200. newMesh->mPrimitiveTypes = pMesh->mPrimitiveTypes;
  201. poNewMeshes.push_back( newMesh);
  202. // create all the arrays for this mesh if the old mesh contained them
  203. newMesh->mNumVertices = numSubMeshVertices;
  204. newMesh->mNumFaces = static_cast<unsigned int>(subMeshFaces.size());
  205. newMesh->mVertices = new aiVector3D[newMesh->mNumVertices];
  206. if( pMesh->HasNormals() )
  207. newMesh->mNormals = new aiVector3D[newMesh->mNumVertices];
  208. if( pMesh->HasTangentsAndBitangents() )
  209. {
  210. newMesh->mTangents = new aiVector3D[newMesh->mNumVertices];
  211. newMesh->mBitangents = new aiVector3D[newMesh->mNumVertices];
  212. }
  213. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  214. {
  215. if( pMesh->HasTextureCoords( a) )
  216. newMesh->mTextureCoords[a] = new aiVector3D[newMesh->mNumVertices];
  217. newMesh->mNumUVComponents[a] = pMesh->mNumUVComponents[a];
  218. }
  219. for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
  220. {
  221. if( pMesh->HasVertexColors( a) )
  222. newMesh->mColors[a] = new aiColor4D[newMesh->mNumVertices];
  223. }
  224. // and copy over the data, generating faces with linear indices along the way
  225. newMesh->mFaces = new aiFace[subMeshFaces.size()];
  226. unsigned int nvi = 0; // next vertex index
  227. std::vector<unsigned int> previousVertexIndices( numSubMeshVertices, std::numeric_limits<unsigned int>::max()); // per new vertex: its index in the source mesh
  228. for( unsigned int a = 0; a < subMeshFaces.size(); ++a )
  229. {
  230. const aiFace& srcFace = pMesh->mFaces[subMeshFaces[a]];
  231. aiFace& dstFace = newMesh->mFaces[a];
  232. dstFace.mNumIndices = srcFace.mNumIndices;
  233. dstFace.mIndices = new unsigned int[dstFace.mNumIndices];
  234. // accumulate linearly all the vertices of the source face
  235. for( unsigned int b = 0; b < dstFace.mNumIndices; ++b )
  236. {
  237. unsigned int srcIndex = srcFace.mIndices[b];
  238. dstFace.mIndices[b] = nvi;
  239. previousVertexIndices[nvi] = srcIndex;
  240. newMesh->mVertices[nvi] = pMesh->mVertices[srcIndex];
  241. if( pMesh->HasNormals() )
  242. newMesh->mNormals[nvi] = pMesh->mNormals[srcIndex];
  243. if( pMesh->HasTangentsAndBitangents() )
  244. {
  245. newMesh->mTangents[nvi] = pMesh->mTangents[srcIndex];
  246. newMesh->mBitangents[nvi] = pMesh->mBitangents[srcIndex];
  247. }
  248. for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++c )
  249. {
  250. if( pMesh->HasTextureCoords( c) )
  251. newMesh->mTextureCoords[c][nvi] = pMesh->mTextureCoords[c][srcIndex];
  252. }
  253. for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; ++c )
  254. {
  255. if( pMesh->HasVertexColors( c) )
  256. newMesh->mColors[c][nvi] = pMesh->mColors[c][srcIndex];
  257. }
  258. nvi++;
  259. }
  260. }
  261. ai_assert( nvi == numSubMeshVertices );
  262. // Create the bones for the new submesh: first create the bone array
  263. newMesh->mNumBones = 0;
  264. newMesh->mBones = new aiBone*[numBones];
  265. std::vector<unsigned int> mappedBoneIndex( pMesh->mNumBones, std::numeric_limits<unsigned int>::max());
  266. for( unsigned int a = 0; a < pMesh->mNumBones; ++a )
  267. {
  268. if( !isBoneUsed[a] )
  269. continue;
  270. // create the new bone
  271. const aiBone* srcBone = pMesh->mBones[a];
  272. aiBone* dstBone = new aiBone;
  273. mappedBoneIndex[a] = newMesh->mNumBones;
  274. newMesh->mBones[newMesh->mNumBones++] = dstBone;
  275. dstBone->mName = srcBone->mName;
  276. dstBone->mOffsetMatrix = srcBone->mOffsetMatrix;
  277. dstBone->mNumWeights = 0;
  278. }
  279. ai_assert( newMesh->mNumBones == numBones );
  280. // iterate over all new vertices and count which bones affected its old vertex in the source mesh
  281. for( unsigned int a = 0; a < numSubMeshVertices; ++a )
  282. {
  283. unsigned int oldIndex = previousVertexIndices[a];
  284. const std::vector<BoneWeight>& bonesOnThisVertex = vertexBones[oldIndex];
  285. for( unsigned int b = 0; b < bonesOnThisVertex.size(); ++b )
  286. {
  287. unsigned int newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
  288. if( newBoneIndex != std::numeric_limits<unsigned int>::max() )
  289. newMesh->mBones[newBoneIndex]->mNumWeights++;
  290. }
  291. }
  292. // allocate all bone weight arrays accordingly
  293. for( unsigned int a = 0; a < newMesh->mNumBones; ++a )
  294. {
  295. aiBone* bone = newMesh->mBones[a];
  296. ai_assert( bone->mNumWeights > 0 );
  297. bone->mWeights = new aiVertexWeight[bone->mNumWeights];
  298. bone->mNumWeights = 0; // for counting up in the next step
  299. }
  300. // now copy all the bone vertex weights for all the vertices which made it into the new submesh
  301. for( unsigned int a = 0; a < numSubMeshVertices; ++a)
  302. {
  303. // find the source vertex for it in the source mesh
  304. unsigned int previousIndex = previousVertexIndices[a];
  305. // these bones were affecting it
  306. const std::vector<BoneWeight>& bonesOnThisVertex = vertexBones[previousIndex];
  307. // all of the bones affecting it should be present in the new submesh, or else
  308. // the face it comprises shouldn't be present
  309. for( unsigned int b = 0; b < bonesOnThisVertex.size(); ++b)
  310. {
  311. unsigned int newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
  312. ai_assert( newBoneIndex != std::numeric_limits<unsigned int>::max() );
  313. aiVertexWeight* dstWeight = newMesh->mBones[newBoneIndex]->mWeights + newMesh->mBones[newBoneIndex]->mNumWeights;
  314. newMesh->mBones[newBoneIndex]->mNumWeights++;
  315. dstWeight->mVertexId = a;
  316. dstWeight->mWeight = bonesOnThisVertex[b].second;
  317. }
  318. }
  319. // I have the strange feeling that this will break apart at some point in time...
  320. }
  321. }
  322. // ------------------------------------------------------------------------------------------------
  323. // Recursively updates the node's mesh list to account for the changed mesh list
  324. void SplitByBoneCountProcess::UpdateNode( aiNode* pNode) const
  325. {
  326. // rebuild the node's mesh index list
  327. if( pNode->mNumMeshes > 0 )
  328. {
  329. std::vector<unsigned int> newMeshList;
  330. for( unsigned int a = 0; a < pNode->mNumMeshes; ++a)
  331. {
  332. unsigned int srcIndex = pNode->mMeshes[a];
  333. const std::vector<unsigned int>& replaceMeshes = mSubMeshIndices[srcIndex];
  334. newMeshList.insert( newMeshList.end(), replaceMeshes.begin(), replaceMeshes.end());
  335. }
  336. delete pNode->mMeshes;
  337. pNode->mNumMeshes = static_cast<unsigned int>(newMeshList.size());
  338. pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
  339. std::copy( newMeshList.begin(), newMeshList.end(), pNode->mMeshes);
  340. }
  341. // do that also recursively for all children
  342. for( unsigned int a = 0; a < pNode->mNumChildren; ++a )
  343. {
  344. UpdateNode( pNode->mChildren[a]);
  345. }
  346. }