SplitByBoneCountProcess.cpp 17 KB

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