You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

404 lines
14 KiB

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