D3MFImporter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "D3MFImporter.h"
  34. #include <assimp/scene.h>
  35. #include <assimp/IOStream.hpp>
  36. #include <assimp/IOSystem.hpp>
  37. #include <assimp/DefaultLogger.hpp>
  38. #include "StringComparison.h"
  39. #include "StringUtils.h"
  40. #include <string>
  41. #include <sstream>
  42. #include <vector>
  43. #include <map>
  44. #include <algorithm>
  45. #include <cassert>
  46. #include <cstdlib>
  47. #include <memory>
  48. #include <assimp/ai_assert.h>
  49. #ifndef ASSIMP_BUILD_NO_3MF_IMPORTER
  50. #include "D3MFOpcPackage.h"
  51. #include <contrib/unzip/unzip.h>
  52. #include "irrXMLWrapper.h"
  53. namespace Assimp {
  54. namespace D3MF {
  55. namespace XmlTag {
  56. static const std::string model = "model";
  57. static const std::string metadata = "metadata";
  58. static const std::string resources = "resources";
  59. static const std::string object = "object";
  60. static const std::string mesh = "mesh";
  61. static const std::string vertices = "vertices";
  62. static const std::string vertex = "vertex";
  63. static const std::string triangles = "triangles";
  64. static const std::string triangle = "triangle";
  65. static const std::string x = "x";
  66. static const std::string y = "y";
  67. static const std::string z = "z";
  68. static const std::string v1 = "v1";
  69. static const std::string v2 = "v2";
  70. static const std::string v3 = "v3";
  71. static const std::string id = "id";
  72. static const std::string name = "name";
  73. static const std::string type = "type";
  74. static const std::string build = "build";
  75. static const std::string item = "item";
  76. static const std::string objectid = "objectid";
  77. static const std::string transform = "transform";
  78. }
  79. class XmlSerializer
  80. {
  81. public:
  82. XmlSerializer(XmlReader* xmlReader)
  83. : xmlReader(xmlReader)
  84. {
  85. }
  86. void ImportXml(aiScene* scene)
  87. {
  88. scene->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
  89. scene->mRootNode = new aiNode();
  90. std::vector<aiNode*> children;
  91. while(ReadToEndElement(D3MF::XmlTag::model))
  92. {
  93. if(xmlReader->getNodeName() == D3MF::XmlTag::object)
  94. {
  95. children.push_back(ReadObject(scene));
  96. }
  97. else if(xmlReader->getNodeName() == D3MF::XmlTag::build)
  98. {
  99. }
  100. }
  101. if(scene->mRootNode->mName.length == 0)
  102. scene->mRootNode->mName.Set("3MF");
  103. scene->mNumMeshes = static_cast<unsigned int>(meshes.size());
  104. scene->mMeshes = new aiMesh*[scene->mNumMeshes]();
  105. std::copy(meshes.begin(), meshes.end(), scene->mMeshes);
  106. scene->mRootNode->mNumChildren = static_cast<unsigned int>(children.size());
  107. scene->mRootNode->mChildren = new aiNode*[scene->mRootNode->mNumChildren]();
  108. std::copy(children.begin(), children.end(), scene->mRootNode->mChildren);
  109. }
  110. private:
  111. aiNode* ReadObject(aiScene* scene)
  112. {
  113. ScopeGuard<aiNode> node(new aiNode());
  114. std::vector<unsigned long> meshIds;
  115. const char *attrib( nullptr );
  116. std::string name, type;
  117. attrib = xmlReader->getAttributeValue( D3MF::XmlTag::name.c_str() );
  118. if ( nullptr != attrib ) {
  119. name = attrib;
  120. }
  121. attrib = xmlReader->getAttributeValue( D3MF::XmlTag::name.c_str() );
  122. if ( nullptr != attrib ) {
  123. type = attrib;
  124. }
  125. node->mParent = scene->mRootNode;
  126. node->mName.Set(name);
  127. size_t meshIdx = meshes.size();
  128. while(ReadToEndElement(D3MF::XmlTag::object))
  129. {
  130. if(xmlReader->getNodeName() == D3MF::XmlTag::mesh)
  131. {
  132. auto mesh = ReadMesh();
  133. mesh->mName.Set(name);
  134. meshes.push_back(mesh);
  135. meshIds.push_back(static_cast<unsigned long>(meshIdx));
  136. meshIdx++;
  137. }
  138. }
  139. node->mNumMeshes = static_cast<unsigned int>(meshIds.size());
  140. node->mMeshes = new unsigned int[node->mNumMeshes];
  141. std::copy(meshIds.begin(), meshIds.end(), node->mMeshes);
  142. return node.dismiss();
  143. }
  144. aiMesh* ReadMesh()
  145. {
  146. aiMesh* mesh = new aiMesh();
  147. while(ReadToEndElement(D3MF::XmlTag::mesh))
  148. {
  149. if(xmlReader->getNodeName() == D3MF::XmlTag::vertices)
  150. {
  151. ImportVertices(mesh);
  152. }
  153. else if(xmlReader->getNodeName() == D3MF::XmlTag::triangles)
  154. {
  155. ImportTriangles(mesh);
  156. }
  157. }
  158. return mesh;
  159. }
  160. void ImportVertices(aiMesh* mesh)
  161. {
  162. std::vector<aiVector3D> vertices;
  163. while(ReadToEndElement(D3MF::XmlTag::vertices))
  164. {
  165. if(xmlReader->getNodeName() == D3MF::XmlTag::vertex)
  166. {
  167. vertices.push_back(ReadVertex());
  168. }
  169. }
  170. mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
  171. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  172. std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
  173. }
  174. aiVector3D ReadVertex()
  175. {
  176. aiVector3D vertex;
  177. vertex.x = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::x.c_str()), nullptr);
  178. vertex.y = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::y.c_str()), nullptr);
  179. vertex.z = ai_strtof>(xmlReader->getAttributeValue(D3MF::XmlTag::z.c_str()), nullptr);
  180. return vertex;
  181. }
  182. void ImportTriangles(aiMesh* mesh)
  183. {
  184. std::vector<aiFace> faces;
  185. while(ReadToEndElement(D3MF::XmlTag::triangles))
  186. {
  187. if(xmlReader->getNodeName() == D3MF::XmlTag::triangle)
  188. {
  189. faces.push_back(ReadTriangle());
  190. }
  191. }
  192. mesh->mNumFaces = static_cast<unsigned int>(faces.size());
  193. mesh->mFaces = new aiFace[mesh->mNumFaces];
  194. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  195. std::copy(faces.begin(), faces.end(), mesh->mFaces);
  196. }
  197. aiFace ReadTriangle()
  198. {
  199. aiFace face;
  200. face.mNumIndices = 3;
  201. face.mIndices = new unsigned int[face.mNumIndices];
  202. face.mIndices[0] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v1.c_str())));
  203. face.mIndices[1] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v2.c_str())));
  204. face.mIndices[2] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v3.c_str())));
  205. return face;
  206. }
  207. private:
  208. bool ReadToStartElement(const std::string& startTag)
  209. {
  210. while(xmlReader->read())
  211. {
  212. if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT && xmlReader->getNodeName() == startTag)
  213. {
  214. return true;
  215. }
  216. else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END &&
  217. xmlReader->getNodeName() == startTag)
  218. {
  219. return false;
  220. }
  221. }
  222. //DefaultLogger::get()->error("unexpected EOF, expected closing <" + closeTag + "> tag");
  223. return false;
  224. }
  225. bool ReadToEndElement(const std::string& closeTag)
  226. {
  227. while(xmlReader->read())
  228. {
  229. if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT) {
  230. return true;
  231. }
  232. else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END
  233. && xmlReader->getNodeName() == closeTag)
  234. {
  235. return false;
  236. }
  237. }
  238. DefaultLogger::get()->error("unexpected EOF, expected closing <" + closeTag + "> tag");
  239. return false;
  240. }
  241. private:
  242. std::vector<aiMesh*> meshes;
  243. XmlReader* xmlReader;
  244. };
  245. } //namespace D3MF
  246. static const aiImporterDesc desc = {
  247. "3mf Importer",
  248. "",
  249. "",
  250. "http://3mf.io/",
  251. aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
  252. 0,
  253. 0,
  254. 0,
  255. 0,
  256. "3mf"
  257. };
  258. D3MFImporter::D3MFImporter()
  259. {
  260. }
  261. D3MFImporter::~D3MFImporter()
  262. {
  263. }
  264. bool D3MFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const
  265. {
  266. const std::string extension = GetExtension(pFile);
  267. if(extension == "3mf") {
  268. return true;
  269. } else if ( !extension.length() || checkSig ) {
  270. if (nullptr == pIOHandler ) {
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. void D3MFImporter::SetupProperties(const Importer *pImp)
  277. {
  278. }
  279. const aiImporterDesc *D3MFImporter::GetInfo() const
  280. {
  281. return &desc;
  282. }
  283. void D3MFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler)
  284. {
  285. D3MF::D3MFOpcPackage opcPackage(pIOHandler, pFile);
  286. std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(opcPackage.RootStream()));
  287. std::unique_ptr<D3MF::XmlReader> xmlReader(irr::io::createIrrXMLReader(xmlStream.get()));
  288. D3MF::XmlSerializer xmlSerializer(xmlReader.get());
  289. xmlSerializer.ImportXml(pScene);
  290. }
  291. } // Namespace Assimp
  292. #endif // ASSIMP_BUILD_NO_3MF_IMPORTER