vrshoot
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/PlyParser.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,501 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 + 1.45 +/** @file Defines the helper data structures for importing PLY files */ 1.46 +#ifndef AI_PLYFILEHELPER_H_INC 1.47 +#define AI_PLYFILEHELPER_H_INC 1.48 + 1.49 + 1.50 +#include "ParsingUtils.h" 1.51 + 1.52 + 1.53 +namespace Assimp 1.54 +{ 1.55 + 1.56 +// http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/ 1.57 +// http://w3.impa.br/~lvelho/outgoing/sossai/old/ViHAP_D4.4.2_PLY_format_v1.1.pdf 1.58 +// http://www.okino.com/conv/exp_ply.htm 1.59 +namespace PLY 1.60 +{ 1.61 + 1.62 + 1.63 +// --------------------------------------------------------------------------------- 1.64 +/* 1.65 +name type number of bytes 1.66 +--------------------------------------- 1.67 +char character 1 1.68 +uchar unsigned character 1 1.69 +short short integer 2 1.70 +ushort unsigned short integer 2 1.71 +int integer 4 1.72 +uint unsigned integer 4 1.73 +float single-precision float 4 1.74 +double double-precision float 8 1.75 + 1.76 +int8 1.77 +int16 1.78 +uint8 ... forms are also used 1.79 +*/ 1.80 +enum EDataType 1.81 +{ 1.82 + EDT_Char = 0x0u, 1.83 + EDT_UChar, 1.84 + EDT_Short, 1.85 + EDT_UShort, 1.86 + EDT_Int, 1.87 + EDT_UInt, 1.88 + EDT_Float, 1.89 + EDT_Double, 1.90 + 1.91 + // Marks invalid entries 1.92 + EDT_INVALID 1.93 +}; 1.94 + 1.95 +// --------------------------------------------------------------------------------- 1.96 +/** \brief Specifies semantics for PLY element properties 1.97 + * 1.98 + * Semantics define the usage of a property, e.g. x coordinate 1.99 +*/ 1.100 +enum ESemantic 1.101 +{ 1.102 + //! vertex position x coordinate 1.103 + EST_XCoord = 0x0u, 1.104 + //! vertex position x coordinate 1.105 + EST_YCoord, 1.106 + //! vertex position x coordinate 1.107 + EST_ZCoord, 1.108 + 1.109 + //! vertex normal x coordinate 1.110 + EST_XNormal, 1.111 + //! vertex normal y coordinate 1.112 + EST_YNormal, 1.113 + //! vertex normal z coordinate 1.114 + EST_ZNormal, 1.115 + 1.116 + //! u texture coordinate 1.117 + EST_UTextureCoord, 1.118 + //! v texture coordinate 1.119 + EST_VTextureCoord, 1.120 + 1.121 + //! vertex colors, red channel 1.122 + EST_Red, 1.123 + //! vertex colors, green channel 1.124 + EST_Green, 1.125 + //! vertex colors, blue channel 1.126 + EST_Blue, 1.127 + //! vertex colors, alpha channel 1.128 + EST_Alpha, 1.129 + 1.130 + //! vertex index list 1.131 + EST_VertexIndex, 1.132 + 1.133 + //! texture index 1.134 + EST_TextureIndex, 1.135 + 1.136 + //! texture coordinates (stored as element of a face) 1.137 + EST_TextureCoordinates, 1.138 + 1.139 + //! material index 1.140 + EST_MaterialIndex, 1.141 + 1.142 + //! ambient color, red channel 1.143 + EST_AmbientRed, 1.144 + //! ambient color, green channel 1.145 + EST_AmbientGreen, 1.146 + //! ambient color, blue channel 1.147 + EST_AmbientBlue, 1.148 + //! ambient color, alpha channel 1.149 + EST_AmbientAlpha, 1.150 + 1.151 + //! diffuse color, red channel 1.152 + EST_DiffuseRed, 1.153 + //! diffuse color, green channel 1.154 + EST_DiffuseGreen, 1.155 + //! diffuse color, blue channel 1.156 + EST_DiffuseBlue, 1.157 + //! diffuse color, alpha channel 1.158 + EST_DiffuseAlpha, 1.159 + 1.160 + //! specular color, red channel 1.161 + EST_SpecularRed, 1.162 + //! specular color, green channel 1.163 + EST_SpecularGreen, 1.164 + //! specular color, blue channel 1.165 + EST_SpecularBlue, 1.166 + //! specular color, alpha channel 1.167 + EST_SpecularAlpha, 1.168 + 1.169 + //! specular power for phong shading 1.170 + EST_PhongPower, 1.171 + 1.172 + //! opacity between 0 and 1 1.173 + EST_Opacity, 1.174 + 1.175 + //! Marks invalid entries 1.176 + EST_INVALID 1.177 +}; 1.178 + 1.179 +// --------------------------------------------------------------------------------- 1.180 +/** \brief Specifies semantics for PLY elements 1.181 + * 1.182 + * Semantics define the usage of an element, e.g. vertex or material 1.183 +*/ 1.184 +enum EElementSemantic 1.185 +{ 1.186 + //! The element is a vertex 1.187 + EEST_Vertex = 0x0u, 1.188 + 1.189 + //! The element is a face description (index table) 1.190 + EEST_Face, 1.191 + 1.192 + //! The element is a tristrip description (index table) 1.193 + EEST_TriStrip, 1.194 + 1.195 + //! The element is an edge description (ignored) 1.196 + EEST_Edge, 1.197 + 1.198 + //! The element is a material description 1.199 + EEST_Material, 1.200 + 1.201 + //! Marks invalid entries 1.202 + EEST_INVALID 1.203 +}; 1.204 + 1.205 +// --------------------------------------------------------------------------------- 1.206 +/** \brief Helper class for a property in a PLY file. 1.207 + * 1.208 + * This can e.g. be a part of the vertex declaration 1.209 + */ 1.210 +class Property 1.211 +{ 1.212 +public: 1.213 + 1.214 + //! Default constructor 1.215 + Property() 1.216 + : eType (EDT_Int), bIsList(false), eFirstType(EDT_UChar) 1.217 + {} 1.218 + 1.219 + //! Data type of the property 1.220 + EDataType eType; 1.221 + 1.222 + //! Semantical meaning of the property 1.223 + ESemantic Semantic; 1.224 + 1.225 + //! Of the semantic of the property could not be parsed: 1.226 + //! Contains the semantic specified in the file 1.227 + std::string szName; 1.228 + 1.229 + //! Specifies whether the data type is a list where 1.230 + //! the first element specifies the size of the list 1.231 + bool bIsList; 1.232 + EDataType eFirstType; 1.233 + 1.234 + // ------------------------------------------------------------------- 1.235 + //! Parse a property from a string. The end of the 1.236 + //! string is either '\n', '\r' or '\0'. Return valie is false 1.237 + //! if the input string is NOT a valid property (E.g. does 1.238 + //! not start with the "property" keyword) 1.239 + static bool ParseProperty (const char* pCur, const char** pCurOut, 1.240 + Property* pOut); 1.241 + 1.242 + // ------------------------------------------------------------------- 1.243 + //! Parse a data type from a string 1.244 + static EDataType ParseDataType(const char* pCur,const char** pCurOut); 1.245 + 1.246 + // ------------------------------------------------------------------- 1.247 + //! Parse a semantic from a string 1.248 + static ESemantic ParseSemantic(const char* pCur,const char** pCurOut); 1.249 +}; 1.250 + 1.251 +// --------------------------------------------------------------------------------- 1.252 +/** \brief Helper class for an element in a PLY file. 1.253 + * 1.254 + * This can e.g. be the vertex declaration. Elements contain a 1.255 + * well-defined number of properties. 1.256 + */ 1.257 +class Element 1.258 +{ 1.259 +public: 1.260 + 1.261 + //! Default constructor 1.262 + Element() 1.263 + : eSemantic (EEST_INVALID) 1.264 + , NumOccur(0) 1.265 + {} 1.266 + 1.267 + //! List of properties assigned to the element 1.268 + //! std::vector to support operator[] 1.269 + std::vector<Property> alProperties; 1.270 + 1.271 + //! Semantic of the element 1.272 + EElementSemantic eSemantic; 1.273 + 1.274 + //! Of the semantic of the element could not be parsed: 1.275 + //! Contains the semantic specified in the file 1.276 + std::string szName; 1.277 + 1.278 + //! How many times will the element occur? 1.279 + unsigned int NumOccur; 1.280 + 1.281 + 1.282 + // ------------------------------------------------------------------- 1.283 + //! Parse an element from a string. 1.284 + //! The function will parse all properties contained in the 1.285 + //! element, too. 1.286 + static bool ParseElement (const char* pCur, const char** pCurOut, 1.287 + Element* pOut); 1.288 + 1.289 + // ------------------------------------------------------------------- 1.290 + //! Parse a semantic from a string 1.291 + static EElementSemantic ParseSemantic(const char* pCur, 1.292 + const char** pCurOut); 1.293 +}; 1.294 + 1.295 +// --------------------------------------------------------------------------------- 1.296 +/** \brief Instance of a property in a PLY file 1.297 + */ 1.298 +class PropertyInstance 1.299 +{ 1.300 +public: 1.301 + 1.302 + //! Default constructor 1.303 + PropertyInstance () 1.304 + {} 1.305 + 1.306 + union ValueUnion 1.307 + { 1.308 + 1.309 + //! uInt32 representation of the property. All 1.310 + // uint types are automatically converted to uint32 1.311 + uint32_t iUInt; 1.312 + 1.313 + //! Int32 representation of the property. All 1.314 + // int types are automatically converted to int32 1.315 + int32_t iInt; 1.316 + 1.317 + //! Float32 representation of the property 1.318 + float fFloat; 1.319 + 1.320 + //! Float64 representation of the property 1.321 + double fDouble; 1.322 + 1.323 + }; 1.324 + 1.325 + // ------------------------------------------------------------------- 1.326 + //! List of all values parsed. Contains only one value 1.327 + // for non-list properties 1.328 + std::vector<ValueUnion> avList; 1.329 + 1.330 + // ------------------------------------------------------------------- 1.331 + //! Parse a property instance 1.332 + static bool ParseInstance (const char* pCur,const char** pCurOut, 1.333 + const Property* prop, PropertyInstance* p_pcOut); 1.334 + 1.335 + // ------------------------------------------------------------------- 1.336 + //! Parse a property instance in binary format 1.337 + static bool ParseInstanceBinary (const char* pCur,const char** pCurOut, 1.338 + const Property* prop, PropertyInstance* p_pcOut,bool p_bBE); 1.339 + 1.340 + // ------------------------------------------------------------------- 1.341 + //! Get the default value for a given data type 1.342 + static ValueUnion DefaultValue(EDataType eType); 1.343 + 1.344 + // ------------------------------------------------------------------- 1.345 + //! Parse a value 1.346 + static bool ParseValue(const char* pCur,const char** pCurOut, 1.347 + EDataType eType,ValueUnion* out); 1.348 + 1.349 + // ------------------------------------------------------------------- 1.350 + //! Parse a binary value 1.351 + static bool ParseValueBinary(const char* pCur,const char** pCurOut, 1.352 + EDataType eType,ValueUnion* out,bool p_bBE); 1.353 + 1.354 + // ------------------------------------------------------------------- 1.355 + //! Convert a property value to a given type TYPE 1.356 + template <typename TYPE> 1.357 + static TYPE ConvertTo(ValueUnion v, EDataType eType); 1.358 +}; 1.359 + 1.360 +// --------------------------------------------------------------------------------- 1.361 +/** \brief Class for an element instance in a PLY file 1.362 + */ 1.363 +class ElementInstance 1.364 +{ 1.365 +public: 1.366 + 1.367 + //! Default constructor 1.368 + ElementInstance () 1.369 + {} 1.370 + 1.371 + //! List of all parsed properties 1.372 + std::vector< PropertyInstance > alProperties; 1.373 + 1.374 + // ------------------------------------------------------------------- 1.375 + //! Parse an element instance 1.376 + static bool ParseInstance (const char* pCur,const char** pCurOut, 1.377 + const Element* pcElement, ElementInstance* p_pcOut); 1.378 + 1.379 + // ------------------------------------------------------------------- 1.380 + //! Parse a binary element instance 1.381 + static bool ParseInstanceBinary (const char* pCur,const char** pCurOut, 1.382 + const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE); 1.383 +}; 1.384 + 1.385 +// --------------------------------------------------------------------------------- 1.386 +/** \brief Class for an element instance list in a PLY file 1.387 + */ 1.388 +class ElementInstanceList 1.389 +{ 1.390 +public: 1.391 + 1.392 + //! Default constructor 1.393 + ElementInstanceList () 1.394 + {} 1.395 + 1.396 + //! List of all element instances 1.397 + std::vector< ElementInstance > alInstances; 1.398 + 1.399 + // ------------------------------------------------------------------- 1.400 + //! Parse an element instance list 1.401 + static bool ParseInstanceList (const char* pCur,const char** pCurOut, 1.402 + const Element* pcElement, ElementInstanceList* p_pcOut); 1.403 + 1.404 + // ------------------------------------------------------------------- 1.405 + //! Parse a binary element instance list 1.406 + static bool ParseInstanceListBinary (const char* pCur,const char** pCurOut, 1.407 + const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE); 1.408 +}; 1.409 +// --------------------------------------------------------------------------------- 1.410 +/** \brief Class to represent the document object model of an ASCII or binary 1.411 + * (both little and big-endian) PLY file 1.412 + */ 1.413 +class DOM 1.414 +{ 1.415 +public: 1.416 + 1.417 + //! Default constructor 1.418 + DOM() 1.419 + {} 1.420 + 1.421 + 1.422 + //! Contains all elements of the file format 1.423 + std::vector<Element> alElements; 1.424 + //! Contains the real data of each element's instance list 1.425 + std::vector<ElementInstanceList> alElementData; 1.426 + 1.427 + //! Parse the DOM for a PLY file. The input string is assumed 1.428 + //! to be terminated with zero 1.429 + static bool ParseInstance (const char* pCur,DOM* p_pcOut); 1.430 + static bool ParseInstanceBinary (const char* pCur, 1.431 + DOM* p_pcOut,bool p_bBE); 1.432 + 1.433 + //! Skip all comment lines after this 1.434 + static bool SkipComments (const char* pCur,const char** pCurOut); 1.435 + 1.436 +private: 1.437 + 1.438 + // ------------------------------------------------------------------- 1.439 + //! Handle the file header and read all element descriptions 1.440 + bool ParseHeader (const char* pCur,const char** pCurOut); 1.441 + 1.442 + // ------------------------------------------------------------------- 1.443 + //! Read in all element instance lists 1.444 + bool ParseElementInstanceLists (const char* pCur,const char** pCurOut); 1.445 + 1.446 + // ------------------------------------------------------------------- 1.447 + //! Read in all element instance lists for a binary file format 1.448 + bool ParseElementInstanceListsBinary (const char* pCur, 1.449 + const char** pCurOut,bool p_bBE); 1.450 +}; 1.451 + 1.452 +// --------------------------------------------------------------------------------- 1.453 +/** \brief Helper class to represent a loaded PLY face 1.454 + */ 1.455 +class Face 1.456 +{ 1.457 +public: 1.458 + 1.459 + Face() 1.460 + : iMaterialIndex(0xFFFFFFFF) 1.461 + { 1.462 + // set all indices to zero by default 1.463 + mIndices.resize(3,0); 1.464 + } 1.465 + 1.466 +public: 1.467 + 1.468 + //! List of vertex indices 1.469 + std::vector<unsigned int> mIndices; 1.470 + 1.471 + //! Material index 1.472 + unsigned int iMaterialIndex; 1.473 +}; 1.474 + 1.475 +// --------------------------------------------------------------------------------- 1.476 +template <typename TYPE> 1.477 +inline TYPE PLY::PropertyInstance::ConvertTo( 1.478 + PLY::PropertyInstance::ValueUnion v, PLY::EDataType eType) 1.479 +{ 1.480 + switch (eType) 1.481 + { 1.482 + case EDT_Float: 1.483 + return (TYPE)v.fFloat; 1.484 + case EDT_Double: 1.485 + return (TYPE)v.fDouble; 1.486 + 1.487 + case EDT_UInt: 1.488 + case EDT_UShort: 1.489 + case EDT_UChar: 1.490 + return (TYPE)v.iUInt; 1.491 + 1.492 + case EDT_Int: 1.493 + case EDT_Short: 1.494 + case EDT_Char: 1.495 + return (TYPE)v.iInt; 1.496 + default: ; 1.497 + }; 1.498 + return (TYPE)0; 1.499 +} 1.500 + 1.501 +} // Namespace PLY 1.502 +} // Namespace AssImp 1.503 + 1.504 +#endif // !! include guard