utScenePreprocessor.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "Common/ScenePreprocessor.h"
  36. #include <assimp/mesh.h>
  37. #include <assimp/scene.h>
  38. #include <assimp/Importer.hpp>
  39. using namespace std;
  40. using namespace Assimp;
  41. class ScenePreprocessorTest : public ::testing::Test {
  42. public:
  43. ScenePreprocessorTest() :
  44. Test(), mScenePreprocessor(nullptr), mScene(nullptr) {
  45. // empty
  46. }
  47. protected:
  48. virtual void SetUp();
  49. virtual void TearDown();
  50. protected:
  51. void CheckIfOnly(aiMesh *p, unsigned int num, unsigned flag);
  52. void ProcessAnimation(aiAnimation *anim) { mScenePreprocessor->ProcessAnimation(anim); }
  53. void ProcessMesh(aiMesh *mesh) { mScenePreprocessor->ProcessMesh(mesh); }
  54. private:
  55. ScenePreprocessor *mScenePreprocessor;
  56. aiScene *mScene;
  57. };
  58. // ------------------------------------------------------------------------------------------------
  59. void ScenePreprocessorTest::SetUp() {
  60. // setup a dummy scene with a single node
  61. mScene = new aiScene();
  62. mScene->mRootNode = new aiNode();
  63. mScene->mRootNode->mName.Set("<test>");
  64. // add some translation
  65. mScene->mRootNode->mTransformation.a4 = 1.f;
  66. mScene->mRootNode->mTransformation.b4 = 2.f;
  67. mScene->mRootNode->mTransformation.c4 = 3.f;
  68. // and allocate a ScenePreprocessor to operate on the scene
  69. mScenePreprocessor = new ScenePreprocessor(mScene);
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. void ScenePreprocessorTest::TearDown() {
  73. delete mScenePreprocessor;
  74. delete mScene;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Check whether ProcessMesh() returns flag for a mesh that consist of primitives with num indices
  78. void ScenePreprocessorTest::CheckIfOnly(aiMesh *p, unsigned int num, unsigned int flag) {
  79. // Triangles only
  80. for (unsigned i = 0; i < p->mNumFaces; ++i) {
  81. p->mFaces[i].mNumIndices = num;
  82. }
  83. mScenePreprocessor->ProcessMesh(p);
  84. EXPECT_EQ(flag, p->mPrimitiveTypes);
  85. p->mPrimitiveTypes = 0;
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // Check whether a mesh is preprocessed correctly. Case 1: The mesh needs preprocessing
  89. TEST_F(ScenePreprocessorTest, testMeshPreprocessingPos) {
  90. aiMesh *p = new aiMesh;
  91. p->mNumFaces = 100;
  92. p->mFaces = new aiFace[p->mNumFaces];
  93. p->mTextureCoords[0] = new aiVector3D[10];
  94. p->mNumUVComponents[0] = 0;
  95. p->mNumUVComponents[1] = 0;
  96. CheckIfOnly(p, 1, aiPrimitiveType_POINT);
  97. CheckIfOnly(p, 2, aiPrimitiveType_LINE);
  98. CheckIfOnly(p, 3, aiPrimitiveType_TRIANGLE);
  99. CheckIfOnly(p, 4, aiPrimitiveType_POLYGON);
  100. CheckIfOnly(p, 1249, aiPrimitiveType_POLYGON);
  101. // Polygons and triangles mixed
  102. unsigned i;
  103. for (i = 0; i < p->mNumFaces / 2; ++i) {
  104. p->mFaces[i].mNumIndices = 3;
  105. }
  106. for (; i < p->mNumFaces - p->mNumFaces / 4; ++i) {
  107. p->mFaces[i].mNumIndices = 4;
  108. }
  109. for (; i < p->mNumFaces; ++i) {
  110. p->mFaces[i].mNumIndices = 10;
  111. }
  112. ProcessMesh(p);
  113. EXPECT_EQ(static_cast<unsigned int>(aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON),
  114. p->mPrimitiveTypes);
  115. EXPECT_EQ(2U, p->mNumUVComponents[0]);
  116. EXPECT_EQ(0U, p->mNumUVComponents[1]);
  117. delete p;
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. // Check whether a mesh is preprocessed correctly. Case 1: The mesh doesn't need preprocessing
  121. TEST_F(ScenePreprocessorTest, testMeshPreprocessingNeg) {
  122. aiMesh *p = new aiMesh;
  123. p->mPrimitiveTypes = aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON;
  124. ProcessMesh(p);
  125. // should be unmodified
  126. EXPECT_EQ(static_cast<unsigned int>(aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON),
  127. p->mPrimitiveTypes);
  128. delete p;
  129. }
  130. // ------------------------------------------------------------------------------------------------
  131. // Make a dummy animation with a single channel, '<test>'
  132. aiAnimation *MakeDummyAnimation() {
  133. aiAnimation *p = new aiAnimation();
  134. p->mNumChannels = 1;
  135. p->mChannels = new aiNodeAnim *[1];
  136. aiNodeAnim *anim = p->mChannels[0] = new aiNodeAnim();
  137. anim->mNodeName.Set("<test>");
  138. return p;
  139. }
  140. // ------------------------------------------------------------------------------------------------
  141. // Check whether an anim is preprocessed correctly. Case 1: The anim needs preprocessing
  142. TEST_F(ScenePreprocessorTest, testAnimationPreprocessingPos) {
  143. aiAnimation *p = MakeDummyAnimation();
  144. aiNodeAnim *anim = p->mChannels[0];
  145. // we don't set the animation duration, but generate scaling channels
  146. anim->mNumScalingKeys = 10;
  147. anim->mScalingKeys = new aiVectorKey[10];
  148. for (unsigned int i = 0; i < 10; ++i) {
  149. anim->mScalingKeys[i].mTime = i;
  150. anim->mScalingKeys[i].mValue = aiVector3D((float)i);
  151. }
  152. ProcessAnimation(p);
  153. // we should now have a proper duration
  154. EXPECT_NEAR(p->mDuration, 9., 0.005);
  155. // ... one scaling key
  156. EXPECT_TRUE(anim->mNumPositionKeys == 1 &&
  157. anim->mPositionKeys &&
  158. anim->mPositionKeys[0].mTime == 0.0 &&
  159. anim->mPositionKeys[0].mValue == aiVector3D(1.f, 2.f, 3.f));
  160. // ... and one rotation key
  161. EXPECT_TRUE(anim->mNumRotationKeys == 1 && anim->mRotationKeys &&
  162. anim->mRotationKeys[0].mTime == 0.0);
  163. delete p;
  164. }