SplitByBoneCountProcess.cpp 18 KB

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