Debian Codegraph @

src/thirdparty/PoissonRecon/PlyFile.h

raw
1 /*
2
3 Header for PLY polygon files.
4
5 - Greg Turk, March 1994
6
7 A PLY file contains a single polygonal _object_.
8
9 An object is composed of lists of _elements_. Typical elements are
10 vertices, faces, edges and materials.
11
12 Each type of element for a given object has one or more _properties_
13 associated with the element type. For instance, a vertex element may
14 have as properties three floating-point values x,y,z and three unsigned
15 chars for red, green and blue.
16
17 ---------------------------------------------------------------
18
19 Copyright (c) 2020, Georgia Institute of Technology
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24
25 Redistributions of source code must retain the above copyright notice, this
26 list of conditions and the following disclaimer. Redistributions in binary form
27 must reproduce the above copyright notice, this list of conditions and the
28 following disclaimer in the documentation and/or other materials provided with
29 the distribution.
30
31 Neither the name of the Georgia Institute of Technology nor the names of its
32 contributors may be used to endorse or promote products derived from this
33 software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
39 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46 */
47
48 #ifndef PLY_FILE_INCLUDED
49 #define PLY_FILE_INCLUDED
50
51 #include <string>
52 #include <vector>
53
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <stddef.h>
57 #include <string.h>
58 #include <math.h>
59 #include "MyMiscellany.h"
60 #include "Streams.h"
61
62 namespace PoissonRecon
63 {
64
65 #define PLY_ASCII 1 /* ascii PLY file */
66 #define PLY_BINARY_BE 2 /* binary PLY file, big endian */
67 #define PLY_BINARY_LE 3 /* binary PLY file, little endian */
68 #define PLY_BINARY_NATIVE 4 /* binary PLY file, same endianness as current architecture */
69
70 #define PLY_OKAY 0 /* ply routine worked okay */
71 #define PLY_ERROR -1 /* error in ply routine */
72
73 /* scalar data types supported by PLY format */
74
75 #define PLY_START_TYPE 0
76 #define PLY_CHAR 1
77 #define PLY_SHORT 2
78 #define PLY_INT 3
79 #define PLY_LONGLONG 4
80 #define PLY_UCHAR 5
81 #define PLY_USHORT 6
82 #define PLY_UINT 7
83 #define PLY_ULONGLONG 8
84 #define PLY_FLOAT 9
85 #define PLY_DOUBLE 10
86 #define PLY_INT_8 11
87 #define PLY_UINT_8 12
88 #define PLY_INT_16 13
89 #define PLY_UINT_16 14
90 #define PLY_INT_32 15
91 #define PLY_UINT_32 16
92 #define PLY_INT_64 17
93 #define PLY_UINT_64 18
94 #define PLY_FLOAT_32 19
95 #define PLY_FLOAT_64 20
96
97 #define PLY_END_TYPE 21
98
99 #define PLY_SCALAR 0
100 #define PLY_LIST 1
101
102 #define PLY_STRIP_COMMENT_HEADER 0
103
104 const std::string PlyTypes[]
105 {
106 "start type" ,
107 "char" ,
108 "short" ,
109 "int" ,
110 "long long " ,
111 "unsigned char" ,
112 "unsigned short" ,
113 "unsigned int" ,
114 "unsigned long long" ,
115 "float" ,
116 "double" ,
117 "int8" ,
118 "unsigned int8" ,
119 "int16" ,
120 "unsigned int16" ,
121 "int32" ,
122 "unsigned int32" ,
123 "int64" ,
124 "unsigned int64" ,
125 "float32" ,
126 "float64"
127 };
128
129 /* description of a property */
130 struct PlyProperty
131 {
132 std::string name; /* property name */
133 int external_type; /* file's data type */
134 int internal_type; /* program's data type */
135 int offset; /* offset bytes of prop in a struct */
136
137 int is_list; /* 1 = list, 0 = scalar */
138 int count_external; /* file's count type */
139 int count_internal; /* program's count type */
140 int count_offset; /* offset byte for list count */
141
142 PlyProperty( const std::string &n , int et , int it , int o , int il=0 , int ce=0 , int ci=0 , int co=0 ) : name(n) , external_type(et) , internal_type(it) , offset(o) , is_list(il) , count_external(ce) , count_internal(ci) , count_offset(co){ }
143 PlyProperty( const std::string &n ) : PlyProperty( n , 0 , 0 , 0 , 0 , 0 , 0 , 0 ){ }
144 PlyProperty( void ) : external_type(0) , internal_type(0) , offset(0) , is_list(0) , count_external(0) , count_internal(0) , count_offset(0){ }
145
146 void write( BinaryStream &stream ) const
147 {
148 stream.write( name );
149 stream.write( external_type );
150 stream.write( offset );
151 stream.write( is_list );
152 stream.write( count_external );
153 stream.write( count_internal );
154 stream.write( count_offset );
155 }
156 void read( BinaryStream &stream )
157 {
158 if( !stream.read( name ) ) MK_THROW( "Failed to read name" );
159 if( !stream.read( external_type ) ) MK_THROW( "Failed to read external_type" );
160 if( !stream.read( offset ) ) MK_THROW( "Failed to read offset" );
161 if( !stream.read( is_list ) ) MK_THROW( "Failed to read is_list" );
162 if( !stream.read( count_external ) ) MK_THROW( "Failed to read count_external" );
163 if( !stream.read( count_internal ) ) MK_THROW( "Failed to read count_internal" );
164 if( !stream.read( count_offset ) ) MK_THROW( "Failed to read count_offset" );
165 }
166 };
167
168 inline std::ostream &operator << ( std::ostream &os , PlyProperty p )
169 {
170 if( p.is_list ) return os << "{ " << p.name << " , " << PlyTypes[ p.count_external ] << " -> " << PlyTypes[ p.count_internal ] << " , " << PlyTypes[ p.external_type ] << " -> " << PlyTypes[ p.internal_type ] << " , " << p.offset << " }";
171 else return os << "{ " << p.name << " , " << PlyTypes[ p.external_type ] << " -> " << PlyTypes[ p.internal_type ] << " , " << p.offset << " }";
172 }
173
174 struct PlyStoredProperty
175 {
176 PlyProperty prop ; char store;
177 PlyStoredProperty( void ){ }
178 PlyStoredProperty( const PlyProperty &p , char s ) : prop(p) , store(s){ }
179 };
180
181 /* description of an element */
182 struct PlyElement
183 {
184 std::string name; /* element name */
185 size_t num; /* number of elements in this object */
186 int size; /* size of element (bytes) or -1 if variable */
187 std::vector< PlyStoredProperty > props; /* list of properties in the file */
188 int other_offset; /* offset to un-asked-for props, or -1 if none*/
189 int other_size; /* size of other_props structure */
190 inline PlyProperty *find_property( const std::string &prop_name , int &index );
191 };
192
193 /* describes other properties in an element */
194 struct PlyOtherProp
195 {
196 std::string name; /* element name */
197 int size; /* size of other_props */
198 std::vector< PlyProperty > props; /* list of properties in other_props */
199 };
200
201 /* storing other_props for an other element */
202 struct OtherData
203 {
204 void *other_props;
205 OtherData( void ) : other_props(NULL){ }
206 ~OtherData( void ){ if( other_props ) free( other_props ); }
207 };
208
209 /* data for one "other" element */
210 struct OtherElem
211 {
212 std::string elem_name; /* names of other elements */
213 std::vector< OtherData > other_data; /* actual property data for the elements */
214 PlyOtherProp other_props; /* description of the property data */
215 };
216
217 /* "other" elements, not interpreted by user */
218 struct PlyOtherElems
219 {
220 std::vector< OtherElem > other_list; /* list of data for other elements */
221 };
222
223 /* description of PLY file */
224 struct PlyFile
225 {
226 FILE *fp; /* file pointer */
227 int file_type; /* ascii or binary */
228 float version; /* version number of file */
229 std::vector< PlyElement > elems; /* list of elements of object */
230 std::vector< std::string > comments; /* list of comments */
231 std::vector< std::string > obj_info; /* list of object info items */
232 PlyElement *which_elem; /* which element we're currently writing */
233 PlyOtherElems *other_elems; /* "other" elements from a PLY file */
234
235 static inline PlyFile *Write( const std::string & , const std::vector< std::string > & , int , float & );
236 static inline PlyFile *Read ( const std::string & , std::vector< std::string > & , int & , float & );
237
238 PlyFile( FILE *f ) : fp(f) , other_elems(NULL) , version(1.) { }
239 ~PlyFile( void ){ if( fp ) fclose(fp) ; if(other_elems) delete other_elems; }
240
241 inline void describe_element ( const std::string & , size_t , int , const PlyProperty * );
242 inline void describe_property( const std::string & , const PlyProperty * );
243 inline void describe_other_elements( PlyOtherElems * );
244 inline PlyElement *find_element( const std::string & );
245 inline void element_count( const std::string & , size_t );
246 inline void header_complete( void );
247 inline void put_element_setup( const std::string & );
248 inline void put_element ( void * );
249 inline void put_comment ( const std::string & );
250 inline void put_obj_info( const std::string & );
251 inline void put_other_elements( void );
252 inline void add_element ( const std::vector< std::string > & );
253 inline void add_property( const std::vector< std::string > & );
254 inline void add_comment ( const std::string & );
255 inline void add_obj_info( const std::string & );
256
257 inline std::vector< PlyProperty > get_element_description( const std::string & , size_t & );
258 inline void get_element_setup( const std::string & , int , PlyProperty * );
259 inline int get_property( const std::string & , const PlyProperty * );
260 inline void describe_other_properties( const PlyOtherProp & , int );
261 inline bool set_other_properties( const std::string & , int , PlyOtherProp & );
262 inline void get_element( void * );
263 inline std::vector< std::string > &get_comments( void );
264 inline std::vector< std::string > &get_obj_info( void );
265 inline void get_info( float & , int & );
266 inline PlyOtherElems *get_other_element( std::string & , size_t );
267 protected:
268 inline void _ascii_get_element ( void * );
269 inline void _binary_get_element( void * );
270 static inline PlyFile *_Write( FILE * , const std::vector< std::string > & , int );
271 static inline PlyFile *_Read ( FILE * , std::vector< std::string > & );
272 };
273
274 #include "PlyFile.inl"
275 }
276 #endif // PLY_FILE_INCLUDED