PlyParser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. //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. name type number of bytes
  50. ---------------------------------------
  51. char character 1
  52. uchar unsigned character 1
  53. short short integer 2
  54. ushort unsigned short integer 2
  55. int integer 4
  56. uint unsigned integer 4
  57. float single-precision float 4
  58. double double-precision float 8
  59. int8
  60. int16
  61. uint8 ... forms are also used
  62. */
  63. enum EDataType {
  64. EDT_Char = 0x0u,
  65. EDT_UChar,
  66. EDT_Short,
  67. EDT_UShort,
  68. EDT_Int,
  69. EDT_UInt,
  70. EDT_Float,
  71. EDT_Double,
  72. // Marks invalid entries
  73. EDT_INVALID
  74. };
  75. // ---------------------------------------------------------------------------------
  76. /** \brief Specifies semantics for PLY element properties
  77. *
  78. * Semantics define the usage of a property, e.g. x coordinate
  79. */
  80. enum ESemantic {
  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. //! The element is a vertex
  151. EEST_Vertex = 0x0u,
  152. //! The element is a face description (index table)
  153. EEST_Face,
  154. //! The element is a triangle-strip description (index table)
  155. EEST_TriStrip,
  156. //! The element is an edge description (ignored)
  157. EEST_Edge,
  158. //! The element is a material description
  159. EEST_Material,
  160. //! texture path
  161. EEST_TextureFile,
  162. //! Marks invalid entries
  163. EEST_INVALID
  164. };
  165. // ---------------------------------------------------------------------------------
  166. /** \brief Helper class for a property in a PLY file.
  167. *
  168. * This can e.g. be a part of the vertex declaration
  169. */
  170. class Property {
  171. public:
  172. //! Default constructor
  173. Property() AI_NO_EXCEPT
  174. : eType (EDT_Int)
  175. , Semantic()
  176. , bIsList(false)
  177. , eFirstType(EDT_UChar) {
  178. // empty
  179. }
  180. //! Data type of the property
  181. EDataType eType;
  182. //! Semantical meaning of the property
  183. ESemantic Semantic;
  184. //! Of the semantic of the property could not be parsed:
  185. //! Contains the semantic specified in the file
  186. std::string szName;
  187. //! Specifies whether the data type is a list where
  188. //! the first element specifies the size of the list
  189. bool bIsList;
  190. EDataType eFirstType;
  191. // -------------------------------------------------------------------
  192. //! Parse a property from a string. The end of the
  193. //! string is either '\n', '\r' or '\0'. Return value is false
  194. //! if the input string is NOT a valid property (E.g. does
  195. //! not start with the "property" keyword)
  196. static bool ParseProperty(std::vector<char> &buffer, Property* pOut);
  197. // -------------------------------------------------------------------
  198. //! Parse a data type from a string
  199. static EDataType ParseDataType(std::vector<char> &buffer);
  200. // -------------------------------------------------------------------
  201. //! Parse a semantic from a string
  202. static ESemantic ParseSemantic(std::vector<char> &buffer);
  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. public:
  212. //! Default constructor
  213. Element() AI_NO_EXCEPT
  214. : eSemantic (EEST_INVALID)
  215. , NumOccur(0) {
  216. // empty
  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(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, Element* pOut);
  233. // -------------------------------------------------------------------
  234. //! Parse a semantic from a string
  235. static EElementSemantic ParseSemantic(std::vector<char> &buffer);
  236. };
  237. // ---------------------------------------------------------------------------------
  238. /** \brief Instance of a property in a PLY file
  239. */
  240. class PropertyInstance
  241. {
  242. public:
  243. //! Default constructor
  244. PropertyInstance() AI_NO_EXCEPT = default;
  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 *end,
  265. const Property* prop, PropertyInstance* p_pcOut);
  266. // -------------------------------------------------------------------
  267. //! Parse a property instance in binary format
  268. static bool ParseInstanceBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
  269. const char* &pCur, unsigned int &bufferSize, 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, EDataType eType, ValueUnion* out);
  276. // -------------------------------------------------------------------
  277. //! Parse a binary value
  278. static bool ParseValueBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
  279. const char* &pCur, unsigned int &bufferSize, EDataType eType, ValueUnion* out, bool p_bBE);
  280. // -------------------------------------------------------------------
  281. //! Convert a property value to a given type TYPE
  282. template <typename TYPE>
  283. static TYPE ConvertTo(ValueUnion v, EDataType eType);
  284. };
  285. // ---------------------------------------------------------------------------------
  286. /** \brief Class for an element instance in a PLY file
  287. */
  288. class ElementInstance {
  289. public:
  290. //! Default constructor
  291. ElementInstance() AI_NO_EXCEPT = default;
  292. //! List of all parsed properties
  293. std::vector< PropertyInstance > alProperties;
  294. // -------------------------------------------------------------------
  295. //! Parse an element instance
  296. static bool ParseInstance(const char *&pCur, const char *end,
  297. const Element* pcElement, ElementInstance* p_pcOut);
  298. // -------------------------------------------------------------------
  299. //! Parse a binary element instance
  300. static bool ParseInstanceBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
  301. const char* &pCur, unsigned int &bufferSize, const Element* pcElement, ElementInstance* p_pcOut, bool p_bBE);
  302. };
  303. // ---------------------------------------------------------------------------------
  304. /** \brief Class for an element instance list in a PLY file
  305. */
  306. class ElementInstanceList
  307. {
  308. public:
  309. //! Default constructor
  310. ElementInstanceList() AI_NO_EXCEPT = default;
  311. //! List of all element instances
  312. std::vector< ElementInstance > alInstances;
  313. // -------------------------------------------------------------------
  314. //! Parse an element instance list
  315. static bool ParseInstanceList(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
  316. const Element* pcElement, ElementInstanceList* p_pcOut, PLYImporter* loader);
  317. // -------------------------------------------------------------------
  318. //! Parse a binary element instance list
  319. static bool ParseInstanceListBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
  320. const char* &pCur, unsigned int &bufferSize, const Element* pcElement, ElementInstanceList* p_pcOut, PLYImporter* loader, bool p_bBE);
  321. };
  322. // ---------------------------------------------------------------------------------
  323. /** \brief Class to represent the document object model of an ASCII or binary
  324. * (both little and big-endian) PLY file
  325. */
  326. class DOM
  327. {
  328. public:
  329. //! Default constructor
  330. DOM() AI_NO_EXCEPT = default;
  331. //! Contains all elements of the file format
  332. std::vector<Element> alElements;
  333. //! Contains the real data of each element's instance list
  334. std::vector<ElementInstanceList> alElementData;
  335. //! Parse the DOM for a PLY file. The input string is assumed
  336. //! to be terminated with zero
  337. static bool ParseInstance(IOStreamBuffer<char> &streamBuffer, DOM* p_pcOut, PLYImporter* loader);
  338. static bool ParseInstanceBinary(IOStreamBuffer<char> &streamBuffer, DOM* p_pcOut, PLYImporter* loader, bool p_bBE);
  339. //! Skip all comment lines after this
  340. static bool SkipComments(std::vector<char> buffer);
  341. static bool SkipSpaces(std::vector<char> &buffer);
  342. static bool SkipLine(std::vector<char> &buffer);
  343. static bool TokenMatch(std::vector<char> &buffer, const char* token, unsigned int len);
  344. static bool SkipSpacesAndLineEnd(std::vector<char> &buffer);
  345. private:
  346. // -------------------------------------------------------------------
  347. //! Handle the file header and read all element descriptions
  348. bool ParseHeader(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, bool p_bBE);
  349. // -------------------------------------------------------------------
  350. //! Read in all element instance lists
  351. bool ParseElementInstanceLists(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, PLYImporter* loader);
  352. // -------------------------------------------------------------------
  353. //! Read in all element instance lists for a binary file format
  354. bool ParseElementInstanceListsBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, const char* &pCur, unsigned int &bufferSize, PLYImporter* loader, bool p_bBE);
  355. };
  356. // ---------------------------------------------------------------------------------
  357. template <typename TYPE>
  358. inline TYPE PLY::PropertyInstance::ConvertTo(
  359. PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType)
  360. {
  361. switch (eType)
  362. {
  363. case EDT_Float:
  364. return (TYPE)v.fFloat;
  365. case EDT_Double:
  366. return (TYPE)v.fDouble;
  367. case EDT_UInt:
  368. case EDT_UShort:
  369. case EDT_UChar:
  370. return (TYPE)v.iUInt;
  371. case EDT_Int:
  372. case EDT_Short:
  373. case EDT_Char:
  374. return (TYPE)v.iInt;
  375. default: ;
  376. };
  377. return (TYPE)0;
  378. }
  379. } // Namespace PLY
  380. } // Namespace AssImp
  381. #endif // !! include guard