utColladaImportExport.cpp 17 KB

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