utSTLImportExport.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "AbstractImportExportBase.h"
  35. #include "UnitTestPCH.h"
  36. #include <assimp/postprocess.h>
  37. #include <assimp/scene.h>
  38. #include <assimp/Exporter.hpp>
  39. #include <assimp/Importer.hpp>
  40. #include <vector>
  41. using namespace Assimp;
  42. class utSTLImporterExporter : public AbstractImportExportBase {
  43. public:
  44. virtual bool importerTest() {
  45. Assimp::Importer importer;
  46. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/Spider_ascii.stl", aiProcess_ValidateDataStructure);
  47. return nullptr != scene;
  48. }
  49. };
  50. TEST_F(utSTLImporterExporter, importSTLFromFileTest) {
  51. EXPECT_TRUE(importerTest());
  52. }
  53. TEST_F(utSTLImporterExporter, test_multiple) {
  54. // import same file twice, each with its own importer
  55. // must work both times and not crash
  56. Assimp::Importer importer1;
  57. const aiScene *scene1 = importer1.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/Spider_ascii.stl", aiProcess_ValidateDataStructure);
  58. EXPECT_NE(nullptr, scene1);
  59. Assimp::Importer importer2;
  60. const aiScene *scene2 = importer2.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/Spider_ascii.stl", aiProcess_ValidateDataStructure);
  61. EXPECT_NE(nullptr, scene2);
  62. }
  63. TEST_F(utSTLImporterExporter, importSTLformatdetection) {
  64. ::Assimp::Importer importer;
  65. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/formatDetection", aiProcess_ValidateDataStructure);
  66. EXPECT_NE(nullptr, scene);
  67. }
  68. TEST_F(utSTLImporterExporter, test_with_two_solids) {
  69. Assimp::Importer importer;
  70. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/triangle_with_two_solids.stl", aiProcess_ValidateDataStructure);
  71. EXPECT_NE(nullptr, scene);
  72. }
  73. TEST_F(utSTLImporterExporter, test_with_empty_solid) {
  74. Assimp::Importer importer;
  75. //STL File with empty mesh. We should still be able to import other meshes in this file. ValidateDataStructure should fail.
  76. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/triangle_with_empty_solid.stl", 0);
  77. EXPECT_NE(nullptr, scene);
  78. const aiScene *scene2 = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/triangle_with_empty_solid.stl", aiProcess_ValidateDataStructure);
  79. EXPECT_EQ(nullptr, scene2);
  80. }
  81. #ifndef ASSIMP_BUILD_NO_EXPORT
  82. TEST_F(utSTLImporterExporter, exporterTest) {
  83. Assimp::Importer importer;
  84. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/Spider_ascii.stl", aiProcess_ValidateDataStructure);
  85. Assimp::Exporter mAiExporter;
  86. const char *stlFileName = "spiderExport.stl";
  87. mAiExporter.Export(scene, "stl", stlFileName);
  88. const aiScene *scene2 = importer.ReadFile(stlFileName, aiProcess_ValidateDataStructure);
  89. EXPECT_NE(nullptr, scene2);
  90. // Cleanup, delete the exported file
  91. std::remove(stlFileName);
  92. }
  93. TEST_F(utSTLImporterExporter, test_export_pointclouds) {
  94. struct XYZ {
  95. float x, y, z;
  96. };
  97. std::vector<XYZ> points;
  98. for (size_t i = 0; i < 10; ++i) {
  99. XYZ current;
  100. current.x = static_cast<float>(i);
  101. current.y = static_cast<float>(i);
  102. current.z = static_cast<float>(i);
  103. points.push_back(current);
  104. }
  105. aiScene scene;
  106. scene.mRootNode = new aiNode();
  107. scene.mMeshes = new aiMesh *[1];
  108. scene.mMeshes[0] = nullptr;
  109. scene.mNumMeshes = 1;
  110. scene.mMaterials = new aiMaterial *[1];
  111. scene.mMaterials[0] = nullptr;
  112. scene.mNumMaterials = 1;
  113. scene.mMaterials[0] = new aiMaterial();
  114. scene.mMeshes[0] = new aiMesh();
  115. scene.mMeshes[0]->mMaterialIndex = 0;
  116. scene.mRootNode->mMeshes = new unsigned int[1];
  117. scene.mRootNode->mMeshes[0] = 0;
  118. scene.mRootNode->mNumMeshes = 1;
  119. auto pMesh = scene.mMeshes[0];
  120. size_t numValidPoints = points.size();
  121. pMesh->mVertices = new aiVector3D[numValidPoints];
  122. pMesh->mNumVertices = static_cast<unsigned int>(numValidPoints);
  123. int i = 0;
  124. for (XYZ &p : points) {
  125. pMesh->mVertices[i] = aiVector3D(p.x, p.y, p.z);
  126. ++i;
  127. }
  128. Assimp::Exporter mAiExporter;
  129. ExportProperties *properties = new ExportProperties;
  130. properties->SetPropertyBool(AI_CONFIG_EXPORT_POINT_CLOUDS, true);
  131. const char *stlFileName = "testExport.stl";
  132. mAiExporter.Export(&scene, "stl", stlFileName, 0, properties);
  133. // Cleanup, delete the exported file
  134. ::remove(stlFileName);
  135. delete properties;
  136. }
  137. #endif