PlyParser.cpp 27 KB

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