AMFImporter.cpp 24 KB

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