FindDegenerates.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the DeterminePTypeHelperProcess and
  35. * SortByPTypeProcess post-process steps.
  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. {
  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. // Executes the post processing step on the given imported data.
  61. void FindDegeneratesProcess::Execute( aiScene* pScene)
  62. {
  63. DefaultLogger::get()->debug("FindDegeneratesProcess begin");
  64. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  65. {
  66. aiMesh* mesh = pScene->mMeshes[i];
  67. mesh->mPrimitiveTypes = 0;
  68. unsigned int deg = 0;
  69. for (unsigned int a = 0; a < mesh->mNumFaces; ++a)
  70. {
  71. aiFace& face = mesh->mFaces[a];
  72. bool first = true;
  73. // check whether the face contains degenerated entries
  74. for (register unsigned int i = 0; i < face.mNumIndices; ++i)
  75. {
  76. for (register unsigned int a = i+1; a < face.mNumIndices; ++a)
  77. {
  78. if (mesh->mVertices[face.mIndices[i]] == mesh->mVertices[face.mIndices[a]])
  79. {
  80. // we have found a matching vertex position
  81. // remove the corresponding index from the array
  82. for (unsigned int m = a; m < face.mNumIndices-1; ++m)
  83. {
  84. face.mIndices[m] = face.mIndices[m+1];
  85. }
  86. --a;
  87. --face.mNumIndices;
  88. // NOTE: we set the removed vertex index to an unique value
  89. // to make sure the developer gets notified when his
  90. // application attemps to access this data.
  91. face.mIndices[face.mNumIndices] = 0xdeadbeef;
  92. if(first)
  93. {
  94. ++deg;
  95. first = false;
  96. }
  97. }
  98. }
  99. }
  100. // We need to update the primitive flags array of the mesh.
  101. // Unfortunately it is not possible to execute
  102. // FindDegenerates before DeterminePType. The latter does
  103. // nothing if the primitive flags have already been set by
  104. // the loader - our changes would be ignored. Although
  105. // we could use some tricks regarding - i.e setting
  106. // mPrimitiveTypes to 0 in every case - but this is the cleanest
  107. // way and causes no additional dependencies in the pipeline.
  108. switch (face.mNumIndices)
  109. {
  110. case 1u:
  111. mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  112. break;
  113. case 2u:
  114. mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  115. break;
  116. case 3u:
  117. mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  118. break;
  119. default:
  120. mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  121. break;
  122. };
  123. }
  124. if (deg && !DefaultLogger::isNullLogger())
  125. {
  126. char s[64];
  127. itoa10(s,deg);
  128. DefaultLogger::get()->warn(std::string("Found ") + s + " degenerated primitives");
  129. }
  130. }
  131. DefaultLogger::get()->debug("FindDegeneratesProcess finished");
  132. }