PlyParser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, 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 occurrences 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. if ( !IsLineEnd(pCur[-1]) )
  384. {
  385. SkipLine(pCur,&pCur);
  386. }
  387. SkipComments(pCur,&pCur);
  388. *pCurOut = pCur;
  389. return true;
  390. }
  391. *pCurOut = pCur;
  392. return false;
  393. }
  394. // ------------------------------------------------------------------------------------------------
  395. bool PLY::DOM::ParseHeader (const char* pCur,const char** pCurOut,bool isBinary)
  396. {
  397. ai_assert(NULL != pCur && NULL != pCurOut);
  398. DefaultLogger::get()->debug("PLY::DOM::ParseHeader() begin");
  399. // after ply and format line
  400. *pCurOut = pCur;
  401. // parse all elements
  402. while ((*pCur) != '\0')
  403. {
  404. // skip all comments
  405. PLY::DOM::SkipComments(pCur,&pCur);
  406. PLY::Element out;
  407. if(PLY::Element::ParseElement(pCur,&pCur,&out))
  408. {
  409. // add the element to the list of elements
  410. alElements.push_back(out);
  411. }
  412. else if (TokenMatch(pCur,"end_header",10))
  413. {
  414. // we have reached the end of the header
  415. break;
  416. }
  417. else
  418. {
  419. // ignore unknown header elements
  420. SkipLine(&pCur);
  421. }
  422. }
  423. if(!isBinary)
  424. { // it would occur an error, if binary data start with values as space or line end.
  425. SkipSpacesAndLineEnd(pCur,&pCur);
  426. }
  427. *pCurOut = pCur;
  428. DefaultLogger::get()->debug("PLY::DOM::ParseHeader() succeeded");
  429. return true;
  430. }
  431. // ------------------------------------------------------------------------------------------------
  432. bool PLY::DOM::ParseElementInstanceLists (
  433. const char* pCur,
  434. const char** pCurOut)
  435. {
  436. ai_assert(NULL != pCur && NULL != pCurOut);
  437. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceLists() begin");
  438. *pCurOut = pCur;
  439. alElementData.resize(alElements.size());
  440. std::vector<PLY::Element>::const_iterator i = alElements.begin();
  441. std::vector<PLY::ElementInstanceList>::iterator a = alElementData.begin();
  442. // parse all element instances
  443. for (;i != alElements.end();++i,++a)
  444. {
  445. (*a).alInstances.resize((*i).NumOccur);
  446. PLY::ElementInstanceList::ParseInstanceList(pCur,&pCur,&(*i),&(*a));
  447. }
  448. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceLists() succeeded");
  449. *pCurOut = pCur;
  450. return true;
  451. }
  452. // ------------------------------------------------------------------------------------------------
  453. bool PLY::DOM::ParseElementInstanceListsBinary (
  454. const char* pCur,
  455. const char** pCurOut,
  456. bool p_bBE)
  457. {
  458. ai_assert(NULL != pCur && NULL != pCurOut);
  459. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceListsBinary() begin");
  460. *pCurOut = pCur;
  461. alElementData.resize(alElements.size());
  462. std::vector<PLY::Element>::const_iterator i = alElements.begin();
  463. std::vector<PLY::ElementInstanceList>::iterator a = alElementData.begin();
  464. // parse all element instances
  465. for (;i != alElements.end();++i,++a)
  466. {
  467. (*a).alInstances.resize((*i).NumOccur);
  468. PLY::ElementInstanceList::ParseInstanceListBinary(pCur,&pCur,&(*i),&(*a),p_bBE);
  469. }
  470. DefaultLogger::get()->debug("PLY::DOM::ParseElementInstanceListsBinary() succeeded");
  471. *pCurOut = pCur;
  472. return true;
  473. }
  474. // ------------------------------------------------------------------------------------------------
  475. bool PLY::DOM::ParseInstanceBinary (const char* pCur,DOM* p_pcOut,bool p_bBE)
  476. {
  477. ai_assert(NULL != pCur && NULL != p_pcOut);
  478. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() begin");
  479. if(!p_pcOut->ParseHeader(pCur,&pCur,true))
  480. {
  481. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() failure");
  482. return false;
  483. }
  484. if(!p_pcOut->ParseElementInstanceListsBinary(pCur,&pCur,p_bBE))
  485. {
  486. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() failure");
  487. return false;
  488. }
  489. DefaultLogger::get()->debug("PLY::DOM::ParseInstanceBinary() succeeded");
  490. return true;
  491. }
  492. // ------------------------------------------------------------------------------------------------
  493. bool PLY::DOM::ParseInstance (const char* pCur,DOM* p_pcOut)
  494. {
  495. ai_assert(NULL != pCur);
  496. ai_assert(NULL != p_pcOut);
  497. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() begin");
  498. if(!p_pcOut->ParseHeader(pCur,&pCur,false))
  499. {
  500. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() failure");
  501. return false;
  502. }
  503. if(!p_pcOut->ParseElementInstanceLists(pCur,&pCur))
  504. {
  505. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() failure");
  506. return false;
  507. }
  508. DefaultLogger::get()->debug("PLY::DOM::ParseInstance() succeeded");
  509. return true;
  510. }
  511. // ------------------------------------------------------------------------------------------------
  512. bool PLY::ElementInstanceList::ParseInstanceList (
  513. const char* pCur,
  514. const char** pCurOut,
  515. const PLY::Element* pcElement,
  516. PLY::ElementInstanceList* p_pcOut)
  517. {
  518. ai_assert(NULL != pCur && NULL != pCurOut && NULL != pcElement && NULL != p_pcOut);
  519. if (EEST_INVALID == pcElement->eSemantic || pcElement->alProperties.empty())
  520. {
  521. // if the element has an unknown semantic we can skip all lines
  522. // However, there could be comments
  523. for (unsigned int i = 0; i < pcElement->NumOccur;++i)
  524. {
  525. PLY::DOM::SkipComments(pCur,&pCur);
  526. SkipLine(pCur,&pCur);
  527. }
  528. }
  529. else
  530. {
  531. // be sure to have enough storage
  532. for (unsigned int i = 0; i < pcElement->NumOccur;++i)
  533. {
  534. PLY::DOM::SkipComments(pCur,&pCur);
  535. PLY::ElementInstance::ParseInstance(pCur, &pCur,pcElement,
  536. &p_pcOut->alInstances[i]);
  537. }
  538. }
  539. *pCurOut = pCur;
  540. return true;
  541. }
  542. // ------------------------------------------------------------------------------------------------
  543. bool PLY::ElementInstanceList::ParseInstanceListBinary (
  544. const char* pCur,
  545. const char** pCurOut,
  546. const PLY::Element* pcElement,
  547. PLY::ElementInstanceList* p_pcOut,
  548. bool p_bBE /* = false */)
  549. {
  550. ai_assert(NULL != pCur && NULL != pCurOut && NULL != pcElement && NULL != p_pcOut);
  551. // we can add special handling code for unknown element semantics since
  552. // we can't skip it as a whole block (we don't know its exact size
  553. // due to the fact that lists could be contained in the property list
  554. // of the unknown element)
  555. for (unsigned int i = 0; i < pcElement->NumOccur;++i)
  556. {
  557. PLY::ElementInstance::ParseInstanceBinary(pCur, &pCur,pcElement,
  558. &p_pcOut->alInstances[i], p_bBE);
  559. }
  560. *pCurOut = pCur;
  561. return true;
  562. }
  563. // ------------------------------------------------------------------------------------------------
  564. bool PLY::ElementInstance::ParseInstance (
  565. const char* pCur,
  566. const char** pCurOut,
  567. const PLY::Element* pcElement,
  568. PLY::ElementInstance* p_pcOut)
  569. {
  570. ai_assert(NULL != pCur && NULL != pCurOut && NULL != pcElement && NULL != p_pcOut);
  571. if (!SkipSpaces(pCur, &pCur))return false;
  572. // allocate enough storage
  573. p_pcOut->alProperties.resize(pcElement->alProperties.size());
  574. std::vector<PLY::PropertyInstance>::iterator i = p_pcOut->alProperties.begin();
  575. std::vector<PLY::Property>::const_iterator a = pcElement->alProperties.begin();
  576. for (;i != p_pcOut->alProperties.end();++i,++a)
  577. {
  578. if(!(PLY::PropertyInstance::ParseInstance(pCur, &pCur,&(*a),&(*i))))
  579. {
  580. DefaultLogger::get()->warn("Unable to parse property instance. "
  581. "Skipping this element instance");
  582. // skip the rest of the instance
  583. SkipLine(pCur, &pCur);
  584. PLY::PropertyInstance::ValueUnion v = PLY::PropertyInstance::DefaultValue((*a).eType);
  585. (*i).avList.push_back(v);
  586. }
  587. }
  588. *pCurOut = pCur;
  589. return true;
  590. }
  591. // ------------------------------------------------------------------------------------------------
  592. bool PLY::ElementInstance::ParseInstanceBinary (
  593. const char* pCur,
  594. const char** pCurOut,
  595. const PLY::Element* pcElement,
  596. PLY::ElementInstance* p_pcOut,
  597. bool p_bBE /* = false */)
  598. {
  599. ai_assert(NULL != pCur && NULL != pCurOut && NULL != pcElement && NULL != p_pcOut);
  600. // allocate enough storage
  601. p_pcOut->alProperties.resize(pcElement->alProperties.size());
  602. std::vector<PLY::PropertyInstance>::iterator i = p_pcOut->alProperties.begin();
  603. std::vector<PLY::Property>::const_iterator a = pcElement->alProperties.begin();
  604. for (;i != p_pcOut->alProperties.end();++i,++a)
  605. {
  606. if(!(PLY::PropertyInstance::ParseInstanceBinary(pCur, &pCur,&(*a),&(*i),p_bBE)))
  607. {
  608. DefaultLogger::get()->warn("Unable to parse binary property instance. "
  609. "Skipping this element instance");
  610. (*i).avList.push_back(PLY::PropertyInstance::DefaultValue((*a).eType));
  611. }
  612. }
  613. *pCurOut = pCur;
  614. return true;
  615. }
  616. // ------------------------------------------------------------------------------------------------
  617. bool PLY::PropertyInstance::ParseInstance (const char* pCur,const char** pCurOut,
  618. const PLY::Property* prop, PLY::PropertyInstance* p_pcOut)
  619. {
  620. ai_assert(NULL != pCur && NULL != pCurOut && NULL != prop && NULL != p_pcOut);
  621. *pCurOut = pCur;
  622. // skip spaces at the beginning
  623. if (!SkipSpaces(pCur, &pCur))return false;
  624. if (prop->bIsList)
  625. {
  626. // parse the number of elements in the list
  627. PLY::PropertyInstance::ValueUnion v;
  628. PLY::PropertyInstance::ParseValue(pCur, &pCur,prop->eFirstType,&v);
  629. // convert to unsigned int
  630. unsigned int iNum = PLY::PropertyInstance::ConvertTo<unsigned int>(v,prop->eFirstType);
  631. // parse all list elements
  632. p_pcOut->avList.resize(iNum);
  633. for (unsigned int i = 0; i < iNum;++i)
  634. {
  635. if (!SkipSpaces(pCur, &pCur))return false;
  636. PLY::PropertyInstance::ParseValue(pCur, &pCur,prop->eType,&p_pcOut->avList[i]);
  637. }
  638. }
  639. else
  640. {
  641. // parse the property
  642. PLY::PropertyInstance::ValueUnion v;
  643. PLY::PropertyInstance::ParseValue(pCur, &pCur,prop->eType,&v);
  644. p_pcOut->avList.push_back(v);
  645. }
  646. SkipSpacesAndLineEnd(pCur, &pCur);
  647. *pCurOut = pCur;
  648. return true;
  649. }
  650. // ------------------------------------------------------------------------------------------------
  651. bool PLY::PropertyInstance::ParseInstanceBinary (
  652. const char* pCur,
  653. const char** pCurOut,
  654. const PLY::Property* prop,
  655. PLY::PropertyInstance* p_pcOut,
  656. bool p_bBE)
  657. {
  658. ai_assert(NULL != pCur && NULL != pCurOut && NULL != prop && NULL != p_pcOut);
  659. if (prop->bIsList)
  660. {
  661. // parse the number of elements in the list
  662. PLY::PropertyInstance::ValueUnion v;
  663. PLY::PropertyInstance::ParseValueBinary(pCur, &pCur,prop->eFirstType,&v,p_bBE);
  664. // convert to unsigned int
  665. unsigned int iNum = PLY::PropertyInstance::ConvertTo<unsigned int>(v,prop->eFirstType);
  666. // parse all list elements
  667. p_pcOut->avList.resize(iNum);
  668. for (unsigned int i = 0; i < iNum;++i){
  669. PLY::PropertyInstance::ParseValueBinary(pCur, &pCur,prop->eType,&p_pcOut->avList[i],p_bBE);
  670. }
  671. }
  672. else
  673. {
  674. // parse the property
  675. PLY::PropertyInstance::ValueUnion v;
  676. PLY::PropertyInstance::ParseValueBinary(pCur, &pCur,prop->eType,&v,p_bBE);
  677. p_pcOut->avList.push_back(v);
  678. }
  679. *pCurOut = pCur;
  680. return true;
  681. }
  682. // ------------------------------------------------------------------------------------------------
  683. PLY::PropertyInstance::ValueUnion PLY::PropertyInstance::DefaultValue(
  684. PLY::EDataType eType)
  685. {
  686. PLY::PropertyInstance::ValueUnion out;
  687. switch (eType)
  688. {
  689. case EDT_Float:
  690. out.fFloat = 0.f;
  691. return out;
  692. case EDT_Double:
  693. out.fDouble = 0.;
  694. return out;
  695. default: ;
  696. };
  697. out.iUInt = 0;
  698. return out;
  699. }
  700. // ------------------------------------------------------------------------------------------------
  701. bool PLY::PropertyInstance::ParseValue(
  702. const char* pCur,
  703. const char** pCurOut,
  704. PLY::EDataType eType,
  705. PLY::PropertyInstance::ValueUnion* out)
  706. {
  707. ai_assert(NULL != pCur && NULL != pCurOut && NULL != out);
  708. bool ret = true;
  709. *pCurOut = pCur;
  710. switch (eType)
  711. {
  712. case EDT_UInt:
  713. case EDT_UShort:
  714. case EDT_UChar:
  715. out->iUInt = (uint32_t)strtoul10(pCur, &pCur);
  716. break;
  717. case EDT_Int:
  718. case EDT_Short:
  719. case EDT_Char:
  720. out->iInt = (int32_t)strtol10(pCur, &pCur);
  721. break;
  722. case EDT_Float:
  723. pCur = fast_atoreal_move<float>(pCur,out->fFloat);
  724. break;
  725. case EDT_Double:
  726. float f;
  727. pCur = fast_atoreal_move<float>(pCur,f);
  728. out->fDouble = (double)f;
  729. break;
  730. default:
  731. ret = false;
  732. }
  733. *pCurOut = pCur;
  734. return ret;
  735. }
  736. // ------------------------------------------------------------------------------------------------
  737. bool PLY::PropertyInstance::ParseValueBinary(
  738. const char* pCur,
  739. const char** pCurOut,
  740. PLY::EDataType eType,
  741. PLY::PropertyInstance::ValueUnion* out,
  742. bool p_bBE)
  743. {
  744. ai_assert(NULL != pCur && NULL != pCurOut && NULL != out);
  745. bool ret = true;
  746. switch (eType)
  747. {
  748. case EDT_UInt:
  749. out->iUInt = (uint32_t)*((uint32_t*)pCur);
  750. pCur += 4;
  751. // Swap endianness
  752. if (p_bBE)ByteSwap::Swap((int32_t*)&out->iUInt);
  753. break;
  754. case EDT_UShort:
  755. {
  756. uint16_t i = *((uint16_t*)pCur);
  757. // Swap endianness
  758. if (p_bBE)ByteSwap::Swap(&i);
  759. out->iUInt = (uint32_t)i;
  760. pCur += 2;
  761. break;
  762. }
  763. case EDT_UChar:
  764. {
  765. out->iUInt = (uint32_t)(*((uint8_t*)pCur));
  766. pCur ++;
  767. break;
  768. }
  769. case EDT_Int:
  770. out->iInt = *((int32_t*)pCur);
  771. pCur += 4;
  772. // Swap endianness
  773. if (p_bBE)ByteSwap::Swap(&out->iInt);
  774. break;
  775. case EDT_Short:
  776. {
  777. int16_t i = *((int16_t*)pCur);
  778. // Swap endianness
  779. if (p_bBE)ByteSwap::Swap(&i);
  780. out->iInt = (int32_t)i;
  781. pCur += 2;
  782. break;
  783. }
  784. case EDT_Char:
  785. out->iInt = (int32_t)*((int8_t*)pCur);
  786. pCur ++;
  787. break;
  788. case EDT_Float:
  789. {
  790. out->fFloat = *((float*)pCur);
  791. // Swap endianness
  792. if (p_bBE)ByteSwap::Swap((int32_t*)&out->fFloat);
  793. pCur += 4;
  794. break;
  795. }
  796. case EDT_Double:
  797. {
  798. out->fDouble = *((double*)pCur);
  799. // Swap endianness
  800. if (p_bBE)ByteSwap::Swap((int64_t*)&out->fDouble);
  801. pCur += 8;
  802. break;
  803. }
  804. default:
  805. ret = false;
  806. }
  807. *pCurOut = pCur;
  808. return ret;
  809. }
  810. #endif // !! ASSIMP_BUILD_NO_PLY_IMPORTER