PlyParser.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the PLY parser class */
  35. #include "PlyLoader.h"
  36. #include "MaterialSystem.h"
  37. #include "fast_atof.h"
  38. #include "StringComparison.h"
  39. #include "ByteSwap.h"
  40. #include "../include/IOStream.h"
  41. #include "../include/IOSystem.h"
  42. #include "../include/aiMesh.h"
  43. #include "../include/aiScene.h"
  44. #include "../include/aiAssert.h"
  45. #include "../include/DefaultLogger.h"
  46. #include <boost/scoped_ptr.hpp>
  47. using namespace Assimp;
  48. // ------------------------------------------------------------------------------------------------
  49. PLY::EDataType PLY::Property::ParseDataType(const char* p_szIn,const char** p_szOut)
  50. {
  51. ai_assert(NULL != p_szIn && NULL != p_szOut);
  52. PLY::EDataType eOut = PLY::EDT_INVALID;
  53. if (0 == ASSIMP_strincmp(p_szIn,"char",4) ||
  54. 0 == ASSIMP_strincmp(p_szIn,"int8",4))
  55. {
  56. p_szIn+=4;
  57. eOut = PLY::EDT_Char;
  58. }
  59. else if (0 == ASSIMP_strincmp(p_szIn,"uchar",5) ||
  60. 0 == ASSIMP_strincmp(p_szIn,"uint8",5))
  61. {
  62. p_szIn+=5;
  63. eOut = PLY::EDT_UChar;
  64. }
  65. else if (0 == ASSIMP_strincmp(p_szIn,"short",5) ||
  66. 0 == ASSIMP_strincmp(p_szIn,"int16",5))
  67. {
  68. p_szIn+=5;
  69. eOut = PLY::EDT_Short;
  70. }
  71. else if (0 == ASSIMP_strincmp(p_szIn,"ushort",6) ||
  72. 0 == ASSIMP_strincmp(p_szIn,"uint16",6))
  73. {
  74. p_szIn+=6;
  75. eOut = PLY::EDT_UShort;
  76. }
  77. else if (0 == ASSIMP_strincmp(p_szIn,"int32",5))
  78. {
  79. p_szIn+=5;
  80. eOut = PLY::EDT_Int;
  81. }
  82. else if (0 == ASSIMP_strincmp(p_szIn,"uint32",6))
  83. {
  84. p_szIn+=6;
  85. eOut = PLY::EDT_UInt;
  86. }
  87. else if (0 == ASSIMP_strincmp(p_szIn,"int",3))
  88. {
  89. p_szIn+=3;
  90. eOut = PLY::EDT_Int;
  91. }
  92. else if (0 == ASSIMP_strincmp(p_szIn,"uint",4))
  93. {
  94. p_szIn+=4;
  95. eOut = PLY::EDT_UInt;
  96. }
  97. else if (0 == ASSIMP_strincmp(p_szIn,"float32",7))
  98. {
  99. p_szIn+=7;
  100. eOut = PLY::EDT_Float;
  101. }
  102. else if (0 == ASSIMP_strincmp(p_szIn,"float",5))
  103. {
  104. p_szIn+=5;
  105. eOut = PLY::EDT_Float;
  106. }
  107. else if (0 == ASSIMP_strincmp(p_szIn,"float64",7))
  108. {
  109. p_szIn+=7;
  110. eOut = PLY::EDT_Double;
  111. }
  112. else if (0 == ASSIMP_strincmp(p_szIn,"double64",8))
  113. {
  114. p_szIn+=8;
  115. eOut = PLY::EDT_Double;
  116. }
  117. else if (0 == ASSIMP_strincmp(p_szIn,"double",6))
  118. {
  119. p_szIn+=6;
  120. eOut = PLY::EDT_Double;
  121. }
  122. // either end of line or space, but no other characters allowed
  123. if (!(IsSpace(*p_szIn) || IsLineEnd(*p_szIn)))
  124. {
  125. eOut = PLY::EDT_INVALID;
  126. }
  127. if (PLY::EDT_INVALID == eOut)
  128. {
  129. DefaultLogger::get()->info("Found unknown data type in PLY file. This is OK");
  130. }
  131. *p_szOut = p_szIn;
  132. return eOut;
  133. }
  134. // ------------------------------------------------------------------------------------------------
  135. PLY::ESemantic PLY::Property::ParseSemantic(const char* p_szIn,const char** p_szOut)
  136. {
  137. ai_assert(NULL != p_szIn && NULL != p_szOut);
  138. PLY::ESemantic eOut = PLY::EST_INVALID;
  139. if (0 == ASSIMP_strincmp(p_szIn,"red",3))
  140. {
  141. p_szIn+=3;
  142. eOut = PLY::EST_Red;
  143. }
  144. else if (0 == ASSIMP_strincmp(p_szIn,"green",4))
  145. {
  146. p_szIn+=5;
  147. eOut = PLY::EST_Green;
  148. }
  149. else if (0 == ASSIMP_strincmp(p_szIn,"blue",4))
  150. {
  151. p_szIn+=4;
  152. eOut = PLY::EST_Blue;
  153. }
  154. else if (0 == ASSIMP_strincmp(p_szIn,"alpha",5))
  155. {
  156. p_szIn+=5;
  157. eOut = PLY::EST_Alpha;
  158. }
  159. else if (0 == ASSIMP_strincmp(p_szIn,"vertex_index",12))
  160. {
  161. p_szIn+=12;
  162. eOut = PLY::EST_VertexIndex;
  163. }
  164. else if (0 == ASSIMP_strincmp(p_szIn,"vertex_indices",14))
  165. {
  166. p_szIn+=14;
  167. eOut = PLY::EST_VertexIndex;
  168. }
  169. else if (0 == ASSIMP_strincmp(p_szIn,"material_index",14))
  170. {
  171. p_szIn+=14;
  172. eOut = PLY::EST_MaterialIndex;
  173. }
  174. else if (0 == ASSIMP_strincmp(p_szIn,"ambient_red",11))
  175. {
  176. p_szIn+=11;
  177. eOut = PLY::EST_AmbientRed;
  178. }
  179. else if (0 == ASSIMP_strincmp(p_szIn,"ambient_green",13))
  180. {
  181. p_szIn+=13;
  182. eOut = PLY::EST_AmbientGreen;
  183. }
  184. else if (0 == ASSIMP_strincmp(p_szIn,"ambient_blue",12))
  185. {
  186. p_szIn+=12;
  187. eOut = PLY::EST_AmbientBlue;
  188. }
  189. else if (0 == ASSIMP_strincmp(p_szIn,"ambient_alpha",13))
  190. {
  191. p_szIn+=13;
  192. eOut = PLY::EST_AmbientAlpha;
  193. }
  194. else if (0 == ASSIMP_strincmp(p_szIn,"diffuse_red",11))
  195. {
  196. p_szIn+=11;
  197. eOut = PLY::EST_DiffuseRed;
  198. }
  199. else if (0 == ASSIMP_strincmp(p_szIn,"diffuse_green",13))
  200. {
  201. p_szIn+=13;
  202. eOut = PLY::EST_DiffuseGreen;
  203. }
  204. else if (0 == ASSIMP_strincmp(p_szIn,"diffuse_blue",12))
  205. {
  206. p_szIn+=12;
  207. eOut = PLY::EST_DiffuseBlue;
  208. }
  209. else if (0 == ASSIMP_strincmp(p_szIn,"diffuse_alpha",13))
  210. {
  211. p_szIn+=13;
  212. eOut = PLY::EST_DiffuseAlpha;
  213. }
  214. else if (0 == ASSIMP_strincmp(p_szIn,"specular_red",12))
  215. {
  216. p_szIn+=12;
  217. eOut = PLY::EST_SpecularRed;
  218. }
  219. else if (0 == ASSIMP_strincmp(p_szIn,"specular_green",14))
  220. {
  221. p_szIn+=14;
  222. eOut = PLY::EST_SpecularGreen;
  223. }
  224. else if (0 == ASSIMP_strincmp(p_szIn,"specular_blue",13))
  225. {
  226. p_szIn+=13;
  227. eOut = PLY::EST_SpecularBlue;
  228. }
  229. else if (0 == ASSIMP_strincmp(p_szIn,"specular_alpha",14))
  230. {
  231. p_szIn+=14;
  232. eOut = PLY::EST_SpecularAlpha;
  233. }
  234. else if (0 == ASSIMP_strincmp(p_szIn,"opacity",7))
  235. {
  236. p_szIn+=7;
  237. eOut = PLY::EST_Opacity;
  238. }
  239. else if (0 == ASSIMP_strincmp(p_szIn,"specular_power",6))
  240. {
  241. p_szIn+=7;
  242. eOut = PLY::EST_PhongPower;
  243. }
  244. else if (0 == ASSIMP_strincmp(p_szIn,"r",1))
  245. {
  246. p_szIn++;
  247. eOut = PLY::EST_Red;
  248. }
  249. else if (0 == ASSIMP_strincmp(p_szIn,"g",1))
  250. {
  251. p_szIn++;
  252. eOut = PLY::EST_Green;
  253. }
  254. else if (0 == ASSIMP_strincmp(p_szIn,"b",1))
  255. {
  256. p_szIn++;
  257. eOut = PLY::EST_Blue;
  258. }
  259. else if (0 == ASSIMP_strincmp(p_szIn,"tx",2))
  260. {
  261. p_szIn+=2;
  262. eOut = PLY::EST_UTextureCoord;
  263. }
  264. else if (0 == ASSIMP_strincmp(p_szIn,"ty",2))
  265. {
  266. p_szIn+=2;
  267. eOut = PLY::EST_VTextureCoord;
  268. }
  269. else if (0 == ASSIMP_strincmp(p_szIn,"u",1))
  270. {
  271. p_szIn++;
  272. eOut = PLY::EST_UTextureCoord;
  273. }
  274. else if (0 == ASSIMP_strincmp(p_szIn,"v",1))
  275. {
  276. p_szIn++;
  277. eOut = PLY::EST_VTextureCoord;
  278. }
  279. else if (0 == ASSIMP_strincmp(p_szIn,"x",1))
  280. {
  281. p_szIn++;
  282. eOut = PLY::EST_XCoord;
  283. }
  284. else if (0 == ASSIMP_strincmp(p_szIn,"y",1))
  285. {
  286. p_szIn++;
  287. eOut = PLY::EST_YCoord;
  288. }
  289. else if (0 == ASSIMP_strincmp(p_szIn,"z",1))
  290. {
  291. p_szIn++;
  292. eOut = PLY::EST_ZCoord;
  293. }
  294. else
  295. {
  296. DefaultLogger::get()->info("Found unknown property in file. This is ok");
  297. // ... find the next space or new line
  298. while (*p_szIn != ' ' && *p_szIn != '\t' &&
  299. *p_szIn != '\r' && *p_szIn != '\0' && *p_szIn != '\n')p_szIn++;
  300. }
  301. // either end of line or space, but no other characters allowed
  302. if (!(IsSpace(*p_szIn) || IsLineEnd(*p_szIn)))
  303. {
  304. eOut = PLY::EST_INVALID;
  305. }
  306. *p_szOut = p_szIn;
  307. return eOut;
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. bool PLY::Property::ParseProperty (const char* p_szIn,
  311. const char** p_szOut,
  312. PLY::Property* pOut)
  313. {
  314. ai_assert(NULL != p_szIn && NULL != p_szOut);
  315. // Forms supported:
  316. // "property float x"
  317. // "property list uchar int vertex_index"
  318. *p_szOut = p_szIn;
  319. // skip leading spaces
  320. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  321. // skip the "property" string at the beginning
  322. if (0 != ASSIMP_strincmp(p_szIn,"property",8) || !IsSpace(*(p_szIn+8)))
  323. {
  324. // seems not to be a valid property entry
  325. return false;
  326. }
  327. // get next word
  328. p_szIn += 9;
  329. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  330. if (0 == ASSIMP_strincmp(p_szIn,"list",4) && IsSpace(*(p_szIn+4)))
  331. {
  332. pOut->bIsList = true;
  333. // seems to be a list.
  334. p_szIn += 5;
  335. if(EDT_INVALID == (pOut->eFirstType = PLY::Property::ParseDataType(p_szIn, &p_szIn)))
  336. {
  337. // unable to parse list size data type
  338. SkipLine(p_szIn,&p_szIn);
  339. *p_szOut = p_szIn;
  340. return false;
  341. }
  342. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  343. if(EDT_INVALID == (pOut->eType = PLY::Property::ParseDataType(p_szIn, &p_szIn)))
  344. {
  345. // unable to parse list data type
  346. SkipLine(p_szIn,&p_szIn);
  347. *p_szOut = p_szIn;
  348. return false;
  349. }
  350. }
  351. else
  352. {
  353. if(EDT_INVALID == (pOut->eType = PLY::Property::ParseDataType(p_szIn, &p_szIn)))
  354. {
  355. // unable to parse data type. Skip the property
  356. SkipLine(p_szIn,&p_szIn);
  357. *p_szOut = p_szIn;
  358. return false;
  359. }
  360. }
  361. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  362. const char* szCur = p_szIn;
  363. pOut->Semantic = PLY::Property::ParseSemantic(p_szIn, &p_szIn);
  364. if (PLY::EST_INVALID == pOut->Semantic)
  365. {
  366. // store the name of the semantic
  367. uintptr_t iDiff = (uintptr_t)p_szIn - (uintptr_t)szCur;
  368. DefaultLogger::get()->info("Found unknown semantic in PLY file. This is OK");
  369. pOut->szName = std::string(szCur,iDiff);
  370. }
  371. SkipSpacesAndLineEnd(p_szIn,&p_szIn);
  372. *p_szOut = p_szIn;
  373. return true;
  374. }
  375. // ------------------------------------------------------------------------------------------------
  376. PLY::EElementSemantic PLY::Element::ParseSemantic(const char* p_szIn,
  377. const char** p_szOut)
  378. {
  379. ai_assert(NULL != p_szIn && NULL != p_szOut);
  380. PLY::EElementSemantic eOut = PLY::EEST_INVALID;
  381. if (0 == ASSIMP_strincmp(p_szIn,"vertex",6))
  382. {
  383. p_szIn+=6;
  384. eOut = PLY::EEST_Vertex;
  385. }
  386. else if (0 == ASSIMP_strincmp(p_szIn,"face",4))
  387. {
  388. p_szIn+=4;
  389. eOut = PLY::EEST_Face;
  390. }
  391. #if 0
  392. else if (0 == ASSIMP_strincmp(p_szIn,"range_grid",10))
  393. {
  394. p_szIn+=10;
  395. eOut = PLY::EEST_Face;
  396. }
  397. #endif
  398. else if (0 == ASSIMP_strincmp(p_szIn,"tristrips",9))
  399. {
  400. p_szIn+=9;
  401. eOut = PLY::EEST_TriStrip;
  402. }
  403. else if (0 == ASSIMP_strincmp(p_szIn,"edge",4))
  404. {
  405. p_szIn+=4;
  406. eOut = PLY::EEST_Edge;
  407. }
  408. else if (0 == ASSIMP_strincmp(p_szIn,"material",8))
  409. {
  410. p_szIn+=8;
  411. eOut = PLY::EEST_Material;
  412. }
  413. // either end of line or space, but no other characters allowed
  414. if (!(IsSpace(*p_szIn) || IsLineEnd(*p_szIn)))
  415. {
  416. eOut = PLY::EEST_INVALID;
  417. }
  418. *p_szOut = p_szIn;
  419. return eOut;
  420. }
  421. // ------------------------------------------------------------------------------------------------
  422. bool PLY::Element::ParseElement (const char* p_szIn,
  423. const char** p_szOut,
  424. PLY::Element* pOut)
  425. {
  426. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != pOut);
  427. // Example format: "element vertex 8"
  428. *p_szOut = p_szIn;
  429. // skip leading spaces
  430. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  431. // skip the "element" string at the beginning
  432. if (0 != ASSIMP_strincmp(p_szIn,"element",7) || !IsSpace(*(p_szIn+7)))
  433. {
  434. // seems not to be a valid property entry
  435. return false;
  436. }
  437. // get next word
  438. p_szIn += 8;
  439. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  440. // parse the semantic of the element
  441. const char* szCur = p_szIn;
  442. pOut->eSemantic = PLY::Element::ParseSemantic(p_szIn,&p_szIn);
  443. if (PLY::EEST_INVALID == pOut->eSemantic)
  444. {
  445. // store the name of the semantic
  446. uintptr_t iDiff = (uintptr_t)p_szIn - (uintptr_t)szCur;
  447. pOut->szName = std::string(szCur,iDiff);
  448. }
  449. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  450. //parse the number of occurences of this element
  451. pOut->NumOccur = strtol10(p_szIn,&p_szIn);
  452. // go to the next line
  453. SkipSpacesAndLineEnd(p_szIn,&p_szIn);
  454. // now parse all properties of the element
  455. while(true)
  456. {
  457. // skip all comments
  458. PLY::DOM::SkipComments(p_szIn,&p_szIn);
  459. PLY::Property* prop = new PLY::Property();
  460. if(!PLY::Property::ParseProperty(p_szIn,&p_szIn,prop))break;
  461. // add the property to the property list
  462. pOut->alProperties.push_back(prop);
  463. }
  464. *p_szOut = p_szIn;
  465. return true;
  466. }
  467. // ------------------------------------------------------------------------------------------------
  468. bool PLY::DOM::SkipComments (const char* p_szIn,
  469. const char** p_szOut)
  470. {
  471. ai_assert(NULL != p_szIn && NULL != p_szOut);
  472. *p_szOut = p_szIn;
  473. // skip spaces
  474. if (!SkipSpaces(p_szIn,&p_szIn))return false;
  475. if (0 == ASSIMP_strincmp(p_szIn,"comment",7))
  476. {
  477. p_szIn += 7;
  478. SkipLine(p_szIn,&p_szIn);
  479. SkipComments(p_szIn,&p_szIn);
  480. *p_szOut = p_szIn;
  481. return true;
  482. }
  483. *p_szOut = p_szIn;
  484. return false;
  485. }
  486. // ------------------------------------------------------------------------------------------------
  487. bool PLY::DOM::ParseHeader (const char* p_szIn,const char** p_szOut)
  488. {
  489. ai_assert(NULL != p_szIn && NULL != p_szOut);
  490. DefaultLogger::get()->debug("PLY::DOM::ParseHeader() begin");
  491. // after ply and format line
  492. *p_szOut = p_szIn;
  493. // parse all elements
  494. while (true)
  495. {
  496. // skip all comments
  497. PLY::DOM::SkipComments(p_szIn,&p_szIn);
  498. PLY::Element* out = new PLY::Element();
  499. if(PLY::Element::ParseElement(p_szIn,&p_szIn,out))
  500. {
  501. // add the element to the list of elements
  502. this->alElements.push_back(out);
  503. }
  504. else if (0 == ASSIMP_strincmp(p_szIn,"end_header",10) && IsSpaceOrNewLine(*(p_szIn+10)))
  505. {
  506. // we have reached the end of the header
  507. p_szIn += 11;
  508. break;
  509. }
  510. // ignore unknown header elements
  511. }
  512. SkipSpacesAndLineEnd(p_szIn,&p_szIn);
  513. *p_szOut = p_szIn;
  514. DefaultLogger::get()->debug("PLY::DOM::ParseHeader() succeeded");
  515. return true;
  516. }
  517. // ------------------------------------------------------------------------------------------------
  518. bool PLY::DOM::ParseElementInstanceLists (
  519. const char* p_szIn,
  520. const char** p_szOut)
  521. {
  522. ai_assert(NULL != p_szIn && NULL != p_szOut);
  523. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceLists() begin");
  524. *p_szOut = p_szIn;
  525. this->alElementData.resize(this->alElements.size());
  526. std::vector<PLY::Element*>::const_iterator i = this->alElements.begin();
  527. std::vector<PLY::ElementInstanceList*>::iterator a = this->alElementData.begin();
  528. // parse all element instances
  529. for (;i != this->alElements.end();++i,++a)
  530. {
  531. *a = new PLY::ElementInstanceList((*i)); // reserve enough storage
  532. PLY::ElementInstanceList::ParseInstanceList(p_szIn,&p_szIn,(*i),(*a));
  533. }
  534. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceLists() succeeded");
  535. *p_szOut = p_szIn;
  536. return true;
  537. }
  538. // ------------------------------------------------------------------------------------------------
  539. bool PLY::DOM::ParseElementInstanceListsBinary (
  540. const char* p_szIn,
  541. const char** p_szOut,
  542. bool p_bBE)
  543. {
  544. ai_assert(NULL != p_szIn && NULL != p_szOut);
  545. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceListsBinary() begin");
  546. *p_szOut = p_szIn;
  547. this->alElementData.resize(this->alElements.size());
  548. std::vector<PLY::Element*>::const_iterator i = this->alElements.begin();
  549. std::vector<PLY::ElementInstanceList*>::iterator a = this->alElementData.begin();
  550. // parse all element instances
  551. for (;i != this->alElements.end();++i,++a)
  552. {
  553. *a = new PLY::ElementInstanceList((*i)); // reserve enough storage
  554. PLY::ElementInstanceList::ParseInstanceListBinary(p_szIn,&p_szIn,(*i),(*a),p_bBE);
  555. }
  556. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceListsBinary() succeeded");
  557. *p_szOut = p_szIn;
  558. return true;
  559. }
  560. // ------------------------------------------------------------------------------------------------
  561. bool PLY::DOM::ParseInstanceBinary (const char* p_szIn,DOM* p_pcOut,bool p_bBE)
  562. {
  563. ai_assert(NULL != p_szIn && NULL != p_pcOut);
  564. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() begin");
  565. if(!p_pcOut->ParseHeader(p_szIn,&p_szIn))
  566. {
  567. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() failure");
  568. return false;
  569. }
  570. if(!p_pcOut->ParseElementInstanceListsBinary(p_szIn,&p_szIn,p_bBE))
  571. {
  572. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() failure");
  573. return false;
  574. }
  575. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() succeeded");
  576. return true;
  577. }
  578. // ------------------------------------------------------------------------------------------------
  579. bool PLY::DOM::ParseInstance (const char* p_szIn,DOM* p_pcOut)
  580. {
  581. ai_assert(NULL != p_szIn);
  582. ai_assert(NULL != p_pcOut);
  583. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() begin");
  584. if(!p_pcOut->ParseHeader(p_szIn,&p_szIn))
  585. {
  586. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() failure");
  587. return false;
  588. }
  589. if(!p_pcOut->ParseElementInstanceLists(p_szIn,&p_szIn))
  590. {
  591. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() failure");
  592. return false;
  593. }
  594. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() succeeded");
  595. return true;
  596. }
  597. // ------------------------------------------------------------------------------------------------
  598. bool PLY::ElementInstanceList::ParseInstanceList (
  599. const char* p_szIn,
  600. const char** p_szOut,
  601. const PLY::Element* pcElement,
  602. PLY::ElementInstanceList* p_pcOut)
  603. {
  604. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != pcElement && NULL != p_pcOut);
  605. if (EEST_INVALID == pcElement->eSemantic)
  606. {
  607. // if the element has an unknown semantic we can skip all lines
  608. // However, there could be comments
  609. for (unsigned int i = 0; i < pcElement->NumOccur;++i)
  610. {
  611. PLY::DOM::SkipComments(p_szIn,&p_szIn);
  612. SkipLine(p_szIn,&p_szIn);
  613. }
  614. }
  615. else
  616. {
  617. // be sure to have enough storage
  618. p_pcOut->alInstances.resize(pcElement->NumOccur);
  619. for (unsigned int i = 0; i < pcElement->NumOccur;++i)
  620. {
  621. PLY::DOM::SkipComments(p_szIn,&p_szIn);
  622. PLY::ElementInstance* out = new PLY::ElementInstance();
  623. PLY::ElementInstance::ParseInstance(p_szIn, &p_szIn,pcElement, out);
  624. // add it to the list
  625. p_pcOut->alInstances[i] = out;
  626. }
  627. }
  628. *p_szOut = p_szIn;
  629. return true;
  630. }
  631. // ------------------------------------------------------------------------------------------------
  632. bool PLY::ElementInstanceList::ParseInstanceListBinary (
  633. const char* p_szIn,
  634. const char** p_szOut,
  635. const PLY::Element* pcElement,
  636. PLY::ElementInstanceList* p_pcOut,
  637. bool p_bBE /* = false */)
  638. {
  639. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != pcElement && NULL != p_pcOut);
  640. // we can add special handling code for unknown element semantics since
  641. // we can't skip it as a whole block (we don't know its exact size
  642. // due to the fact that lists could be contained in the property list
  643. // of the unknown element)
  644. for (unsigned int i = 0; i < pcElement->NumOccur;++i)
  645. {
  646. PLY::ElementInstance* out = new PLY::ElementInstance();
  647. PLY::ElementInstance::ParseInstanceBinary(p_szIn, &p_szIn,pcElement, out, p_bBE);
  648. // add it to the list
  649. p_pcOut->alInstances[i] = out;
  650. }
  651. *p_szOut = p_szIn;
  652. return true;
  653. }
  654. // ------------------------------------------------------------------------------------------------
  655. bool PLY::ElementInstance::ParseInstance (
  656. const char* p_szIn,
  657. const char** p_szOut,
  658. const PLY::Element* pcElement,
  659. PLY::ElementInstance* p_pcOut)
  660. {
  661. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != pcElement && NULL != p_pcOut);
  662. if (!SkipSpaces(p_szIn, &p_szIn))return false;
  663. // allocate enough storage
  664. p_pcOut->alProperties.resize(pcElement->alProperties.size());
  665. std::vector<PLY::PropertyInstance>::iterator i = p_pcOut->alProperties.begin();
  666. std::vector<PLY::Property*>::const_iterator a = pcElement->alProperties.begin();
  667. for (;i != p_pcOut->alProperties.end();++i,++a)
  668. {
  669. if(!(PLY::PropertyInstance::ParseInstance(p_szIn, &p_szIn,(*a),&(*i))))
  670. {
  671. DefaultLogger::get()->warn("Unable to parse property instance. "
  672. "Skipping this element instance");
  673. // skip the rest of the instance
  674. SkipLine(p_szIn, &p_szIn);
  675. PLY::PropertyInstance::ValueUnion v = PLY::PropertyInstance::DefaultValue((*a)->eType);
  676. (*i).avList.push_back(v);
  677. }
  678. }
  679. *p_szOut = p_szIn;
  680. return true;
  681. }
  682. // ------------------------------------------------------------------------------------------------
  683. bool PLY::ElementInstance::ParseInstanceBinary (
  684. const char* p_szIn,
  685. const char** p_szOut,
  686. const PLY::Element* pcElement,
  687. PLY::ElementInstance* p_pcOut,
  688. bool p_bBE /* = false */)
  689. {
  690. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != pcElement && NULL != p_pcOut);
  691. // allocate enough storage
  692. p_pcOut->alProperties.resize(pcElement->alProperties.size());
  693. std::vector<PLY::PropertyInstance>::iterator i = p_pcOut->alProperties.begin();
  694. std::vector<PLY::Property*>::const_iterator a = pcElement->alProperties.begin();
  695. for (;i != p_pcOut->alProperties.end();++i,++a)
  696. {
  697. if(!(PLY::PropertyInstance::ParseInstanceBinary(p_szIn, &p_szIn,(*a),&(*i),p_bBE)))
  698. {
  699. DefaultLogger::get()->warn("Unable to parse binary property instance. "
  700. "Skipping this element instance");
  701. PLY::PropertyInstance::ValueUnion v = PLY::PropertyInstance::DefaultValue((*a)->eType);
  702. (*i).avList.push_back(v);
  703. }
  704. }
  705. *p_szOut = p_szIn;
  706. return true;
  707. }
  708. // ------------------------------------------------------------------------------------------------
  709. bool PLY::PropertyInstance::ParseInstance (const char* p_szIn,const char** p_szOut,
  710. const PLY::Property* prop, PLY::PropertyInstance* p_pcOut)
  711. {
  712. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != prop && NULL != p_pcOut);
  713. *p_szOut = p_szIn;
  714. // skip spaces at the beginning
  715. if (!SkipSpaces(p_szIn, &p_szIn))return false;
  716. if (prop->bIsList)
  717. {
  718. // parse the number of elements in the list
  719. PLY::PropertyInstance::ValueUnion v;
  720. PLY::PropertyInstance::ParseValue(p_szIn, &p_szIn,prop->eFirstType,&v);
  721. // convert to unsigned int
  722. unsigned int iNum = PLY::PropertyInstance::ConvertTo<unsigned int>(v,prop->eFirstType);
  723. // parse all list elements
  724. for (unsigned int i = 0; i < iNum;++i)
  725. {
  726. if (!SkipSpaces(p_szIn, &p_szIn))return false;
  727. PLY::PropertyInstance::ParseValue(p_szIn, &p_szIn,prop->eType,&v);
  728. p_pcOut->avList.push_back(v);
  729. }
  730. }
  731. else
  732. {
  733. // parse the property
  734. PLY::PropertyInstance::ValueUnion v;
  735. PLY::PropertyInstance::ParseValue(p_szIn, &p_szIn,prop->eType,&v);
  736. p_pcOut->avList.push_back(v);
  737. }
  738. SkipSpacesAndLineEnd(p_szIn, &p_szIn);
  739. *p_szOut = p_szIn;
  740. return true;
  741. }
  742. // ------------------------------------------------------------------------------------------------
  743. bool PLY::PropertyInstance::ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  744. const PLY::Property* prop, PLY::PropertyInstance* p_pcOut,bool p_bBE)
  745. {
  746. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != prop && NULL != p_pcOut);
  747. if (prop->bIsList)
  748. {
  749. // parse the number of elements in the list
  750. PLY::PropertyInstance::ValueUnion v;
  751. PLY::PropertyInstance::ParseValueBinary(p_szIn, &p_szIn,prop->eFirstType,&v,p_bBE);
  752. // convert to unsigned int
  753. unsigned int iNum = PLY::PropertyInstance::ConvertTo<unsigned int>(v,prop->eFirstType);
  754. // parse all list elements
  755. for (unsigned int i = 0; i < iNum;++i)
  756. {
  757. PLY::PropertyInstance::ParseValueBinary(p_szIn, &p_szIn,prop->eType,&v,p_bBE);
  758. p_pcOut->avList.push_back(v);
  759. }
  760. }
  761. else
  762. {
  763. // parse the property
  764. PLY::PropertyInstance::ValueUnion v;
  765. PLY::PropertyInstance::ParseValueBinary(p_szIn, &p_szIn,prop->eType,&v,p_bBE);
  766. p_pcOut->avList.push_back(v);
  767. }
  768. *p_szOut = p_szIn;
  769. return true;
  770. }
  771. // ------------------------------------------------------------------------------------------------
  772. PLY::PropertyInstance::ValueUnion PLY::PropertyInstance::DefaultValue(
  773. PLY::EDataType eType)
  774. {
  775. PLY::PropertyInstance::ValueUnion out;
  776. switch (eType)
  777. {
  778. case EDT_Float:
  779. out.fFloat = 0.0f;
  780. return out;
  781. case EDT_Double:
  782. out.fDouble = 0.0;
  783. return out;
  784. default: ;
  785. };
  786. out.iUInt = 0;
  787. return out;
  788. }
  789. // ------------------------------------------------------------------------------------------------
  790. bool PLY::PropertyInstance::ParseValue(const char* p_szIn,const char** p_szOut,
  791. PLY::EDataType eType,PLY::PropertyInstance::ValueUnion* out)
  792. {
  793. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != out);
  794. switch (eType)
  795. {
  796. case EDT_UInt:
  797. case EDT_UShort:
  798. case EDT_UChar:
  799. // simply parse in a full uint
  800. out->iUInt = (uint32_t)strtol10(p_szIn, &p_szIn);
  801. break;
  802. case EDT_Int:
  803. case EDT_Short:
  804. case EDT_Char:
  805. {
  806. // simply parse in a full int
  807. // Take care of the sign at the beginning
  808. bool bMinus = false;
  809. if (*p_szIn == '-')
  810. {
  811. p_szIn++;
  812. bMinus = true;
  813. }
  814. out->iInt = (int32_t)strtol10(p_szIn, &p_szIn);
  815. if (bMinus)out->iInt *= -1;
  816. break;
  817. }
  818. case EDT_Float:
  819. // parse a simple float
  820. p_szIn = fast_atof_move(p_szIn,out->fFloat);
  821. break;
  822. case EDT_Double:
  823. // Parse a double float. .. TODO: support this
  824. float f;
  825. p_szIn = fast_atof_move(p_szIn,f);
  826. out->fDouble = (double)f;
  827. break;
  828. default:
  829. *p_szOut = p_szIn;
  830. return false;
  831. }
  832. *p_szOut = p_szIn;
  833. return true;
  834. }
  835. // ------------------------------------------------------------------------------------------------
  836. bool PLY::PropertyInstance::ParseValueBinary(
  837. const char* p_szIn,
  838. const char** p_szOut,
  839. PLY::EDataType eType,
  840. PLY::PropertyInstance::ValueUnion* out,
  841. bool p_bBE)
  842. {
  843. ai_assert(NULL != p_szIn && NULL != p_szOut && NULL != out);
  844. switch (eType)
  845. {
  846. case EDT_UInt:
  847. out->iUInt = (uint32_t)*((uint32_t*)p_szIn);
  848. p_szIn += 4;
  849. if (p_bBE)
  850. {
  851. ByteSwap::Swap((int32_t*)&out->iUInt);
  852. }
  853. break;
  854. case EDT_UShort:
  855. {
  856. uint16_t i = *((uint16_t*)p_szIn);
  857. if (p_bBE)
  858. {
  859. ByteSwap::Swap((int16_t*)&i);
  860. }
  861. out->iUInt = (uint32_t)i;
  862. p_szIn += 2;
  863. break;
  864. }
  865. case EDT_UChar:
  866. {
  867. uint8_t i = *((uint8_t*)p_szIn);
  868. out->iUInt = (uint32_t)i;
  869. p_szIn ++;
  870. break;
  871. }
  872. case EDT_Int:
  873. out->iInt = *((int32_t*)p_szIn);
  874. p_szIn += 4;
  875. if (p_bBE)
  876. {
  877. ByteSwap::Swap((int32_t*)&out->iInt);
  878. }
  879. break;
  880. case EDT_Short:
  881. {
  882. int16_t i = *((int16_t*)p_szIn);
  883. if (p_bBE)
  884. {
  885. ByteSwap::Swap((int16_t*)&i);
  886. }
  887. out->iInt = (int32_t)i;
  888. p_szIn += 2;
  889. break;
  890. }
  891. case EDT_Char:
  892. out->iInt = (int32_t)*((int8_t*)p_szIn);
  893. p_szIn ++;
  894. break;
  895. case EDT_Float:
  896. {
  897. int32_t* pf = (int32_t*)p_szIn;
  898. if (p_bBE)
  899. {
  900. ByteSwap::Swap((int32_t*)&pf);
  901. }
  902. p_szIn += 4;
  903. out->fFloat = *((float*)&pf);
  904. break;
  905. }
  906. case EDT_Double:
  907. {
  908. int64_t* pf = (int64_t*)p_szIn;
  909. if (p_bBE)
  910. {
  911. ByteSwap::Swap((int64_t*)&pf);
  912. }
  913. p_szIn += 8;
  914. out->fDouble = *((double*)&pf);
  915. break;
  916. }
  917. default:
  918. *p_szOut = p_szIn;
  919. return false;
  920. }
  921. *p_szOut = p_szIn;
  922. return true;
  923. }