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.
 
 
 
 
 
 

217 lines
6.9 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 "AssimpPCH.h"
  38. // internal headers
  39. #include "ProcessHelper.h"
  40. #include "FindDegenerates.h"
  41. using namespace Assimp;
  42. // ------------------------------------------------------------------------------------------------
  43. // Constructor to be privately used by Importer
  44. FindDegeneratesProcess::FindDegeneratesProcess()
  45. : configRemoveDegenerates (false)
  46. {}
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. FindDegeneratesProcess::~FindDegeneratesProcess()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Returns whether the processing step is present in the given flag field.
  55. bool FindDegeneratesProcess::IsActive( unsigned int pFlags) const
  56. {
  57. return 0 != (pFlags & aiProcess_FindDegenerates);
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Setup import configuration
  61. void FindDegeneratesProcess::SetupProperties(const Importer* pImp)
  62. {
  63. // Get the current value of AI_CONFIG_PP_FD_REMOVE
  64. configRemoveDegenerates = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_FD_REMOVE,0));
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Executes the post processing step on the given imported data.
  68. void FindDegeneratesProcess::Execute( aiScene* pScene)
  69. {
  70. DefaultLogger::get()->debug("FindDegeneratesProcess begin");
  71. for (unsigned int i = 0; i < pScene->mNumMeshes;++i){
  72. ExecuteOnMesh( pScene->mMeshes[i]);
  73. }
  74. DefaultLogger::get()->debug("FindDegeneratesProcess finished");
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Executes the post processing step on the given imported mesh
  78. void FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh)
  79. {
  80. mesh->mPrimitiveTypes = 0;
  81. std::vector<bool> remove_me;
  82. if (configRemoveDegenerates)
  83. remove_me.resize(mesh->mNumFaces,false);
  84. unsigned int deg = 0, limit;
  85. for (unsigned int a = 0; a < mesh->mNumFaces; ++a)
  86. {
  87. aiFace& face = mesh->mFaces[a];
  88. bool first = true;
  89. // check whether the face contains degenerated entries
  90. for (register unsigned int i = 0; i < face.mNumIndices; ++i)
  91. {
  92. // Polygons with more than 4 points are allowed to have double points, that is
  93. // simulating polygons with holes just with concave polygons. However,
  94. // double points may not come directly after another.
  95. limit = face.mNumIndices;
  96. if (face.mNumIndices > 4)
  97. limit = std::min(limit,i+2);
  98. for (register unsigned int t = i+1; t < limit; ++t)
  99. {
  100. if (mesh->mVertices[face.mIndices[i]] == mesh->mVertices[face.mIndices[t]])
  101. {
  102. // we have found a matching vertex position
  103. // remove the corresponding index from the array
  104. --face.mNumIndices;--limit;
  105. for (unsigned int m = t; m < face.mNumIndices; ++m)
  106. {
  107. face.mIndices[m] = face.mIndices[m+1];
  108. }
  109. --t;
  110. // NOTE: we set the removed vertex index to an unique value
  111. // to make sure the developer gets notified when his
  112. // application attemps to access this data.
  113. face.mIndices[face.mNumIndices] = 0xdeadbeef;
  114. if(first)
  115. {
  116. ++deg;
  117. first = false;
  118. }
  119. if (configRemoveDegenerates) {
  120. remove_me[a] = true;
  121. goto evil_jump_outside; // hrhrhrh ... yeah, this rocks baby!
  122. }
  123. }
  124. }
  125. }
  126. // We need to update the primitive flags array of the mesh.
  127. switch (face.mNumIndices)
  128. {
  129. case 1u:
  130. mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  131. break;
  132. case 2u:
  133. mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  134. break;
  135. case 3u:
  136. mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  137. break;
  138. default:
  139. mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  140. break;
  141. };
  142. evil_jump_outside:
  143. continue;
  144. }
  145. // If AI_CONFIG_PP_FD_REMOVE is true, remove degenerated faces from the import
  146. if (configRemoveDegenerates && deg) {
  147. unsigned int n = 0;
  148. for (unsigned int a = 0; a < mesh->mNumFaces; ++a)
  149. {
  150. aiFace& face_src = mesh->mFaces[a];
  151. if (!remove_me[a]) {
  152. aiFace& face_dest = mesh->mFaces[n++];
  153. // Do a manual copy, keep the index array
  154. face_dest.mNumIndices = face_src.mNumIndices;
  155. face_dest.mIndices = face_src.mIndices;
  156. if (&face_src != &face_dest) {
  157. // clear source
  158. face_src.mNumIndices = 0;
  159. face_src.mIndices = NULL;
  160. }
  161. }
  162. else {
  163. // Otherwise delete it if we don't need this face
  164. delete[] face_src.mIndices;
  165. face_src.mIndices = NULL;
  166. face_src.mNumIndices = 0;
  167. }
  168. }
  169. // Just leave the rest of the array unreferenced, we don't care for now
  170. mesh->mNumFaces = n;
  171. if (!mesh->mNumFaces) {
  172. // WTF!?
  173. // OK ... for completeness and because I'm not yet tired,
  174. // let's write code that willl hopefully never be called
  175. // (famous last words)
  176. // OK ... bad idea.
  177. throw DeadlyImportError("Mesh is empty after removal of degenerated primitives ... WTF!?");
  178. }
  179. }
  180. if (deg && !DefaultLogger::isNullLogger())
  181. {
  182. char s[64];
  183. ASSIMP_itoa10(s,deg);
  184. DefaultLogger::get()->warn(std::string("Found ") + s + " degenerated primitives");
  185. }
  186. }