FindDegenerates.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file FindDegenerates.cpp
  35. * @brief Implementation of the FindDegenerates post-process step.
  36. */
  37. #include "ProcessHelper.h"
  38. #include "FindDegenerates.h"
  39. #include <assimp/Exceptional.h>
  40. #include <unordered_map>
  41. using namespace Assimp;
  42. // Correct node indices to meshes and remove references to deleted mesh
  43. static void updateSceneGraph(aiNode* pNode, const std::unordered_map<unsigned int, unsigned int>& meshMap);
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. FindDegeneratesProcess::FindDegeneratesProcess() :
  47. mConfigRemoveDegenerates( false ),
  48. mConfigCheckAreaOfTriangle( false ){
  49. // empty
  50. }
  51. // ------------------------------------------------------------------------------------------------
  52. // Destructor, private as well
  53. FindDegeneratesProcess::~FindDegeneratesProcess() {
  54. // nothing to do here
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Returns whether the processing step is present in the given flag field.
  58. bool FindDegeneratesProcess::IsActive( unsigned int pFlags) const {
  59. return 0 != (pFlags & aiProcess_FindDegenerates);
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // Setup import configuration
  63. void FindDegeneratesProcess::SetupProperties(const Importer* pImp) {
  64. // Get the current value of AI_CONFIG_PP_FD_REMOVE
  65. mConfigRemoveDegenerates = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_REMOVE,0));
  66. mConfigCheckAreaOfTriangle = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_CHECKAREA) );
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Executes the post processing step on the given imported data.
  70. void FindDegeneratesProcess::Execute( aiScene* pScene) {
  71. ASSIMP_LOG_DEBUG("FindDegeneratesProcess begin");
  72. if ( nullptr == pScene) {
  73. return;
  74. }
  75. std::unordered_map<unsigned int, unsigned int> meshMap;
  76. meshMap.reserve(pScene->mNumMeshes);
  77. const unsigned int originalNumMeshes = pScene->mNumMeshes;
  78. unsigned int targetIndex = 0;
  79. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  80. // Do not process point cloud, ExecuteOnMesh works only with faces data
  81. if ((pScene->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType::aiPrimitiveType_POINT) && ExecuteOnMesh(pScene->mMeshes[i])) {
  82. delete pScene->mMeshes[i];
  83. // Not strictly required, but clean:
  84. pScene->mMeshes[i] = nullptr;
  85. } else {
  86. meshMap[i] = targetIndex;
  87. pScene->mMeshes[targetIndex] = pScene->mMeshes[i];
  88. ++targetIndex;
  89. }
  90. }
  91. pScene->mNumMeshes = targetIndex;
  92. if (meshMap.size() < originalNumMeshes) {
  93. updateSceneGraph(pScene->mRootNode, meshMap);
  94. }
  95. ASSIMP_LOG_DEBUG("FindDegeneratesProcess finished");
  96. }
  97. static void updateSceneGraph(aiNode* pNode, const std::unordered_map<unsigned int, unsigned int>& meshMap) {
  98. unsigned int targetIndex = 0;
  99. for (unsigned i = 0; i < pNode->mNumMeshes; ++i) {
  100. const unsigned int sourceMeshIndex = pNode->mMeshes[i];
  101. auto it = meshMap.find(sourceMeshIndex);
  102. if (it != meshMap.end()) {
  103. pNode->mMeshes[targetIndex] = it->second;
  104. ++targetIndex;
  105. }
  106. }
  107. pNode->mNumMeshes = targetIndex;
  108. //recurse to all children
  109. for (unsigned i = 0; i < pNode->mNumChildren; ++i) {
  110. updateSceneGraph(pNode->mChildren[i], meshMap);
  111. }
  112. }
  113. static ai_real heron( ai_real a, ai_real b, ai_real c ) {
  114. ai_real s = (a + b + c) / 2;
  115. ai_real area = pow((s * ( s - a ) * ( s - b ) * ( s - c ) ), (ai_real)0.5 );
  116. return area;
  117. }
  118. static ai_real distance3D( const aiVector3D &vA, aiVector3D &vB ) {
  119. const ai_real lx = ( vB.x - vA.x );
  120. const ai_real ly = ( vB.y - vA.y );
  121. const ai_real lz = ( vB.z - vA.z );
  122. ai_real a = lx*lx + ly*ly + lz*lz;
  123. ai_real d = pow( a, (ai_real)0.5 );
  124. return d;
  125. }
  126. static ai_real calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh ) {
  127. ai_real area = 0;
  128. aiVector3D vA( mesh->mVertices[ face.mIndices[ 0 ] ] );
  129. aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] );
  130. aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] );
  131. ai_real a( distance3D( vA, vB ) );
  132. ai_real b( distance3D( vB, vC ) );
  133. ai_real c( distance3D( vC, vA ) );
  134. area = heron( a, b, c );
  135. return area;
  136. }
  137. // ------------------------------------------------------------------------------------------------
  138. // Executes the post processing step on the given imported mesh
  139. bool FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh) {
  140. mesh->mPrimitiveTypes = 0;
  141. std::vector<bool> remove_me;
  142. if (mConfigRemoveDegenerates) {
  143. remove_me.resize( mesh->mNumFaces, false );
  144. }
  145. unsigned int deg = 0, limit;
  146. for ( unsigned int a = 0; a < mesh->mNumFaces; ++a ) {
  147. aiFace& face = mesh->mFaces[a];
  148. bool first = true;
  149. // check whether the face contains degenerated entries
  150. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  151. // Polygons with more than 4 points are allowed to have double points, that is
  152. // simulating polygons with holes just with concave polygons. However,
  153. // double points may not come directly after another.
  154. limit = face.mNumIndices;
  155. if (face.mNumIndices > 4) {
  156. limit = std::min( limit, i+2 );
  157. }
  158. for (unsigned int t = i+1; t < limit; ++t) {
  159. if (mesh->mVertices[face.mIndices[ i ] ] == mesh->mVertices[ face.mIndices[ t ] ]) {
  160. // we have found a matching vertex position
  161. // remove the corresponding index from the array
  162. --face.mNumIndices;
  163. --limit;
  164. for (unsigned int m = t; m < face.mNumIndices; ++m) {
  165. face.mIndices[ m ] = face.mIndices[ m+1 ];
  166. }
  167. --t;
  168. // NOTE: we set the removed vertex index to an unique value
  169. // to make sure the developer gets notified when his
  170. // application attempts to access this data.
  171. face.mIndices[ face.mNumIndices ] = 0xdeadbeef;
  172. if(first) {
  173. ++deg;
  174. first = false;
  175. }
  176. if ( mConfigRemoveDegenerates ) {
  177. remove_me[ a ] = true;
  178. goto evil_jump_outside; // hrhrhrh ... yeah, this rocks baby!
  179. }
  180. }
  181. }
  182. if ( mConfigCheckAreaOfTriangle ) {
  183. if ( face.mNumIndices == 3 ) {
  184. ai_real area = calculateAreaOfTriangle( face, mesh );
  185. if (area < ai_epsilon) {
  186. if ( mConfigRemoveDegenerates ) {
  187. remove_me[ a ] = true;
  188. ++deg;
  189. goto evil_jump_outside;
  190. }
  191. // todo: check for index which is corrupt.
  192. }
  193. }
  194. }
  195. }
  196. // We need to update the primitive flags array of the mesh.
  197. switch (face.mNumIndices)
  198. {
  199. case 1u:
  200. mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  201. break;
  202. case 2u:
  203. mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  204. break;
  205. case 3u:
  206. mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  207. break;
  208. default:
  209. mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  210. break;
  211. };
  212. evil_jump_outside:
  213. continue;
  214. }
  215. // If AI_CONFIG_PP_FD_REMOVE is true, remove degenerated faces from the import
  216. if (mConfigRemoveDegenerates && deg) {
  217. unsigned int n = 0;
  218. for (unsigned int a = 0; a < mesh->mNumFaces; ++a)
  219. {
  220. aiFace& face_src = mesh->mFaces[a];
  221. if (!remove_me[a]) {
  222. aiFace& face_dest = mesh->mFaces[n++];
  223. // Do a manual copy, keep the index array
  224. face_dest.mNumIndices = face_src.mNumIndices;
  225. face_dest.mIndices = face_src.mIndices;
  226. if (&face_src != &face_dest) {
  227. // clear source
  228. face_src.mNumIndices = 0;
  229. face_src.mIndices = nullptr;
  230. }
  231. }
  232. else {
  233. // Otherwise delete it if we don't need this face
  234. delete[] face_src.mIndices;
  235. face_src.mIndices = nullptr;
  236. face_src.mNumIndices = 0;
  237. }
  238. }
  239. // Just leave the rest of the array unreferenced, we don't care for now
  240. mesh->mNumFaces = n;
  241. if (!mesh->mNumFaces) {
  242. //The whole mesh consists of degenerated faces
  243. //signal upward, that this mesh should be deleted.
  244. ASSIMP_LOG_VERBOSE_DEBUG("FindDegeneratesProcess removed a mesh full of degenerated primitives");
  245. return true;
  246. }
  247. }
  248. if (deg && !DefaultLogger::isNullLogger()) {
  249. ASSIMP_LOG_WARN( "Found ", deg, " degenerated primitives");
  250. }
  251. return false;
  252. }