PlyParser.cpp 26 KB

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