utPretransformVertices.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "PostProcessing/PretransformVertices.h"
  36. #include <assimp/scene.h>
  37. using namespace std;
  38. using namespace Assimp;
  39. class PretransformVerticesTest : public ::testing::Test {
  40. public:
  41. PretransformVerticesTest() :
  42. Test(), mScene(nullptr), mProcess(nullptr) {
  43. // empty
  44. }
  45. protected:
  46. virtual void SetUp();
  47. virtual void TearDown();
  48. protected:
  49. aiScene *mScene;
  50. PretransformVertices *mProcess;
  51. };
  52. // ------------------------------------------------------------------------------------------------
  53. void AddNodes(unsigned int num, aiNode *father, unsigned int depth) {
  54. father->mChildren = new aiNode *[father->mNumChildren = 5];
  55. for (unsigned int i = 0; i < 5; ++i) {
  56. aiNode *nd = father->mChildren[i] = new aiNode();
  57. nd->mName.length = snprintf(nd->mName.data, AI_MAXLEN, "%i%i", depth, i);
  58. // spawn two meshes
  59. nd->mMeshes = new unsigned int[nd->mNumMeshes = 2];
  60. nd->mMeshes[0] = num * 5 + i;
  61. nd->mMeshes[1] = 24 - (num * 5 + i); // mesh 12 is special ... it references the same mesh twice
  62. // setup an unique transformation matrix
  63. nd->mTransformation.a1 = num * 5.f + i + 1;
  64. }
  65. if (depth > 1) {
  66. for (unsigned int i = 0; i < 5; ++i) {
  67. AddNodes(i, father->mChildren[i], depth - 1);
  68. }
  69. }
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. void PretransformVerticesTest::SetUp() {
  73. mScene = new aiScene();
  74. // add 5 empty materials
  75. mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials = 5];
  76. for (unsigned int i = 0; i < 5; ++i) {
  77. mScene->mMaterials[i] = new aiMaterial();
  78. }
  79. // add 25 test meshes
  80. mScene->mMeshes = new aiMesh *[mScene->mNumMeshes = 25];
  81. for (unsigned int i = 0; i < 25; ++i) {
  82. aiMesh *mesh = mScene->mMeshes[i] = new aiMesh();
  83. mesh->mPrimitiveTypes = aiPrimitiveType_POINT;
  84. mesh->mFaces = new aiFace[mesh->mNumFaces = 10 + i];
  85. mesh->mVertices = new aiVector3D[mesh->mNumVertices = mesh->mNumFaces];
  86. for (unsigned int a = 0; a < mesh->mNumFaces; ++a) {
  87. aiFace &f = mesh->mFaces[a];
  88. f.mIndices = new unsigned int[f.mNumIndices = 1];
  89. f.mIndices[0] = a * 3;
  90. mesh->mVertices[a] = aiVector3D((float)i, (float)a, 0.f);
  91. }
  92. mesh->mMaterialIndex = i % 5;
  93. if (i % 2) {
  94. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  95. for (unsigned int normalIdx = 0; normalIdx < mesh->mNumVertices; ++normalIdx) {
  96. mesh->mNormals[normalIdx].x = 1.0f;
  97. mesh->mNormals[normalIdx].y = 1.0f;
  98. mesh->mNormals[normalIdx].z = 1.0f;
  99. mesh->mNormals[normalIdx].Normalize();
  100. }
  101. }
  102. }
  103. // construct some nodes (1+25)
  104. mScene->mRootNode = new aiNode();
  105. mScene->mRootNode->mName.Set("Root");
  106. AddNodes(0, mScene->mRootNode, 2);
  107. mProcess = new PretransformVertices();
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. void PretransformVerticesTest::TearDown() {
  111. delete mScene;
  112. delete mProcess;
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. TEST_F(PretransformVerticesTest, testProcessCollapseHierarchy) {
  116. mProcess->KeepHierarchy(false);
  117. mProcess->Execute(mScene);
  118. EXPECT_EQ(5U, mScene->mNumMaterials);
  119. EXPECT_EQ(10U, mScene->mNumMeshes); // every second mesh has normals
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. TEST_F(PretransformVerticesTest, testProcessKeepHierarchy) {
  123. mProcess->KeepHierarchy(true);
  124. mProcess->Execute(mScene);
  125. EXPECT_EQ(5U, mScene->mNumMaterials);
  126. EXPECT_EQ(49U, mScene->mNumMeshes); // see note on mesh 12 above
  127. }