AMFImporter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. /// \file AMFImporter.cpp
  35. /// \brief AMF-format files importer for Assimp: main algorithm implementation.
  36. /// \date 2016
  37. /// \author [email protected]
  38. #ifndef ASSIMP_BUILD_NO_AMF_IMPORTER
  39. // Header files, Assimp.
  40. #include "AMFImporter.hpp"
  41. #include "AMFImporter_Macro.hpp"
  42. #include <assimp/DefaultIOSystem.h>
  43. #include <assimp/fast_atof.h>
  44. // Header files, stdlib.
  45. #include <memory>
  46. namespace Assimp {
  47. /// \var aiImporterDesc AMFImporter::Description
  48. /// Conastant which hold importer description
  49. const aiImporterDesc AMFImporter::Description = {
  50. "Additive manufacturing file format(AMF) Importer",
  51. "smalcom",
  52. "",
  53. "See documentation in source code. Chapter: Limitations.",
  54. aiImporterFlags_SupportTextFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
  55. 0,
  56. 0,
  57. 0,
  58. 0,
  59. "amf"
  60. };
  61. void AMFImporter::Clear() {
  62. mNodeElement_Cur = nullptr;
  63. mUnit.clear();
  64. mMaterial_Converted.clear();
  65. mTexture_Converted.clear();
  66. // Delete all elements
  67. if (!mNodeElement_List.empty()) {
  68. for (CAMFImporter_NodeElement *ne : mNodeElement_List) {
  69. delete ne;
  70. }
  71. mNodeElement_List.clear();
  72. }
  73. }
  74. AMFImporter::~AMFImporter() {
  75. if (mReader != nullptr) delete mReader;
  76. // Clear() is accounting if data already is deleted. So, just check again if all data is deleted.
  77. Clear();
  78. }
  79. /*********************************************************************************************************************************************/
  80. /************************************************************ Functions: find set ************************************************************/
  81. /*********************************************************************************************************************************************/
  82. bool AMFImporter::Find_NodeElement(const std::string &pID, const CAMFImporter_NodeElement::EType pType, CAMFImporter_NodeElement **pNodeElement) const {
  83. for (CAMFImporter_NodeElement *ne : mNodeElement_List) {
  84. if ((ne->ID == pID) && (ne->Type == pType)) {
  85. if (pNodeElement != nullptr) *pNodeElement = ne;
  86. return true;
  87. }
  88. } // for(CAMFImporter_NodeElement* ne: mNodeElement_List)
  89. return false;
  90. }
  91. bool AMFImporter::Find_ConvertedNode(const std::string &pID, std::list<aiNode *> &pNodeList, aiNode **pNode) const {
  92. aiString node_name(pID.c_str());
  93. for (aiNode *node : pNodeList) {
  94. if (node->mName == node_name) {
  95. if (pNode != nullptr) *pNode = node;
  96. return true;
  97. }
  98. } // for(aiNode* node: pNodeList)
  99. return false;
  100. }
  101. bool AMFImporter::Find_ConvertedMaterial(const std::string &pID, const SPP_Material **pConvertedMaterial) const {
  102. for (const SPP_Material &mat : mMaterial_Converted) {
  103. if (mat.ID == pID) {
  104. if (pConvertedMaterial != nullptr) *pConvertedMaterial = &mat;
  105. return true;
  106. }
  107. } // for(const SPP_Material& mat: mMaterial_Converted)
  108. return false;
  109. }
  110. /*********************************************************************************************************************************************/
  111. /************************************************************ Functions: throw set ***********************************************************/
  112. /*********************************************************************************************************************************************/
  113. void AMFImporter::Throw_CloseNotFound(const std::string &pNode) {
  114. throw DeadlyImportError("Close tag for node <" + pNode + "> not found. Seems file is corrupt.");
  115. }
  116. void AMFImporter::Throw_IncorrectAttr(const std::string &pAttrName) {
  117. throw DeadlyImportError("Node <" + std::string(mReader->getNodeName()) + "> has incorrect attribute \"" + pAttrName + "\".");
  118. }
  119. void AMFImporter::Throw_IncorrectAttrValue(const std::string &pAttrName) {
  120. throw DeadlyImportError("Attribute \"" + pAttrName + "\" in node <" + std::string(mReader->getNodeName()) + "> has incorrect value.");
  121. }
  122. void AMFImporter::Throw_MoreThanOnceDefined(const std::string &pNodeType, const std::string &pDescription) {
  123. throw DeadlyImportError("\"" + pNodeType + "\" node can be used only once in " + mReader->getNodeName() + ". Description: " + pDescription);
  124. }
  125. void AMFImporter::Throw_ID_NotFound(const std::string &pID) const {
  126. throw DeadlyImportError("Not found node with name \"" + pID + "\".");
  127. }
  128. /*********************************************************************************************************************************************/
  129. /************************************************************* Functions: XML set ************************************************************/
  130. /*********************************************************************************************************************************************/
  131. void AMFImporter::XML_CheckNode_MustHaveChildren() {
  132. if (mReader->isEmptyElement()) throw DeadlyImportError(std::string("Node <") + mReader->getNodeName() + "> must have children.");
  133. }
  134. void AMFImporter::XML_CheckNode_SkipUnsupported(const std::string &pParentNodeName) {
  135. static const size_t Uns_Skip_Len = 3;
  136. const char *Uns_Skip[Uns_Skip_Len] = { "composite", "edge", "normal" };
  137. static bool skipped_before[Uns_Skip_Len] = { false, false, false };
  138. std::string nn(mReader->getNodeName());
  139. bool found = false;
  140. bool close_found = false;
  141. size_t sk_idx;
  142. for (sk_idx = 0; sk_idx < Uns_Skip_Len; sk_idx++) {
  143. if (nn != Uns_Skip[sk_idx]) continue;
  144. found = true;
  145. if (mReader->isEmptyElement()) {
  146. close_found = true;
  147. goto casu_cres;
  148. }
  149. while (mReader->read()) {
  150. if ((mReader->getNodeType() == irr::io::EXN_ELEMENT_END) && (nn == mReader->getNodeName())) {
  151. close_found = true;
  152. goto casu_cres;
  153. }
  154. }
  155. } // for(sk_idx = 0; sk_idx < Uns_Skip_Len; sk_idx++)
  156. casu_cres:
  157. if (!found) throw DeadlyImportError("Unknown node \"" + nn + "\" in " + pParentNodeName + ".");
  158. if (!close_found) Throw_CloseNotFound(nn);
  159. if (!skipped_before[sk_idx]) {
  160. skipped_before[sk_idx] = true;
  161. ASSIMP_LOG_WARN_F("Skipping node \"", nn, "\" in ", pParentNodeName, ".");
  162. }
  163. }
  164. bool AMFImporter::XML_SearchNode(const std::string &pNodeName) {
  165. while (mReader->read()) {
  166. if ((mReader->getNodeType() == irr::io::EXN_ELEMENT) && XML_CheckNode_NameEqual(pNodeName)) return true;
  167. }
  168. return false;
  169. }
  170. bool AMFImporter::XML_ReadNode_GetAttrVal_AsBool(const int pAttrIdx) {
  171. std::string val(mReader->getAttributeValue(pAttrIdx));
  172. if ((val == "false") || (val == "0"))
  173. return false;
  174. else if ((val == "true") || (val == "1"))
  175. return true;
  176. else
  177. throw DeadlyImportError("Bool attribute value can contain \"false\"/\"0\" or \"true\"/\"1\" not the \"" + val + "\"");
  178. }
  179. float AMFImporter::XML_ReadNode_GetAttrVal_AsFloat(const int pAttrIdx) {
  180. std::string val;
  181. float tvalf;
  182. ParseHelper_FixTruncatedFloatString(mReader->getAttributeValue(pAttrIdx), val);
  183. fast_atoreal_move(val.c_str(), tvalf, false);
  184. return tvalf;
  185. }
  186. uint32_t AMFImporter::XML_ReadNode_GetAttrVal_AsU32(const int pAttrIdx) {
  187. return strtoul10(mReader->getAttributeValue(pAttrIdx));
  188. }
  189. float AMFImporter::XML_ReadNode_GetVal_AsFloat() {
  190. std::string val;
  191. float tvalf;
  192. if (!mReader->read()) throw DeadlyImportError("XML_ReadNode_GetVal_AsFloat. No data, seems file is corrupt.");
  193. if (mReader->getNodeType() != irr::io::EXN_TEXT) throw DeadlyImportError("XML_ReadNode_GetVal_AsFloat. Invalid type of XML element, seems file is corrupt.");
  194. ParseHelper_FixTruncatedFloatString(mReader->getNodeData(), val);
  195. fast_atoreal_move(val.c_str(), tvalf, false);
  196. return tvalf;
  197. }
  198. uint32_t AMFImporter::XML_ReadNode_GetVal_AsU32() {
  199. if (!mReader->read()) throw DeadlyImportError("XML_ReadNode_GetVal_AsU32. No data, seems file is corrupt.");
  200. if (mReader->getNodeType() != irr::io::EXN_TEXT) throw DeadlyImportError("XML_ReadNode_GetVal_AsU32. Invalid type of XML element, seems file is corrupt.");
  201. return strtoul10(mReader->getNodeData());
  202. }
  203. void AMFImporter::XML_ReadNode_GetVal_AsString(std::string &pValue) {
  204. if (!mReader->read()) throw DeadlyImportError("XML_ReadNode_GetVal_AsString. No data, seems file is corrupt.");
  205. if (mReader->getNodeType() != irr::io::EXN_TEXT)
  206. throw DeadlyImportError("XML_ReadNode_GetVal_AsString. Invalid type of XML element, seems file is corrupt.");
  207. pValue = mReader->getNodeData();
  208. }
  209. /*********************************************************************************************************************************************/
  210. /************************************************************ Functions: parse set ***********************************************************/
  211. /*********************************************************************************************************************************************/
  212. void AMFImporter::ParseHelper_Node_Enter(CAMFImporter_NodeElement *pNode) {
  213. mNodeElement_Cur->Child.push_back(pNode); // add new element to current element child list.
  214. mNodeElement_Cur = pNode; // switch current element to new one.
  215. }
  216. void AMFImporter::ParseHelper_Node_Exit() {
  217. // check if we can walk up.
  218. if (mNodeElement_Cur != nullptr) mNodeElement_Cur = mNodeElement_Cur->Parent;
  219. }
  220. void AMFImporter::ParseHelper_FixTruncatedFloatString(const char *pInStr, std::string &pOutString) {
  221. size_t instr_len;
  222. pOutString.clear();
  223. instr_len = strlen(pInStr);
  224. if (!instr_len) return;
  225. pOutString.reserve(instr_len * 3 / 2);
  226. // check and correct floats in format ".x". Must be "x.y".
  227. if (pInStr[0] == '.') pOutString.push_back('0');
  228. pOutString.push_back(pInStr[0]);
  229. for (size_t ci = 1; ci < instr_len; ci++) {
  230. if ((pInStr[ci] == '.') && ((pInStr[ci - 1] == ' ') || (pInStr[ci - 1] == '-') || (pInStr[ci - 1] == '+') || (pInStr[ci - 1] == '\t'))) {
  231. pOutString.push_back('0');
  232. pOutString.push_back('.');
  233. } else {
  234. pOutString.push_back(pInStr[ci]);
  235. }
  236. }
  237. }
  238. static bool ParseHelper_Decode_Base64_IsBase64(const char pChar) {
  239. return (isalnum(pChar) || (pChar == '+') || (pChar == '/'));
  240. }
  241. void AMFImporter::ParseHelper_Decode_Base64(const std::string &pInputBase64, std::vector<uint8_t> &pOutputData) const {
  242. // With help from
  243. // René Nyffenegger http://www.adp-gmbh.ch/cpp/common/base64.html
  244. const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  245. uint8_t tidx = 0;
  246. uint8_t arr4[4], arr3[3];
  247. // check input data
  248. if (pInputBase64.size() % 4) throw DeadlyImportError("Base64-encoded data must have size multiply of four.");
  249. // prepare output place
  250. pOutputData.clear();
  251. pOutputData.reserve(pInputBase64.size() / 4 * 3);
  252. for (size_t in_len = pInputBase64.size(), in_idx = 0; (in_len > 0) && (pInputBase64[in_idx] != '='); in_len--) {
  253. if (ParseHelper_Decode_Base64_IsBase64(pInputBase64[in_idx])) {
  254. arr4[tidx++] = pInputBase64[in_idx++];
  255. if (tidx == 4) {
  256. for (tidx = 0; tidx < 4; tidx++)
  257. arr4[tidx] = (uint8_t)base64_chars.find(arr4[tidx]);
  258. arr3[0] = (arr4[0] << 2) + ((arr4[1] & 0x30) >> 4);
  259. arr3[1] = ((arr4[1] & 0x0F) << 4) + ((arr4[2] & 0x3C) >> 2);
  260. arr3[2] = ((arr4[2] & 0x03) << 6) + arr4[3];
  261. for (tidx = 0; tidx < 3; tidx++)
  262. pOutputData.push_back(arr3[tidx]);
  263. tidx = 0;
  264. } // if(tidx == 4)
  265. } // if(ParseHelper_Decode_Base64_IsBase64(pInputBase64[in_idx]))
  266. else {
  267. in_idx++;
  268. } // if(ParseHelper_Decode_Base64_IsBase64(pInputBase64[in_idx])) else
  269. }
  270. if (tidx) {
  271. for (uint8_t i = tidx; i < 4; i++)
  272. arr4[i] = 0;
  273. for (uint8_t i = 0; i < 4; i++)
  274. arr4[i] = (uint8_t)(base64_chars.find(arr4[i]));
  275. arr3[0] = (arr4[0] << 2) + ((arr4[1] & 0x30) >> 4);
  276. arr3[1] = ((arr4[1] & 0x0F) << 4) + ((arr4[2] & 0x3C) >> 2);
  277. arr3[2] = ((arr4[2] & 0x03) << 6) + arr4[3];
  278. for (uint8_t i = 0; i < (tidx - 1); i++)
  279. pOutputData.push_back(arr3[i]);
  280. }
  281. }
  282. void AMFImporter::ParseFile(const std::string &pFile, IOSystem *pIOHandler) {
  283. irr::io::IrrXMLReader *OldReader = mReader; // store current XMLreader.
  284. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
  285. // Check whether we can read from the file
  286. if (file.get() == nullptr) {
  287. throw DeadlyImportError("Failed to open AMF file " + pFile + ".");
  288. }
  289. // generate a XML reader for it
  290. std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper(new CIrrXML_IOStreamReader(file.get()));
  291. mReader = irr::io::createIrrXMLReader(mIOWrapper.get());
  292. if (!mReader) throw DeadlyImportError("Failed to create XML reader for file" + pFile + ".");
  293. //
  294. // start reading
  295. // search for root tag <amf>
  296. if (XML_SearchNode("amf"))
  297. ParseNode_Root();
  298. else
  299. throw DeadlyImportError("Root node \"amf\" not found.");
  300. delete mReader;
  301. // restore old XMLreader
  302. mReader = OldReader;
  303. }
  304. // <amf
  305. // unit="" - The units to be used. May be "inch", "millimeter", "meter", "feet", or "micron".
  306. // version="" - Version of file format.
  307. // >
  308. // </amf>
  309. // Root XML element.
  310. // Multi elements - No.
  311. void AMFImporter::ParseNode_Root() {
  312. std::string unit, version;
  313. CAMFImporter_NodeElement *ne(nullptr);
  314. // Read attributes for node <amf>.
  315. MACRO_ATTRREAD_LOOPBEG;
  316. MACRO_ATTRREAD_CHECK_RET("unit", unit, mReader->getAttributeValue);
  317. MACRO_ATTRREAD_CHECK_RET("version", version, mReader->getAttributeValue);
  318. MACRO_ATTRREAD_LOOPEND_WSKIP;
  319. // Check attributes
  320. if (!mUnit.empty()) {
  321. if ((mUnit != "inch") && (mUnit != "millimeter") && (mUnit != "meter") && (mUnit != "feet") && (mUnit != "micron")) Throw_IncorrectAttrValue("unit");
  322. }
  323. // create root node element.
  324. ne = new CAMFImporter_NodeElement_Root(nullptr);
  325. mNodeElement_Cur = ne; // set first "current" element
  326. // and assign attribute's values
  327. ((CAMFImporter_NodeElement_Root *)ne)->Unit = unit;
  328. ((CAMFImporter_NodeElement_Root *)ne)->Version = version;
  329. // Check for child nodes
  330. if (!mReader->isEmptyElement()) {
  331. MACRO_NODECHECK_LOOPBEGIN("amf");
  332. if (XML_CheckNode_NameEqual("object")) {
  333. ParseNode_Object();
  334. continue;
  335. }
  336. if (XML_CheckNode_NameEqual("material")) {
  337. ParseNode_Material();
  338. continue;
  339. }
  340. if (XML_CheckNode_NameEqual("texture")) {
  341. ParseNode_Texture();
  342. continue;
  343. }
  344. if (XML_CheckNode_NameEqual("constellation")) {
  345. ParseNode_Constellation();
  346. continue;
  347. }
  348. if (XML_CheckNode_NameEqual("metadata")) {
  349. ParseNode_Metadata();
  350. continue;
  351. }
  352. MACRO_NODECHECK_LOOPEND("amf");
  353. mNodeElement_Cur = ne; // force restore "current" element
  354. } // if(!mReader->isEmptyElement())
  355. mNodeElement_List.push_back(ne); // add to node element list because its a new object in graph.
  356. }
  357. // <constellation
  358. // id="" - The Object ID of the new constellation being defined.
  359. // >
  360. // </constellation>
  361. // A collection of objects or constellations with specific relative locations.
  362. // Multi elements - Yes.
  363. // Parent element - <amf>.
  364. void AMFImporter::ParseNode_Constellation() {
  365. std::string id;
  366. CAMFImporter_NodeElement *ne(nullptr);
  367. // Read attributes for node <constellation>.
  368. MACRO_ATTRREAD_LOOPBEG;
  369. MACRO_ATTRREAD_CHECK_RET("id", id, mReader->getAttributeValue);
  370. MACRO_ATTRREAD_LOOPEND;
  371. // create and if needed - define new grouping object.
  372. ne = new CAMFImporter_NodeElement_Constellation(mNodeElement_Cur);
  373. CAMFImporter_NodeElement_Constellation &als = *((CAMFImporter_NodeElement_Constellation *)ne); // alias for convenience
  374. if (!id.empty()) als.ID = id;
  375. // Check for child nodes
  376. if (!mReader->isEmptyElement()) {
  377. ParseHelper_Node_Enter(ne);
  378. MACRO_NODECHECK_LOOPBEGIN("constellation");
  379. if (XML_CheckNode_NameEqual("instance")) {
  380. ParseNode_Instance();
  381. continue;
  382. }
  383. if (XML_CheckNode_NameEqual("metadata")) {
  384. ParseNode_Metadata();
  385. continue;
  386. }
  387. MACRO_NODECHECK_LOOPEND("constellation");
  388. ParseHelper_Node_Exit();
  389. } // if(!mReader->isEmptyElement())
  390. else {
  391. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  392. } // if(!mReader->isEmptyElement()) else
  393. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  394. }
  395. // <instance
  396. // objectid="" - The Object ID of the new constellation being defined.
  397. // >
  398. // </instance>
  399. // A collection of objects or constellations with specific relative locations.
  400. // Multi elements - Yes.
  401. // Parent element - <amf>.
  402. void AMFImporter::ParseNode_Instance() {
  403. std::string objectid;
  404. CAMFImporter_NodeElement *ne(nullptr);
  405. // Read attributes for node <constellation>.
  406. MACRO_ATTRREAD_LOOPBEG;
  407. MACRO_ATTRREAD_CHECK_RET("objectid", objectid, mReader->getAttributeValue);
  408. MACRO_ATTRREAD_LOOPEND;
  409. // used object id must be defined, check that.
  410. if (objectid.empty()) throw DeadlyImportError("\"objectid\" in <instance> must be defined.");
  411. // create and define new grouping object.
  412. ne = new CAMFImporter_NodeElement_Instance(mNodeElement_Cur);
  413. CAMFImporter_NodeElement_Instance &als = *((CAMFImporter_NodeElement_Instance *)ne); // alias for convenience
  414. als.ObjectID = objectid;
  415. // Check for child nodes
  416. if (!mReader->isEmptyElement()) {
  417. bool read_flag[6] = { false, false, false, false, false, false };
  418. als.Delta.Set(0, 0, 0);
  419. als.Rotation.Set(0, 0, 0);
  420. ParseHelper_Node_Enter(ne);
  421. MACRO_NODECHECK_LOOPBEGIN("instance");
  422. MACRO_NODECHECK_READCOMP_F("deltax", read_flag[0], als.Delta.x);
  423. MACRO_NODECHECK_READCOMP_F("deltay", read_flag[1], als.Delta.y);
  424. MACRO_NODECHECK_READCOMP_F("deltaz", read_flag[2], als.Delta.z);
  425. MACRO_NODECHECK_READCOMP_F("rx", read_flag[3], als.Rotation.x);
  426. MACRO_NODECHECK_READCOMP_F("ry", read_flag[4], als.Rotation.y);
  427. MACRO_NODECHECK_READCOMP_F("rz", read_flag[5], als.Rotation.z);
  428. MACRO_NODECHECK_LOOPEND("instance");
  429. ParseHelper_Node_Exit();
  430. // also convert degrees to radians.
  431. als.Rotation.x = AI_MATH_PI_F * als.Rotation.x / 180.0f;
  432. als.Rotation.y = AI_MATH_PI_F * als.Rotation.y / 180.0f;
  433. als.Rotation.z = AI_MATH_PI_F * als.Rotation.z / 180.0f;
  434. } // if(!mReader->isEmptyElement())
  435. else {
  436. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  437. } // if(!mReader->isEmptyElement()) else
  438. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  439. }
  440. // <object
  441. // id="" - A unique ObjectID for the new object being defined.
  442. // >
  443. // </object>
  444. // An object definition.
  445. // Multi elements - Yes.
  446. // Parent element - <amf>.
  447. void AMFImporter::ParseNode_Object() {
  448. std::string id;
  449. CAMFImporter_NodeElement *ne(nullptr);
  450. // Read attributes for node <object>.
  451. MACRO_ATTRREAD_LOOPBEG;
  452. MACRO_ATTRREAD_CHECK_RET("id", id, mReader->getAttributeValue);
  453. MACRO_ATTRREAD_LOOPEND;
  454. // create and if needed - define new geometry object.
  455. ne = new CAMFImporter_NodeElement_Object(mNodeElement_Cur);
  456. CAMFImporter_NodeElement_Object &als = *((CAMFImporter_NodeElement_Object *)ne); // alias for convenience
  457. if (!id.empty()) als.ID = id;
  458. // Check for child nodes
  459. if (!mReader->isEmptyElement()) {
  460. bool col_read = false;
  461. ParseHelper_Node_Enter(ne);
  462. MACRO_NODECHECK_LOOPBEGIN("object");
  463. if (XML_CheckNode_NameEqual("color")) {
  464. // Check if color already defined for object.
  465. if (col_read) Throw_MoreThanOnceDefined("color", "Only one color can be defined for <object>.");
  466. // read data and set flag about it
  467. ParseNode_Color();
  468. col_read = true;
  469. continue;
  470. }
  471. if (XML_CheckNode_NameEqual("mesh")) {
  472. ParseNode_Mesh();
  473. continue;
  474. }
  475. if (XML_CheckNode_NameEqual("metadata")) {
  476. ParseNode_Metadata();
  477. continue;
  478. }
  479. MACRO_NODECHECK_LOOPEND("object");
  480. ParseHelper_Node_Exit();
  481. } // if(!mReader->isEmptyElement())
  482. else {
  483. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  484. } // if(!mReader->isEmptyElement()) else
  485. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  486. }
  487. // <metadata
  488. // type="" - The type of the attribute.
  489. // >
  490. // </metadata>
  491. // Specify additional information about an entity.
  492. // Multi elements - Yes.
  493. // Parent element - <amf>, <object>, <volume>, <material>, <vertex>.
  494. //
  495. // Reserved types are:
  496. // "Name" - The alphanumeric label of the entity, to be used by the interpreter if interacting with the user.
  497. // "Description" - A description of the content of the entity
  498. // "URL" - A link to an external resource relating to the entity
  499. // "Author" - Specifies the name(s) of the author(s) of the entity
  500. // "Company" - Specifying the company generating the entity
  501. // "CAD" - specifies the name of the originating CAD software and version
  502. // "Revision" - specifies the revision of the entity
  503. // "Tolerance" - specifies the desired manufacturing tolerance of the entity in entity's unit system
  504. // "Volume" - specifies the total volume of the entity, in the entity's unit system, to be used for verification (object and volume only)
  505. void AMFImporter::ParseNode_Metadata() {
  506. std::string type, value;
  507. CAMFImporter_NodeElement *ne(nullptr);
  508. // read attribute
  509. MACRO_ATTRREAD_LOOPBEG;
  510. MACRO_ATTRREAD_CHECK_RET("type", type, mReader->getAttributeValue);
  511. MACRO_ATTRREAD_LOOPEND;
  512. // and value of node.
  513. value = mReader->getNodeData();
  514. // Create node element and assign read data.
  515. ne = new CAMFImporter_NodeElement_Metadata(mNodeElement_Cur);
  516. ((CAMFImporter_NodeElement_Metadata *)ne)->Type = type;
  517. ((CAMFImporter_NodeElement_Metadata *)ne)->Value = value;
  518. mNodeElement_Cur->Child.push_back(ne); // Add element to child list of current element
  519. mNodeElement_List.push_back(ne); // and to node element list because its a new object in graph.
  520. }
  521. /*********************************************************************************************************************************************/
  522. /******************************************************** Functions: BaseImporter set ********************************************************/
  523. /*********************************************************************************************************************************************/
  524. bool AMFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool pCheckSig) const {
  525. const std::string extension = GetExtension(pFile);
  526. if (extension == "amf") {
  527. return true;
  528. }
  529. if (!extension.length() || pCheckSig) {
  530. const char *tokens[] = { "<amf" };
  531. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
  532. }
  533. return false;
  534. }
  535. void AMFImporter::GetExtensionList(std::set<std::string> &pExtensionList) {
  536. pExtensionList.insert("amf");
  537. }
  538. const aiImporterDesc *AMFImporter::GetInfo() const {
  539. return &Description;
  540. }
  541. void AMFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
  542. Clear(); // delete old graph.
  543. ParseFile(pFile, pIOHandler);
  544. Postprocess_BuildScene(pScene);
  545. // scene graph is ready, exit.
  546. }
  547. } // namespace Assimp
  548. #endif // !ASSIMP_BUILD_NO_AMF_IMPORTER