PlyParser.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Defines the helper data structures for importing PLY files */
  34. #ifndef AI_PLYFILEHELPER_H_INC
  35. #define AI_PLYFILEHELPER_H_INC
  36. #include <string>
  37. #include <vector>
  38. #include <list>
  39. #include <sstream>
  40. #include "../include/aiTypes.h"
  41. #include "../include/aiMesh.h"
  42. #include "../include/aiAnim.h"
  43. namespace Assimp
  44. {
  45. // http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/
  46. // http://w3.impa.br/~lvelho/outgoing/sossai/old/ViHAP_D4.4.2_PLY_format_v1.1.pdf
  47. // http://www.okino.com/conv/exp_ply.htm
  48. namespace PLY
  49. {
  50. // ---------------------------------------------------------------------------------
  51. /*
  52. name type number of bytes
  53. ---------------------------------------
  54. char character 1
  55. uchar unsigned character 1
  56. short short integer 2
  57. ushort unsigned short integer 2
  58. int integer 4
  59. uint unsigned integer 4
  60. float single-precision float 4
  61. double double-precision float 8
  62. int8
  63. int16
  64. uint8 ... forms are also used
  65. */
  66. enum EDataType
  67. {
  68. EDT_Char = 0x0u,
  69. EDT_UChar,
  70. EDT_Short,
  71. EDT_UShort,
  72. EDT_Int,
  73. EDT_UInt,
  74. EDT_Float,
  75. EDT_Double,
  76. // Marks invalid entries
  77. EDT_INVALID
  78. };
  79. // ---------------------------------------------------------------------------------
  80. /** \brief Specifies semantics for PLY element properties
  81. *
  82. * Semantics define the usage of a property, e.g. x coordinate
  83. */
  84. enum ESemantic
  85. {
  86. //! vertex position x coordinate
  87. EST_XCoord = 0x0u,
  88. //! vertex position x coordinate
  89. EST_YCoord,
  90. //! vertex position x coordinate
  91. EST_ZCoord,
  92. //! vertex normal x coordinate
  93. EST_XNormal,
  94. //! vertex normal y coordinate
  95. EST_YNormal,
  96. //! vertex normal z coordinate
  97. EST_ZNormal,
  98. //! u texture coordinate
  99. EST_UTextureCoord,
  100. //! v texture coordinate
  101. EST_VTextureCoord,
  102. //! vertex colors, red channel
  103. EST_Red,
  104. //! vertex colors, green channel
  105. EST_Green,
  106. //! vertex colors, blue channel
  107. EST_Blue,
  108. //! vertex colors, alpha channel
  109. EST_Alpha,
  110. //! vertex index list
  111. EST_VertexIndex,
  112. //! texture index
  113. EST_TextureIndex,
  114. //! texture coordinates (stored as element of a face)
  115. EST_TextureCoordinates,
  116. //! material index
  117. EST_MaterialIndex,
  118. //! ambient color, red channel
  119. EST_AmbientRed,
  120. //! ambient color, green channel
  121. EST_AmbientGreen,
  122. //! ambient color, blue channel
  123. EST_AmbientBlue,
  124. //! ambient color, alpha channel
  125. EST_AmbientAlpha,
  126. //! diffuse color, red channel
  127. EST_DiffuseRed,
  128. //! diffuse color, green channel
  129. EST_DiffuseGreen,
  130. //! diffuse color, blue channel
  131. EST_DiffuseBlue,
  132. //! diffuse color, alpha channel
  133. EST_DiffuseAlpha,
  134. //! specular color, red channel
  135. EST_SpecularRed,
  136. //! specular color, green channel
  137. EST_SpecularGreen,
  138. //! specular color, blue channel
  139. EST_SpecularBlue,
  140. //! specular color, alpha channel
  141. EST_SpecularAlpha,
  142. //! specular power for phong shading
  143. EST_PhongPower,
  144. //! opacity between 0 and 1
  145. EST_Opacity,
  146. //! Marks invalid entries
  147. EST_INVALID
  148. };
  149. // ---------------------------------------------------------------------------------
  150. /** \brief Specifies semantics for PLY elements
  151. *
  152. * Semantics define the usage of an element, e.g. vertex or material
  153. */
  154. enum EElementSemantic
  155. {
  156. //! The element is a vertex
  157. EEST_Vertex = 0x0u,
  158. //! The element is a face description (index table)
  159. EEST_Face,
  160. //! The element is a tristrip description (index table)
  161. EEST_TriStrip,
  162. //! The element is an edge description (ignored)
  163. EEST_Edge,
  164. //! The element is a material description
  165. EEST_Material,
  166. //! Marks invalid entries
  167. EEST_INVALID
  168. };
  169. // ---------------------------------------------------------------------------------
  170. /** \brief Helper class for a property in a PLY file.
  171. *
  172. * This can e.g. be a part of the vertex declaration
  173. */
  174. class Property
  175. {
  176. public:
  177. //! Default constructor
  178. Property()
  179. : eType (EDT_Int), bIsList(false), eFirstType(EDT_UChar)
  180. {}
  181. //! Data type of the property
  182. EDataType eType;
  183. //! Semantical meaning of the property
  184. ESemantic Semantic;
  185. //! Of the semantic of the property could not be parsed:
  186. //! Contains the semantic specified in the file
  187. std::string szName;
  188. //! Specifies whether the data type is a list where
  189. //! the first element specifies the size of the list
  190. bool bIsList;
  191. EDataType eFirstType;
  192. // -------------------------------------------------------------------
  193. //! Parse a property from a string. The end of the
  194. //! string is either '\n', '\r' or '\0'. Return valie is false
  195. //! if the input string is NOT a valid property (E.g. does
  196. //! not start with the "property" keyword)
  197. static bool ParseProperty (const char* p_szIn, const char** p_szOut,
  198. Property* pOut);
  199. // -------------------------------------------------------------------
  200. //! Parse a data type from a string
  201. static EDataType ParseDataType(const char* p_szIn,const char** p_szOut);
  202. // -------------------------------------------------------------------
  203. //! Parse a semantic from a string
  204. static ESemantic ParseSemantic(const char* p_szIn,const char** p_szOut);
  205. };
  206. // ---------------------------------------------------------------------------------
  207. /** \brief Helper class for an element in a PLY file.
  208. *
  209. * This can e.g. be the vertex declaration. Elements contain a
  210. * well-defined number of properties.
  211. */
  212. class Element
  213. {
  214. public:
  215. //! Default constructor
  216. Element()
  217. : NumOccur(0), eSemantic (EEST_INVALID)
  218. {}
  219. //! Destructor. Dallocates all storage
  220. ~Element()
  221. {
  222. // delete all elements
  223. for (std::vector<Property*>::const_iterator
  224. i = this->alProperties.begin();
  225. i != this->alProperties.end();++i)
  226. {
  227. delete (*i);
  228. }
  229. return;
  230. }
  231. //! List of properties assigned to the element
  232. //! std::vector to support operator[]
  233. std::vector<Property*> alProperties;
  234. //! Semantic of the element
  235. EElementSemantic eSemantic;
  236. //! Of the semantic of the element could not be parsed:
  237. //! Contains the semantic specified in the file
  238. std::string szName;
  239. //! How many times will the element occur?
  240. unsigned int NumOccur;
  241. // -------------------------------------------------------------------
  242. //! Parse an element from a string.
  243. //! The function will parse all properties contained in the
  244. //! element, too.
  245. static bool ParseElement (const char* p_szIn, const char** p_szOut,
  246. Element* pOut);
  247. // -------------------------------------------------------------------
  248. //! Parse a semantic from a string
  249. static EElementSemantic ParseSemantic(const char* p_szIn,
  250. const char** p_szOut);
  251. };
  252. // ---------------------------------------------------------------------------------
  253. /** \brief Instance of a property in a PLY file
  254. */
  255. class PropertyInstance
  256. {
  257. public:
  258. //! Default constructor
  259. PropertyInstance ()
  260. {}
  261. union ValueUnion
  262. {
  263. //! uInt32 representation of the property. All
  264. // uint types are automatically converted to uint32
  265. uint32_t iUInt;
  266. //! Int32 representation of the property. All
  267. // int types are automatically converted to int32
  268. int32_t iInt;
  269. //! Float32 representation of the property
  270. float fFloat;
  271. //! Float64 representation of the property
  272. double fDouble;
  273. };
  274. // -------------------------------------------------------------------
  275. //! List of all values parsed. Contains only one value
  276. // for non-list propertys
  277. std::list<ValueUnion> avList;
  278. // -------------------------------------------------------------------
  279. //! Parse a property instance
  280. static bool ParseInstance (const char* p_szIn,const char** p_szOut,
  281. const Property* prop, PropertyInstance* p_pcOut);
  282. // -------------------------------------------------------------------
  283. //! Parse a property instance in binary format
  284. static bool ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  285. const Property* prop, PropertyInstance* p_pcOut,bool p_bBE);
  286. // -------------------------------------------------------------------
  287. //! Get the default value for a given data type
  288. static ValueUnion DefaultValue(EDataType eType);
  289. // -------------------------------------------------------------------
  290. //! Parse a value
  291. static bool ParseValue(const char* p_szIn,const char** p_szOut,
  292. EDataType eType,ValueUnion* out);
  293. // -------------------------------------------------------------------
  294. //! Parse a binary value
  295. static bool ParseValueBinary(const char* p_szIn,const char** p_szOut,
  296. EDataType eType,ValueUnion* out,bool p_bBE);
  297. // -------------------------------------------------------------------
  298. //! Convert a property value to a given type TYPE
  299. template <typename TYPE>
  300. static TYPE ConvertTo(ValueUnion v, EDataType eType);
  301. };
  302. // ---------------------------------------------------------------------------------
  303. /** \brief Class for an element instance in a PLY file
  304. */
  305. class ElementInstance
  306. {
  307. public:
  308. //! Default constructor
  309. ElementInstance ()
  310. {}
  311. //! List of all parsed properties
  312. std::vector< PropertyInstance > alProperties;
  313. // -------------------------------------------------------------------
  314. //! Parse an element instance
  315. static bool ParseInstance (const char* p_szIn,const char** p_szOut,
  316. const Element* pcElement, ElementInstance* p_pcOut);
  317. // -------------------------------------------------------------------
  318. //! Parse a binary element instance
  319. static bool ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  320. const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE);
  321. };
  322. // ---------------------------------------------------------------------------------
  323. /** \brief Class for an element instance list in a PLY file
  324. */
  325. class ElementInstanceList
  326. {
  327. public:
  328. //! Default constructor
  329. ElementInstanceList ()
  330. {}
  331. //! Construction from a given element description
  332. ElementInstanceList (const Element* pc)
  333. {
  334. // reserve enough storage to speedup the process
  335. alInstances.resize(pc->NumOccur);
  336. }
  337. //! Destructor. Dallocates all storage
  338. ~ElementInstanceList()
  339. {
  340. // delete all elements
  341. for (std::vector<ElementInstance*>::const_iterator
  342. i = this->alInstances.begin();
  343. i != this->alInstances.end();++i)
  344. {
  345. delete (*i);
  346. }
  347. return;
  348. }
  349. //! List of all element instances
  350. std::vector< ElementInstance* > alInstances;
  351. // -------------------------------------------------------------------
  352. //! Parse an element instance list
  353. static bool ParseInstanceList (const char* p_szIn,const char** p_szOut,
  354. const Element* pcElement, ElementInstanceList* p_pcOut);
  355. // -------------------------------------------------------------------
  356. //! Parse a binary element instance list
  357. static bool ParseInstanceListBinary (const char* p_szIn,const char** p_szOut,
  358. const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE);
  359. };
  360. // ---------------------------------------------------------------------------------
  361. /** \brief Class to represent the document object model of an ASCII or binary
  362. * (both little and big-endian) PLY file
  363. */
  364. class DOM
  365. {
  366. public:
  367. //! Default constructor
  368. DOM()
  369. {}
  370. //! Destructor. Dallocates all storage
  371. ~DOM()
  372. {
  373. // delete all elements
  374. for (std::vector<Element*>::const_iterator
  375. i = this->alElements.begin();
  376. i != this->alElements.end();++i)
  377. {
  378. delete (*i);
  379. }
  380. // delete all instance lists
  381. for (std::vector<ElementInstanceList*>::const_iterator
  382. i = this->alElementData.begin();
  383. i != this->alElementData.end();++i)
  384. {
  385. delete (*i);
  386. }
  387. return;
  388. }
  389. //! Contains all elements of the file format
  390. std::vector<Element*> alElements;
  391. //! Contains the real data of each element's instance list
  392. std::vector<ElementInstanceList*> alElementData;
  393. //! Parse the DOM for a PLY file. The input string is assumed
  394. //! to be terminated with zero
  395. static bool ParseInstance (const char* p_szIn,DOM* p_pcOut, unsigned int iLen);
  396. static bool ParseInstanceBinary (const char* p_szIn,
  397. DOM* p_pcOut,bool p_bBE, unsigned int iLen);
  398. //! Skip all comment lines after this
  399. static bool SkipComments (const char* p_szIn,const char** p_szOut);
  400. private:
  401. // -------------------------------------------------------------------
  402. //! Handle the file header and read all element descriptions
  403. bool ParseHeader (const char* p_szIn,const char** p_szOut);
  404. // -------------------------------------------------------------------
  405. //! Read in all element instance lists
  406. bool ParseElementInstanceLists (const char* p_szIn,const char** p_szOut);
  407. // -------------------------------------------------------------------
  408. //! Read in all element instance lists for a binary file format
  409. bool ParseElementInstanceListsBinary (const char* p_szIn,
  410. const char** p_szOut,bool p_bBE);
  411. };
  412. // ---------------------------------------------------------------------------------
  413. /** \brief Helper class to represent a loaded PLY face
  414. */
  415. class Face
  416. {
  417. public:
  418. Face()
  419. : iMaterialIndex(0xFFFFFFFF)
  420. {
  421. // set all indices to zero by default
  422. mIndices.resize(3,0);
  423. }
  424. public:
  425. //! List of vertex indices
  426. std::vector<unsigned int> mIndices;
  427. //! Material index
  428. unsigned int iMaterialIndex;
  429. };
  430. // ---------------------------------------------------------------------------------
  431. template <typename TYPE>
  432. TYPE PLY::PropertyInstance::ConvertTo(
  433. PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType)
  434. {
  435. switch (eType)
  436. {
  437. case EDT_Float:
  438. return (TYPE)v.fFloat;
  439. case EDT_Double:
  440. return (TYPE)v.fDouble;
  441. case EDT_UInt:
  442. case EDT_UShort:
  443. case EDT_UChar:
  444. return (TYPE)v.iUInt;
  445. case EDT_Int:
  446. case EDT_Short:
  447. case EDT_Char:
  448. return (TYPE)v.iInt;
  449. };
  450. return (TYPE)0;
  451. }
  452. };
  453. // ---------------------------------------------------------------------------------
  454. inline bool IsSpace( const char in)
  455. {
  456. return (in == ' ' || in == '\t');
  457. }
  458. // ---------------------------------------------------------------------------------
  459. inline bool IsLineEnd( const char in)
  460. {
  461. return (in == '\r' || in == '\n' || in == '\0');
  462. }
  463. // ---------------------------------------------------------------------------------
  464. inline bool IsSpaceOrNewLine( const char in)
  465. {
  466. return IsSpace(in) || IsLineEnd(in);
  467. }
  468. // ---------------------------------------------------------------------------------
  469. inline bool SkipSpaces( const char* in, const char** out)
  470. {
  471. while (*in == ' ' || *in == '\t')in++;
  472. *out = in;
  473. return !IsLineEnd(*in);
  474. }
  475. // ---------------------------------------------------------------------------------
  476. inline bool SkipLine( const char* in, const char** out)
  477. {
  478. while (*in != '\r' && *in != '\n' && *in != '\0')in++;
  479. if (*in == '\0')
  480. {
  481. *out = in;
  482. return false;
  483. }
  484. in++;
  485. *out = in;
  486. return true;
  487. }
  488. // ---------------------------------------------------------------------------------
  489. inline void SkipSpacesAndLineEnd( const char* in, const char** out)
  490. {
  491. while (*in == ' ' || *in == '\t' || *in == '\r' || *in == '\n')in++;
  492. *out = in;
  493. }
  494. };
  495. #endif // !! include guard