PlyParser.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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),
  175. Semantic(),
  176. bIsList(false),
  177. eFirstType(EDT_UChar)
  178. {}
  179. //! Data type of the property
  180. EDataType eType;
  181. //! Semantical meaning of the property
  182. ESemantic Semantic;
  183. //! Of the semantic of the property could not be parsed:
  184. //! Contains the semantic specified in the file
  185. std::string szName;
  186. //! Specifies whether the data type is a list where
  187. //! the first element specifies the size of the list
  188. bool bIsList;
  189. EDataType eFirstType;
  190. // -------------------------------------------------------------------
  191. //! Parse a property from a string. The end of the
  192. //! string is either '\n', '\r' or '\0'. Return value is false
  193. //! if the input string is NOT a valid property (E.g. does
  194. //! not start with the "property" keyword)
  195. static bool ParseProperty (const char* pCur, const char** pCurOut,
  196. Property* pOut);
  197. // -------------------------------------------------------------------
  198. //! Parse a data type from a string
  199. static EDataType ParseDataType(const char* pCur,const char** pCurOut);
  200. // -------------------------------------------------------------------
  201. //! Parse a semantic from a string
  202. static ESemantic ParseSemantic(const char* pCur,const char** pCurOut);
  203. };
  204. // ---------------------------------------------------------------------------------
  205. /** \brief Helper class for an element in a PLY file.
  206. *
  207. * This can e.g. be the vertex declaration. Elements contain a
  208. * well-defined number of properties.
  209. */
  210. class Element
  211. {
  212. public:
  213. //! Default constructor
  214. Element()
  215. : eSemantic (EEST_INVALID)
  216. , NumOccur(0)
  217. {}
  218. //! List of properties assigned to the element
  219. //! std::vector to support operator[]
  220. std::vector<Property> alProperties;
  221. //! Semantic of the element
  222. EElementSemantic eSemantic;
  223. //! Of the semantic of the element could not be parsed:
  224. //! Contains the semantic specified in the file
  225. std::string szName;
  226. //! How many times will the element occur?
  227. unsigned int NumOccur;
  228. // -------------------------------------------------------------------
  229. //! Parse an element from a string.
  230. //! The function will parse all properties contained in the
  231. //! element, too.
  232. static bool ParseElement (const char* pCur, const char** pCurOut,
  233. Element* pOut);
  234. // -------------------------------------------------------------------
  235. //! Parse a semantic from a string
  236. static EElementSemantic ParseSemantic(const char* pCur,
  237. const char** pCurOut);
  238. };
  239. // ---------------------------------------------------------------------------------
  240. /** \brief Instance of a property in a PLY file
  241. */
  242. class PropertyInstance
  243. {
  244. public:
  245. //! Default constructor
  246. PropertyInstance ()
  247. {}
  248. union ValueUnion
  249. {
  250. //! uInt32 representation of the property. All
  251. // uint types are automatically converted to uint32
  252. uint32_t iUInt;
  253. //! Int32 representation of the property. All
  254. // int types are automatically converted to int32
  255. int32_t iInt;
  256. //! Float32 representation of the property
  257. float fFloat;
  258. //! Float64 representation of the property
  259. double fDouble;
  260. };
  261. // -------------------------------------------------------------------
  262. //! List of all values parsed. Contains only one value
  263. // for non-list properties
  264. std::vector<ValueUnion> avList;
  265. // -------------------------------------------------------------------
  266. //! Parse a property instance
  267. static bool ParseInstance (const char* pCur,const char** pCurOut,
  268. const Property* prop, PropertyInstance* p_pcOut);
  269. // -------------------------------------------------------------------
  270. //! Parse a property instance in binary format
  271. static bool ParseInstanceBinary (const char* pCur,const char** pCurOut,
  272. const Property* prop, PropertyInstance* p_pcOut,bool p_bBE);
  273. // -------------------------------------------------------------------
  274. //! Get the default value for a given data type
  275. static ValueUnion DefaultValue(EDataType eType);
  276. // -------------------------------------------------------------------
  277. //! Parse a value
  278. static bool ParseValue(const char* pCur,const char** pCurOut,
  279. EDataType eType,ValueUnion* out);
  280. // -------------------------------------------------------------------
  281. //! Parse a binary value
  282. static bool ParseValueBinary(const char* pCur,const char** pCurOut,
  283. EDataType eType,ValueUnion* out,bool p_bBE);
  284. // -------------------------------------------------------------------
  285. //! Convert a property value to a given type TYPE
  286. template <typename TYPE>
  287. static TYPE ConvertTo(ValueUnion v, EDataType eType);
  288. };
  289. // ---------------------------------------------------------------------------------
  290. /** \brief Class for an element instance in a PLY file
  291. */
  292. class ElementInstance
  293. {
  294. public:
  295. //! Default constructor
  296. ElementInstance ()
  297. {}
  298. //! List of all parsed properties
  299. std::vector< PropertyInstance > alProperties;
  300. // -------------------------------------------------------------------
  301. //! Parse an element instance
  302. static bool ParseInstance (const char* pCur,const char** pCurOut,
  303. const Element* pcElement, ElementInstance* p_pcOut);
  304. // -------------------------------------------------------------------
  305. //! Parse a binary element instance
  306. static bool ParseInstanceBinary (const char* pCur,const char** pCurOut,
  307. const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE);
  308. };
  309. // ---------------------------------------------------------------------------------
  310. /** \brief Class for an element instance list in a PLY file
  311. */
  312. class ElementInstanceList
  313. {
  314. public:
  315. //! Default constructor
  316. ElementInstanceList ()
  317. {}
  318. //! List of all element instances
  319. std::vector< ElementInstance > alInstances;
  320. // -------------------------------------------------------------------
  321. //! Parse an element instance list
  322. static bool ParseInstanceList (const char* pCur,const char** pCurOut,
  323. const Element* pcElement, ElementInstanceList* p_pcOut);
  324. // -------------------------------------------------------------------
  325. //! Parse a binary element instance list
  326. static bool ParseInstanceListBinary (const char* pCur,const char** pCurOut,
  327. const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE);
  328. };
  329. // ---------------------------------------------------------------------------------
  330. /** \brief Class to represent the document object model of an ASCII or binary
  331. * (both little and big-endian) PLY file
  332. */
  333. class DOM
  334. {
  335. public:
  336. //! Default constructor
  337. DOM()
  338. {}
  339. //! Contains all elements of the file format
  340. std::vector<Element> alElements;
  341. //! Contains the real data of each element's instance list
  342. std::vector<ElementInstanceList> alElementData;
  343. //! Parse the DOM for a PLY file. The input string is assumed
  344. //! to be terminated with zero
  345. static bool ParseInstance (const char* pCur,DOM* p_pcOut);
  346. static bool ParseInstanceBinary (const char* pCur,
  347. DOM* p_pcOut,bool p_bBE);
  348. //! Skip all comment lines after this
  349. static bool SkipComments (const char* pCur,const char** pCurOut);
  350. private:
  351. // -------------------------------------------------------------------
  352. //! Handle the file header and read all element descriptions
  353. bool ParseHeader (const char* pCur,const char** pCurOut, bool p_bBE);
  354. // -------------------------------------------------------------------
  355. //! Read in all element instance lists
  356. bool ParseElementInstanceLists (const char* pCur,const char** pCurOut);
  357. // -------------------------------------------------------------------
  358. //! Read in all element instance lists for a binary file format
  359. bool ParseElementInstanceListsBinary (const char* pCur,
  360. const char** pCurOut,bool p_bBE);
  361. };
  362. // ---------------------------------------------------------------------------------
  363. /** \brief Helper class to represent a loaded PLY face
  364. */
  365. class Face
  366. {
  367. public:
  368. Face()
  369. : iMaterialIndex(0xFFFFFFFF)
  370. {
  371. // set all indices to zero by default
  372. mIndices.resize(3,0);
  373. }
  374. public:
  375. //! List of vertex indices
  376. std::vector<unsigned int> mIndices;
  377. //! Material index
  378. unsigned int iMaterialIndex;
  379. };
  380. // ---------------------------------------------------------------------------------
  381. template <typename TYPE>
  382. inline TYPE PLY::PropertyInstance::ConvertTo(
  383. PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType)
  384. {
  385. switch (eType)
  386. {
  387. case EDT_Float:
  388. return (TYPE)v.fFloat;
  389. case EDT_Double:
  390. return (TYPE)v.fDouble;
  391. case EDT_UInt:
  392. case EDT_UShort:
  393. case EDT_UChar:
  394. return (TYPE)v.iUInt;
  395. case EDT_Int:
  396. case EDT_Short:
  397. case EDT_Char:
  398. return (TYPE)v.iInt;
  399. default: ;
  400. };
  401. return (TYPE)0;
  402. }
  403. } // Namespace PLY
  404. } // Namespace AssImp
  405. #endif // !! include guard