vrshoot

view libs/assimp/PlyParser.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
42 /** @file Defines the helper data structures for importing PLY files */
43 #ifndef AI_PLYFILEHELPER_H_INC
44 #define AI_PLYFILEHELPER_H_INC
47 #include "ParsingUtils.h"
50 namespace Assimp
51 {
53 // http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/
54 // http://w3.impa.br/~lvelho/outgoing/sossai/old/ViHAP_D4.4.2_PLY_format_v1.1.pdf
55 // http://www.okino.com/conv/exp_ply.htm
56 namespace PLY
57 {
60 // ---------------------------------------------------------------------------------
61 /*
62 name type number of bytes
63 ---------------------------------------
64 char character 1
65 uchar unsigned character 1
66 short short integer 2
67 ushort unsigned short integer 2
68 int integer 4
69 uint unsigned integer 4
70 float single-precision float 4
71 double double-precision float 8
73 int8
74 int16
75 uint8 ... forms are also used
76 */
77 enum EDataType
78 {
79 EDT_Char = 0x0u,
80 EDT_UChar,
81 EDT_Short,
82 EDT_UShort,
83 EDT_Int,
84 EDT_UInt,
85 EDT_Float,
86 EDT_Double,
88 // Marks invalid entries
89 EDT_INVALID
90 };
92 // ---------------------------------------------------------------------------------
93 /** \brief Specifies semantics for PLY element properties
94 *
95 * Semantics define the usage of a property, e.g. x coordinate
96 */
97 enum ESemantic
98 {
99 //! vertex position x coordinate
100 EST_XCoord = 0x0u,
101 //! vertex position x coordinate
102 EST_YCoord,
103 //! vertex position x coordinate
104 EST_ZCoord,
106 //! vertex normal x coordinate
107 EST_XNormal,
108 //! vertex normal y coordinate
109 EST_YNormal,
110 //! vertex normal z coordinate
111 EST_ZNormal,
113 //! u texture coordinate
114 EST_UTextureCoord,
115 //! v texture coordinate
116 EST_VTextureCoord,
118 //! vertex colors, red channel
119 EST_Red,
120 //! vertex colors, green channel
121 EST_Green,
122 //! vertex colors, blue channel
123 EST_Blue,
124 //! vertex colors, alpha channel
125 EST_Alpha,
127 //! vertex index list
128 EST_VertexIndex,
130 //! texture index
131 EST_TextureIndex,
133 //! texture coordinates (stored as element of a face)
134 EST_TextureCoordinates,
136 //! material index
137 EST_MaterialIndex,
139 //! ambient color, red channel
140 EST_AmbientRed,
141 //! ambient color, green channel
142 EST_AmbientGreen,
143 //! ambient color, blue channel
144 EST_AmbientBlue,
145 //! ambient color, alpha channel
146 EST_AmbientAlpha,
148 //! diffuse color, red channel
149 EST_DiffuseRed,
150 //! diffuse color, green channel
151 EST_DiffuseGreen,
152 //! diffuse color, blue channel
153 EST_DiffuseBlue,
154 //! diffuse color, alpha channel
155 EST_DiffuseAlpha,
157 //! specular color, red channel
158 EST_SpecularRed,
159 //! specular color, green channel
160 EST_SpecularGreen,
161 //! specular color, blue channel
162 EST_SpecularBlue,
163 //! specular color, alpha channel
164 EST_SpecularAlpha,
166 //! specular power for phong shading
167 EST_PhongPower,
169 //! opacity between 0 and 1
170 EST_Opacity,
172 //! Marks invalid entries
173 EST_INVALID
174 };
176 // ---------------------------------------------------------------------------------
177 /** \brief Specifies semantics for PLY elements
178 *
179 * Semantics define the usage of an element, e.g. vertex or material
180 */
181 enum EElementSemantic
182 {
183 //! The element is a vertex
184 EEST_Vertex = 0x0u,
186 //! The element is a face description (index table)
187 EEST_Face,
189 //! The element is a tristrip description (index table)
190 EEST_TriStrip,
192 //! The element is an edge description (ignored)
193 EEST_Edge,
195 //! The element is a material description
196 EEST_Material,
198 //! Marks invalid entries
199 EEST_INVALID
200 };
202 // ---------------------------------------------------------------------------------
203 /** \brief Helper class for a property in a PLY file.
204 *
205 * This can e.g. be a part of the vertex declaration
206 */
207 class Property
208 {
209 public:
211 //! Default constructor
212 Property()
213 : eType (EDT_Int), bIsList(false), eFirstType(EDT_UChar)
214 {}
216 //! Data type of the property
217 EDataType eType;
219 //! Semantical meaning of the property
220 ESemantic Semantic;
222 //! Of the semantic of the property could not be parsed:
223 //! Contains the semantic specified in the file
224 std::string szName;
226 //! Specifies whether the data type is a list where
227 //! the first element specifies the size of the list
228 bool bIsList;
229 EDataType eFirstType;
231 // -------------------------------------------------------------------
232 //! Parse a property from a string. The end of the
233 //! string is either '\n', '\r' or '\0'. Return valie is false
234 //! if the input string is NOT a valid property (E.g. does
235 //! not start with the "property" keyword)
236 static bool ParseProperty (const char* pCur, const char** pCurOut,
237 Property* pOut);
239 // -------------------------------------------------------------------
240 //! Parse a data type from a string
241 static EDataType ParseDataType(const char* pCur,const char** pCurOut);
243 // -------------------------------------------------------------------
244 //! Parse a semantic from a string
245 static ESemantic ParseSemantic(const char* pCur,const char** pCurOut);
246 };
248 // ---------------------------------------------------------------------------------
249 /** \brief Helper class for an element in a PLY file.
250 *
251 * This can e.g. be the vertex declaration. Elements contain a
252 * well-defined number of properties.
253 */
254 class Element
255 {
256 public:
258 //! Default constructor
259 Element()
260 : eSemantic (EEST_INVALID)
261 , NumOccur(0)
262 {}
264 //! List of properties assigned to the element
265 //! std::vector to support operator[]
266 std::vector<Property> alProperties;
268 //! Semantic of the element
269 EElementSemantic eSemantic;
271 //! Of the semantic of the element could not be parsed:
272 //! Contains the semantic specified in the file
273 std::string szName;
275 //! How many times will the element occur?
276 unsigned int NumOccur;
279 // -------------------------------------------------------------------
280 //! Parse an element from a string.
281 //! The function will parse all properties contained in the
282 //! element, too.
283 static bool ParseElement (const char* pCur, const char** pCurOut,
284 Element* pOut);
286 // -------------------------------------------------------------------
287 //! Parse a semantic from a string
288 static EElementSemantic ParseSemantic(const char* pCur,
289 const char** pCurOut);
290 };
292 // ---------------------------------------------------------------------------------
293 /** \brief Instance of a property in a PLY file
294 */
295 class PropertyInstance
296 {
297 public:
299 //! Default constructor
300 PropertyInstance ()
301 {}
303 union ValueUnion
304 {
306 //! uInt32 representation of the property. All
307 // uint types are automatically converted to uint32
308 uint32_t iUInt;
310 //! Int32 representation of the property. All
311 // int types are automatically converted to int32
312 int32_t iInt;
314 //! Float32 representation of the property
315 float fFloat;
317 //! Float64 representation of the property
318 double fDouble;
320 };
322 // -------------------------------------------------------------------
323 //! List of all values parsed. Contains only one value
324 // for non-list properties
325 std::vector<ValueUnion> avList;
327 // -------------------------------------------------------------------
328 //! Parse a property instance
329 static bool ParseInstance (const char* pCur,const char** pCurOut,
330 const Property* prop, PropertyInstance* p_pcOut);
332 // -------------------------------------------------------------------
333 //! Parse a property instance in binary format
334 static bool ParseInstanceBinary (const char* pCur,const char** pCurOut,
335 const Property* prop, PropertyInstance* p_pcOut,bool p_bBE);
337 // -------------------------------------------------------------------
338 //! Get the default value for a given data type
339 static ValueUnion DefaultValue(EDataType eType);
341 // -------------------------------------------------------------------
342 //! Parse a value
343 static bool ParseValue(const char* pCur,const char** pCurOut,
344 EDataType eType,ValueUnion* out);
346 // -------------------------------------------------------------------
347 //! Parse a binary value
348 static bool ParseValueBinary(const char* pCur,const char** pCurOut,
349 EDataType eType,ValueUnion* out,bool p_bBE);
351 // -------------------------------------------------------------------
352 //! Convert a property value to a given type TYPE
353 template <typename TYPE>
354 static TYPE ConvertTo(ValueUnion v, EDataType eType);
355 };
357 // ---------------------------------------------------------------------------------
358 /** \brief Class for an element instance in a PLY file
359 */
360 class ElementInstance
361 {
362 public:
364 //! Default constructor
365 ElementInstance ()
366 {}
368 //! List of all parsed properties
369 std::vector< PropertyInstance > alProperties;
371 // -------------------------------------------------------------------
372 //! Parse an element instance
373 static bool ParseInstance (const char* pCur,const char** pCurOut,
374 const Element* pcElement, ElementInstance* p_pcOut);
376 // -------------------------------------------------------------------
377 //! Parse a binary element instance
378 static bool ParseInstanceBinary (const char* pCur,const char** pCurOut,
379 const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE);
380 };
382 // ---------------------------------------------------------------------------------
383 /** \brief Class for an element instance list in a PLY file
384 */
385 class ElementInstanceList
386 {
387 public:
389 //! Default constructor
390 ElementInstanceList ()
391 {}
393 //! List of all element instances
394 std::vector< ElementInstance > alInstances;
396 // -------------------------------------------------------------------
397 //! Parse an element instance list
398 static bool ParseInstanceList (const char* pCur,const char** pCurOut,
399 const Element* pcElement, ElementInstanceList* p_pcOut);
401 // -------------------------------------------------------------------
402 //! Parse a binary element instance list
403 static bool ParseInstanceListBinary (const char* pCur,const char** pCurOut,
404 const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE);
405 };
406 // ---------------------------------------------------------------------------------
407 /** \brief Class to represent the document object model of an ASCII or binary
408 * (both little and big-endian) PLY file
409 */
410 class DOM
411 {
412 public:
414 //! Default constructor
415 DOM()
416 {}
419 //! Contains all elements of the file format
420 std::vector<Element> alElements;
421 //! Contains the real data of each element's instance list
422 std::vector<ElementInstanceList> alElementData;
424 //! Parse the DOM for a PLY file. The input string is assumed
425 //! to be terminated with zero
426 static bool ParseInstance (const char* pCur,DOM* p_pcOut);
427 static bool ParseInstanceBinary (const char* pCur,
428 DOM* p_pcOut,bool p_bBE);
430 //! Skip all comment lines after this
431 static bool SkipComments (const char* pCur,const char** pCurOut);
433 private:
435 // -------------------------------------------------------------------
436 //! Handle the file header and read all element descriptions
437 bool ParseHeader (const char* pCur,const char** pCurOut);
439 // -------------------------------------------------------------------
440 //! Read in all element instance lists
441 bool ParseElementInstanceLists (const char* pCur,const char** pCurOut);
443 // -------------------------------------------------------------------
444 //! Read in all element instance lists for a binary file format
445 bool ParseElementInstanceListsBinary (const char* pCur,
446 const char** pCurOut,bool p_bBE);
447 };
449 // ---------------------------------------------------------------------------------
450 /** \brief Helper class to represent a loaded PLY face
451 */
452 class Face
453 {
454 public:
456 Face()
457 : iMaterialIndex(0xFFFFFFFF)
458 {
459 // set all indices to zero by default
460 mIndices.resize(3,0);
461 }
463 public:
465 //! List of vertex indices
466 std::vector<unsigned int> mIndices;
468 //! Material index
469 unsigned int iMaterialIndex;
470 };
472 // ---------------------------------------------------------------------------------
473 template <typename TYPE>
474 inline TYPE PLY::PropertyInstance::ConvertTo(
475 PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType)
476 {
477 switch (eType)
478 {
479 case EDT_Float:
480 return (TYPE)v.fFloat;
481 case EDT_Double:
482 return (TYPE)v.fDouble;
484 case EDT_UInt:
485 case EDT_UShort:
486 case EDT_UChar:
487 return (TYPE)v.iUInt;
489 case EDT_Int:
490 case EDT_Short:
491 case EDT_Char:
492 return (TYPE)v.iInt;
493 default: ;
494 };
495 return (TYPE)0;
496 }
498 } // Namespace PLY
499 } // Namespace AssImp
501 #endif // !! include guard