PlyParser.h 15 KB

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