PlyParser.cpp 30 KB

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