FindDegenerates.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. // internal headers
  38. #include "ProcessHelper.h"
  39. #include "FindDegenerates.h"
  40. #include <assimp/Exceptional.h>
  41. using namespace Assimp;
  42. //remove mesh at position 'index' from the scene
  43. static void removeMesh(aiScene* pScene, unsigned const index);
  44. //correct node indices to meshes and remove references to deleted mesh
  45. static void updateSceneGraph(aiNode* pNode, unsigned const index);
  46. // ------------------------------------------------------------------------------------------------
  47. // Constructor to be privately used by Importer
  48. FindDegeneratesProcess::FindDegeneratesProcess()
  49. : mConfigRemoveDegenerates( false )
  50. , mConfigCheckAreaOfTriangle( false ){
  51. // empty
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Destructor, private as well
  55. FindDegeneratesProcess::~FindDegeneratesProcess() {
  56. // nothing to do here
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Returns whether the processing step is present in the given flag field.
  60. bool FindDegeneratesProcess::IsActive( unsigned int pFlags) const {
  61. return 0 != (pFlags & aiProcess_FindDegenerates);
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Setup import configuration
  65. void FindDegeneratesProcess::SetupProperties(const Importer* pImp) {
  66. // Get the current value of AI_CONFIG_PP_FD_REMOVE
  67. mConfigRemoveDegenerates = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_REMOVE,0));
  68. mConfigCheckAreaOfTriangle = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_CHECKAREA) );
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Executes the post processing step on the given imported data.
  72. void FindDegeneratesProcess::Execute( aiScene* pScene) {
  73. ASSIMP_LOG_DEBUG("FindDegeneratesProcess begin");
  74. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  75. {
  76. //Do not process point cloud, ExecuteOnMesh works only with faces data
  77. if ((pScene->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType::aiPrimitiveType_POINT) && ExecuteOnMesh(pScene->mMeshes[i])) {
  78. removeMesh(pScene, i);
  79. --i; //the current i is removed, do not skip the next one
  80. }
  81. }
  82. ASSIMP_LOG_DEBUG("FindDegeneratesProcess finished");
  83. }
  84. static void removeMesh(aiScene* pScene, unsigned const index) {
  85. //we start at index and copy the pointers one position forward
  86. //save the mesh pointer to delete it later
  87. auto delete_me = pScene->mMeshes[index];
  88. for (unsigned i = index; i < pScene->mNumMeshes - 1; ++i) {
  89. pScene->mMeshes[i] = pScene->mMeshes[i+1];
  90. }
  91. pScene->mMeshes[pScene->mNumMeshes - 1] = nullptr;
  92. --(pScene->mNumMeshes);
  93. delete delete_me;
  94. //removing a mesh also requires updating all references to it in the scene graph
  95. updateSceneGraph(pScene->mRootNode, index);
  96. }
  97. static void updateSceneGraph(aiNode* pNode, unsigned const index) {
  98. for (unsigned i = 0; i < pNode->mNumMeshes; ++i) {
  99. if (pNode->mMeshes[i] > index) {
  100. --(pNode->mMeshes[i]);
  101. continue;
  102. }
  103. if (pNode->mMeshes[i] == index) {
  104. for (unsigned j = i; j < pNode->mNumMeshes -1; ++j) {
  105. pNode->mMeshes[j] = pNode->mMeshes[j+1];
  106. }
  107. --(pNode->mNumMeshes);
  108. --i;
  109. continue;
  110. }
  111. }
  112. //recurse to all children
  113. for (unsigned i = 0; i < pNode->mNumChildren; ++i) {
  114. updateSceneGraph(pNode->mChildren[i], index);
  115. }
  116. }
  117. static ai_real heron( ai_real a, ai_real b, ai_real c ) {
  118. ai_real s = (a + b + c) / 2;
  119. ai_real area = pow((s * ( s - a ) * ( s - b ) * ( s - c ) ), (ai_real)0.5 );
  120. return area;
  121. }
  122. static ai_real distance3D( const aiVector3D &vA, aiVector3D &vB ) {
  123. const ai_real lx = ( vB.x - vA.x );
  124. const ai_real ly = ( vB.y - vA.y );
  125. const ai_real lz = ( vB.z - vA.z );
  126. ai_real a = lx*lx + ly*ly + lz*lz;
  127. ai_real d = pow( a, (ai_real)0.5 );
  128. return d;
  129. }
  130. static ai_real calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh ) {
  131. ai_real area = 0;
  132. aiVector3D vA( mesh->mVertices[ face.mIndices[ 0 ] ] );
  133. aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] );
  134. aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] );
  135. ai_real a( distance3D( vA, vB ) );
  136. ai_real b( distance3D( vB, vC ) );
  137. ai_real c( distance3D( vC, vA ) );
  138. area = heron( a, b, c );
  139. return area;
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. // Executes the post processing step on the given imported mesh
  143. bool FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh) {
  144. mesh->mPrimitiveTypes = 0;
  145. std::vector<bool> remove_me;
  146. if (mConfigRemoveDegenerates) {
  147. remove_me.resize( mesh->mNumFaces, false );
  148. }
  149. unsigned int deg = 0, limit;
  150. for ( unsigned int a = 0; a < mesh->mNumFaces; ++a ) {
  151. aiFace& face = mesh->mFaces[a];
  152. bool first = true;
  153. // check whether the face contains degenerated entries
  154. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  155. // Polygons with more than 4 points are allowed to have double points, that is
  156. // simulating polygons with holes just with concave polygons. However,
  157. // double points may not come directly after another.
  158. limit = face.mNumIndices;
  159. if (face.mNumIndices > 4) {
  160. limit = std::min( limit, i+2 );
  161. }
  162. for (unsigned int t = i+1; t < limit; ++t) {
  163. if (mesh->mVertices[face.mIndices[ i ] ] == mesh->mVertices[ face.mIndices[ t ] ]) {
  164. // we have found a matching vertex position
  165. // remove the corresponding index from the array
  166. --face.mNumIndices;
  167. --limit;
  168. for (unsigned int m = t; m < face.mNumIndices; ++m) {
  169. face.mIndices[ m ] = face.mIndices[ m+1 ];
  170. }
  171. --t;
  172. // NOTE: we set the removed vertex index to an unique value
  173. // to make sure the developer gets notified when his
  174. // application attempts to access this data.
  175. face.mIndices[ face.mNumIndices ] = 0xdeadbeef;
  176. if(first) {
  177. ++deg;
  178. first = false;
  179. }
  180. if ( mConfigRemoveDegenerates ) {
  181. remove_me[ a ] = true;
  182. goto evil_jump_outside; // hrhrhrh ... yeah, this rocks baby!
  183. }
  184. }
  185. }
  186. if ( mConfigCheckAreaOfTriangle ) {
  187. if ( face.mNumIndices == 3 ) {
  188. ai_real area = calculateAreaOfTriangle( face, mesh );
  189. if ( area < 1e-6 ) {
  190. if ( mConfigRemoveDegenerates ) {
  191. remove_me[ a ] = true;
  192. ++deg;
  193. goto evil_jump_outside;
  194. }
  195. // todo: check for index which is corrupt.
  196. }
  197. }
  198. }
  199. }
  200. // We need to update the primitive flags array of the mesh.
  201. switch (face.mNumIndices)
  202. {
  203. case 1u:
  204. mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  205. break;
  206. case 2u:
  207. mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  208. break;
  209. case 3u:
  210. mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  211. break;
  212. default:
  213. mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  214. break;
  215. };
  216. evil_jump_outside:
  217. continue;
  218. }
  219. // If AI_CONFIG_PP_FD_REMOVE is true, remove degenerated faces from the import
  220. if (mConfigRemoveDegenerates && deg) {
  221. unsigned int n = 0;
  222. for (unsigned int a = 0; a < mesh->mNumFaces; ++a)
  223. {
  224. aiFace& face_src = mesh->mFaces[a];
  225. if (!remove_me[a]) {
  226. aiFace& face_dest = mesh->mFaces[n++];
  227. // Do a manual copy, keep the index array
  228. face_dest.mNumIndices = face_src.mNumIndices;
  229. face_dest.mIndices = face_src.mIndices;
  230. if (&face_src != &face_dest) {
  231. // clear source
  232. face_src.mNumIndices = 0;
  233. face_src.mIndices = nullptr;
  234. }
  235. }
  236. else {
  237. // Otherwise delete it if we don't need this face
  238. delete[] face_src.mIndices;
  239. face_src.mIndices = nullptr;
  240. face_src.mNumIndices = 0;
  241. }
  242. }
  243. // Just leave the rest of the array unreferenced, we don't care for now
  244. mesh->mNumFaces = n;
  245. if (!mesh->mNumFaces) {
  246. //The whole mesh consists of degenerated faces
  247. //signal upward, that this mesh should be deleted.
  248. ASSIMP_LOG_VERBOSE_DEBUG("FindDegeneratesProcess removed a mesh full of degenerated primitives");
  249. return true;
  250. }
  251. }
  252. if (deg && !DefaultLogger::isNullLogger()) {
  253. ASSIMP_LOG_WARN_F( "Found ", deg, " degenerated primitives");
  254. }
  255. return false;
  256. }