2
0

SplitByBoneCountProcess.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. mMaxBoneCount = pImp->GetPropertyInteger(AI_CONFIG_PP_SBBC_MAX_BONES,AI_SBBC_DEFAULT_MAX_BONES);
  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. size_t boneIndex = vb[c].first;
  163. // if the bone is already used in this submesh, it's ok
  164. if( isBoneUsed[boneIndex] )
  165. continue;
  166. // if it's not used, yet, we would need to add it. Store its bone index
  167. if( std::find( newBonesAtCurrentFace.begin(), newBonesAtCurrentFace.end(), boneIndex) == newBonesAtCurrentFace.end() )
  168. newBonesAtCurrentFace.push_back( boneIndex);
  169. }
  170. }
  171. // leave out the face if the new bones required for this face don't fit the bone count limit anymore
  172. if( numBones + newBonesAtCurrentFace.size() > mMaxBoneCount )
  173. continue;
  174. // mark all new bones as necessary
  175. while( !newBonesAtCurrentFace.empty() )
  176. {
  177. size_t newIndex = newBonesAtCurrentFace.back();
  178. newBonesAtCurrentFace.pop_back(); // this also avoids the deallocation which comes with a clear()
  179. if( isBoneUsed[newIndex] )
  180. continue;
  181. isBoneUsed[newIndex] = true;
  182. numBones++;
  183. }
  184. // store the face index and the vertex count
  185. subMeshFaces.push_back( a);
  186. numSubMeshVertices += face.mNumIndices;
  187. // remember that this face is handled
  188. isFaceHandled[a] = true;
  189. numFacesHandled++;
  190. }
  191. // create a new mesh to hold this subset of the source mesh
  192. aiMesh* newMesh = new aiMesh;
  193. if( pMesh->mName.length > 0 )
  194. newMesh->mName.Set( boost::str( boost::format( "%s_sub%d") % pMesh->mName.data % poNewMeshes.size()));
  195. newMesh->mMaterialIndex = pMesh->mMaterialIndex;
  196. newMesh->mPrimitiveTypes = pMesh->mPrimitiveTypes;
  197. poNewMeshes.push_back( newMesh);
  198. // create all the arrays for this mesh if the old mesh contained them
  199. newMesh->mNumVertices = numSubMeshVertices;
  200. newMesh->mNumFaces = subMeshFaces.size();
  201. newMesh->mVertices = new aiVector3D[newMesh->mNumVertices];
  202. if( pMesh->HasNormals() )
  203. newMesh->mNormals = new aiVector3D[newMesh->mNumVertices];
  204. if( pMesh->HasTangentsAndBitangents() )
  205. {
  206. newMesh->mTangents = new aiVector3D[newMesh->mNumVertices];
  207. newMesh->mBitangents = new aiVector3D[newMesh->mNumVertices];
  208. }
  209. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  210. {
  211. if( pMesh->HasTextureCoords( a) )
  212. newMesh->mTextureCoords[a] = new aiVector3D[newMesh->mNumVertices];
  213. newMesh->mNumUVComponents[a] = pMesh->mNumUVComponents[a];
  214. }
  215. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
  216. {
  217. if( pMesh->HasVertexColors( a) )
  218. newMesh->mColors[a] = new aiColor4D[newMesh->mNumVertices];
  219. }
  220. // and copy over the data, generating faces with linear indices along the way
  221. newMesh->mFaces = new aiFace[subMeshFaces.size()];
  222. size_t nvi = 0; // next vertex index
  223. std::vector<size_t> previousVertexIndices( numSubMeshVertices, SIZE_MAX); // per new vertex: its index in the source mesh
  224. for( size_t a = 0; a < subMeshFaces.size(); ++a )
  225. {
  226. const aiFace& srcFace = pMesh->mFaces[subMeshFaces[a]];
  227. aiFace& dstFace = newMesh->mFaces[a];
  228. dstFace.mNumIndices = srcFace.mNumIndices;
  229. dstFace.mIndices = new unsigned int[dstFace.mNumIndices];
  230. // accumulate linearly all the vertices of the source face
  231. for( size_t b = 0; b < dstFace.mNumIndices; ++b )
  232. {
  233. size_t srcIndex = srcFace.mIndices[b];
  234. dstFace.mIndices[b] = nvi;
  235. previousVertexIndices[nvi] = srcIndex;
  236. newMesh->mVertices[nvi] = pMesh->mVertices[srcIndex];
  237. if( pMesh->HasNormals() )
  238. newMesh->mNormals[nvi] = pMesh->mNormals[srcIndex];
  239. if( pMesh->HasTangentsAndBitangents() )
  240. {
  241. newMesh->mTangents[nvi] = pMesh->mTangents[srcIndex];
  242. newMesh->mBitangents[nvi] = pMesh->mBitangents[srcIndex];
  243. }
  244. for( size_t c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++c )
  245. {
  246. if( pMesh->HasTextureCoords( c) )
  247. newMesh->mTextureCoords[c][nvi] = pMesh->mTextureCoords[c][srcIndex];
  248. }
  249. for( size_t c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; ++c )
  250. {
  251. if( pMesh->HasVertexColors( c) )
  252. newMesh->mColors[c][nvi] = pMesh->mColors[c][srcIndex];
  253. }
  254. nvi++;
  255. }
  256. }
  257. ai_assert( nvi == numSubMeshVertices );
  258. // Create the bones for the new submesh: first create the bone array
  259. newMesh->mNumBones = 0;
  260. newMesh->mBones = new aiBone*[numBones];
  261. std::vector<size_t> mappedBoneIndex( pMesh->mNumBones, SIZE_MAX);
  262. for( size_t a = 0; a < pMesh->mNumBones; ++a )
  263. {
  264. if( !isBoneUsed[a] )
  265. continue;
  266. // create the new bone
  267. const aiBone* srcBone = pMesh->mBones[a];
  268. aiBone* dstBone = new aiBone;
  269. mappedBoneIndex[a] = newMesh->mNumBones;
  270. newMesh->mBones[newMesh->mNumBones++] = dstBone;
  271. dstBone->mName = srcBone->mName;
  272. dstBone->mOffsetMatrix = srcBone->mOffsetMatrix;
  273. dstBone->mNumWeights = 0;
  274. }
  275. ai_assert( newMesh->mNumBones == numBones );
  276. // iterate over all new vertices and count which bones affected its old vertex in the source mesh
  277. for( size_t a = 0; a < numSubMeshVertices; ++a )
  278. {
  279. size_t oldIndex = previousVertexIndices[a];
  280. const std::vector<BoneWeight>& bonesOnThisVertex = vertexBones[oldIndex];
  281. for( size_t b = 0; b < bonesOnThisVertex.size(); ++b )
  282. {
  283. size_t newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
  284. if( newBoneIndex != SIZE_MAX )
  285. newMesh->mBones[newBoneIndex]->mNumWeights++;
  286. }
  287. }
  288. // allocate all bone weight arrays accordingly
  289. for( size_t a = 0; a < newMesh->mNumBones; ++a )
  290. {
  291. aiBone* bone = newMesh->mBones[a];
  292. ai_assert( bone->mNumWeights > 0 );
  293. bone->mWeights = new aiVertexWeight[bone->mNumWeights];
  294. bone->mNumWeights = 0; // for counting up in the next step
  295. }
  296. // now copy all the bone vertex weights for all the vertices which made it into the new submesh
  297. for( size_t a = 0; a < numSubMeshVertices; ++a)
  298. {
  299. // find the source vertex for it in the source mesh
  300. size_t previousIndex = previousVertexIndices[a];
  301. // these bones were affecting it
  302. const std::vector<BoneWeight>& bonesOnThisVertex = vertexBones[previousIndex];
  303. // all of the bones affecting it should be present in the new submesh, or else
  304. // the face it comprises shouldn't be present
  305. for( size_t b = 0; b < bonesOnThisVertex.size(); ++b)
  306. {
  307. size_t newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
  308. ai_assert( newBoneIndex != SIZE_MAX );
  309. aiVertexWeight* dstWeight = newMesh->mBones[newBoneIndex]->mWeights + newMesh->mBones[newBoneIndex]->mNumWeights;
  310. newMesh->mBones[newBoneIndex]->mNumWeights++;
  311. dstWeight->mVertexId = a;
  312. dstWeight->mWeight = bonesOnThisVertex[b].second;
  313. }
  314. }
  315. // I have the strange feeling that this will break apart at some point in time...
  316. }
  317. }
  318. // ------------------------------------------------------------------------------------------------
  319. // Recursively updates the node's mesh list to account for the changed mesh list
  320. void SplitByBoneCountProcess::UpdateNode( aiNode* pNode) const
  321. {
  322. // rebuild the node's mesh index list
  323. if( pNode->mNumMeshes > 0 )
  324. {
  325. std::vector<size_t> newMeshList;
  326. for( size_t a = 0; a < pNode->mNumMeshes; ++a)
  327. {
  328. size_t srcIndex = pNode->mMeshes[a];
  329. const std::vector<size_t>& replaceMeshes = mSubMeshIndices[srcIndex];
  330. newMeshList.insert( newMeshList.end(), replaceMeshes.begin(), replaceMeshes.end());
  331. }
  332. delete pNode->mMeshes;
  333. pNode->mNumMeshes = newMeshList.size();
  334. pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
  335. std::copy( newMeshList.begin(), newMeshList.end(), pNode->mMeshes);
  336. }
  337. // do that also recursively for all children
  338. for( size_t a = 0; a < pNode->mNumChildren; ++a )
  339. {
  340. UpdateNode( pNode->mChildren[a]);
  341. }
  342. }