PlyParser.cpp 30 KB

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