PlyParser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. : eSemantic (EEST_INVALID)
  219. , NumOccur(0)
  220. {}
  221. //! Destructor. Dallocates all storage
  222. ~Element()
  223. {
  224. // delete all elements
  225. for (std::vector<Property*>::const_iterator
  226. i = this->alProperties.begin();
  227. i != this->alProperties.end();++i)
  228. {
  229. delete (*i);
  230. }
  231. return;
  232. }
  233. //! List of properties assigned to the element
  234. //! std::vector to support operator[]
  235. std::vector<Property*> alProperties;
  236. //! Semantic of the element
  237. EElementSemantic eSemantic;
  238. //! Of the semantic of the element could not be parsed:
  239. //! Contains the semantic specified in the file
  240. std::string szName;
  241. //! How many times will the element occur?
  242. unsigned int NumOccur;
  243. // -------------------------------------------------------------------
  244. //! Parse an element from a string.
  245. //! The function will parse all properties contained in the
  246. //! element, too.
  247. static bool ParseElement (const char* p_szIn, const char** p_szOut,
  248. Element* pOut);
  249. // -------------------------------------------------------------------
  250. //! Parse a semantic from a string
  251. static EElementSemantic ParseSemantic(const char* p_szIn,
  252. const char** p_szOut);
  253. };
  254. // ---------------------------------------------------------------------------------
  255. /** \brief Instance of a property in a PLY file
  256. */
  257. class PropertyInstance
  258. {
  259. public:
  260. //! Default constructor
  261. PropertyInstance ()
  262. {}
  263. union ValueUnion
  264. {
  265. //! uInt32 representation of the property. All
  266. // uint types are automatically converted to uint32
  267. uint32_t iUInt;
  268. //! Int32 representation of the property. All
  269. // int types are automatically converted to int32
  270. int32_t iInt;
  271. //! Float32 representation of the property
  272. float fFloat;
  273. //! Float64 representation of the property
  274. double fDouble;
  275. };
  276. // -------------------------------------------------------------------
  277. //! List of all values parsed. Contains only one value
  278. // for non-list propertys
  279. std::list<ValueUnion> avList;
  280. // -------------------------------------------------------------------
  281. //! Parse a property instance
  282. static bool ParseInstance (const char* p_szIn,const char** p_szOut,
  283. const Property* prop, PropertyInstance* p_pcOut);
  284. // -------------------------------------------------------------------
  285. //! Parse a property instance in binary format
  286. static bool ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  287. const Property* prop, PropertyInstance* p_pcOut,bool p_bBE);
  288. // -------------------------------------------------------------------
  289. //! Get the default value for a given data type
  290. static ValueUnion DefaultValue(EDataType eType);
  291. // -------------------------------------------------------------------
  292. //! Parse a value
  293. static bool ParseValue(const char* p_szIn,const char** p_szOut,
  294. EDataType eType,ValueUnion* out);
  295. // -------------------------------------------------------------------
  296. //! Parse a binary value
  297. static bool ParseValueBinary(const char* p_szIn,const char** p_szOut,
  298. EDataType eType,ValueUnion* out,bool p_bBE);
  299. // -------------------------------------------------------------------
  300. //! Convert a property value to a given type TYPE
  301. template <typename TYPE>
  302. static TYPE ConvertTo(ValueUnion v, EDataType eType);
  303. };
  304. // ---------------------------------------------------------------------------------
  305. /** \brief Class for an element instance in a PLY file
  306. */
  307. class ElementInstance
  308. {
  309. public:
  310. //! Default constructor
  311. ElementInstance ()
  312. {}
  313. //! List of all parsed properties
  314. std::vector< PropertyInstance > alProperties;
  315. // -------------------------------------------------------------------
  316. //! Parse an element instance
  317. static bool ParseInstance (const char* p_szIn,const char** p_szOut,
  318. const Element* pcElement, ElementInstance* p_pcOut);
  319. // -------------------------------------------------------------------
  320. //! Parse a binary element instance
  321. static bool ParseInstanceBinary (const char* p_szIn,const char** p_szOut,
  322. const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE);
  323. };
  324. // ---------------------------------------------------------------------------------
  325. /** \brief Class for an element instance list in a PLY file
  326. */
  327. class ElementInstanceList
  328. {
  329. public:
  330. //! Default constructor
  331. ElementInstanceList ()
  332. {}
  333. //! Construction from a given element description
  334. ElementInstanceList (const Element* pc)
  335. {
  336. // reserve enough storage to speedup the process
  337. alInstances.resize(pc->NumOccur);
  338. }
  339. //! Destructor. Dallocates all storage
  340. ~ElementInstanceList()
  341. {
  342. // delete all elements
  343. for (std::vector<ElementInstance*>::const_iterator
  344. i = this->alInstances.begin();
  345. i != this->alInstances.end();++i)
  346. {
  347. delete (*i);
  348. }
  349. return;
  350. }
  351. //! List of all element instances
  352. std::vector< ElementInstance* > alInstances;
  353. // -------------------------------------------------------------------
  354. //! Parse an element instance list
  355. static bool ParseInstanceList (const char* p_szIn,const char** p_szOut,
  356. const Element* pcElement, ElementInstanceList* p_pcOut);
  357. // -------------------------------------------------------------------
  358. //! Parse a binary element instance list
  359. static bool ParseInstanceListBinary (const char* p_szIn,const char** p_szOut,
  360. const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE);
  361. };
  362. // ---------------------------------------------------------------------------------
  363. /** \brief Class to represent the document object model of an ASCII or binary
  364. * (both little and big-endian) PLY file
  365. */
  366. class DOM
  367. {
  368. public:
  369. //! Default constructor
  370. DOM()
  371. {}
  372. //! Destructor. Dallocates all storage
  373. ~DOM()
  374. {
  375. // delete all elements
  376. for (std::vector<Element*>::const_iterator
  377. i = this->alElements.begin();
  378. i != this->alElements.end();++i)
  379. {
  380. delete (*i);
  381. }
  382. // delete all instance lists
  383. for (std::vector<ElementInstanceList*>::const_iterator
  384. i = this->alElementData.begin();
  385. i != this->alElementData.end();++i)
  386. {
  387. delete (*i);
  388. }
  389. return;
  390. }
  391. //! Contains all elements of the file format
  392. std::vector<Element*> alElements;
  393. //! Contains the real data of each element's instance list
  394. std::vector<ElementInstanceList*> alElementData;
  395. //! Parse the DOM for a PLY file. The input string is assumed
  396. //! to be terminated with zero
  397. static bool ParseInstance (const char* p_szIn,DOM* p_pcOut);
  398. static bool ParseInstanceBinary (const char* p_szIn,
  399. DOM* p_pcOut,bool p_bBE);
  400. //! Skip all comment lines after this
  401. static bool SkipComments (const char* p_szIn,const char** p_szOut);
  402. private:
  403. // -------------------------------------------------------------------
  404. //! Handle the file header and read all element descriptions
  405. bool ParseHeader (const char* p_szIn,const char** p_szOut);
  406. // -------------------------------------------------------------------
  407. //! Read in all element instance lists
  408. bool ParseElementInstanceLists (const char* p_szIn,const char** p_szOut);
  409. // -------------------------------------------------------------------
  410. //! Read in all element instance lists for a binary file format
  411. bool ParseElementInstanceListsBinary (const char* p_szIn,
  412. const char** p_szOut,bool p_bBE);
  413. };
  414. // ---------------------------------------------------------------------------------
  415. /** \brief Helper class to represent a loaded PLY face
  416. */
  417. class Face
  418. {
  419. public:
  420. Face()
  421. : iMaterialIndex(0xFFFFFFFF)
  422. {
  423. // set all indices to zero by default
  424. mIndices.resize(3,0);
  425. }
  426. public:
  427. //! List of vertex indices
  428. std::vector<unsigned int> mIndices;
  429. //! Material index
  430. unsigned int iMaterialIndex;
  431. };
  432. // ---------------------------------------------------------------------------------
  433. template <typename TYPE>
  434. TYPE PLY::PropertyInstance::ConvertTo(
  435. PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType)
  436. {
  437. switch (eType)
  438. {
  439. case EDT_Float:
  440. return (TYPE)v.fFloat;
  441. case EDT_Double:
  442. return (TYPE)v.fDouble;
  443. case EDT_UInt:
  444. case EDT_UShort:
  445. case EDT_UChar:
  446. return (TYPE)v.iUInt;
  447. case EDT_Int:
  448. case EDT_Short:
  449. case EDT_Char:
  450. return (TYPE)v.iInt;
  451. default: ;
  452. };
  453. return (TYPE)0;
  454. }
  455. } // Namespace PLY
  456. } // Namespace AssImp
  457. #endif // !! include guard