serialize_xml.cpp 31 KB

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