2
0

utObjImportExport.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 "SceneDiffer.h"
  36. #include "UnitTestPCH.h"
  37. #include <assimp/postprocess.h>
  38. #include <assimp/scene.h>
  39. #include <assimp/Exporter.hpp>
  40. #include <assimp/Importer.hpp>
  41. using namespace Assimp;
  42. static const float VertComponents[24 * 3] = {
  43. -0.500000, 0.500000, 0.500000,
  44. -0.500000, 0.500000, -0.500000,
  45. -0.500000, -0.500000, -0.500000,
  46. -0.500000, -0.500000, 0.500000,
  47. -0.500000, -0.500000, -0.500000,
  48. 0.500000, -0.500000, -0.500000,
  49. 0.500000, -0.500000, 0.500000,
  50. -0.500000, -0.500000, 0.500000,
  51. -0.500000, 0.500000, -0.500000,
  52. 0.500000, 0.500000, -0.500000,
  53. 0.500000, -0.500000, -0.500000,
  54. -0.500000, -0.500000, -0.500000,
  55. 0.500000, 0.500000, 0.500000,
  56. 0.500000, 0.500000, -0.500000,
  57. -0.500000, 0.500000, -0.500000,
  58. -0.500000, 0.500000, 0.500000,
  59. 0.500000, -0.500000, 0.500000,
  60. 0.500000, 0.500000, 0.500000,
  61. -0.500000, 0.500000, 0.500000,
  62. -0.500000, -0.500000, 0.500000,
  63. 0.500000, -0.500000, -0.500000,
  64. 0.500000, 0.500000, -0.500000,
  65. 0.500000, 0.500000, 0.500000f,
  66. 0.500000, -0.500000, 0.500000f
  67. };
  68. static const char *ObjModel =
  69. "o 1\n"
  70. "\n"
  71. "# Vertex list\n"
  72. "\n"
  73. "v -0.5 -0.5 0.5\n"
  74. "v -0.5 -0.5 -0.5\n"
  75. "v -0.5 0.5 -0.5\n"
  76. "v -0.5 0.5 0.5\n"
  77. "v 0.5 -0.5 0.5\n"
  78. "v 0.5 -0.5 -0.5\n"
  79. "v 0.5 0.5 -0.5\n"
  80. "v 0.5 0.5 0.5\n"
  81. "\n"
  82. "# Point / Line / Face list\n"
  83. "\n"
  84. "g Box01\n"
  85. "usemtl Default\n"
  86. "f 4 3 2 1\n"
  87. "f 2 6 5 1\n"
  88. "f 3 7 6 2\n"
  89. "f 8 7 3 4\n"
  90. "f 5 8 4 1\n"
  91. "f 6 7 8 5\n"
  92. "\n"
  93. "# End of file\n";
  94. static const char *ObjModel_Issue1111 =
  95. "o 1\n"
  96. "\n"
  97. "# Vertex list\n"
  98. "\n"
  99. "v -0.5 -0.5 0.5\n"
  100. "v -0.5 -0.5 -0.5\n"
  101. "v -0.5 0.5 -0.5\n"
  102. "\n"
  103. "usemtl\n"
  104. "f 1 2 3\n"
  105. "\n"
  106. "# End of file\n";
  107. class utObjImportExport : public AbstractImportExportBase {
  108. protected:
  109. void SetUp() override {
  110. m_im = new Assimp::Importer;
  111. }
  112. void TearDown() override {
  113. delete m_im;
  114. m_im = nullptr;
  115. }
  116. aiScene *createScene() {
  117. aiScene *expScene = new aiScene;
  118. expScene->mNumMeshes = 1;
  119. expScene->mMeshes = new aiMesh *[1];
  120. aiMesh *mesh = new aiMesh;
  121. mesh->mName.Set("Box01");
  122. mesh->mNumVertices = 24;
  123. mesh->mVertices = new aiVector3D[24];
  124. ::memcpy(&mesh->mVertices->x, &VertComponents[0], sizeof(float) * 24 * 3);
  125. mesh->mNumFaces = 6;
  126. mesh->mFaces = new aiFace[mesh->mNumFaces];
  127. mesh->mFaces[0].mNumIndices = 4;
  128. mesh->mFaces[0].mIndices = new unsigned int[mesh->mFaces[0].mNumIndices];
  129. mesh->mFaces[0].mIndices[0] = 0;
  130. mesh->mFaces[0].mIndices[1] = 1;
  131. mesh->mFaces[0].mIndices[2] = 2;
  132. mesh->mFaces[0].mIndices[3] = 3;
  133. mesh->mFaces[1].mNumIndices = 4;
  134. mesh->mFaces[1].mIndices = new unsigned int[mesh->mFaces[0].mNumIndices];
  135. mesh->mFaces[1].mIndices[0] = 4;
  136. mesh->mFaces[1].mIndices[1] = 5;
  137. mesh->mFaces[1].mIndices[2] = 6;
  138. mesh->mFaces[1].mIndices[3] = 7;
  139. mesh->mFaces[2].mNumIndices = 4;
  140. mesh->mFaces[2].mIndices = new unsigned int[mesh->mFaces[0].mNumIndices];
  141. mesh->mFaces[2].mIndices[0] = 8;
  142. mesh->mFaces[2].mIndices[1] = 9;
  143. mesh->mFaces[2].mIndices[2] = 10;
  144. mesh->mFaces[2].mIndices[3] = 11;
  145. mesh->mFaces[3].mNumIndices = 4;
  146. mesh->mFaces[3].mIndices = new unsigned int[mesh->mFaces[0].mNumIndices];
  147. mesh->mFaces[3].mIndices[0] = 12;
  148. mesh->mFaces[3].mIndices[1] = 13;
  149. mesh->mFaces[3].mIndices[2] = 14;
  150. mesh->mFaces[3].mIndices[3] = 15;
  151. mesh->mFaces[4].mNumIndices = 4;
  152. mesh->mFaces[4].mIndices = new unsigned int[mesh->mFaces[0].mNumIndices];
  153. mesh->mFaces[4].mIndices[0] = 16;
  154. mesh->mFaces[4].mIndices[1] = 17;
  155. mesh->mFaces[4].mIndices[2] = 18;
  156. mesh->mFaces[4].mIndices[3] = 19;
  157. mesh->mFaces[5].mNumIndices = 4;
  158. mesh->mFaces[5].mIndices = new unsigned int[mesh->mFaces[0].mNumIndices];
  159. mesh->mFaces[5].mIndices[0] = 20;
  160. mesh->mFaces[5].mIndices[1] = 21;
  161. mesh->mFaces[5].mIndices[2] = 22;
  162. mesh->mFaces[5].mIndices[3] = 23;
  163. expScene->mMeshes[0] = mesh;
  164. expScene->mNumMaterials = 1;
  165. expScene->mMaterials = new aiMaterial *[expScene->mNumMaterials];
  166. return expScene;
  167. }
  168. bool importerTest() override {
  169. ::Assimp::Importer importer;
  170. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/spider.obj", aiProcess_ValidateDataStructure);
  171. return nullptr != scene;
  172. }
  173. #ifndef ASSIMP_BUILD_NO_EXPORT
  174. bool exporterTest() override {
  175. ::Assimp::Importer importer;
  176. ::Assimp::Exporter exporter;
  177. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/spider.obj", aiProcess_ValidateDataStructure);
  178. EXPECT_NE(nullptr, scene);
  179. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/OBJ/spider_out.obj"));
  180. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "objnomtl", ASSIMP_TEST_MODELS_DIR "/OBJ/spider_nomtl_out.obj"));
  181. return true;
  182. }
  183. #endif // ASSIMP_BUILD_NO_EXPORT
  184. protected:
  185. ::Assimp::Importer *m_im;
  186. aiScene *m_expectedScene;
  187. };
  188. TEST_F(utObjImportExport, importObjFromFileTest) {
  189. EXPECT_TRUE(importerTest());
  190. }
  191. #ifndef ASSIMP_BUILD_NO_EXPORT
  192. TEST_F(utObjImportExport, exportObjFromFileTest) {
  193. EXPECT_TRUE(exporterTest());
  194. }
  195. #endif // ASSIMP_BUILD_NO_EXPORT
  196. TEST_F(utObjImportExport, obj_import_test) {
  197. const aiScene *scene = m_im->ReadFileFromMemory((void *)ObjModel, strlen(ObjModel), 0);
  198. aiScene *expected = createScene();
  199. EXPECT_NE(nullptr, scene);
  200. SceneDiffer differ;
  201. EXPECT_TRUE(differ.isEqual(expected, scene));
  202. differ.showReport();
  203. m_im->FreeScene();
  204. for (unsigned int i = 0; i < expected->mNumMeshes; ++i) {
  205. delete expected->mMeshes[i];
  206. }
  207. delete[] expected->mMeshes;
  208. expected->mMeshes = nullptr;
  209. delete[] expected->mMaterials;
  210. expected->mMaterials = nullptr;
  211. delete expected;
  212. }
  213. TEST_F(utObjImportExport, issue1111_no_mat_name_Test) {
  214. const aiScene *scene = m_im->ReadFileFromMemory((void *)ObjModel_Issue1111, strlen(ObjModel_Issue1111), 0);
  215. EXPECT_NE(nullptr, scene);
  216. }
  217. TEST_F(utObjImportExport, issue809_vertex_color_Test) {
  218. ::Assimp::Importer importer;
  219. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/cube_with_vertexcolors.obj", aiProcess_ValidateDataStructure);
  220. EXPECT_NE(nullptr, scene);
  221. #ifndef ASSIMP_BUILD_NO_EXPORT
  222. ::Assimp::Exporter exporter;
  223. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/OBJ/test_out.obj"));
  224. #endif // ASSIMP_BUILD_NO_EXPORT
  225. }
  226. TEST_F(utObjImportExport, issue1923_vertex_color_Test) {
  227. ::Assimp::Importer importer;
  228. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/cube_with_vertexcolors_uni.obj", aiProcess_ValidateDataStructure);
  229. EXPECT_NE(nullptr, scene);
  230. scene = importer.GetOrphanedScene();
  231. #ifndef ASSIMP_BUILD_NO_EXPORT
  232. ::Assimp::Exporter exporter;
  233. const aiExportDataBlob *blob = exporter.ExportToBlob(scene, "obj");
  234. EXPECT_NE(nullptr, blob);
  235. const aiScene *sceneReImport = importer.ReadFileFromMemory(blob->data, blob->size, aiProcess_ValidateDataStructure);
  236. EXPECT_NE(nullptr, scene);
  237. SceneDiffer differ;
  238. EXPECT_TRUE(differ.isEqual(scene, sceneReImport));
  239. #endif // ASSIMP_BUILD_NO_EXPORT
  240. delete scene;
  241. }
  242. TEST_F(utObjImportExport, only_a_part_of_vertex_colors_Test) {
  243. ::Assimp::Importer importer;
  244. const aiScene *const scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/only_a_part_of_vertexcolors.obj", aiProcess_ValidateDataStructure);
  245. EXPECT_NE(nullptr, scene);
  246. EXPECT_EQ(scene->mNumMeshes, 1U);
  247. const aiMesh *const mesh = scene->mMeshes[0];
  248. EXPECT_EQ(mesh->mNumVertices, 9U);
  249. EXPECT_EQ(mesh->mNumFaces, 3U);
  250. EXPECT_TRUE(mesh->HasVertexColors(0));
  251. const aiVector3D *const vertices = mesh->mVertices;
  252. const aiColor4D *const colors = mesh->mColors[0];
  253. EXPECT_EQ(aiVector3D(0.0f, 0.0f, 0.0f), vertices[0]);
  254. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 0.0f, 1.0f), colors[0]);
  255. EXPECT_EQ(aiVector3D(0.0f, 0.0f, 1.0f), vertices[1]);
  256. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 1.0f, 1.0f), colors[1]);
  257. EXPECT_EQ(aiVector3D(0.0f, 1.0f, 0.0f), vertices[2]);
  258. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 0.0f, 1.0f), colors[2]);
  259. EXPECT_EQ(aiVector3D(0.0f, 0.0f, 0.0f), vertices[3]);
  260. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 0.0f, 1.0f), colors[3]);
  261. EXPECT_EQ(aiVector3D(1.0f, 0.0f, 0.0f), vertices[4]);
  262. EXPECT_EQ(aiColor4D(1.0f, 0.6f, 0.3f, 1.0f), colors[4]);
  263. EXPECT_EQ(aiVector3D(0.0f, 1.0f, 0.0f), vertices[5]);
  264. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 0.0f, 1.0f), colors[5]);
  265. EXPECT_EQ(aiVector3D(0.0f, 0.0f, 1.0f), vertices[6]);
  266. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 1.0f, 1.0f), colors[6]);
  267. EXPECT_EQ(aiVector3D(1.0f, 1.0f, 0.0f), vertices[7]);
  268. EXPECT_EQ(aiColor4D(0.0f, 0.0f, 0.0f, 1.0f), colors[7]);
  269. EXPECT_EQ(aiVector3D(1.0f, 0.0f, 0.0f), vertices[8]);
  270. EXPECT_EQ(aiColor4D(1.0f, 0.6f, 0.3f, 1.0f), colors[8]);
  271. #ifndef ASSIMP_BUILD_NO_EXPORT
  272. ::Assimp::Exporter exporter;
  273. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/OBJ/test_out.obj"));
  274. #endif // ASSIMP_BUILD_NO_EXPORT
  275. }
  276. TEST_F(utObjImportExport, no_vertex_colors_Test) {
  277. ::Assimp::Importer importer;
  278. const aiScene *const scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/box.obj", aiProcess_ValidateDataStructure);
  279. EXPECT_NE(nullptr, scene);
  280. EXPECT_EQ(scene->mNumMeshes, 1U);
  281. const aiMesh *const mesh = scene->mMeshes[0];
  282. EXPECT_FALSE(mesh->HasVertexColors(0));
  283. }
  284. TEST_F(utObjImportExport, issue1453_segfault) {
  285. static const char *curObjModel =
  286. "v 0.0 0.0 0.0\n"
  287. "v 0.0 0.0 1.0\n"
  288. "v 0.0 1.0 0.0\n"
  289. "v 0.0 1.0 1.0\n"
  290. "v 1.0 0.0 0.0\n"
  291. "v 1.0 0.0 1.0\n"
  292. "v 1.0 1.0 0.0\n"
  293. "v 1.0 1.0 1.0\nB";
  294. Assimp::Importer myimporter;
  295. const aiScene *scene = myimporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), aiProcess_ValidateDataStructure);
  296. EXPECT_EQ(nullptr, scene);
  297. }
  298. TEST_F(utObjImportExport, relative_indices_Test) {
  299. static const char *curObjModel =
  300. "v -0.500000 0.000000 0.400000\n"
  301. "v -0.500000 0.000000 -0.800000\n"
  302. "v -0.500000 1.000000 -0.800000\n"
  303. "v -0.500000 1.000000 0.400000\n"
  304. "f -4 -3 -2 -1\nB";
  305. Assimp::Importer myimporter;
  306. const aiScene *scene = myimporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), aiProcess_ValidateDataStructure);
  307. EXPECT_NE(nullptr, scene);
  308. EXPECT_EQ(scene->mNumMeshes, 1U);
  309. const aiMesh *mesh = scene->mMeshes[0];
  310. EXPECT_EQ(mesh->mNumVertices, 4U);
  311. EXPECT_EQ(mesh->mNumFaces, 1U);
  312. const aiFace face = mesh->mFaces[0];
  313. EXPECT_EQ(face.mNumIndices, 4U);
  314. for (unsigned int i = 0; i < face.mNumIndices; ++i) {
  315. EXPECT_EQ(face.mIndices[i], i);
  316. }
  317. }
  318. TEST_F(utObjImportExport, homogeneous_coordinates_Test) {
  319. static const char *curObjModel =
  320. "v -0.500000 0.000000 0.400000 0.50000\n"
  321. "v -0.500000 0.000000 -0.800000 1.00000\n"
  322. "v 0.500000 1.000000 -0.800000 0.5000\n"
  323. "f 1 2 3\nB";
  324. Assimp::Importer myimporter;
  325. const aiScene *scene = myimporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), aiProcess_ValidateDataStructure);
  326. EXPECT_NE(nullptr, scene);
  327. EXPECT_EQ(scene->mNumMeshes, 1U);
  328. const aiMesh *mesh = scene->mMeshes[0];
  329. EXPECT_EQ(mesh->mNumVertices, 3U);
  330. EXPECT_EQ(mesh->mNumFaces, 1U);
  331. const aiFace face = mesh->mFaces[0];
  332. EXPECT_EQ(face.mNumIndices, 3U);
  333. const aiVector3D vertice = mesh->mVertices[0];
  334. EXPECT_EQ(vertice.x, -1.0f);
  335. EXPECT_EQ(vertice.y, 0.0f);
  336. EXPECT_EQ(vertice.z, 0.8f);
  337. }
  338. TEST_F(utObjImportExport, homogeneous_coordinates_divide_by_zero_Test) {
  339. static const char *curObjModel =
  340. "v -0.500000 0.000000 0.400000 0.\n"
  341. "v -0.500000 0.000000 -0.800000 1.00000\n"
  342. "v 0.500000 1.000000 -0.800000 0.5000\n"
  343. "f 1 2 3\nB";
  344. Assimp::Importer myimporter;
  345. const aiScene *scene = myimporter.ReadFileFromMemory(curObjModel, std::strlen(curObjModel), aiProcess_ValidateDataStructure);
  346. EXPECT_EQ(nullptr, scene);
  347. }
  348. TEST_F(utObjImportExport, 0based_array_Test) {
  349. static const char *curObjModel =
  350. "v -0.500000 0.000000 0.400000\n"
  351. "v -0.500000 0.000000 -0.800000\n"
  352. "v -0.500000 1.000000 -0.800000\n"
  353. "f 0 1 2\nB";
  354. Assimp::Importer myImporter;
  355. const aiScene *scene = myImporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), 0);
  356. EXPECT_EQ(nullptr, scene);
  357. }
  358. TEST_F(utObjImportExport, invalid_normals_uvs) {
  359. static const char *curObjModel =
  360. "v -0.500000 0.000000 0.400000\n"
  361. "v -0.500000 0.000000 -0.800000\n"
  362. "v -0.500000 1.000000 -0.800000\n"
  363. "vt 0 0\n"
  364. "vn 0 1 0\n"
  365. "f 1/1/1 1/1/1 2/2/2\nB";
  366. Assimp::Importer myImporter;
  367. const aiScene *scene = myImporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), 0);
  368. EXPECT_NE(nullptr, scene);
  369. }
  370. TEST_F(utObjImportExport, no_vt_just_vns) {
  371. static const char *curObjModel =
  372. "v 0 0 0\n"
  373. "v 0 0 0\n"
  374. "v 0 0 0\n"
  375. "v 0 0 0\n"
  376. "v 0 0 0\n"
  377. "v 0 0 0\n"
  378. "v 0 0 0\n"
  379. "v 0 0 0\n"
  380. "v 0 0 0\n"
  381. "v 0 0 0\n"
  382. "v 10 0 0\n"
  383. "v 0 10 0\n"
  384. "vn 0 0 1\n"
  385. "vn 0 0 1\n"
  386. "vn 0 0 1\n"
  387. "vn 0 0 1\n"
  388. "vn 0 0 1\n"
  389. "vn 0 0 1\n"
  390. "vn 0 0 1\n"
  391. "vn 0 0 1\n"
  392. "vn 0 0 1\n"
  393. "vn 0 0 1\n"
  394. "vn 0 0 1\n"
  395. "vn 0 0 1\n"
  396. "f 10/10 11/11 12/12\n";
  397. Assimp::Importer myImporter;
  398. const aiScene *scene = myImporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), 0);
  399. EXPECT_NE(nullptr, scene);
  400. }
  401. TEST_F(utObjImportExport, mtllib_after_g) {
  402. ::Assimp::Importer importer;
  403. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/cube_mtllib_after_g.obj", aiProcess_ValidateDataStructure);
  404. ASSERT_NE(nullptr, scene);
  405. EXPECT_EQ(scene->mNumMeshes, 1U);
  406. const aiMesh *mesh = scene->mMeshes[0];
  407. const aiMaterial *mat = scene->mMaterials[mesh->mMaterialIndex];
  408. aiString name;
  409. ASSERT_EQ(aiReturn_SUCCESS, mat->Get(AI_MATKEY_NAME, name));
  410. EXPECT_STREQ("MyMaterial", name.C_Str());
  411. }
  412. TEST_F(utObjImportExport, import_point_cloud) {
  413. ::Assimp::Importer importer;
  414. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/point_cloud.obj", 0);
  415. ASSERT_NE(nullptr, scene);
  416. }
  417. TEST_F(utObjImportExport, import_without_linend) {
  418. Assimp::Importer myImporter;
  419. const aiScene *scene = myImporter.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/box_without_lineending.obj", 0);
  420. ASSERT_NE(nullptr, scene);
  421. }
  422. TEST_F(utObjImportExport, import_with_line_continuations) {
  423. static const char *curObjModel =
  424. "v -0.5 -0.5 0.5\n"
  425. "v -0.5 \\\n"
  426. " -0.5 -0.5\n"
  427. "v -0.5 \\\n"
  428. " 0.5 \\\n"
  429. " -0.5\n"
  430. "f 1 2 3\n";
  431. Assimp::Importer myImporter;
  432. const aiScene *scene = myImporter.ReadFileFromMemory(curObjModel, strlen(curObjModel), 0);
  433. EXPECT_NE(nullptr, scene);
  434. EXPECT_EQ(scene->mNumMeshes, 1U);
  435. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 3U);
  436. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 1U);
  437. auto vertices = scene->mMeshes[0]->mVertices;
  438. const float threshold = 0.0001f;
  439. EXPECT_NEAR(vertices[0].x, -0.5f, threshold);
  440. EXPECT_NEAR(vertices[0].y, -0.5f, threshold);
  441. EXPECT_NEAR(vertices[0].z, 0.5f, threshold);
  442. EXPECT_NEAR(vertices[1].x, -0.5f, threshold);
  443. EXPECT_NEAR(vertices[1].y, -0.5f, threshold);
  444. EXPECT_NEAR(vertices[1].z, -0.5f, threshold);
  445. EXPECT_NEAR(vertices[2].x, -0.5f, threshold);
  446. EXPECT_NEAR(vertices[2].y, 0.5f, threshold);
  447. EXPECT_NEAR(vertices[2].z, -0.5f, threshold);
  448. }
  449. TEST_F(utObjImportExport, issue2355_mtl_texture_prefix) {
  450. ::Assimp::Importer importer;
  451. const aiScene *const scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/mtl_different_folder.obj", aiProcess_ValidateDataStructure);
  452. EXPECT_NE(nullptr, scene);
  453. EXPECT_EQ(scene->mNumMaterials, 2U);
  454. const aiMaterial *const material = scene->mMaterials[1];
  455. aiString texturePath;
  456. material->GetTexture(aiTextureType_DIFFUSE, 0, &texturePath);
  457. // The MTL file is in `folder`, the image path should have been prefixed with the folder
  458. EXPECT_STREQ("folder/image.jpg", texturePath.C_Str());
  459. }