PlyParser.h 14 KB

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