You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

502 lines
14 KiB

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