PlyParser.h 15 KB

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