PlyParser.cpp 30 KB

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