SplitByBoneCountProcess.cpp 15 KB

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