D3MFImporter.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2017, 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. #ifndef ASSIMP_BUILD_NO_3MF_IMPORTER
  34. #include "D3MFImporter.h"
  35. #include <assimp/scene.h>
  36. #include <assimp/IOSystem.hpp>
  37. #include <assimp/DefaultLogger.hpp>
  38. #include <assimp/importerdesc.h>
  39. #include "StringComparison.h"
  40. #include "StringUtils.h"
  41. #include <string>
  42. #include <vector>
  43. #include <map>
  44. #include <cassert>
  45. #include <memory>
  46. #include "D3MFOpcPackage.h"
  47. #include <contrib/unzip/unzip.h>
  48. #include "irrXMLWrapper.h"
  49. #include "3MFXmlTags.h"
  50. namespace Assimp {
  51. namespace D3MF {
  52. class XmlSerializer {
  53. public:
  54. XmlSerializer(XmlReader* xmlReader)
  55. : xmlReader(xmlReader) {
  56. // empty
  57. }
  58. ~XmlSerializer() {
  59. // empty
  60. }
  61. void ImportXml(aiScene* scene) {
  62. scene->mRootNode = new aiNode();
  63. std::vector<aiNode*> children;
  64. while(ReadToEndElement(D3MF::XmlTag::model)) {
  65. if(xmlReader->getNodeName() == D3MF::XmlTag::object) {
  66. children.push_back(ReadObject(scene));
  67. } else if(xmlReader->getNodeName() == D3MF::XmlTag::build) {
  68. }
  69. }
  70. if ( scene->mRootNode->mName.length == 0 ) {
  71. scene->mRootNode->mName.Set( "3MF" );
  72. }
  73. scene->mNumMeshes = static_cast<unsigned int>(meshes.size());
  74. scene->mMeshes = new aiMesh*[scene->mNumMeshes]();
  75. std::copy(meshes.begin(), meshes.end(), scene->mMeshes);
  76. scene->mRootNode->mNumChildren = static_cast<unsigned int>(children.size());
  77. scene->mRootNode->mChildren = new aiNode*[scene->mRootNode->mNumChildren]();
  78. std::copy(children.begin(), children.end(), scene->mRootNode->mChildren);
  79. }
  80. private:
  81. aiNode* ReadObject(aiScene* scene)
  82. {
  83. std::unique_ptr<aiNode> node(new aiNode());
  84. std::vector<unsigned long> meshIds;
  85. const char *attrib( nullptr );
  86. std::string name, type;
  87. attrib = xmlReader->getAttributeValue( D3MF::XmlTag::name.c_str() );
  88. if ( nullptr != attrib ) {
  89. name = attrib;
  90. }
  91. attrib = xmlReader->getAttributeValue( D3MF::XmlTag::name.c_str() );
  92. if ( nullptr != attrib ) {
  93. type = attrib;
  94. }
  95. node->mParent = scene->mRootNode;
  96. node->mName.Set(name);
  97. size_t meshIdx = meshes.size();
  98. while(ReadToEndElement(D3MF::XmlTag::object))
  99. {
  100. if(xmlReader->getNodeName() == D3MF::XmlTag::mesh)
  101. {
  102. auto mesh = ReadMesh();
  103. mesh->mName.Set(name);
  104. meshes.push_back(mesh);
  105. meshIds.push_back(static_cast<unsigned long>(meshIdx));
  106. meshIdx++;
  107. }
  108. }
  109. node->mNumMeshes = static_cast<unsigned int>(meshIds.size());
  110. node->mMeshes = new unsigned int[node->mNumMeshes];
  111. std::copy(meshIds.begin(), meshIds.end(), node->mMeshes);
  112. return node.release();
  113. }
  114. aiMesh* ReadMesh() {
  115. aiMesh* mesh = new aiMesh();
  116. while(ReadToEndElement(D3MF::XmlTag::mesh))
  117. {
  118. if(xmlReader->getNodeName() == D3MF::XmlTag::vertices)
  119. {
  120. ImportVertices(mesh);
  121. }
  122. else if(xmlReader->getNodeName() == D3MF::XmlTag::triangles)
  123. {
  124. ImportTriangles(mesh);
  125. }
  126. }
  127. return mesh;
  128. }
  129. void ImportVertices(aiMesh* mesh)
  130. {
  131. std::vector<aiVector3D> vertices;
  132. while(ReadToEndElement(D3MF::XmlTag::vertices))
  133. {
  134. if(xmlReader->getNodeName() == D3MF::XmlTag::vertex)
  135. {
  136. vertices.push_back(ReadVertex());
  137. }
  138. }
  139. mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
  140. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  141. std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
  142. }
  143. aiVector3D ReadVertex()
  144. {
  145. aiVector3D vertex;
  146. vertex.x = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::x.c_str()), nullptr);
  147. vertex.y = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::y.c_str()), nullptr);
  148. vertex.z = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::z.c_str()), nullptr);
  149. return vertex;
  150. }
  151. void ImportTriangles(aiMesh* mesh)
  152. {
  153. std::vector<aiFace> faces;
  154. while(ReadToEndElement(D3MF::XmlTag::triangles))
  155. {
  156. if(xmlReader->getNodeName() == D3MF::XmlTag::triangle)
  157. {
  158. faces.push_back(ReadTriangle());
  159. }
  160. }
  161. mesh->mNumFaces = static_cast<unsigned int>(faces.size());
  162. mesh->mFaces = new aiFace[mesh->mNumFaces];
  163. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  164. std::copy(faces.begin(), faces.end(), mesh->mFaces);
  165. }
  166. aiFace ReadTriangle()
  167. {
  168. aiFace face;
  169. face.mNumIndices = 3;
  170. face.mIndices = new unsigned int[face.mNumIndices];
  171. face.mIndices[0] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v1.c_str())));
  172. face.mIndices[1] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v2.c_str())));
  173. face.mIndices[2] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v3.c_str())));
  174. return face;
  175. }
  176. private:
  177. bool ReadToStartElement(const std::string& startTag)
  178. {
  179. while(xmlReader->read())
  180. {
  181. if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT && xmlReader->getNodeName() == startTag)
  182. {
  183. return true;
  184. }
  185. else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END &&
  186. xmlReader->getNodeName() == startTag)
  187. {
  188. return false;
  189. }
  190. }
  191. //DefaultLogger::get()->error("unexpected EOF, expected closing <" + closeTag + "> tag");
  192. return false;
  193. }
  194. bool ReadToEndElement(const std::string& closeTag)
  195. {
  196. while(xmlReader->read())
  197. {
  198. if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT) {
  199. return true;
  200. }
  201. else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END
  202. && xmlReader->getNodeName() == closeTag)
  203. {
  204. return false;
  205. }
  206. }
  207. DefaultLogger::get()->error("unexpected EOF, expected closing <" + closeTag + "> tag");
  208. return false;
  209. }
  210. private:
  211. std::vector<aiMesh*> meshes;
  212. XmlReader* xmlReader;
  213. };
  214. } //namespace D3MF
  215. static const std::string Extension = "3mf";
  216. static const aiImporterDesc desc = {
  217. "3mf Importer",
  218. "",
  219. "",
  220. "http://3mf.io/",
  221. aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
  222. 0,
  223. 0,
  224. 0,
  225. 0,
  226. Extension.c_str()
  227. };
  228. D3MFImporter::D3MFImporter()
  229. : BaseImporter() {
  230. // empty
  231. }
  232. D3MFImporter::~D3MFImporter() {
  233. // empty
  234. }
  235. bool D3MFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
  236. const std::string extension = GetExtension(pFile);
  237. if(extension == Extension ) {
  238. return true;
  239. } else if ( !extension.length() || checkSig ) {
  240. if (nullptr == pIOHandler ) {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. void D3MFImporter::SetupProperties(const Importer * /*pImp*/) {
  247. // empty
  248. }
  249. const aiImporterDesc *D3MFImporter::GetInfo() const {
  250. return &desc;
  251. }
  252. void D3MFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
  253. D3MF::D3MFOpcPackage opcPackage(pIOHandler, pFile);
  254. std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(opcPackage.RootStream()));
  255. std::unique_ptr<D3MF::XmlReader> xmlReader(irr::io::createIrrXMLReader(xmlStream.get()));
  256. D3MF::XmlSerializer xmlSerializer(xmlReader.get());
  257. xmlSerializer.ImportXml(pScene);
  258. }
  259. } // Namespace Assimp
  260. #endif // ASSIMP_BUILD_NO_3MF_IMPORTER