D3MFImporter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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/StringComparison.h>
  36. #include <assimp/StringUtils.h>
  37. #include <assimp/XmlParser.h>
  38. #include <assimp/ZipArchiveIOSystem.h>
  39. #include <assimp/importerdesc.h>
  40. #include <assimp/scene.h>
  41. #include <assimp/DefaultLogger.hpp>
  42. #include <assimp/IOSystem.hpp>
  43. #include <cassert>
  44. #include <map>
  45. #include <memory>
  46. #include <string>
  47. #include <vector>
  48. #include "3MFXmlTags.h"
  49. #include "D3MFOpcPackage.h"
  50. #include <assimp/fast_atof.h>
  51. #include <iomanip>
  52. namespace Assimp {
  53. namespace D3MF {
  54. class XmlSerializer {
  55. public:
  56. using MatArray = std::vector<aiMaterial *>;
  57. using MatId2MatArray = std::map<unsigned int, std::vector<unsigned int>>;
  58. XmlSerializer(XmlParser *xmlParser) :
  59. mMeshes(),
  60. mMatArray(),
  61. mActiveMatGroup(99999999),
  62. mMatId2MatArray(),
  63. mXmlParser(xmlParser) {
  64. // empty
  65. }
  66. ~XmlSerializer() {
  67. // empty
  68. }
  69. void ImportXml(aiScene *scene) {
  70. if (nullptr == scene) {
  71. return;
  72. }
  73. scene->mRootNode = new aiNode();
  74. std::vector<aiNode *> children;
  75. std::string nodeName;
  76. XmlNode node = *mXmlParser->getRootNode();
  77. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  78. const std::string &currentNodeName = currentNode.name();
  79. if (currentNodeName == D3MF::XmlTag::object) {
  80. children.push_back(ReadObject(currentNode, scene));
  81. } else if (currentNodeName == D3MF::XmlTag::build) {
  82. //
  83. } else if (currentNodeName == D3MF::XmlTag::basematerials) {
  84. ReadBaseMaterials(currentNode);
  85. } else if (currentNodeName == D3MF::XmlTag::meta) {
  86. ReadMetadata(currentNode);
  87. }
  88. }
  89. if (scene->mRootNode->mName.length == 0) {
  90. scene->mRootNode->mName.Set("3MF");
  91. }
  92. // import the metadata
  93. if (!mMetaData.empty()) {
  94. const size_t numMeta(mMetaData.size());
  95. scene->mMetaData = aiMetadata::Alloc(static_cast<unsigned int>(numMeta));
  96. for (size_t i = 0; i < numMeta; ++i) {
  97. aiString val(mMetaData[i].value);
  98. scene->mMetaData->Set(static_cast<unsigned int>(i), mMetaData[i].name, val);
  99. }
  100. }
  101. // import the meshes
  102. scene->mNumMeshes = static_cast<unsigned int>(mMeshes.size());
  103. scene->mMeshes = new aiMesh *[scene->mNumMeshes]();
  104. std::copy(mMeshes.begin(), mMeshes.end(), scene->mMeshes);
  105. // import the materials
  106. scene->mNumMaterials = static_cast<unsigned int>(mMatArray.size());
  107. if (0 != scene->mNumMaterials) {
  108. scene->mMaterials = new aiMaterial *[scene->mNumMaterials];
  109. std::copy(mMatArray.begin(), mMatArray.end(), scene->mMaterials);
  110. }
  111. // create the scene-graph
  112. scene->mRootNode->mNumChildren = static_cast<unsigned int>(children.size());
  113. scene->mRootNode->mChildren = new aiNode *[scene->mRootNode->mNumChildren]();
  114. std::copy(children.begin(), children.end(), scene->mRootNode->mChildren);
  115. }
  116. private:
  117. aiNode *ReadObject(XmlNode &node, aiScene *scene) {
  118. std::unique_ptr<aiNode> nodePtr(new aiNode());
  119. std::vector<unsigned long> meshIds;
  120. std::string name, type;
  121. pugi::xml_attribute attr = node.attribute(D3MF::XmlTag::id.c_str());
  122. if (!attr.empty()) {
  123. name = attr.as_string();
  124. }
  125. attr = node.attribute(D3MF::XmlTag::id.c_str());
  126. if (!attr.empty()) {
  127. type = attr.as_string();
  128. }
  129. nodePtr->mParent = scene->mRootNode;
  130. nodePtr->mName.Set(name);
  131. size_t meshIdx = mMeshes.size();
  132. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  133. const std::string &currentName = currentNode.name();
  134. if (currentName == D3MF::XmlTag::mesh) {
  135. auto mesh = ReadMesh(currentNode);
  136. mesh->mName.Set(name);
  137. mMeshes.push_back(mesh);
  138. meshIds.push_back(static_cast<unsigned long>(meshIdx));
  139. ++meshIdx;
  140. }
  141. }
  142. nodePtr->mNumMeshes = static_cast<unsigned int>(meshIds.size());
  143. nodePtr->mMeshes = new unsigned int[nodePtr->mNumMeshes];
  144. std::copy(meshIds.begin(), meshIds.end(), nodePtr->mMeshes);
  145. return nodePtr.release();
  146. }
  147. aiMesh *ReadMesh(XmlNode &node) {
  148. aiMesh *mesh = new aiMesh();
  149. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  150. const std::string &currentName = currentNode.name();
  151. if (currentName == D3MF::XmlTag::vertices) {
  152. ImportVertices(currentNode, mesh);
  153. } else if (currentName == D3MF::XmlTag::triangles) {
  154. ImportTriangles(currentNode, mesh);
  155. }
  156. }
  157. return mesh;
  158. }
  159. void ReadMetadata(XmlNode &node) {
  160. pugi::xml_attribute attribute = node.attribute(D3MF::XmlTag::meta_name.c_str());
  161. const std::string name = attribute.as_string();
  162. const std::string value = node.value();
  163. if (name.empty()) {
  164. return;
  165. }
  166. MetaEntry entry;
  167. entry.name = name;
  168. entry.value = value;
  169. mMetaData.push_back(entry);
  170. }
  171. void ImportVertices(XmlNode &node, aiMesh *mesh) {
  172. std::vector<aiVector3D> vertices;
  173. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  174. const std::string &currentName = currentNode.name();
  175. if (currentName == D3MF::XmlTag::vertex) {
  176. vertices.push_back(ReadVertex(currentNode));
  177. }
  178. }
  179. mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
  180. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  181. std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
  182. }
  183. aiVector3D ReadVertex(XmlNode &node) {
  184. aiVector3D vertex;
  185. vertex.x = ai_strtof(node.attribute(D3MF::XmlTag::x.c_str()).as_string(), nullptr);
  186. vertex.x = ai_strtof(node.attribute(D3MF::XmlTag::y.c_str()).as_string(), nullptr);
  187. vertex.z = ai_strtof(node.attribute(D3MF::XmlTag::z.c_str()).as_string(), nullptr);
  188. return vertex;
  189. }
  190. void ImportTriangles(XmlNode &node, aiMesh *mesh) {
  191. std::vector<aiFace> faces;
  192. for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
  193. const std::string &currentName = currentNode.name();
  194. if (currentName == D3MF::XmlTag::triangle) {
  195. faces.push_back(ReadTriangle(currentNode));
  196. const char *pidToken = currentNode.attribute(D3MF::XmlTag::p1.c_str()).as_string();
  197. if (nullptr != pidToken) {
  198. int matIdx(std::atoi(pidToken));
  199. mesh->mMaterialIndex = matIdx;
  200. }
  201. }
  202. }
  203. mesh->mNumFaces = static_cast<unsigned int>(faces.size());
  204. mesh->mFaces = new aiFace[mesh->mNumFaces];
  205. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  206. std::copy(faces.begin(), faces.end(), mesh->mFaces);
  207. }
  208. aiFace ReadTriangle(XmlNode &node) {
  209. aiFace face;
  210. face.mNumIndices = 3;
  211. face.mIndices = new unsigned int[face.mNumIndices];
  212. face.mIndices[0] = static_cast<unsigned int>(std::atoi(node.attribute(D3MF::XmlTag::v1.c_str()).as_string()));
  213. face.mIndices[1] = static_cast<unsigned int>(std::atoi(node.attribute(D3MF::XmlTag::v2.c_str()).as_string()));
  214. face.mIndices[2] = static_cast<unsigned int>(std::atoi(node.attribute(D3MF::XmlTag::v3.c_str()).as_string()));
  215. return face;
  216. }
  217. void ReadBaseMaterials(XmlNode &node) {
  218. std::vector<unsigned int> MatIdArray;
  219. const char *baseMaterialId = node.attribute(D3MF::XmlTag::basematerials_id.c_str()).as_string();
  220. if (nullptr != baseMaterialId) {
  221. unsigned int id = std::atoi(baseMaterialId);
  222. const size_t newMatIdx(mMatArray.size());
  223. if (id != mActiveMatGroup) {
  224. mActiveMatGroup = id;
  225. MatId2MatArray::const_iterator it(mMatId2MatArray.find(id));
  226. if (mMatId2MatArray.end() == it) {
  227. MatIdArray.clear();
  228. mMatId2MatArray[id] = MatIdArray;
  229. } else {
  230. MatIdArray = it->second;
  231. }
  232. }
  233. MatIdArray.push_back(static_cast<unsigned int>(newMatIdx));
  234. mMatId2MatArray[mActiveMatGroup] = MatIdArray;
  235. }
  236. mMatArray.push_back(readMaterialDef(node));
  237. }
  238. bool parseColor(const char *color, aiColor4D &diffuse) {
  239. if (nullptr == color) {
  240. return false;
  241. }
  242. //format of the color string: #RRGGBBAA or #RRGGBB (3MF Core chapter 5.1.1)
  243. const size_t len(strlen(color));
  244. if (9 != len && 7 != len) {
  245. return false;
  246. }
  247. const char *buf(color);
  248. if ('#' != *buf) {
  249. return false;
  250. }
  251. ++buf;
  252. char comp[3] = { 0, 0, '\0' };
  253. comp[0] = *buf;
  254. ++buf;
  255. comp[1] = *buf;
  256. ++buf;
  257. diffuse.r = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
  258. comp[0] = *buf;
  259. ++buf;
  260. comp[1] = *buf;
  261. ++buf;
  262. diffuse.g = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
  263. comp[0] = *buf;
  264. ++buf;
  265. comp[1] = *buf;
  266. ++buf;
  267. diffuse.b = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
  268. if (7 == len)
  269. return true;
  270. comp[0] = *buf;
  271. ++buf;
  272. comp[1] = *buf;
  273. ++buf;
  274. diffuse.a = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
  275. return true;
  276. }
  277. void assignDiffuseColor(XmlNode &node, aiMaterial *mat) {
  278. const char *color = node.attribute(D3MF::XmlTag::basematerials_displaycolor.c_str()).as_string();
  279. aiColor4D diffuse;
  280. if (parseColor(color, diffuse)) {
  281. mat->AddProperty<aiColor4D>(&diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  282. }
  283. }
  284. aiMaterial *readMaterialDef(XmlNode &node) {
  285. aiMaterial *mat(nullptr);
  286. const char *name(nullptr);
  287. const std::string nodeName = node.name();
  288. if (nodeName == D3MF::XmlTag::basematerials_base) {
  289. name = node.attribute(D3MF::XmlTag::basematerials_name.c_str()).as_string();
  290. std::string stdMatName;
  291. aiString matName;
  292. std::string strId(to_string(mActiveMatGroup));
  293. stdMatName += "id";
  294. stdMatName += strId;
  295. stdMatName += "_";
  296. if (nullptr != name) {
  297. stdMatName += std::string(name);
  298. } else {
  299. stdMatName += "basemat";
  300. }
  301. matName.Set(stdMatName);
  302. mat = new aiMaterial;
  303. mat->AddProperty(&matName, AI_MATKEY_NAME);
  304. assignDiffuseColor(node, mat);
  305. }
  306. return mat;
  307. }
  308. private:
  309. struct MetaEntry {
  310. std::string name;
  311. std::string value;
  312. };
  313. std::vector<MetaEntry> mMetaData;
  314. std::vector<aiMesh *> mMeshes;
  315. MatArray mMatArray;
  316. unsigned int mActiveMatGroup;
  317. MatId2MatArray mMatId2MatArray;
  318. XmlParser *mXmlParser;
  319. };
  320. } //namespace D3MF
  321. static const aiImporterDesc desc = {
  322. "3mf Importer",
  323. "",
  324. "",
  325. "http://3mf.io/",
  326. aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
  327. 0,
  328. 0,
  329. 0,
  330. 0,
  331. "3mf"
  332. };
  333. D3MFImporter::D3MFImporter() :
  334. BaseImporter() {
  335. // empty
  336. }
  337. D3MFImporter::~D3MFImporter() {
  338. // empty
  339. }
  340. bool D3MFImporter::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig) const {
  341. const std::string extension(GetExtension(filename));
  342. if (extension == desc.mFileExtensions) {
  343. return true;
  344. } else if (!extension.length() || checkSig) {
  345. if (nullptr == pIOHandler) {
  346. return false;
  347. }
  348. if (!ZipArchiveIOSystem::isZipArchive(pIOHandler, filename)) {
  349. return false;
  350. }
  351. D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename);
  352. return opcPackage.validate();
  353. }
  354. return false;
  355. }
  356. void D3MFImporter::SetupProperties(const Importer * /*pImp*/) {
  357. // empty
  358. }
  359. const aiImporterDesc *D3MFImporter::GetInfo() const {
  360. return &desc;
  361. }
  362. void D3MFImporter::InternReadFile(const std::string &filename, aiScene *pScene, IOSystem *pIOHandler) {
  363. D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename);
  364. XmlParser xmlParser;
  365. if (xmlParser.parse(opcPackage.RootStream())) {
  366. D3MF::XmlSerializer xmlSerializer(&xmlParser);
  367. xmlSerializer.ImportXml(pScene);
  368. }
  369. }
  370. } // Namespace Assimp
  371. #endif // ASSIMP_BUILD_NO_3MF_IMPORTER