serialize_xml.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Christian Schüller <schuellchr@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "serialize_xml.h"
  9. namespace igl
  10. {
  11. template <typename T>
  12. IGL_INLINE void serialize_xml(const T& obj,const std::string& filename)
  13. {
  14. serialize_xml(obj,"object",filename,false,true);
  15. }
  16. template <typename T>
  17. IGL_INLINE void serialize_xml(const T& obj,const std::string& objectName,const std::string& filename,bool binary,bool overwrite)
  18. {
  19. tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
  20. if(overwrite == false)
  21. {
  22. // Check if file exists
  23. tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
  24. if(error != tinyxml2::XML_NO_ERROR)
  25. {
  26. doc->Clear();
  27. }
  28. }
  29. tinyxml2::XMLElement* element = doc->FirstChildElement("serialization");
  30. if(element == NULL)
  31. {
  32. element = doc->NewElement("serialization");
  33. doc->InsertEndChild(element);
  34. }
  35. serialize_xml(obj,objectName,doc,element,binary);
  36. // Save
  37. tinyxml2::XMLError error = doc->SaveFile(filename.c_str());
  38. if(error != tinyxml2::XML_NO_ERROR)
  39. {
  40. doc->PrintError();
  41. }
  42. delete doc;
  43. }
  44. template <typename T>
  45. IGL_INLINE void serialize_xml(const T& obj,const std::string& objectName,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,bool binary)
  46. {
  47. static_assert(serialization_xml::is_serializable<T>::value,"'igl::serialize_xml': type is not serializable");
  48. std::string name(objectName);
  49. serialization_xml::encodeXMLElementName(name);
  50. tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  51. if(child != NULL)
  52. element->DeleteChild(child);
  53. child = doc->NewElement(name.c_str());
  54. element->InsertEndChild(child);
  55. if(binary)
  56. {
  57. std::vector<char> buffer;
  58. serialize(obj,name,buffer);
  59. std::string data = serialization_xml::base64_encode(reinterpret_cast<const unsigned char*>(buffer.data()),buffer.size());
  60. child->SetAttribute("binary",true);
  61. serialization_xml::serialize(data,doc,element,name);
  62. }
  63. else
  64. {
  65. serialization_xml::serialize(obj,doc,element,name);
  66. }
  67. }
  68. template <typename T>
  69. IGL_INLINE void deserialize_xml(T& obj,const std::string& filename)
  70. {
  71. deserialize_xml(obj,"object",filename);
  72. }
  73. template <typename T>
  74. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const std::string& filename)
  75. {
  76. tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
  77. tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
  78. if(error != tinyxml2::XML_NO_ERROR)
  79. {
  80. std::cerr << "File not found!" << std::endl;
  81. doc->PrintError();
  82. doc = NULL;
  83. }
  84. else
  85. {
  86. tinyxml2::XMLElement* element = doc->FirstChildElement("serialization");
  87. if(element == NULL)
  88. {
  89. std::cerr << "Name of object not found! Initialized with default value." << std::endl;
  90. obj = T();
  91. }
  92. else
  93. {
  94. deserialize_xml(obj,objectName,doc,element);
  95. }
  96. delete doc;
  97. }
  98. }
  99. template <typename T>
  100. IGL_INLINE void deserialize_xml(T& obj,const std::string& objectName,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
  101. {
  102. static_assert(serialization::is_serializable<T>::value,"'igl::deserialize_xml': type is not deserializable");
  103. std::string name(objectName);
  104. serialization_xml::encodeXMLElementName(name);
  105. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  106. if(child != NULL)
  107. {
  108. bool isBinary = false;
  109. const tinyxml2::XMLAttribute* attr = child->FindAttribute("binary");
  110. if(attr != NULL)
  111. {
  112. std::string code;
  113. serialization_xml::deserialize(code,doc,element,name);
  114. std::string decoded = serialization_xml::base64_decode(code);
  115. std::vector<char> buffer;
  116. std::copy(decoded.c_str(),decoded.c_str()+decoded.length(),std::back_inserter(buffer));
  117. deserialize(obj,name,buffer);
  118. }
  119. else
  120. {
  121. serialization_xml::deserialize(obj,doc,element,name);
  122. }
  123. }
  124. }
  125. IGL_INLINE bool XMLSerializable::PreSerialization() const
  126. {
  127. return true;
  128. }
  129. IGL_INLINE void XMLSerializable::PostSerialization() const
  130. {
  131. }
  132. IGL_INLINE bool XMLSerializable::PreDeserialization()
  133. {
  134. return true;
  135. }
  136. IGL_INLINE void XMLSerializable::PostDeserialization()
  137. {
  138. }
  139. IGL_INLINE void XMLSerializable::Serialize(std::vector<char>& buffer) const
  140. {
  141. if(this->PreSerialization())
  142. {
  143. if(initialized == false)
  144. {
  145. objects.clear();
  146. (const_cast<XMLSerializable*>(this))->InitSerialization();
  147. initialized = true;
  148. }
  149. for(unsigned int i=0;i<objects.size();i++)
  150. objects[i]->Serialize(buffer);
  151. this->PostSerialization();
  152. }
  153. }
  154. IGL_INLINE void XMLSerializable::Deserialize(const std::vector<char>& buffer)
  155. {
  156. if(this->PreDeserialization())
  157. {
  158. if(initialized == false)
  159. {
  160. objects.clear();
  161. (const_cast<XMLSerializable*>(this))->InitSerialization();
  162. initialized = true;
  163. }
  164. for(unsigned int i=0;i<objects.size();i++)
  165. objects[i]->Deserialize(buffer);
  166. this->PostDeserialization();
  167. }
  168. }
  169. IGL_INLINE void XMLSerializable::Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const
  170. {
  171. if(this->PreSerialization())
  172. {
  173. if(initialized == false)
  174. {
  175. objects.clear();
  176. (const_cast<XMLSerializable*>(this))->InitSerialization();
  177. initialized = true;
  178. }
  179. for(unsigned int i=0;i<objects.size();i++)
  180. objects[i]->Serialize(doc,element);
  181. this->PostSerialization();
  182. }
  183. }
  184. IGL_INLINE void XMLSerializable::Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
  185. {
  186. if(this->PreDeserialization())
  187. {
  188. if(initialized == false)
  189. {
  190. objects.clear();
  191. (const_cast<XMLSerializable*>(this))->InitSerialization();
  192. initialized = true;
  193. }
  194. for(unsigned int i=0;i<objects.size();i++)
  195. objects[i]->Deserialize(doc,element);
  196. this->PostDeserialization();
  197. }
  198. }
  199. IGL_INLINE XMLSerializable::XMLSerializable()
  200. {
  201. initialized = false;
  202. }
  203. IGL_INLINE XMLSerializable::XMLSerializable(const XMLSerializable& obj)
  204. {
  205. initialized = false;
  206. objects.clear();
  207. }
  208. IGL_INLINE XMLSerializable::~XMLSerializable()
  209. {
  210. initialized = false;
  211. objects.clear();
  212. }
  213. IGL_INLINE XMLSerializable& XMLSerializable::operator=(const XMLSerializable& obj)
  214. {
  215. if(this != &obj)
  216. {
  217. if(initialized)
  218. {
  219. initialized = false;
  220. objects.clear();
  221. }
  222. }
  223. return *this;
  224. }
  225. template <typename T>
  226. IGL_INLINE void XMLSerializable::Add(T& obj,std::string name,bool binary)
  227. {
  228. XMLSerializationObject<T>* object = new XMLSerializationObject<T>();
  229. object->Binary = binary;
  230. object->Name = name;
  231. object->Object = &obj;
  232. objects.push_back(object);
  233. }
  234. namespace serialization_xml
  235. {
  236. // fundamental types
  237. template <typename T>
  238. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  239. {
  240. tinyxml2::XMLElement* child = getElement(doc,element,name.c_str());
  241. child->SetAttribute("val",obj);
  242. }
  243. template <typename T>
  244. IGL_INLINE typename std::enable_if<std::is_fundamental<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  245. {
  246. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  247. if(child != NULL)
  248. {
  249. getAttribute(child->Attribute("val"),obj);
  250. }
  251. else
  252. {
  253. obj = T();
  254. }
  255. }
  256. // std::string
  257. IGL_INLINE void serialize(const std::string& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  258. {
  259. tinyxml2::XMLElement* child = getElement(doc,element,name.c_str());
  260. child->SetAttribute("val",obj.c_str());
  261. }
  262. IGL_INLINE void deserialize(std::string& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  263. {
  264. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  265. if(child != NULL)
  266. {
  267. getAttribute(child->Attribute("val"),obj);
  268. }
  269. else
  270. {
  271. obj = std::string("");
  272. }
  273. }
  274. // Serializable
  275. template <typename T>
  276. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  277. {
  278. // Serialize object implementing Serializable interface
  279. const XMLSerializableBase& object = dynamic_cast<const XMLSerializableBase&>(obj);
  280. tinyxml2::XMLElement* child = getElement(doc,element,name.c_str());
  281. object.Serialize(doc,child);
  282. }
  283. template <typename T>
  284. IGL_INLINE typename std::enable_if<std::is_base_of<XMLSerializableBase,T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  285. {
  286. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  287. if(child != NULL)
  288. {
  289. obj.Deserialize(doc,child);
  290. }
  291. else
  292. {
  293. obj = T();
  294. }
  295. }
  296. // STL containers
  297. template <typename T1,typename T2>
  298. IGL_INLINE void serialize(const std::pair<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  299. {
  300. tinyxml2::XMLElement* pair = getElement(doc,element,name.c_str());
  301. serialize(obj.first,doc,pair,"first");
  302. serialize(obj.second,doc,pair,"second");
  303. }
  304. template <typename T1,typename T2>
  305. IGL_INLINE void deserialize(std::pair<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  306. {
  307. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  308. if(child != NULL)
  309. {
  310. deserialize(obj.first,doc,child,"first");
  311. deserialize(obj.second,doc,child,"second");
  312. }
  313. else
  314. {
  315. obj.first = T1();
  316. obj.second = T2();
  317. }
  318. }
  319. template <typename T1,typename T2>
  320. IGL_INLINE void serialize(const std::vector<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  321. {
  322. tinyxml2::XMLElement* vector = getElement(doc,element,name.c_str());
  323. vector->SetAttribute("size",(unsigned int)obj.size());
  324. std::stringstream num;
  325. for(unsigned int i=0;i<obj.size();i++)
  326. {
  327. num.str("");
  328. num << "value" << i;
  329. serialize(obj[i],doc,vector,num.str());
  330. }
  331. }
  332. template <typename T1,typename T2>
  333. IGL_INLINE void deserialize(std::vector<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  334. {
  335. obj.clear();
  336. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  337. if(child != NULL)
  338. {
  339. unsigned int size = child->UnsignedAttribute("size");
  340. obj.resize(size);
  341. std::stringstream num;
  342. for(unsigned int i=0;i<size;i++)
  343. {
  344. num.str("");
  345. num << "value" << i;
  346. deserialize(obj[i],doc,child,num.str());
  347. }
  348. }
  349. else
  350. {
  351. obj.clear();
  352. }
  353. }
  354. template <typename T>
  355. IGL_INLINE void serialize(const std::set<T>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  356. {
  357. tinyxml2::XMLElement* set = getElement(doc,element,name.c_str());
  358. set->SetAttribute("size",(unsigned int)obj.size());
  359. std::stringstream num;
  360. typename std::set<T>::iterator iter = obj.begin();
  361. for(int i=0;iter!=obj.end();iter++,i++)
  362. {
  363. num.str("");
  364. num << "value" << i;
  365. serialize((T)*iter,doc,set,num.str());
  366. }
  367. }
  368. template <typename T>
  369. IGL_INLINE void deserialize(std::set<T>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  370. {
  371. obj.clear();
  372. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  373. if(child != NULL)
  374. {
  375. unsigned int size = child->UnsignedAttribute("size");
  376. std::stringstream num;
  377. typename std::set<T>::iterator iter = obj.begin();
  378. for(int i=0;i<size;i++)
  379. {
  380. num.str("");
  381. num << "value" << i;
  382. T val;
  383. deserialize(val,doc,child,num.str());
  384. obj.insert(val);
  385. }
  386. }
  387. else
  388. {
  389. obj.clear();
  390. }
  391. }
  392. template <typename T1,typename T2>
  393. IGL_INLINE void serialize(const std::map<T1,T2>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  394. {
  395. tinyxml2::XMLElement* map = getElement(doc,element,name.c_str());
  396. map->SetAttribute("size",(unsigned int)obj.size());
  397. std::stringstream num;
  398. typename std::map<T1,T2>::const_iterator iter = obj.cbegin();
  399. for(int i=0;iter!=obj.end();iter++,i++)
  400. {
  401. num.str("");
  402. num << "value" << i;
  403. serialize((std::pair<T1,T2>)*iter,doc,map,num.str());
  404. }
  405. }
  406. template <typename T1,typename T2>
  407. IGL_INLINE void deserialize(std::map<T1,T2>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  408. {
  409. obj.clear();
  410. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  411. if(child != NULL)
  412. {
  413. unsigned int size = child->UnsignedAttribute("size");
  414. std::stringstream num;
  415. typename std::map<T1,T2>::iterator iter = obj.begin();
  416. for(int i=0;i<size;i++)
  417. {
  418. num.str("");
  419. num << "value" << i;
  420. std::pair<T1,T2> pair;
  421. deserialize(pair,doc,child,num.str());
  422. obj.insert(pair);
  423. }
  424. }
  425. else
  426. {
  427. obj.clear();
  428. }
  429. }
  430. // Eigen types
  431. template<typename T,int R,int C,int P,int MR,int MC>
  432. IGL_INLINE void serialize(const Eigen::Matrix<T,R,C,P,MR,MC>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  433. {
  434. tinyxml2::XMLElement* matrix = getElement(doc,element,name.c_str());
  435. const unsigned int rows = obj.rows();
  436. const unsigned int cols = obj.cols();
  437. matrix->SetAttribute("rows",rows);
  438. matrix->SetAttribute("cols",cols);
  439. char buffer[200];
  440. std::stringstream ms;
  441. ms << "\n";
  442. for(unsigned int r=0;r<rows;r++)
  443. {
  444. for(unsigned int c=0;c<cols;c++)
  445. {
  446. tinyxml2::XMLUtil::ToStr(obj(r,c),buffer,200);
  447. ms << buffer << ",";
  448. }
  449. ms << "\n";
  450. }
  451. std::string mString = ms.str();
  452. if(mString.size() > 1)
  453. mString[mString.size()-2] = '\0';
  454. matrix->SetAttribute("matrix",mString.c_str());
  455. }
  456. template<typename T,int R,int C,int P,int MR,int MC>
  457. IGL_INLINE void deserialize(Eigen::Matrix<T,R,C,P,MR,MC>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  458. {
  459. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  460. bool initialized = false;
  461. if(child != NULL)
  462. {
  463. const unsigned int rows = child->UnsignedAttribute("rows");
  464. const unsigned int cols = child->UnsignedAttribute("cols");
  465. if(rows > 0 && cols > 0)
  466. {
  467. obj.resize(rows,cols);
  468. const tinyxml2::XMLAttribute* attribute = child->FindAttribute("matrix");
  469. if(attribute != NULL)
  470. {
  471. std::string matTemp;
  472. getAttribute(attribute->Value(),matTemp);
  473. std::string line,srows,scols;
  474. std::stringstream mats;
  475. mats << matTemp;
  476. int r=0;
  477. std::string val;
  478. // for each line
  479. getline(mats,line);
  480. while(getline(mats,line))
  481. {
  482. // get current line
  483. std::stringstream liness(line);
  484. for(unsigned int c=0;c<cols-1;c++)
  485. {
  486. // split line
  487. getline(liness,val,',');
  488. // push pack the data if any
  489. if(!val.empty())
  490. getAttribute(val.c_str(),obj.coeffRef(r,c));
  491. }
  492. getline(liness,val);
  493. getAttribute(val.c_str(),obj.coeffRef(r,cols-1));
  494. r++;
  495. }
  496. initialized = true;
  497. }
  498. }
  499. }
  500. if(!initialized)
  501. {
  502. obj = Eigen::Matrix<T,R,C,P,MR,MC>();
  503. }
  504. }
  505. template<typename T,int P,typename I>
  506. IGL_INLINE void serialize(const Eigen::SparseMatrix<T,P,I>& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  507. {
  508. tinyxml2::XMLElement* matrix = getElement(doc,element,name.c_str());
  509. const unsigned int rows = obj.rows();
  510. const unsigned int cols = obj.cols();
  511. matrix->SetAttribute("rows",rows);
  512. matrix->SetAttribute("cols",cols);
  513. char buffer[200];
  514. std::stringstream ms;
  515. ms << "\n";
  516. for(int k=0;k<obj.outerSize();++k)
  517. {
  518. for(typename Eigen::SparseMatrix<T,P,I>::InnerIterator it(obj,k);it;++it)
  519. {
  520. tinyxml2::XMLUtil::ToStr(it.value(),buffer,200);
  521. ms << it.row() << "," << it.col() << "," << buffer << "\n";
  522. }
  523. }
  524. std::string mString = ms.str();
  525. if(mString.size() > 0)
  526. mString[mString.size()-1] = '\0';
  527. matrix->SetAttribute("matrix",mString.c_str());
  528. }
  529. template<typename T,int P,typename I>
  530. IGL_INLINE void deserialize(Eigen::SparseMatrix<T,P,I>& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  531. {
  532. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  533. bool initialized = false;
  534. if(child != NULL)
  535. {
  536. const unsigned int rows = child->UnsignedAttribute("rows");
  537. const unsigned int cols = child->UnsignedAttribute("cols");
  538. if(rows > 0 && cols > 0)
  539. {
  540. obj.resize(rows,cols);
  541. obj.setZero();
  542. const tinyxml2::XMLAttribute* attribute = child->FindAttribute("matrix");
  543. if(attribute != NULL)
  544. {
  545. std::string matTemp;
  546. getAttribute(attribute->Value(),matTemp);
  547. std::string line,srows,scols;
  548. std::stringstream mats;
  549. mats << matTemp;
  550. std::vector<Eigen::Triplet<T,I> > triplets;
  551. int r=0;
  552. std::string val;
  553. // for each line
  554. getline(mats,line);
  555. while(getline(mats,line))
  556. {
  557. // get current line
  558. std::stringstream liness(line);
  559. // row
  560. getline(liness,val,',');
  561. int row = atoi(val.c_str());
  562. // col
  563. getline(liness,val,',');
  564. int col = atoi(val.c_str());
  565. // val
  566. getline(liness,val);
  567. T value;
  568. getAttribute(val.c_str(),value);
  569. triplets.push_back(Eigen::Triplet<T,I>(row,col,value));
  570. r++;
  571. }
  572. obj.setFromTriplets(triplets.begin(),triplets.end());
  573. initialized = true;
  574. }
  575. }
  576. }
  577. if(!initialized)
  578. {
  579. obj = Eigen::SparseMatrix<T,P,I>();
  580. }
  581. }
  582. // pointers
  583. template <typename T>
  584. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type serialize(const T& obj,tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  585. {
  586. tinyxml2::XMLElement* pointer = getElement(doc,element,name.c_str());
  587. bool isNullPtr = (obj == NULL);
  588. pointer->SetAttribute("isNullPtr",isNullPtr);
  589. if(isNullPtr == false)
  590. serialization_xml::serialize(*obj,doc,element,name);
  591. }
  592. template <typename T>
  593. IGL_INLINE typename std::enable_if<std::is_pointer<T>::value>::type deserialize(T& obj,const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element,const std::string& name)
  594. {
  595. const tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  596. if(child != NULL)
  597. {
  598. bool isNullPtr = child->BoolAttribute("isNullPtr");
  599. if(isNullPtr)
  600. {
  601. if(obj != NULL)
  602. {
  603. std::cout << "deserialization: possible memory leak for '" << typeid(obj).name() << "'" << std::endl;
  604. obj = NULL;
  605. }
  606. }
  607. else
  608. {
  609. if(obj != NULL)
  610. std::cout << "deserialization: possible memory leak for '" << typeid(obj).name() << "'" << std::endl;
  611. obj = new typename std::remove_pointer<T>::type();
  612. serialization_xml::deserialize(*obj,doc,element,name);
  613. }
  614. }
  615. }
  616. // helper functions
  617. IGL_INLINE tinyxml2::XMLElement* getElement(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element,const std::string& name)
  618. {
  619. tinyxml2::XMLElement* child = element->FirstChildElement(name.c_str());
  620. if(child == NULL)
  621. {
  622. child = doc->NewElement(name.c_str());
  623. element->InsertEndChild(child);
  624. }
  625. return child;
  626. }
  627. IGL_INLINE void getAttribute(const char* src,bool& dest)
  628. {
  629. tinyxml2::XMLUtil::ToBool(src,&dest);
  630. }
  631. IGL_INLINE void getAttribute(const char* src,char& dest)
  632. {
  633. dest = (char)atoi(src);
  634. }
  635. IGL_INLINE void getAttribute(const char* src,std::string& dest)
  636. {
  637. dest = src;
  638. }
  639. IGL_INLINE void getAttribute(const char* src,float& dest)
  640. {
  641. tinyxml2::XMLUtil::ToFloat(src,&dest);
  642. }
  643. IGL_INLINE void getAttribute(const char* src,double& dest)
  644. {
  645. tinyxml2::XMLUtil::ToDouble(src,&dest);
  646. }
  647. template<typename T>
  648. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest)
  649. {
  650. unsigned int val;
  651. tinyxml2::XMLUtil::ToUnsigned(src,&val);
  652. dest = (T)val;
  653. }
  654. template<typename T>
  655. IGL_INLINE typename std::enable_if<std::is_integral<T>::value && !std::is_unsigned<T>::value>::type getAttribute(const char* src,T& dest)
  656. {
  657. int val;
  658. tinyxml2::XMLUtil::ToInt(src,&val);
  659. dest = (T)val;
  660. }
  661. // tinyXML2 related stuff
  662. static const int numForbiddenChars = 8;
  663. static const char forbiddenChars[] ={' ','/','~','#','&','>','<','='};
  664. IGL_INLINE void replaceSubString(std::string& str,const std::string& search,const std::string& replace)
  665. {
  666. size_t pos = 0;
  667. while((pos = str.find(search,pos)) != std::string::npos)
  668. {
  669. str.replace(pos,search.length(),replace);
  670. pos += replace.length();
  671. }
  672. }
  673. IGL_INLINE void encodeXMLElementName(std::string& name)
  674. {
  675. // must not start with a digit
  676. if(isdigit(*name.begin()))
  677. {
  678. name = ":::" + name;
  679. }
  680. std::stringstream stream;
  681. for(int i=0;i<numForbiddenChars;i++)
  682. {
  683. std::string search;
  684. search = forbiddenChars[i];
  685. std::stringstream replaces;
  686. replaces << ":" << (int)forbiddenChars[i];
  687. std::string replace = replaces.str();
  688. replaceSubString(name,search,replace);
  689. }
  690. }
  691. IGL_INLINE void decodeXMLElementName(std::string& name)
  692. {
  693. if(name.find("::",0) == 0)
  694. name.replace(0,3,"");
  695. for(auto chr : forbiddenChars)
  696. {
  697. std::stringstream searchs;
  698. searchs << ":" << (int)chr;
  699. std::string search = searchs.str();
  700. std::string replace;
  701. replace = chr;
  702. replaceSubString(name,search,replace);
  703. }
  704. }
  705. /* Copyright(C) 2004-2008 Ren� Nyffenegger
  706. This source code is provided 'as-is',without any express or implied
  707. warranty.In no event will the author be held liable for any damages
  708. arising from the use of this software.
  709. Permission is granted to anyone to use this software for any purpose,
  710. including commercial applications,and to alter it and redistribute it
  711. freely,subject to the following restrictions:
  712. 1. The origin of this source code must not be misrepresented; you must not
  713. claim that you wrote the original source code.If you use this source code
  714. in a product,an acknowledgment in the product documentation would be
  715. appreciated but is not required.
  716. 2. Altered source versions must be plainly marked as such,and must not be
  717. misrepresented as being the original source code.
  718. 3. This notice may not be removed or altered from any source distribution.
  719. Ren� Nyffenegger rene.nyffenegger@adp-gmbh.ch
  720. */
  721. static const std::string base64_chars =
  722. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  723. "abcdefghijklmnopqrstuvwxyz"
  724. "0123456789+/";
  725. static inline bool is_base64(unsigned char c) {
  726. return (isalnum(c) || (c == '+') || (c == '/'));
  727. }
  728. std::string base64_encode(unsigned char const* bytes_to_encode,unsigned int in_len)
  729. {
  730. std::string ret;
  731. int i = 0;
  732. int j = 0;
  733. unsigned char char_array_3[3];
  734. unsigned char char_array_4[4];
  735. while(in_len--) {
  736. char_array_3[i++] = *(bytes_to_encode++);
  737. if(i == 3) {
  738. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  739. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  740. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  741. char_array_4[3] = char_array_3[2] & 0x3f;
  742. for(i = 0; (i <4) ; i++)
  743. ret += base64_chars[char_array_4[i]];
  744. i = 0;
  745. }
  746. }
  747. if(i)
  748. {
  749. for(j = i; j < 3; j++)
  750. char_array_3[j] = '\0';
  751. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  752. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  753. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  754. char_array_4[3] = char_array_3[2] & 0x3f;
  755. for(j = 0; (j < i + 1); j++)
  756. ret += base64_chars[char_array_4[j]];
  757. while((i++ < 3))
  758. ret += '=';
  759. }
  760. return ret;
  761. }
  762. std::string base64_decode(std::string const& encoded_string)
  763. {
  764. int in_len = encoded_string.size();
  765. int i = 0;
  766. int j = 0;
  767. int in_ = 0;
  768. unsigned char char_array_4[4],char_array_3[3];
  769. std::string ret;
  770. // construct fast lookup table
  771. // added by Christian Sch�ller (schuellc@inf.ethz.ch)
  772. int charLookup[200];
  773. for(int i=0;i<(int)(base64_chars.length());i++)
  774. charLookup[(int)base64_chars[i]] = i;
  775. while(in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  776. char_array_4[i++] = encoded_string[in_]; in_++;
  777. if(i ==4) {
  778. for(i = 0; i <4; i++)
  779. char_array_4[i] = charLookup[char_array_4[i]]; // new fast lookup
  780. //char_array_4[i] = base64_chars.find(char_array_4[i]); // original version
  781. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  782. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  783. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  784. for(i = 0; (i < 3); i++)
  785. ret += char_array_3[i];
  786. i = 0;
  787. }
  788. }
  789. if(i) {
  790. for(j = i; j <4; j++)
  791. char_array_4[j] = 0;
  792. for(j = 0; j <4; j++)
  793. char_array_4[j] = base64_chars.find(char_array_4[j]);
  794. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  795. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  796. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  797. for(j = 0; (j < i - 1);
  798. j++) ret += char_array_3[j];
  799. }
  800. return ret;
  801. }
  802. }
  803. }
  804. #ifdef IGL_STATIC_LIBRARY
  805. // Explicit template specialization
  806. template void igl::serialize_xml<std::vector<float, std::allocator<float> > >(std::vector<float, std::allocator<float> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, bool);
  807. template void igl::deserialize_xml<std::vector<float, std::allocator<float> > >(std::vector<float, std::allocator<float> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&);
  808. #endif