utPLYImportExport.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "AbstractImportExportBase.h"
  36. #include <assimp/postprocess.h>
  37. #include <assimp/scene.h>
  38. #include <assimp/Exporter.hpp>
  39. #include <assimp/Importer.hpp>
  40. using namespace ::Assimp;
  41. class utPLYImportExport : public AbstractImportExportBase {
  42. public:
  43. virtual bool importerTest() {
  44. Assimp::Importer importer;
  45. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube.ply", aiProcess_ValidateDataStructure);
  46. EXPECT_EQ(1u, scene->mNumMeshes);
  47. EXPECT_NE(nullptr, scene->mMeshes[0]);
  48. if (nullptr == scene->mMeshes[0]) {
  49. return false;
  50. }
  51. EXPECT_EQ(8u, scene->mMeshes[0]->mNumVertices);
  52. EXPECT_EQ(6u, scene->mMeshes[0]->mNumFaces);
  53. return (nullptr != scene);
  54. }
  55. #ifndef ASSIMP_BUILD_NO_EXPORT
  56. virtual bool exporterTest() {
  57. Importer importer;
  58. Exporter exporter;
  59. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube.ply", aiProcess_ValidateDataStructure);
  60. EXPECT_NE(nullptr, scene);
  61. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "ply", ASSIMP_TEST_MODELS_DIR "/PLY/cube_out.ply"));
  62. return true;
  63. }
  64. #endif // ASSIMP_BUILD_NO_EXPORT
  65. };
  66. TEST_F(utPLYImportExport, importTest_Success) {
  67. EXPECT_TRUE(importerTest());
  68. }
  69. #ifndef ASSIMP_BUILD_NO_EXPORT
  70. TEST_F(utPLYImportExport, exportTest_Success) {
  71. EXPECT_TRUE(exporterTest());
  72. }
  73. #endif // ASSIMP_BUILD_NO_EXPORT
  74. // Test issue 1623, crash when loading two PLY files in a row
  75. TEST_F(utPLYImportExport, importerMultipleTest) {
  76. Assimp::Importer importer;
  77. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube.ply", aiProcess_ValidateDataStructure);
  78. EXPECT_NE(nullptr, scene);
  79. scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube.ply", aiProcess_ValidateDataStructure);
  80. EXPECT_NE(nullptr, scene);
  81. EXPECT_NE(nullptr, scene->mMeshes[0]);
  82. EXPECT_EQ(6u, scene->mMeshes[0]->mNumFaces);
  83. }
  84. TEST_F(utPLYImportExport, importPLYwithUV) {
  85. Assimp::Importer importer;
  86. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube_uv.ply", aiProcess_ValidateDataStructure);
  87. EXPECT_NE(nullptr, scene);
  88. EXPECT_NE(nullptr, scene->mMeshes[0]);
  89. // This test model is using n-gons, so 6 faces instead of 12 tris
  90. EXPECT_EQ(6u, scene->mMeshes[0]->mNumFaces);
  91. EXPECT_EQ(aiPrimitiveType_POLYGON, scene->mMeshes[0]->mPrimitiveTypes);
  92. EXPECT_EQ(true, scene->mMeshes[0]->HasTextureCoords(0));
  93. }
  94. TEST_F(utPLYImportExport, importBinaryPLY) {
  95. Assimp::Importer importer;
  96. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube_binary.ply", aiProcess_ValidateDataStructure);
  97. EXPECT_NE(nullptr, scene);
  98. EXPECT_NE(nullptr, scene->mMeshes[0]);
  99. // This test model is double sided, so 12 faces instead of 6
  100. EXPECT_EQ(12u, scene->mMeshes[0]->mNumFaces);
  101. }
  102. // Tests of a PLY file gets read with \r\n as newlines instead of just \n (i.e. solidwork exported ply files)
  103. TEST_F(utPLYImportExport, importBinaryPLYWithRNNewline) {
  104. Assimp::Importer importer;
  105. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube_binary_header_with_RN_newline.ply", aiProcess_ValidateDataStructure);
  106. ASSERT_NE(nullptr, scene);
  107. ASSERT_NE(nullptr, scene->mMeshes[0]);
  108. // This test model is double sided, so 12 faces instead of 6
  109. ASSERT_EQ(12u, scene->mMeshes[0]->mNumFaces);
  110. // Also check if the indices were parsed correctly
  111. ASSERT_EQ(3u, scene->mMeshes[0]->mFaces[0].mNumIndices);
  112. EXPECT_EQ(0u, scene->mMeshes[0]->mFaces[0].mIndices[0]);
  113. EXPECT_EQ(1u, scene->mMeshes[0]->mFaces[0].mIndices[1]);
  114. EXPECT_EQ(2u, scene->mMeshes[0]->mFaces[0].mIndices[2]);
  115. }
  116. // Tests of a PLY file gets read with \n as the fist character in the BINARY part
  117. TEST_F(utPLYImportExport, importBinaryPLYWithNewlineInBinary) {
  118. Assimp::Importer importer;
  119. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube_binary_starts_with_nl.ply", aiProcess_ValidateDataStructure);
  120. ASSERT_NE(nullptr, scene);
  121. ASSERT_NE(nullptr, scene->mMeshes[0]);
  122. ASSERT_EQ(8u, scene->mMeshes[0]->mNumVertices);
  123. // Make sure the first binary float was read correctly
  124. ASSERT_FLOAT_EQ(5.967534f, scene->mMeshes[0]->mVertices[0][0]);
  125. ASSERT_FLOAT_EQ(0, scene->mMeshes[0]->mVertices[0][1]);
  126. ASSERT_FLOAT_EQ(0, scene->mMeshes[0]->mVertices[0][2]);
  127. ASSERT_EQ(6u, scene->mMeshes[0]->mNumFaces);
  128. // Also check if the indices were parsed correctly
  129. ASSERT_EQ(4u, scene->mMeshes[0]->mFaces[0].mNumIndices);
  130. EXPECT_EQ(0u, scene->mMeshes[0]->mFaces[0].mIndices[0]);
  131. EXPECT_EQ(1u, scene->mMeshes[0]->mFaces[0].mIndices[1]);
  132. EXPECT_EQ(2u, scene->mMeshes[0]->mFaces[0].mIndices[2]);
  133. }
  134. TEST_F(utPLYImportExport, vertexColorTest) {
  135. Assimp::Importer importer;
  136. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/float-color.ply", aiProcess_ValidateDataStructure);
  137. EXPECT_NE(nullptr, scene);
  138. EXPECT_EQ(1u, scene->mMeshes[0]->mNumFaces);
  139. EXPECT_EQ(aiPrimitiveType_TRIANGLE, scene->mMeshes[0]->mPrimitiveTypes);
  140. EXPECT_EQ(true, scene->mMeshes[0]->HasVertexColors(0));
  141. auto first_face = scene->mMeshes[0]->mFaces[0];
  142. EXPECT_EQ(3u, first_face.mNumIndices);
  143. EXPECT_EQ(0u, first_face.mIndices[0]);
  144. EXPECT_EQ(1u, first_face.mIndices[1]);
  145. EXPECT_EQ(2u, first_face.mIndices[2]);
  146. }
  147. // Test issue #623, PLY importer should not automatically create faces
  148. TEST_F(utPLYImportExport, pointcloudTest) {
  149. Assimp::Importer importer;
  150. // Could not use aiProcess_ValidateDataStructure since it's missing faces.
  151. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/issue623.ply", 0);
  152. EXPECT_NE(nullptr, scene);
  153. EXPECT_EQ(1u, scene->mNumMeshes);
  154. EXPECT_NE(nullptr, scene->mMeshes[0]);
  155. EXPECT_EQ(24u, scene->mMeshes[0]->mNumVertices);
  156. EXPECT_EQ(aiPrimitiveType::aiPrimitiveType_POINT, scene->mMeshes[0]->mPrimitiveTypes);
  157. EXPECT_EQ(0u, scene->mMeshes[0]->mNumFaces);
  158. }
  159. static const char *test_file =
  160. "ply\n"
  161. "format ascii 1.0\n"
  162. "element vertex 4\n"
  163. "property float x\n"
  164. "property float y\n"
  165. "property float z\n"
  166. "property uchar red\n"
  167. "property uchar green\n"
  168. "property uchar blue\n"
  169. "property float nx\n"
  170. "property float ny\n"
  171. "property float nz\n"
  172. "end_header\n"
  173. "0.0 0.0 0.0 255 255 255 0.0 1.0 0.0\n"
  174. "0.0 0.0 1.0 255 0 255 0.0 0.0 1.0\n"
  175. "0.0 1.0 0.0 255 255 0 1.0 0.0 0.0\n"
  176. "0.0 1.0 1.0 0 255 255 1.0 1.0 0.0\n";
  177. TEST_F(utPLYImportExport, parseErrorTest) {
  178. Assimp::Importer importer;
  179. // Could not use aiProcess_ValidateDataStructure since it's missing faces.
  180. const aiScene *scene = importer.ReadFileFromMemory(test_file, strlen(test_file), 0);
  181. EXPECT_NE(nullptr, scene);
  182. }
  183. // This file is invalid, we just want to ensure that the importer is not crashing
  184. TEST_F(utPLYImportExport, parseInvalid) {
  185. Assimp::Importer importer;
  186. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/invalid/crash-30d6d0f7c529b3b66b4131700b7a4580cd7082df.ply", 0);
  187. EXPECT_EQ(nullptr, scene);
  188. }
  189. TEST_F(utPLYImportExport, payload_JVN42386607) {
  190. Assimp::Importer importer;
  191. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/payload_JVN42386607", 0);
  192. EXPECT_EQ(nullptr, scene);
  193. }
  194. // Tests Issue #5729. Test, if properties defined multiple times. Unclear what to do, better to abort than to crash entirely
  195. TEST_F(utPLYImportExport, parseInvalidDoubleProperty) {
  196. const char data[] = "ply\n"
  197. "format ascii 1.0\n"
  198. "element vertex 4\n"
  199. "property float x\n"
  200. "property float y\n"
  201. "property float z\n"
  202. "element vertex 8\n"
  203. "property float x\n"
  204. "property float y\n"
  205. "property float z\n"
  206. "end_header\n"
  207. "0.0 0.0 0.0 0.0 0.0 0.0\n"
  208. "0.0 0.0 1.0 0.0 0.0 1.0\n"
  209. "0.0 1.0 0.0 0.0 1.0 0.0\n"
  210. "0.0 0.0 1.0\n"
  211. "0.0 1.0 0.0 0.0 0.0 1.0\n"
  212. "0.0 1.0 1.0 0.0 1.0 1.0\n";
  213. Assimp::Importer importer;
  214. const aiScene *scene = importer.ReadFileFromMemory(data, sizeof(data), 0);
  215. EXPECT_EQ(nullptr, scene);
  216. }
  217. // Tests Issue #5729. Test, if properties defined multiple times. Unclear what to do, better to abort than to crash entirely
  218. TEST_F(utPLYImportExport, parseInvalidDoubleCustomProperty) {
  219. const char data[] = "ply\n"
  220. "format ascii 1.0\n"
  221. "element vertex 4\n"
  222. "property float x\n"
  223. "property float y\n"
  224. "property float z\n"
  225. "element name 8\n"
  226. "property float x\n"
  227. "element name 5\n"
  228. "property float x\n"
  229. "end_header\n"
  230. "0.0 0.0 0.0 100.0 10.0\n"
  231. "0.0 0.0 1.0 200.0 20.0\n"
  232. "0.0 1.0 0.0 300.0 30.0\n"
  233. "0.0 1.0 1.0 400.0 40.0\n"
  234. "0.0 0.0 0.0 500.0 50.0\n"
  235. "0.0 0.0 1.0 600.0 60.0\n"
  236. "0.0 1.0 0.0 700.0 70.0\n"
  237. "0.0 1.0 1.0 800.0 80.0\n";
  238. Assimp::Importer importer;
  239. const aiScene *scene = importer.ReadFileFromMemory(data, sizeof(data), 0);
  240. EXPECT_EQ(nullptr, scene);
  241. }