utColladaImportExport.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/ColladaMetaData.h>
  37. #include <assimp/SceneCombiner.h>
  38. #include <assimp/commonMetaData.h>
  39. #include <assimp/postprocess.h>
  40. #include <assimp/scene.h>
  41. #include <assimp/Exporter.hpp>
  42. #include <assimp/Importer.hpp>
  43. using namespace Assimp;
  44. class utColladaImportExport : public AbstractImportExportBase {
  45. public:
  46. // Clones the scene in an exception-safe way
  47. struct SceneCloner {
  48. SceneCloner(const aiScene *scene) {
  49. sceneCopy = nullptr;
  50. SceneCombiner::CopyScene(&sceneCopy, scene);
  51. }
  52. ~SceneCloner() {
  53. delete sceneCopy;
  54. sceneCopy = nullptr;
  55. }
  56. aiScene *sceneCopy;
  57. };
  58. bool importerTest() final {
  59. Importer importer;
  60. {
  61. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck.dae", aiProcess_ValidateDataStructure);
  62. if (scene == nullptr) {
  63. return false;
  64. }
  65. // Expected number of items
  66. EXPECT_EQ(scene->mNumMeshes, 1u);
  67. EXPECT_EQ(scene->mNumMaterials, 1u);
  68. EXPECT_EQ(scene->mNumAnimations, 0u);
  69. EXPECT_EQ(scene->mNumTextures, 0u);
  70. EXPECT_EQ(scene->mNumLights, 1u);
  71. EXPECT_EQ(scene->mNumCameras, 1u);
  72. // Expected common metadata
  73. aiString value;
  74. EXPECT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, value)) << "No importer format metadata";
  75. EXPECT_STREQ("Collada Importer", value.C_Str());
  76. EXPECT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, value)) << "No format version metadata";
  77. EXPECT_STREQ("1.4.1", value.C_Str());
  78. EXPECT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, value)) << "No generator metadata";
  79. EXPECT_EQ(strncmp(value.C_Str(), "Maya 8.0", 8), 0) << "AI_METADATA_SOURCE_GENERATOR was: " << value.C_Str();
  80. EXPECT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_COPYRIGHT, value)) << "No copyright metadata";
  81. EXPECT_EQ(strncmp(value.C_Str(), "Copyright 2006", 14), 0) << "AI_METADATA_SOURCE_COPYRIGHT was: " << value.C_Str();
  82. }
  83. {
  84. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/box_nested_animation.dae", aiProcess_ValidateDataStructure);
  85. if (scene == nullptr) {
  86. return false;
  87. }
  88. // Expect only one animation with the correct name
  89. EXPECT_EQ(scene->mNumAnimations, 1u);
  90. EXPECT_EQ(std::string(scene->mAnimations[0]->mName.C_Str()), std::string("Armature"));
  91. }
  92. return true;
  93. }
  94. using IdNameString = std::pair<std::string, std::string>;
  95. using IdNameMap = std::map<std::string, std::string>;
  96. template <typename T>
  97. static IdNameString GetColladaIdName(const T *item, size_t index) {
  98. std::ostringstream stream;
  99. stream << typeid(T).name() << "@" << index;
  100. if (item->mMetaData) {
  101. if (aiString aiStr; item->mMetaData->Get(AI_METADATA_COLLADA_ID, aiStr)) {
  102. return std::make_pair(std::string(aiStr.C_Str()), stream.str());
  103. }
  104. }
  105. return std::make_pair(std::string(), stream.str());
  106. }
  107. template <typename T>
  108. static IdNameString GetItemIdName(const T *item, size_t index) {
  109. std::ostringstream stream;
  110. stream << typeid(T).name() << "@" << index;
  111. return std::make_pair(std::string(item->mName.C_Str()), stream.str());
  112. }
  113. // Specialisations
  114. static IdNameString GetItemIdName(aiMaterial *item, size_t index) {
  115. std::ostringstream stream;
  116. stream << typeid(aiMaterial).name() << "@" << index;
  117. return std::make_pair(std::string(item->GetName().C_Str()), stream.str());
  118. }
  119. static IdNameString GetItemIdName(const aiTexture *item, size_t index) {
  120. std::ostringstream stream;
  121. stream << typeid(aiTexture).name() << "@" << index;
  122. return std::make_pair(std::string(item->mFilename.C_Str()), stream.str());
  123. }
  124. static void ReportDuplicate(IdNameMap &itemIdMap, const IdNameString &namePair, const char *typeNameStr) {
  125. const auto result = itemIdMap.insert(namePair);
  126. EXPECT_TRUE(result.second) << "Duplicate '" << typeNameStr << "' name: '" << namePair.first << "'. " << namePair.second << " == " << result.first->second;
  127. }
  128. template <typename T>
  129. static void CheckUniqueIds(IdNameMap &itemIdMap, unsigned int itemCount, T **itemArray) {
  130. for (size_t idx = 0; idx < itemCount; ++idx) {
  131. IdNameString namePair = GetItemIdName(itemArray[idx], idx);
  132. ReportDuplicate(itemIdMap, namePair, typeid(T).name());
  133. }
  134. }
  135. static void CheckUniqueIds(IdNameMap &itemIdMap, const aiNode *parent, size_t index) {
  136. IdNameString namePair = GetItemIdName(parent, index);
  137. ReportDuplicate(itemIdMap, namePair, typeid(aiNode).name());
  138. for (size_t idx = 0; idx < parent->mNumChildren; ++idx) {
  139. CheckUniqueIds(itemIdMap, parent->mChildren[idx], idx);
  140. }
  141. }
  142. static void CheckNodeIdNames(IdNameMap &nodeIdMap, IdNameMap &nodeNameMap, const aiNode *parent, size_t index) {
  143. IdNameString namePair = GetItemIdName(parent, index);
  144. IdNameString idPair = GetColladaIdName(parent, index);
  145. ReportDuplicate(nodeIdMap, idPair, typeid(aiNode).name());
  146. for (size_t idx = 0; idx < parent->mNumChildren; ++idx) {
  147. CheckNodeIdNames(nodeIdMap, nodeNameMap, parent->mChildren[idx], idx);
  148. }
  149. }
  150. static void SetAllNodeNames(const aiString &newName, aiNode *node) {
  151. node->mName = newName;
  152. for (size_t idx = 0; idx < node->mNumChildren; ++idx) {
  153. SetAllNodeNames(newName, node->mChildren[idx]);
  154. }
  155. }
  156. void ImportAndCheckIds(const char *file, const aiScene *origScene) {
  157. // Import the Collada using the 'default' where aiNode and aiMesh names are the Collada ids
  158. Importer importer;
  159. const aiScene *scene = importer.ReadFile(file, aiProcess_ValidateDataStructure);
  160. ASSERT_TRUE(scene != nullptr) << "Fatal: could not re-import " << file;
  161. EXPECT_EQ(origScene->mNumMeshes, scene->mNumMeshes) << "in " << file;
  162. // Check the ids are unique
  163. IdNameMap itemIdMap;
  164. // Recurse the Nodes
  165. CheckUniqueIds(itemIdMap, scene->mRootNode, 0);
  166. // Check the lists
  167. CheckUniqueIds(itemIdMap, scene->mNumMeshes, scene->mMeshes);
  168. // The remaining will come in using the name, which may not be unique
  169. // Check we have the right number
  170. EXPECT_EQ(origScene->mNumAnimations, scene->mNumAnimations);
  171. EXPECT_EQ(origScene->mNumMaterials, scene->mNumMaterials);
  172. EXPECT_EQ(origScene->mNumTextures, scene->mNumTextures);
  173. EXPECT_EQ(origScene->mNumLights, scene->mNumLights);
  174. EXPECT_EQ(origScene->mNumCameras, scene->mNumCameras);
  175. }
  176. void ImportAsNames(const char *file, const aiScene *origScene) {
  177. // Import the Collada but using the user-visible names for aiNode and aiMesh
  178. // Note that this mode may not support bones or animations
  179. Importer importer;
  180. importer.SetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_USE_COLLADA_NAMES, 1);
  181. const aiScene *scene = importer.ReadFile(file, aiProcess_ValidateDataStructure);
  182. ASSERT_TRUE(scene != nullptr) << "Fatal: could not re-import " << file;
  183. EXPECT_EQ(origScene->mNumMeshes, scene->mNumMeshes) << "in " << file;
  184. // Check the node ids are unique but the node names are not
  185. IdNameMap nodeIdMap;
  186. IdNameMap nodeNameMap;
  187. // Recurse the Nodes
  188. CheckNodeIdNames(nodeIdMap, nodeNameMap, scene->mRootNode, 0);
  189. // nodeNameMap should have fewer than nodeIdMap
  190. EXPECT_LT(nodeNameMap.size(), nodeIdMap.size()) << "Some nodes should have the same names";
  191. // Check the counts haven't changed
  192. EXPECT_EQ(origScene->mNumAnimations, scene->mNumAnimations);
  193. EXPECT_EQ(origScene->mNumMaterials, scene->mNumMaterials);
  194. EXPECT_EQ(origScene->mNumTextures, scene->mNumTextures);
  195. EXPECT_EQ(origScene->mNumLights, scene->mNumLights);
  196. EXPECT_EQ(origScene->mNumCameras, scene->mNumCameras);
  197. }
  198. };
  199. TEST_F(utColladaImportExport, importDaeFromFileTest) {
  200. EXPECT_TRUE(importerTest());
  201. }
  202. unsigned int GetMeshUseCount(const aiNode *rootNode) {
  203. unsigned int result = rootNode->mNumMeshes;
  204. for (unsigned int i = 0; i < rootNode->mNumChildren; ++i) {
  205. result += GetMeshUseCount(rootNode->mChildren[i]);
  206. }
  207. return result;
  208. }
  209. #ifndef ASSIMP_BUILD_NO_EXPORT
  210. TEST_F(utColladaImportExport, exportRootNodeMeshTest) {
  211. Importer importer;
  212. Exporter exporter;
  213. const char *outFile = "exportRootNodeMeshTest_out.dae";
  214. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck.dae", aiProcess_ValidateDataStructure);
  215. ASSERT_TRUE(scene != nullptr) << "Fatal: could not import duck.dae!";
  216. ASSERT_EQ(0u, scene->mRootNode->mNumMeshes) << "Collada import should not give the root node a mesh";
  217. {
  218. SceneCloner clone(scene);
  219. ASSERT_TRUE(clone.sceneCopy != nullptr) << "Fatal: could not copy scene!";
  220. // Do this by moving the meshes from the first child that has some
  221. aiNode *rootNode = clone.sceneCopy->mRootNode;
  222. ASSERT_TRUE(rootNode->mNumChildren > 0) << "Fatal: root has no children";
  223. aiNode *meshNode = rootNode->mChildren[0];
  224. ASSERT_EQ(1u, meshNode->mNumMeshes) << "Fatal: First child node has no duck mesh";
  225. // Move the meshes to the parent
  226. rootNode->mNumMeshes = meshNode->mNumMeshes;
  227. rootNode->mMeshes = new unsigned int[rootNode->mNumMeshes];
  228. for (unsigned int i = 0; i < rootNode->mNumMeshes; ++i) {
  229. rootNode->mMeshes[i] = meshNode->mMeshes[i];
  230. }
  231. // Remove the meshes from the original node
  232. meshNode->mNumMeshes = 0;
  233. delete[] meshNode->mMeshes;
  234. meshNode->mMeshes = nullptr;
  235. ASSERT_EQ(AI_SUCCESS, exporter.Export(clone.sceneCopy, "collada", outFile)) << "Fatal: Could not export file";
  236. }
  237. // Reimport and look for meshes
  238. scene = importer.ReadFile(outFile, aiProcess_ValidateDataStructure);
  239. ASSERT_TRUE(scene != nullptr) << "Fatal: could not reimport!";
  240. // A Collada root node is not allowed to have a mesh
  241. ASSERT_EQ(0u, scene->mRootNode->mNumMeshes) << "Collada reimport should not give the root node a mesh";
  242. // Walk nodes and counts used meshes
  243. // Should be exactly one
  244. EXPECT_EQ(1u, GetMeshUseCount(scene->mRootNode)) << "Nodes had unexpected number of meshes in use";
  245. }
  246. TEST_F(utColladaImportExport, exporterUniqueIdsTest) {
  247. Importer importer;
  248. Exporter exporter;
  249. const char *outFileEmpty = "exportMeshIdTest_empty_out.dae";
  250. const char *outFileNamed = "exportMeshIdTest_named_out.dae";
  251. // Load a sample file containing multiple meshes
  252. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/teapots.DAE", aiProcess_ValidateDataStructure);
  253. ASSERT_TRUE(scene != nullptr) << "Fatal: could not import teapots.DAE!";
  254. ASSERT_EQ(3u, scene->mNumMeshes) << "Fatal: teapots.DAE initial load failed";
  255. // Clear all the names
  256. for (size_t idx = 0; idx < scene->mNumMeshes; ++idx) {
  257. scene->mMeshes[idx]->mName.Clear();
  258. }
  259. for (size_t idx = 0; idx < scene->mNumMaterials; ++idx) {
  260. scene->mMaterials[idx]->RemoveProperty(AI_MATKEY_NAME);
  261. }
  262. for (size_t idx = 0; idx < scene->mNumAnimations; ++idx) {
  263. scene->mAnimations[idx]->mName.Clear();
  264. }
  265. // Can't clear texture names
  266. for (size_t idx = 0; idx < scene->mNumLights; ++idx) {
  267. scene->mLights[idx]->mName.Clear();
  268. }
  269. for (size_t idx = 0; idx < scene->mNumCameras; ++idx) {
  270. scene->mCameras[idx]->mName.Clear();
  271. }
  272. SetAllNodeNames(aiString(), scene->mRootNode);
  273. ASSERT_EQ(AI_SUCCESS, exporter.Export(scene, "collada", outFileEmpty)) << "Fatal: Could not export un-named meshes file";
  274. ImportAndCheckIds(outFileEmpty, scene);
  275. // Force everything to have the same non-empty name
  276. aiString testName("test_name");
  277. for (size_t idx = 0; idx < scene->mNumMeshes; ++idx) {
  278. scene->mMeshes[idx]->mName = testName;
  279. }
  280. for (size_t idx = 0; idx < scene->mNumMaterials; ++idx) {
  281. scene->mMaterials[idx]->AddProperty(&testName, AI_MATKEY_NAME);
  282. }
  283. for (size_t idx = 0; idx < scene->mNumAnimations; ++idx) {
  284. scene->mAnimations[idx]->mName = testName;
  285. }
  286. // Can't clear texture names
  287. for (size_t idx = 0; idx < scene->mNumLights; ++idx) {
  288. scene->mLights[idx]->mName = testName;
  289. }
  290. for (size_t idx = 0; idx < scene->mNumCameras; ++idx) {
  291. scene->mCameras[idx]->mName = testName;
  292. }
  293. SetAllNodeNames(testName, scene->mRootNode);
  294. ASSERT_EQ(AI_SUCCESS, exporter.Export(scene, "collada", outFileNamed)) << "Fatal: Could not export named meshes file";
  295. ImportAndCheckIds(outFileNamed, scene);
  296. ImportAsNames(outFileNamed, scene);
  297. }
  298. // This file is invalid, we just want to ensure that the importer is not crashing
  299. // This was reported as GH#4286. The "count" parameter in "Cube-mesh-positions-array" is too small.
  300. TEST_F(utColladaImportExport, parseInvalid4286) {
  301. Assimp::Importer importer;
  302. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/invalid/box_nested_animation_4286.dae", 0);
  303. EXPECT_EQ(nullptr, scene);
  304. }
  305. #endif
  306. class utColladaZaeImportExport : public AbstractImportExportBase {
  307. public:
  308. virtual bool importerTest() final {
  309. {
  310. Assimp::Importer importer;
  311. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck.zae", aiProcess_ValidateDataStructure);
  312. if (scene == nullptr)
  313. return false;
  314. // Expected number of items
  315. EXPECT_EQ(scene->mNumMeshes, 1u);
  316. EXPECT_EQ(scene->mNumMaterials, 1u);
  317. EXPECT_EQ(scene->mNumAnimations, 0u);
  318. //EXPECT_EQ(scene->mNumTextures, 1u);
  319. EXPECT_EQ(scene->mNumLights, 1u);
  320. EXPECT_EQ(scene->mNumCameras, 1u);
  321. }
  322. {
  323. Assimp::Importer importer;
  324. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck_nomanifest.zae", aiProcess_ValidateDataStructure);
  325. if (scene == nullptr)
  326. return false;
  327. // Expected number of items
  328. EXPECT_EQ(scene->mNumMeshes, 1u);
  329. EXPECT_EQ(scene->mNumMaterials, 1u);
  330. EXPECT_EQ(scene->mNumAnimations, 0u);
  331. //EXPECT_EQ(scene->mNumTextures, 1u);
  332. EXPECT_EQ(scene->mNumLights, 1u);
  333. EXPECT_EQ(scene->mNumCameras, 1u);
  334. }
  335. return true;
  336. }
  337. };
  338. TEST_F(utColladaZaeImportExport, importBlenFromFileTest) {
  339. EXPECT_TRUE(importerTest());
  340. }
  341. TEST_F(utColladaZaeImportExport, importMakeHumanTest) {
  342. Importer importer;
  343. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/human.zae", aiProcess_ValidateDataStructure);
  344. ASSERT_NE(nullptr, scene);
  345. // Expected number of items
  346. EXPECT_EQ(scene->mNumMeshes, 2u);
  347. EXPECT_EQ(scene->mNumMaterials, 2u);
  348. EXPECT_EQ(scene->mNumAnimations, 0u);
  349. EXPECT_EQ(scene->mNumTextures, 2u);
  350. EXPECT_EQ(scene->mNumLights, 0u);
  351. EXPECT_EQ(scene->mNumCameras, 0u);
  352. // Expected common metadata
  353. aiString value;
  354. EXPECT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, value)) << "No importer format metadata";
  355. EXPECT_STREQ("Collada Importer", value.C_Str());
  356. EXPECT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, value)) << "No format version metadata";
  357. EXPECT_STREQ("1.4.1", value.C_Str());
  358. }
  359. static constexpr char kData[] = " \
  360. <?xml version=\"1.0\" encoding=\"utf-8\"?> \
  361. <COLLADA version=\"1.4.1\"> \
  362. <library_geometries> \
  363. <geometry id=\"fuzz2772-geo\" name=\"fuzz2772\"> \
  364. <mesh> \
  365. <triangles count=\"1\"><input semantic=\"VERTEXERTEX\" source=\"#fuzz2772-verts\" offset=\"0\"/><p>0 1 2</p></triangles> \
  366. </mesh> \
  367. </geometry> \
  368. </library_geometries> \
  369. </COLLADA> \
  370. ";
  371. TEST_F(utColladaZaeImportExport, import_causes_heap_overflow_issue6373) {
  372. Importer importer;
  373. const aiScene *scene = importer.ReadFileFromMemory(kData, sizeof(kData), aiProcess_ValidateDataStructure);
  374. EXPECT_EQ(nullptr, scene);
  375. }