PlyParser.cpp 24 KB

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