utFindDegenerates.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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. #include "UnitTestPCH.h"
  35. #include "../../include/assimp/scene.h"
  36. #include "PostProcessing/FindDegenerates.h"
  37. #include <memory>
  38. using namespace std;
  39. using namespace Assimp;
  40. class FindDegeneratesProcessTest : public ::testing::Test {
  41. public:
  42. FindDegeneratesProcessTest() :
  43. Test(), mMesh(nullptr), mProcess(nullptr) {
  44. // empty
  45. }
  46. protected:
  47. virtual void SetUp();
  48. virtual void TearDown();
  49. protected:
  50. aiMesh *mMesh;
  51. FindDegeneratesProcess *mProcess;
  52. };
  53. void FindDegeneratesProcessTest::SetUp() {
  54. mMesh = new aiMesh();
  55. mProcess = new FindDegeneratesProcess();
  56. mMesh->mNumFaces = 1000;
  57. mMesh->mFaces = new aiFace[1000];
  58. mMesh->mNumVertices = 5000 * 2;
  59. mMesh->mVertices = new aiVector3D[5000 * 2];
  60. for (unsigned int i = 0; i < 5000; ++i) {
  61. mMesh->mVertices[i] = mMesh->mVertices[i + 5000] = aiVector3D((float)i);
  62. }
  63. mMesh->mPrimitiveTypes = aiPrimitiveType_LINE | aiPrimitiveType_POINT |
  64. aiPrimitiveType_POLYGON | aiPrimitiveType_TRIANGLE;
  65. unsigned int numOut = 0, numFaces = 0;
  66. for (unsigned int i = 0; i < 1000; ++i) {
  67. aiFace &f = mMesh->mFaces[i];
  68. f.mNumIndices = (i % 5) + 1; // between 1 and 5
  69. f.mIndices = new unsigned int[f.mNumIndices];
  70. bool had = false;
  71. for (unsigned int n = 0; n < f.mNumIndices; ++n) {
  72. // FIXME
  73. #if 0
  74. // some duplicate indices
  75. if ( n && n == (i / 200)+1) {
  76. f.mIndices[n] = f.mIndices[n-1];
  77. had = true;
  78. }
  79. // and some duplicate vertices
  80. #endif
  81. if (n && i % 2 && 0 == n % 2) {
  82. f.mIndices[n] = f.mIndices[n - 1] + 5000;
  83. had = true;
  84. } else {
  85. f.mIndices[n] = numOut++;
  86. }
  87. }
  88. if (!had)
  89. ++numFaces;
  90. }
  91. mMesh->mNumUVComponents[0] = numOut;
  92. mMesh->mNumUVComponents[1] = numFaces;
  93. }
  94. void FindDegeneratesProcessTest::TearDown() {
  95. delete mMesh;
  96. delete mProcess;
  97. }
  98. TEST_F(FindDegeneratesProcessTest, testDegeneratesDetection) {
  99. mProcess->EnableInstantRemoval(false);
  100. mProcess->ExecuteOnMesh(mMesh);
  101. unsigned int out = 0;
  102. for (unsigned int i = 0; i < 1000; ++i) {
  103. aiFace &f = mMesh->mFaces[i];
  104. out += f.mNumIndices;
  105. }
  106. EXPECT_EQ(1000U, mMesh->mNumFaces);
  107. EXPECT_EQ(10000U, mMesh->mNumVertices);
  108. EXPECT_EQ(out, mMesh->mNumUVComponents[0]);
  109. EXPECT_EQ(static_cast<unsigned int>(
  110. aiPrimitiveType_LINE | aiPrimitiveType_POINT |
  111. aiPrimitiveType_POLYGON | aiPrimitiveType_TRIANGLE),
  112. mMesh->mPrimitiveTypes);
  113. }
  114. TEST_F(FindDegeneratesProcessTest, testDegeneratesRemoval) {
  115. mProcess->EnableAreaCheck(false);
  116. mProcess->EnableInstantRemoval(true);
  117. mProcess->ExecuteOnMesh(mMesh);
  118. EXPECT_EQ(mMesh->mNumUVComponents[1], mMesh->mNumFaces);
  119. }
  120. TEST_F(FindDegeneratesProcessTest, testDegeneratesRemovalWithAreaCheck) {
  121. mProcess->EnableAreaCheck(true);
  122. mProcess->EnableInstantRemoval(true);
  123. mProcess->ExecuteOnMesh(mMesh);
  124. EXPECT_EQ(mMesh->mNumUVComponents[1] - 100, mMesh->mNumFaces);
  125. }
  126. namespace
  127. {
  128. std::unique_ptr<aiMesh> getDegenerateMesh()
  129. {
  130. std::unique_ptr<aiMesh> mesh(new aiMesh);
  131. mesh->mNumVertices = 2;
  132. mesh->mVertices = new aiVector3D[2];
  133. mesh->mVertices[0] = aiVector3D{ 0.0f, 0.0f, 0.0f };
  134. mesh->mVertices[1] = aiVector3D{ 1.0f, 0.0f, 0.0f };
  135. mesh->mNumFaces = 1;
  136. mesh->mFaces = new aiFace[1];
  137. mesh->mFaces[0].mNumIndices = 3;
  138. mesh->mFaces[0].mIndices = new unsigned int[3];
  139. mesh->mFaces[0].mIndices[0] = 0;
  140. mesh->mFaces[0].mIndices[1] = 1;
  141. mesh->mFaces[0].mIndices[2] = 0;
  142. return mesh;
  143. }
  144. }
  145. TEST_F(FindDegeneratesProcessTest, meshRemoval) {
  146. mProcess->EnableAreaCheck(true);
  147. mProcess->EnableInstantRemoval(true);
  148. mProcess->ExecuteOnMesh(mMesh);
  149. std::unique_ptr<aiScene> scene(new aiScene);
  150. scene->mNumMeshes = 5;
  151. scene->mMeshes = new aiMesh*[5];
  152. /// Use the mesh which doesn't get completely stripped of faces from the main test.
  153. aiMesh* meshWhichSurvives = mMesh;
  154. mMesh = nullptr;
  155. scene->mMeshes[0] = getDegenerateMesh().release();
  156. scene->mMeshes[1] = getDegenerateMesh().release();
  157. scene->mMeshes[2] = meshWhichSurvives;
  158. scene->mMeshes[3] = getDegenerateMesh().release();
  159. scene->mMeshes[4] = getDegenerateMesh().release();
  160. scene->mRootNode = new aiNode;
  161. scene->mRootNode->mNumMeshes = 5;
  162. scene->mRootNode->mMeshes = new unsigned int[5];
  163. scene->mRootNode->mMeshes[0] = 0;
  164. scene->mRootNode->mMeshes[1] = 1;
  165. scene->mRootNode->mMeshes[2] = 2;
  166. scene->mRootNode->mMeshes[3] = 3;
  167. scene->mRootNode->mMeshes[4] = 4;
  168. mProcess->Execute(scene.get());
  169. EXPECT_EQ(scene->mNumMeshes, 1u);
  170. EXPECT_EQ(scene->mMeshes[0], meshWhichSurvives);
  171. EXPECT_EQ(scene->mRootNode->mNumMeshes, 1u);
  172. EXPECT_EQ(scene->mRootNode->mMeshes[0], 0u);
  173. }
  174. TEST_F(FindDegeneratesProcessTest, invalidVertexIndex) {
  175. mProcess->EnableAreaCheck(true);
  176. mProcess->EnableInstantRemoval(true);
  177. mProcess->ExecuteOnMesh(mMesh);
  178. std::unique_ptr<aiScene> scene(new aiScene);
  179. scene->mNumMeshes = 1;
  180. scene->mMeshes = new aiMesh *[1];
  181. std::unique_ptr<aiMesh> mesh(new aiMesh);
  182. mesh->mNumVertices = 1;
  183. mesh->mVertices = new aiVector3D[1];
  184. mesh->mVertices[0] = aiVector3D{ 0.0f, 0.0f, 0.0f };
  185. mesh->mNumFaces = 1;
  186. mesh->mFaces = new aiFace[1];
  187. mesh->mFaces[0].mNumIndices = 3;
  188. mesh->mFaces[0].mIndices = new unsigned int[3];
  189. mesh->mFaces[0].mIndices[0] = 0;
  190. mesh->mFaces[0].mIndices[1] = 1;
  191. mesh->mFaces[0].mIndices[2] = 99999;
  192. scene->mMeshes[0] = mesh.release();
  193. scene->mRootNode = new aiNode;
  194. scene->mRootNode->mNumMeshes = 1;
  195. scene->mRootNode->mMeshes = new unsigned int[1];
  196. scene->mRootNode->mMeshes[0] = 0;
  197. mProcess->Execute(scene.get());
  198. EXPECT_EQ(scene->mNumMeshes, 1u);
  199. EXPECT_EQ(scene->mRootNode->mNumMeshes, 1u);
  200. EXPECT_EQ(scene->mRootNode->mMeshes[0], 0u);
  201. }