PlyParser.cpp 28 KB

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