PlyParser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 "ParsingUtils.h"
  41. #include "../include/aiTypes.h"
  42. #include "../include/aiMesh.h"
  43. #include "../include/aiAnim.h"
  44. namespace Assimp
  45. {
  46. // http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/
  47. // http://w3.impa.br/~lvelho/outgoing/sossai/old/ViHAP_D4.4.2_PLY_format_v1.1.pdf
  48. // http://www.okino.com/conv/exp_ply.htm
  49. namespace PLY
  50. {
  51. // ---------------------------------------------------------------------------------
  52. /*
  53. name type number of bytes
  54. ---------------------------------------
  55. char character 1
  56. uchar unsigned character 1
  57. short short integer 2
  58. ushort unsigned short integer 2
  59. int integer 4
  60. uint unsigned integer 4
  61. float single-precision float 4
  62. double double-precision float 8
  63. int8
  64. int16
  65. uint8 ... forms are also used
  66. */
  67. enum EDataType
  68. {
  69. EDT_Char = 0x0u,
  70. EDT_UChar,
  71. EDT_Short,
  72. EDT_UShort,
  73. EDT_Int,
  74. EDT_UInt,
  75. EDT_Float,
  76. EDT_Double,
  77. // Marks invalid entries
  78. EDT_INVALID
  79. };
  80. // ---------------------------------------------------------------------------------
  81. /** \brief Specifies semantics for PLY element properties
  82. *
  83. * Semantics define the usage of a property, e.g. x coordinate
  84. */
  85. enum ESemantic
  86. {
  87. //! vertex position x coordinate
  88. EST_XCoord = 0x0u,
  89. //! vertex position x coordinate
  90. EST_YCoord,
  91. //! vertex position x coordinate
  92. EST_ZCoord,
  93. //! vertex normal x coordinate
  94. EST_XNormal,
  95. //! vertex normal y coordinate
  96. EST_YNormal,
  97. //! vertex normal z coordinate
  98. EST_ZNormal,
  99. //! u texture coordinate
  100. EST_UTextureCoord,
  101. //! v texture coordinate
  102. EST_VTextureCoord,
  103. //! vertex colors, red channel
  104. EST_Red,
  105. //! vertex colors, green channel
  106. EST_Green,
  107. //! vertex colors, blue channel
  108. EST_Blue,
  109. //! vertex colors, alpha channel
  110. EST_Alpha,
  111. //! vertex index list
  112. EST_VertexIndex,
  113. //! texture index
  114. EST_TextureIndex,
  115. //! texture coordinates (stored as element of a face)
  116. EST_TextureCoordinates,
  117. //! material index
  118. EST_MaterialIndex,
  119. //! ambient color, red channel
  120. EST_AmbientRed,
  121. //! ambient color, green channel
  122. EST_AmbientGreen,
  123. //! ambient color, blue channel
  124. EST_AmbientBlue,
  125. //! ambient color, alpha channel
  126. EST_AmbientAlpha,
  127. //! diffuse color, red channel
  128. EST_DiffuseRed,
  129. //! diffuse color, green channel
  130. EST_DiffuseGreen,
  131. //! diffuse color, blue channel
  132. EST_DiffuseBlue,
  133. //! diffuse color, alpha channel
  134. EST_DiffuseAlpha,
  135. //! specular color, red channel
  136. EST_SpecularRed,
  137. //! specular color, green channel
  138. EST_SpecularGreen,
  139. //! specular color, blue channel
  140. EST_SpecularBlue,
  141. //! specular color, alpha channel
  142. EST_SpecularAlpha,
  143. //! specular power for phong shading
  144. EST_PhongPower,
  145. //! opacity between 0 and 1
  146. EST_Opacity,
  147. //! Marks invalid entries
  148. EST_INVALID
  149. };
  150. // ---------------------------------------------------------------------------------
  151. /** \brief Specifies semantics for PLY elements
  152. *
  153. * Semantics define the usage of an element, e.g. vertex or material
  154. */
  155. enum EElementSemantic
  156. {
  157. //! The element is a vertex
  158. EEST_Vertex = 0x0u,
  159. //! The element is a face description (index table)
  160. EEST_Face,
  161. //! The element is a tristrip description (index table)
  162. EEST_TriStrip,
  163. //! The element is an edge description (ignored)
  164. EEST_Edge,
  165. //! The element is a material description
  166. EEST_Material,
  167. //! Marks invalid entries
  168. EEST_INVALID
  169. };
  170. // ---------------------------------------------------------------------------------
  171. /** \brief Helper class for a property in a PLY file.
  172. *
  173. * This can e.g. be a part of the vertex declaration
  174. */
  175. class Property
  176. {
  177. public:
  178. //! Default constructor
  179. Property()
  180. : eType (EDT_Int), bIsList(false), eFirstType(EDT_UChar)
  181. {}
  182. //! Data type of the property
  183. EDataType eType;
  184. //! Semantical meaning of the property
  185. ESemantic Semantic;
  186. //! Of the semantic of the property could not be parsed:
  187. //! Contains the semantic specified in the file
  188. std::string szName;
  189. //! Specifies whether the data type is a list where
  190. //! the first element specifies the size of the list
  191. bool bIsList;
  192. EDataType eFirstType;
  193. // -------------------------------------------------------------------
  194. //! Parse a property from a string. The end of the
  195. //! string is either '\n', '\r' or '\0'. Return valie is false
  196. //! if the input string is NOT a valid property (E.g. does
  197. //! not start with the "property" keyword)
  198. static bool ParseProperty (const char* p_szIn, const char** p_szOut,
  199. Property* pOut);
  200. // -------------------------------------------------------------------
  201. //! Parse a data type from a string
  202. static EDataType ParseDataType(const char* p_szIn,const char** p_szOut);
  203. // -------------------------------------------------------------------
  204. //! Parse a semantic from a string
  205. static ESemantic ParseSemantic(const char* p_szIn,const char** p_szOut);
  206. };
  207. // ---------------------------------------------------------------------------------
  208. /** \brief Helper class for an element in a PLY file.
  209. *
  210. * This can e.g. be the vertex declaration. Elements contain a
  211. * well-defined number of properties.
  212. */
  213. class Element
  214. {
  215. public:
  216. //! Default constructor
  217. Element()
  218. : NumOccur(0), eSemantic (EEST_INVALID)
  219. {}
  220. //! Destructor. Dallocates all storage
  221. ~Element()
  222. {
  223. // delete all elements
  224. for (std::vector<Property*>::const_iterator
  225. i = this->alProperties.begin();
  226. i != this->alProperties.end();++i)
  227. {
  228. delete (*i);
  229. }
  230. return;
  231. }
  232. //! List of properties assigned to the element
  233. //! std::vector to support operator[]
  234. std::vector<Property*> alProperties;
  235. //! Semantic of the element
  236. EElementSemantic eSemantic;
  237. //! Of the semantic of the element could not be parsed:
  238. //! Contains the semantic specified in the file
  239. std::string szName;
  240. //! How many times will the element occur?
  241. unsigned int NumOccur;
  242. // -------------------------------------------------------------------
  243. //! Parse an element from a string.
  244. //! The function will parse all properties contained in the
  245. //! element, too.
  246. static bool ParseElement (const char* p_szIn, const char** p_szOut,
  247. Element* pOut);
  248. // -------------------------------------------------------------------
  249. //! Parse a semantic from a string
  250. static EElementSemantic ParseSemantic(const char* p_szIn,
  251. const char** p_szOut);
  252. };
  253. // ---------------------------------------------------------------------------------
  254. /** \brief Instance of a property in a PLY file
  255. */
  256. class PropertyInstance
  257. {
  258. public:
  259. //! Default constructor
  260. PropertyInstance ()
  261. {}
  262. union ValueUnion
  263. {
  264. //! uInt32 representation of the property. All
  265. // uint types are automatically converted to uint32
  266. uint32_t iUInt;
  267. //! Int32 representation of the property. All
  268. // int types are automatically converted to int32
  269. int32_t iInt;
  270. //! Float32 representation of the property
  271. float fFloat;
  272. //! Float64 representation of the property
  273. double fDouble;
  274. };
  275. // -------------------------------------------------------------------
  276. //! List of all values parsed. Contains only one value
  277. // for non-list propertys
  278. std::list<ValueUnion> avList;
  279. // -------------------------------------------------------------------
  280. //! Parse a property instance
  281. static bool ParseInstance (const char* p_szIn,const char** p_szOut,
  282. const Property* prop, PropertyInstance* p_pcOut);
  283. // -------------------------------------------------------------------
  284. //! Parse a property instance in binary format
  285. static bool ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  286. const Property* prop, PropertyInstance* p_pcOut,bool p_bBE);
  287. // -------------------------------------------------------------------
  288. //! Get the default value for a given data type
  289. static ValueUnion DefaultValue(EDataType eType);
  290. // -------------------------------------------------------------------
  291. //! Parse a value
  292. static bool ParseValue(const char* p_szIn,const char** p_szOut,
  293. EDataType eType,ValueUnion* out);
  294. // -------------------------------------------------------------------
  295. //! Parse a binary value
  296. static bool ParseValueBinary(const char* p_szIn,const char** p_szOut,
  297. EDataType eType,ValueUnion* out,bool p_bBE);
  298. // -------------------------------------------------------------------
  299. //! Convert a property value to a given type TYPE
  300. template <typename TYPE>
  301. static TYPE ConvertTo(ValueUnion v, EDataType eType);
  302. };
  303. // ---------------------------------------------------------------------------------
  304. /** \brief Class for an element instance in a PLY file
  305. */
  306. class ElementInstance
  307. {
  308. public:
  309. //! Default constructor
  310. ElementInstance ()
  311. {}
  312. //! List of all parsed properties
  313. std::vector< PropertyInstance > alProperties;
  314. // -------------------------------------------------------------------
  315. //! Parse an element instance
  316. static bool ParseInstance (const char* p_szIn,const char** p_szOut,
  317. const Element* pcElement, ElementInstance* p_pcOut);
  318. // -------------------------------------------------------------------
  319. //! Parse a binary element instance
  320. static bool ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  321. const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE);
  322. };
  323. // ---------------------------------------------------------------------------------
  324. /** \brief Class for an element instance list in a PLY file
  325. */
  326. class ElementInstanceList
  327. {
  328. public:
  329. //! Default constructor
  330. ElementInstanceList ()
  331. {}
  332. //! Construction from a given element description
  333. ElementInstanceList (const Element* pc)
  334. {
  335. // reserve enough storage to speedup the process
  336. alInstances.resize(pc->NumOccur);
  337. }
  338. //! Destructor. Dallocates all storage
  339. ~ElementInstanceList()
  340. {
  341. // delete all elements
  342. for (std::vector<ElementInstance*>::const_iterator
  343. i = this->alInstances.begin();
  344. i != this->alInstances.end();++i)
  345. {
  346. delete (*i);
  347. }
  348. return;
  349. }
  350. //! List of all element instances
  351. std::vector< ElementInstance* > alInstances;
  352. // -------------------------------------------------------------------
  353. //! Parse an element instance list
  354. static bool ParseInstanceList (const char* p_szIn,const char** p_szOut,
  355. const Element* pcElement, ElementInstanceList* p_pcOut);
  356. // -------------------------------------------------------------------
  357. //! Parse a binary element instance list
  358. static bool ParseInstanceListBinary (const char* p_szIn,const char** p_szOut,
  359. const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE);
  360. };
  361. // ---------------------------------------------------------------------------------
  362. /** \brief Class to represent the document object model of an ASCII or binary
  363. * (both little and big-endian) PLY file
  364. */
  365. class DOM
  366. {
  367. public:
  368. //! Default constructor
  369. DOM()
  370. {}
  371. //! Destructor. Dallocates all storage
  372. ~DOM()
  373. {
  374. // delete all elements
  375. for (std::vector<Element*>::const_iterator
  376. i = this->alElements.begin();
  377. i != this->alElements.end();++i)
  378. {
  379. delete (*i);
  380. }
  381. // delete all instance lists
  382. for (std::vector<ElementInstanceList*>::const_iterator
  383. i = this->alElementData.begin();
  384. i != this->alElementData.end();++i)
  385. {
  386. delete (*i);
  387. }
  388. return;
  389. }
  390. //! Contains all elements of the file format
  391. std::vector<Element*> alElements;
  392. //! Contains the real data of each element's instance list
  393. std::vector<ElementInstanceList*> alElementData;
  394. //! Parse the DOM for a PLY file. The input string is assumed
  395. //! to be terminated with zero
  396. static bool ParseInstance (const char* p_szIn,DOM* p_pcOut);
  397. static bool ParseInstanceBinary (const char* p_szIn,
  398. DOM* p_pcOut,bool p_bBE);
  399. //! Skip all comment lines after this
  400. static bool SkipComments (const char* p_szIn,const char** p_szOut);
  401. private:
  402. // -------------------------------------------------------------------
  403. //! Handle the file header and read all element descriptions
  404. bool ParseHeader (const char* p_szIn,const char** p_szOut);
  405. // -------------------------------------------------------------------
  406. //! Read in all element instance lists
  407. bool ParseElementInstanceLists (const char* p_szIn,const char** p_szOut);
  408. // -------------------------------------------------------------------
  409. //! Read in all element instance lists for a binary file format
  410. bool ParseElementInstanceListsBinary (const char* p_szIn,
  411. const char** p_szOut,bool p_bBE);
  412. };
  413. // ---------------------------------------------------------------------------------
  414. /** \brief Helper class to represent a loaded PLY face
  415. */
  416. class Face
  417. {
  418. public:
  419. Face()
  420. : iMaterialIndex(0xFFFFFFFF)
  421. {
  422. // set all indices to zero by default
  423. mIndices.resize(3,0);
  424. }
  425. public:
  426. //! List of vertex indices
  427. std::vector<unsigned int> mIndices;
  428. //! Material index
  429. unsigned int iMaterialIndex;
  430. };
  431. // ---------------------------------------------------------------------------------
  432. template <typename TYPE>
  433. TYPE PLY::PropertyInstance::ConvertTo(
  434. PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType)
  435. {
  436. switch (eType)
  437. {
  438. case EDT_Float:
  439. return (TYPE)v.fFloat;
  440. case EDT_Double:
  441. return (TYPE)v.fDouble;
  442. case EDT_UInt:
  443. case EDT_UShort:
  444. case EDT_UChar:
  445. return (TYPE)v.iUInt;
  446. case EDT_Int:
  447. case EDT_Short:
  448. case EDT_Char:
  449. return (TYPE)v.iInt;
  450. };
  451. return (TYPE)0;
  452. }
  453. };
  454. };
  455. #endif // !! include guard