utColladaImportExport.cpp 16 KB

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