AMFImporter.cpp 24 KB

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