Debian Codegraph @

src/thirdparty/PoissonRecon/VertexFactory.h

raw
1 /*
2 Copyright (c) 2020, Michael Kazhdan
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 #ifndef VERTEX_FACTORY_INCLUDED
29 #define VERTEX_FACTORY_INCLUDED
30
31 #include <inttypes.h>
32 #include "Ply.h"
33 #include "Array.h"
34
35 namespace PoissonRecon
36 {
37 namespace VertexFactory
38 {
39 // Mimicking the scalar types in Ply.inl
40 // Assuming Real is something that is castable to/from a double
41 enum TypeOnDisk
42 {
43 CHAR ,
44 UCHAR ,
45 INT ,
46 UINT ,
47 FLOAT ,
48 DOUBLE ,
49 INT_8 ,
50 UINT_8 ,
51 INT_16 ,
52 UINT_16 ,
53 INT_32 ,
54 UINT_32 ,
55 INT_64 ,
56 UINT_64 ,
57 UNKNOWN
58 };
59
60 inline int ToPlyType( TypeOnDisk typeOnDisk );
61 inline TypeOnDisk FromPlyType( int plyType );
62 template< typename Real > TypeOnDisk GetTypeOnDisk( void );
63
64 template< typename Real >
65 struct VertexIO
66 {
67 static bool ReadASCII ( FILE *fp , TypeOnDisk typeOnDisk , Real &s );
68 static bool ReadBinary ( FILE *fp , TypeOnDisk typeOnDisk , Real &s );
69 static void WriteASCII ( FILE *fp , TypeOnDisk typeOnDisk , const Real &s );
70 static void WriteBinary( FILE *fp , TypeOnDisk typeOnDisk , const Real &s );
71
72 static bool ReadASCII ( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , Real *s );
73 static bool ReadBinary ( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , Real *s );
74 static void WriteASCII ( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , const Real *s );
75 static void WriteBinary( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , const Real *s );
76
77 protected:
78 template< typename Type > static bool _ReadBinary( FILE *fp , Real &s );
79 template< typename Type > static void _WriteBinary( FILE *fp , Real s );
80 };
81
82 template< typename _VertexType , typename FactoryType >
83 struct _Factory
84 {
85 typedef _VertexType VertexType;
86 virtual VertexType operator()( void ) const = 0;
87
88 // Reading/writing methods for PLY format
89 virtual unsigned int plyReadNum( void ) const = 0;
90 virtual unsigned int plyWriteNum( void ) const = 0;
91 virtual bool plyValidReadProperties( const bool *flags ) const = 0;
92
93 virtual PlyProperty plyReadProperty( unsigned int idx ) const = 0;
94 virtual PlyProperty plyWriteProperty( unsigned int idx ) const = 0;
95
96 // Reading/writing methods for ASCII/Binary files
97 virtual bool readASCII( FILE *fp , VertexType &dt ) const = 0;
98 virtual bool readBinary( FILE *fp , VertexType &dt ) const = 0;
99 virtual void writeASCII( FILE *fp , const VertexType &dt ) const = 0;
100 virtual void writeBinary( FILE *fp , const VertexType &dt ) const = 0;
101
102 virtual size_t bufferSize( void ) const = 0;
103 virtual void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const = 0;
104 virtual void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const = 0;
105
106 virtual PlyProperty plyStaticReadProperty( unsigned int idx ) const = 0;
107 virtual PlyProperty plyStaticWriteProperty( unsigned int idx ) const = 0;
108
109 virtual bool operator == ( const FactoryType &factory ) const = 0;
110 bool operator != ( const FactoryType &factory ) const { return !( (*this)==factory ); }
111 };
112
113 // An empty factory
114 template< typename Real >
115 struct EmptyFactory : _Factory< EmptyVectorType< Real > , EmptyFactory< Real > >
116 {
117 typedef typename _Factory< EmptyVectorType< Real > , EmptyFactory< Real > >::VertexType VertexType;
118
119 struct Transform
120 {
121 Transform( void ){}
122 template< typename X > Transform( X ){}
123 void inPlace( VertexType &dt ) const {}
124 };
125
126 VertexType operator()( void ) const { return VertexType(); }
127
128 unsigned int plyReadNum( void ) const { return 0; }
129 unsigned int plyWriteNum( void ) const { return 0; }
130 bool plyValidReadProperties( const bool *flags ) const { return true ; }
131 PlyProperty plyReadProperty( unsigned int idx ) const { if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ) ; return PlyProperty(); }
132 PlyProperty plyWriteProperty( unsigned int idx ) const { if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ) ; return PlyProperty(); }
133 bool readASCII( FILE *fp , VertexType &dt ) const { return true; }
134 bool readBinary( FILE *fp , VertexType &dt ) const { return true; }
135 void writeASCII( FILE *fp , const VertexType &dt ) const {}
136 void writeBinary( FILE *fp , const VertexType &dt ) const {};
137
138 static constexpr bool IsStaticallyAllocated( void ){ return true; }
139
140 PlyProperty plyStaticReadProperty( unsigned int idx ) const { if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ) ; return PlyProperty(); }
141 PlyProperty plyStaticWriteProperty( unsigned int idx ) const { if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ) ; return PlyProperty(); }
142
143 size_t bufferSize( void ) const { return 0; }
144 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const {}
145 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const {}
146
147 bool operator == ( const EmptyFactory &factory ) const { return true; }
148
149 };
150
151 // The position factory
152 template< typename Real , unsigned int Dim >
153 struct PositionFactory : _Factory< Point< Real , Dim > , PositionFactory< Real , Dim > >
154 {
155 typedef typename _Factory< Point< Real , Dim > , PositionFactory< Real , Dim > >::VertexType VertexType;
156
157 struct Transform
158 {
159 Transform( void ){ _xForm = XForm< Real , Dim+1 >::Identity(); }
160 Transform( XForm< Real , Dim > xForm ){ _xForm = XForm< Real , Dim+1 >::Identity() ; for( int i=0 ; i<Dim ; i++ ) for( int j=0 ; j<Dim ; j++ ) _xForm(i,j) = xForm(i,j); }
161 Transform( XForm< Real , Dim+1 > xForm ) : _xForm(xForm) {}
162 void inPlace( VertexType &dt ) const { dt = _xForm * dt; }
163 protected:
164 XForm< Real , Dim+1 > _xForm;
165 };
166
167 VertexType operator()( void ) const { return VertexType(); }
168
169 unsigned int plyReadNum( void ) const { return Dim; }
170 unsigned int plyWriteNum( void ) const { return Dim; }
171 bool plyValidReadProperties( const bool *flags ) const { for( int d=0 ; d<Dim ; d++ ) if( !flags[d] ) return false ; return true ; }
172 PlyProperty plyReadProperty( unsigned int idx ) const;
173 PlyProperty plyWriteProperty( unsigned int idx ) const;
174 bool readASCII( FILE *fp , VertexType &dt ) const { return VertexIO< Real >:: ReadASCII( fp , _typeOnDisk , Dim , &dt[0] ); }
175 void writeASCII( FILE *fp , const VertexType &dt ) const { VertexIO< Real >:: WriteASCII( fp , _typeOnDisk , Dim , &dt[0] ); }
176 bool readBinary( FILE *fp , VertexType &dt ) const
177 {
178 if( _realTypeOnDisk ) return fread( &dt[0] , sizeof(Real) , Dim , fp )==Dim;
179 else return VertexIO< Real >::ReadBinary( fp , _typeOnDisk , Dim , &dt[0] );
180 }
181 void writeBinary( FILE *fp , const VertexType &dt ) const
182 {
183 if( _realTypeOnDisk ) fwrite( &dt[0] , sizeof(Real) , Dim , fp );
184 else VertexIO< Real >::WriteBinary( fp , _typeOnDisk , Dim , &dt[0] );
185 }
186
187 static constexpr bool IsStaticallyAllocated( void ){ return true; }
188
189 PlyProperty plyStaticReadProperty( unsigned int idx ) const;
190 PlyProperty plyStaticWriteProperty( unsigned int idx ) const;
191
192 PositionFactory( TypeOnDisk typeOnDisk=GetTypeOnDisk< Real >() ) : _typeOnDisk( typeOnDisk ) { _realTypeOnDisk = GetTypeOnDisk< Real >()==_typeOnDisk; }
193
194 size_t bufferSize( void ) const { return sizeof( VertexType ); }
195 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { memcpy( buffer , &dt , sizeof( VertexType ) ); }
196 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { memcpy( &dt , buffer , sizeof( VertexType ) ); }
197
198 bool operator == ( const PositionFactory &factory ) const { return _typeOnDisk==factory._typeOnDisk; }
199 protected:
200 TypeOnDisk _typeOnDisk;
201 bool _realTypeOnDisk;
202 static std::string _PlyName( unsigned int idx );
203 };
204
205 // The vertex factory
206 template< typename Real , unsigned int Dim >
207 struct NormalFactory : public _Factory< Point< Real , Dim > , NormalFactory< Real , Dim > >
208 {
209 typedef typename _Factory< Point< Real , Dim > , NormalFactory< Real , Dim > >::VertexType VertexType;
210
211 struct Transform
212 {
213 Transform( void ){ _xForm = XForm< Real , Dim >::Identity(); }
214 Transform( XForm< Real , Dim > xForm ){ _xForm = xForm.inverse().transpose(); }
215 Transform( XForm< Real , Dim+1 > xForm )
216 {
217 for( int i=0 ; i<Dim ; i++ ) for( int j=0 ; j<Dim ; j++ ) _xForm(i,j) = xForm(i,j);
218 _xForm = _xForm.inverse().transpose();
219 }
220 void inPlace( VertexType &dt ) const { dt = _xForm * dt; }
221 protected:
222 XForm< Real , Dim > _xForm;
223 };
224
225 VertexType operator()( void ) const { return VertexType(); }
226
227 unsigned int plyReadNum( void ) const { return Dim; }
228 unsigned int plyWriteNum( void ) const { return Dim; }
229 bool plyValidReadProperties( const bool *flags ) const { for( int d=0 ; d<Dim ; d++ ) if( !flags[d] ) return false ; return true ; }
230 PlyProperty plyReadProperty( unsigned int idx ) const;
231 PlyProperty plyWriteProperty( unsigned int idx ) const;
232 bool readASCII( FILE *fp , VertexType &dt ) const { return VertexIO< Real >:: ReadASCII( fp , _typeOnDisk , Dim , &dt[0] ); }
233 void writeASCII( FILE *fp , const VertexType &dt ) const { VertexIO< Real >:: WriteASCII( fp , _typeOnDisk , Dim , &dt[0] ); }
234 bool readBinary( FILE *fp , VertexType &dt ) const
235 {
236 if( _realTypeOnDisk ) return fread( &dt[0] , sizeof(Real) , Dim , fp )==Dim;
237 else return VertexIO< Real >::ReadBinary( fp , _typeOnDisk , Dim , &dt[0] );
238 }
239 void writeBinary( FILE *fp , const VertexType &dt ) const
240 {
241 if( _realTypeOnDisk ) fwrite( &dt[0] , sizeof(Real) , Dim , fp );
242 else VertexIO< Real >::WriteBinary( fp , _typeOnDisk , Dim , &dt[0] );
243 }
244
245 static constexpr bool IsStaticallyAllocated( void ){ return true; }
246
247 PlyProperty plyStaticReadProperty( unsigned int idx ) const;
248 PlyProperty plyStaticWriteProperty( unsigned int idx ) const;
249
250 NormalFactory( TypeOnDisk typeOnDisk=GetTypeOnDisk< Real >() ) : _typeOnDisk( typeOnDisk ) { _realTypeOnDisk = GetTypeOnDisk< Real >()==_typeOnDisk; }
251
252 size_t bufferSize( void ) const { return sizeof( VertexType ); }
253 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { memcpy( buffer , &dt , sizeof( VertexType ) ); }
254 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { memcpy( &dt , buffer , sizeof( VertexType ) ); }
255
256 bool operator == ( const NormalFactory &factory ) const { return _typeOnDisk==factory._typeOnDisk; }
257 protected:
258 TypeOnDisk _typeOnDisk;
259 bool _realTypeOnDisk;
260 static std::string _PlyName( unsigned int idx );
261 };
262
263 // The texture factory
264 template< typename Real , unsigned int Dim >
265 struct TextureFactory : public _Factory< Point< Real , Dim > , TextureFactory< Real , Dim > >
266 {
267 typedef typename _Factory< Point< Real , Dim > , TextureFactory< Real , Dim > >::VertexType VertexType;
268
269 struct Transform
270 {
271 Transform( void ){}
272 template< typename XForm > Transform( XForm xForm ){}
273 void inPlace( VertexType &dt ) const {}
274 };
275
276 VertexType operator()( void ) const { return VertexType(); }
277
278 unsigned int plyReadNum( void ) const { return Dim; }
279 unsigned int plyWriteNum( void ) const { return Dim; }
280 bool plyValidReadProperties( const bool *flags ) const { for( int d=0 ; d<Dim ; d++ ) if( !flags[d] ) return false ; return true ; }
281 PlyProperty plyReadProperty( unsigned int idx ) const;
282 PlyProperty plyWriteProperty( unsigned int idx ) const;
283 bool readASCII( FILE *fp , VertexType &dt ) const { return VertexIO< Real >:: ReadASCII( fp , _typeOnDisk , Dim , &dt[0] ); }
284 void writeASCII( FILE *fp , const VertexType &dt ) const { VertexIO< Real >:: WriteASCII( fp , _typeOnDisk , Dim , &dt[0] ); }
285 bool readBinary( FILE *fp , VertexType &dt ) const
286 {
287 if( _realTypeOnDisk ) return fread( &dt[0] , sizeof(Real) , Dim , fp )==Dim;
288 else return VertexIO< Real >::ReadBinary( fp , _typeOnDisk , Dim , &dt[0] );
289 }
290 void writeBinary( FILE *fp , const VertexType &dt ) const
291 {
292 if( _realTypeOnDisk ) fwrite( &dt[0] , sizeof(Real) , Dim , fp );
293 VertexIO< Real >::WriteBinary( fp , _typeOnDisk , Dim , &dt[0] );
294 }
295
296 static constexpr bool IsStaticallyAllocated( void ){ return true; }
297
298 PlyProperty plyStaticReadProperty( unsigned int idx ) const;
299 PlyProperty plyStaticWriteProperty( unsigned int idx ) const;
300
301 TextureFactory( TypeOnDisk typeOnDisk=GetTypeOnDisk< Real >() ) : _typeOnDisk( typeOnDisk ) { _realTypeOnDisk = GetTypeOnDisk< Real >()==_typeOnDisk; }
302
303 size_t bufferSize( void ) const { return sizeof( VertexType ); }
304 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { memcpy( buffer , &dt , sizeof( VertexType ) ); }
305 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { memcpy( &dt , buffer , sizeof( VertexType ) ); }
306
307 bool operator == ( const TextureFactory &factory ) const { return _typeOnDisk==factory._typeOnDisk; }
308
309 protected:
310 TypeOnDisk _typeOnDisk;
311 bool _realTypeOnDisk;
312 static std::string _PlyName( unsigned int idx );
313 };
314
315 // The rgb color factory
316 template< typename Real >
317 struct RGBColorFactory : public _Factory< Point< Real , 3 > , RGBColorFactory< Real > >
318 {
319 typedef typename _Factory< Point< Real , 3 > , RGBColorFactory< Real > >::VertexType VertexType;
320
321 struct Transform
322 {
323 Transform( void ){}
324 template< typename XForm > Transform( XForm xForm ){}
325 void inPlace( VertexType &dt ) const {}
326 };
327
328 VertexType operator()( void ) const { return VertexType(); }
329
330 unsigned int plyReadNum( void ) const { return 6; }
331 unsigned int plyWriteNum( void ) const { return 3; }
332 bool plyValidReadProperties( const bool *flags ) const { for( int d=0 ; d<3 ; d++ ) if( !flags[d] && !flags[d+3] ) return false ; return true ; }
333 PlyProperty plyReadProperty( unsigned int idx ) const;
334 PlyProperty plyWriteProperty( unsigned int idx ) const;
335 bool readASCII( FILE *fp , VertexType &dt ) const { return VertexIO< Real >:: ReadASCII( fp , _typeOnDisk , 3 , &dt[0] ); }
336 void writeASCII( FILE *fp , const VertexType &dt ) const { VertexIO< Real >:: WriteASCII( fp , _typeOnDisk , 3 , &dt[0] ); }
337 bool readBinary( FILE *fp , VertexType &dt ) const
338 {
339 if( _realTypeOnDisk ) return fread( &dt[0] , sizeof(Real) , 3 , fp )==3;
340 else return VertexIO< Real >::ReadBinary( fp , _typeOnDisk , 3 , &dt[0] );
341 }
342 void writeBinary( FILE *fp , const VertexType &dt ) const
343 {
344 if( _realTypeOnDisk ) fwrite( &dt[0] , sizeof(Real) , 3 , fp );
345 VertexIO< Real >::WriteBinary( fp , _typeOnDisk , 3 , &dt[0] );
346 }
347
348 static constexpr bool IsStaticallyAllocated( void ){ return true; }
349
350 PlyProperty plyStaticReadProperty( unsigned int idx ) const;
351 PlyProperty plyStaticWriteProperty( unsigned int idx ) const;
352
353 RGBColorFactory( TypeOnDisk typeOnDisk=GetTypeOnDisk< unsigned char >() ) : _typeOnDisk( typeOnDisk ) { _realTypeOnDisk = GetTypeOnDisk< Real >()==_typeOnDisk; }
354
355 size_t bufferSize( void ) const { return sizeof( VertexType ); }
356 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { memcpy( buffer , &dt , sizeof( VertexType ) ); }
357 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { memcpy( &dt , buffer , sizeof( VertexType ) ); }
358
359 bool operator == ( const RGBColorFactory &factory ) const { return _typeOnDisk==factory._typeOnDisk; }
360 protected:
361 TypeOnDisk _typeOnDisk;
362 bool _realTypeOnDisk;
363 static std::string _PlyName( unsigned int idx );
364 };
365
366 // The rgba color factory
367 template< typename Real >
368 struct RGBAColorFactory : public _Factory< Point< Real , 4 > , RGBAColorFactory< Real > >
369 {
370 typedef typename _Factory< Point< Real , 4 > , RGBAColorFactory< Real > >::VertexType VertexType;
371
372 struct Transform
373 {
374 Transform( void ){}
375 template< typename XForm > Transform( XForm xForm ){}
376 void inPlace( VertexType &dt ) const {}
377 };
378
379 VertexType operator()( void ) const { return VertexType(); }
380
381 unsigned int plyReadNum( void ) const { return 8; }
382 unsigned int plyWriteNum( void ) const { return 4; }
383 bool plyValidReadProperties( const bool *flags ) const { for( int d=0 ; d<4 ; d++ ) if( !flags[d] && !flags[d+4] ) return false ; return true ; }
384 PlyProperty plyReadProperty( unsigned int idx ) const;
385 PlyProperty plyWriteProperty( unsigned int idx ) const;
386 bool readASCII( FILE *fp , VertexType &dt ) const { return VertexIO< Real >:: ReadASCII( fp , _typeOnDisk , 4 , &dt[0] ); }
387 void writeASCII( FILE *fp , const VertexType &dt ) const { VertexIO< Real >:: WriteASCII( fp , _typeOnDisk , 4 , &dt[0] ); }
388 bool readBinary( FILE *fp , VertexType &dt ) const
389 {
390 if( _realTypeOnDisk ) return fread( &dt[0] , sizeof(Real) , 4 , fp )==4;
391 else return VertexIO< Real >::ReadBinary( fp , _typeOnDisk , 4 , &dt[0] );
392 }
393 void writeBinary( FILE *fp , const VertexType &dt ) const
394 {
395 if( _realTypeOnDisk ) fwrite( &dt[0] , sizeof(Real) , 4 , fp );
396 VertexIO< Real >::WriteBinary( fp , _typeOnDisk , 4 , &dt[0] );
397 }
398
399 static constexpr bool IsStaticallyAllocated( void ){ return true; }
400
401 PlyProperty plyStaticReadProperty( unsigned int idx ) const;
402 PlyProperty plyStaticWriteProperty( unsigned int idx ) const;
403
404 RGBAColorFactory( TypeOnDisk typeOnDisk=GetTypeOnDisk< unsigned char >() ) : _typeOnDisk( typeOnDisk ) { _realTypeOnDisk = GetTypeOnDisk< Real >()==_typeOnDisk; }
405 size_t bufferSize( void ) const { return sizeof( VertexType ); }
406 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { memcpy( buffer , &dt , sizeof( VertexType ) ); }
407 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { memcpy( &dt , buffer , sizeof( VertexType ) ); }
408
409 bool operator == ( const RGBAColorFactory &factory ) const { return _typeOnDisk==factory._typeOnDisk; }
410 protected:
411 TypeOnDisk _typeOnDisk;
412 bool _realTypeOnDisk;
413 static std::string _PlyName( unsigned int idx );
414 };
415
416 // The value factory
417 template< typename Real >
418 struct ValueFactory : public _Factory< Real , ValueFactory< Real > >
419 {
420 typedef typename _Factory< Real , ValueFactory< Real > >::VertexType VertexType;
421
422 struct Transform
423 {
424 Transform( void ){}
425 template< typename XForm > Transform( XForm xForm ){}
426 void inPlace( VertexType &dt ) const {}
427 };
428
429 VertexType operator()( void ) const { return (Real)0; }
430
431 unsigned int plyReadNum( void ) const { return 1; }
432 unsigned int plyWriteNum( void ) const { return 1; }
433 bool plyValidReadProperties( const bool *flags ) const { if( !flags[0] ) return false ; return true ; }
434 PlyProperty plyReadProperty( unsigned int idx ) const;
435 PlyProperty plyWriteProperty( unsigned int idx ) const;
436 bool readASCII( FILE *fp , VertexType &dt ) const { return VertexIO< Real >:: ReadASCII( fp , _typeOnDisk , 1 , &dt ); }
437 void writeASCII( FILE *fp , const VertexType &dt ) const { VertexIO< Real >:: WriteASCII( fp , _typeOnDisk , 1 , &dt ); }
438 bool readBinary( FILE *fp , VertexType &dt ) const
439 {
440 if( _realTypeOnDisk ) return fread( &dt , sizeof(Real) , 1 , fp )==1;
441 else return VertexIO< Real >::ReadBinary( fp , _typeOnDisk , 1 , &dt );
442 }
443 void writeBinary( FILE *fp , const VertexType &dt ) const
444 {
445 if( _realTypeOnDisk ) fwrite( &dt , sizeof(Real) , 1 , fp );
446 else VertexIO< Real >::WriteBinary( fp , _typeOnDisk , 1 , &dt );
447 }
448
449 static constexpr bool IsStaticallyAllocated( void ){ return true; }
450
451 PlyProperty plyStaticReadProperty( unsigned int idx ) const;
452 PlyProperty plyStaticWriteProperty( unsigned int idx ) const;
453
454 ValueFactory( TypeOnDisk typeOnDisk=GetTypeOnDisk< Real >() ) : _typeOnDisk( typeOnDisk ) { _realTypeOnDisk = GetTypeOnDisk< Real >()==_typeOnDisk; }
455
456 size_t bufferSize( void ) const { return sizeof( VertexType ); }
457 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { memcpy( buffer , &dt , sizeof( VertexType ) ); }
458 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { memcpy( &dt , buffer , sizeof( VertexType ) ); }
459
460 bool operator == ( const ValueFactory &factory ) const { return _typeOnDisk==factory._typeOnDisk; }
461 protected:
462 TypeOnDisk _typeOnDisk;
463 bool _realTypeOnDisk;
464 static std::string _PlyName( unsigned int idx );
465 };
466
467 // The named array factory
468 template< typename Real >
469 struct DynamicFactory : public _Factory< Point< Real > , DynamicFactory< Real > >
470 {
471 typedef typename _Factory< Point< Real > , DynamicFactory< Real > >::VertexType VertexType;
472
473 struct Transform
474 {
475 Transform( void ){}
476 template< typename XForm > Transform( XForm xForm ){}
477 void inPlace( VertexType &dt ) const {}
478 };
479
480 VertexType operator()( void ) const { return Point< Real >( _namesAndTypesOnDisk.size() ); }
481 unsigned int plyReadNum( void ) const { return (unsigned int )_namesAndTypesOnDisk.size(); }
482 unsigned int plyWriteNum( void ) const { return (unsigned int )_namesAndTypesOnDisk.size(); }
483 bool plyValidReadProperties( const bool *flags ) const { for( int d=0 ; d<_namesAndTypesOnDisk.size() ; d++ ) if( !flags[d] ) return false ; return true ; }
484 PlyProperty plyReadProperty( unsigned int idx ) const;
485 PlyProperty plyWriteProperty( unsigned int idx ) const;
486
487 bool readASCII( FILE *fp , VertexType &dt ) const;
488 bool readBinary( FILE *fp , VertexType &dt ) const;
489 void writeASCII( FILE *fp , const VertexType &dt ) const;
490 void writeBinary( FILE *fp , const VertexType &dt ) const;
491
492 static constexpr bool IsStaticallyAllocated( void ){ return false; }
493
494 PlyProperty plyStaticReadProperty( unsigned int idx ) const { MK_THROW( "does not support static allocation" ) ; return PlyProperty(); }
495 PlyProperty plyStaticWriteProperty( unsigned int idx ) const { MK_THROW( "does not support static allocation" ) ; return PlyProperty(); }
496
497 size_t size( void ) const { return _namesAndTypesOnDisk.size(); }
498
499 DynamicFactory( const std::vector< std::pair< std::string , TypeOnDisk > > &namesAndTypesOnDisk );
500 DynamicFactory( const std::vector< PlyProperty > &plyProperties );
501 size_t bufferSize( void ) const { return sizeof(Real) * _namesAndTypesOnDisk.size(); }
502 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { for( size_t i=0 ; i<_namesAndTypesOnDisk.size() ; i++ ) ( ( Pointer(Real) )(buffer) )[i] = dt[i]; }
503 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { for( size_t i=0 ; i<_namesAndTypesOnDisk.size() ; i++ ) dt[i] = ( ( ConstPointer( Real ) )(buffer) )[i]; }
504
505 bool operator == ( const DynamicFactory &factory ) const;
506 protected:
507 bool _realTypeOnDisk;
508 std::vector< std::pair< std::string , TypeOnDisk > > _namesAndTypesOnDisk;
509 };
510
511 // A factory for building the union of multiple components
512 template< typename Real , typename ... Factories >
513 struct Factory : public _Factory< DirectSum< Real , typename Factories::VertexType ... > , Factory< Real , Factories ... > >
514 {
515 protected:
516 typedef std::tuple< Factories ... > _FactoryTuple;
517 public:
518 typedef typename _Factory< DirectSum< Real , typename Factories::VertexType ... > , Factory< Real , Factories ... > >::VertexType VertexType;
519 template< unsigned int I > using FactoryType = typename std::tuple_element< I , _FactoryTuple >::type;
520 template< unsigned int I > FactoryType< I >& get( void ) { return std::get< I >( _factoryTuple ); }
521 template< unsigned int I > const FactoryType< I >& get( void ) const { return std::get< I >( _factoryTuple ); }
522
523 struct Transform
524 {
525 protected:
526 typedef std::tuple< typename Factories::Transform ... > _TransformTuple;
527 public:
528 Transform( void ){}
529 template< typename XForm >
530 Transform( XForm xForm ){ _set<0>( xForm ); }
531 void inPlace( VertexType &dt ) const { _inPlace< 0 >( dt ); }
532
533 template< unsigned int I > using TransformType = typename std::tuple_element< I , _TransformTuple >::type;
534 template< unsigned int I > TransformType< I >& get( void ) { return std::get< I >( _transformTuple ); }
535 template< unsigned int I > const TransformType< I >& get( void ) const { return std::get< I >( _transformTuple ); }
536 protected:
537 _TransformTuple _transformTuple;
538 template< unsigned int I , typename XForm > typename std::enable_if< I!=sizeof...(Factories) >::type _set( XForm xForm ){ this->template get<I>() = xForm ; _set< I+1 >( xForm ); }
539 template< unsigned int I , typename XForm > typename std::enable_if< I==sizeof...(Factories) >::type _set( XForm xForm ){}
540 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) >::type _inPlace( VertexType &dt ) const { this->template get<I>().inPlace( dt.template get<I>() ) ; _inPlace< I+1 >( dt ); }
541 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) >::type _inPlace( VertexType &dt ) const {}
542 };
543
544 Factory( void ){}
545 Factory( Factories ... factories ) : _factoryTuple( factories ... ){ }
546
547 VertexType operator()( void ) const{ return _VertexType( std::make_index_sequence< sizeof...(Factories) >() ); }
548
549 unsigned int plyReadNum( void ) const { return _plyReadNum<0>(); }
550 unsigned int plyWriteNum( void ) const { return _plyWriteNum<0>(); }
551 bool plyValidReadProperties( const bool *flags ) const { return _plyValidReadProperties<0>( flags ) ; }
552 template< unsigned int I > bool plyValidReadProperties( const bool* flags ) const { return get< I >().plyValidReadProperties( flags + _readOffset< I >() ) ; }
553 PlyProperty plyReadProperty( unsigned int idx ) const { return _plyReadProperty<0>( idx , 0 ); }
554 PlyProperty plyWriteProperty( unsigned int idx ) const { return _plyWriteProperty<0>( idx , 0 ); }
555 bool readASCII( FILE *fp , VertexType &dt ) const { return _readASCII<0>( fp , dt ); }
556 bool readBinary( FILE *fp , VertexType &dt ) const { return _readBinary<0>( fp , dt ); }
557 void writeASCII( FILE *fp , const VertexType &dt ) const { _writeASCII<0>( fp , dt ); }
558 void writeBinary( FILE *fp , const VertexType &dt ) const { _writeBinary<0>( fp , dt ); }
559
560 static constexpr bool IsStaticallyAllocated( void ){ return _IsStaticallyAllocated<0>(); }
561 PlyProperty plyStaticReadProperty( unsigned int idx ) const { if constexpr( IsStaticallyAllocated() ) return _plyStaticReadProperty<0>( idx ) ; else return PlyProperty(); }
562 PlyProperty plyStaticWriteProperty( unsigned int idx ) const { if constexpr( IsStaticallyAllocated() ) return _plyStaticWriteProperty<0>( idx ) ; else return PlyProperty(); }
563
564 size_t bufferSize( void ) const { return _bufferSize<0>(); }
565 void toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { _toBuffer<0>( dt , buffer ); }
566 void fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const { _fromBuffer<0>( buffer , dt ); }
567
568 bool operator == ( const Factory &factory ) const { return _equal<0>( factory ); }
569 protected:
570 _FactoryTuple _factoryTuple;
571 template< size_t ... Is > VertexType _VertexType( std::index_sequence< Is ... > ) const{ return VertexType( get<Is>().operator()() ... ); }
572
573 template< typename Factory1 , typename Factory2 >
574 static typename std::enable_if< !std::is_same< Factory1 , Factory1 >::value , bool >::type _EqualFactories( const Factory1 &f1 , const Factory1 &f2 ){ return false; }
575 template< typename Factory1 , typename Factory2 >
576 static typename std::enable_if< std::is_same< Factory1 , Factory1 >::value , bool >::type _EqualFactories( const Factory1 &f1 , const Factory1 &f2 ){ return f1==f2; }
577
578
579 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , unsigned int >::type _plyReadNum( void ) const { return get<I>().plyReadNum() + _plyReadNum< I+1 >(); }
580 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , unsigned int >::type _plyReadNum( void ) const { return 0; }
581 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , unsigned int >::type _plyWriteNum( void ) const { return get<I>().plyWriteNum() + _plyWriteNum< I+1 >(); }
582 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , unsigned int >::type _plyWriteNum( void ) const { return 0; }
583 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , bool >::type _plyValidReadProperties( const bool *flags ) const { return get< I >().plyValidReadProperties( flags ) && _plyValidReadProperties< I+1 >( flags + get< I >().plyReadNum() ); }
584 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , bool >::type _plyValidReadProperties( const bool *flags ) const { return true; }
585 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type _plyReadProperty( unsigned int idx , size_t offset ) const;
586 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type _plyWriteProperty( unsigned int idx , size_t offset ) const;
587 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , PlyProperty >::type _plyReadProperty( unsigned int idx , size_t offset ) const { MK_THROW( "read property out of bounds" ) ; return PlyProperty(); }
588 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , PlyProperty >::type _plyWriteProperty( unsigned int idx , size_t offset ) const { MK_THROW( "write property out of bounds" ) ; return PlyProperty(); }
589 template< unsigned int I > typename std::enable_if< I==0 , unsigned int >::type _readOffset( void ) const { return 0; }
590 template< unsigned int I > typename std::enable_if< I!=0 , unsigned int >::type _readOffset( void ) const { return _readOffset< I-1 >() + get< I-1 >().plyReadNum(); }
591 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , bool >::type _readASCII( FILE *fp , VertexType &dt ) const { return this->template get<I>().readASCII( fp , dt.template get<I>() ) && _readASCII< I+1 >( fp , dt ); }
592 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , bool >::type _readASCII( FILE *fp , VertexType &dt ) const { return true; }
593 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , bool >::type _readBinary( FILE *fp , VertexType &dt ) const { return this->template get<I>().readBinary( fp , dt.template get<I>() ) && _readBinary< I+1 >( fp , dt ); }
594 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , bool >::type _readBinary( FILE *fp , VertexType &dt ) const { return true; }
595 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) >::type _writeASCII( FILE *fp , const VertexType &dt ) const { this->template get<I>().writeASCII( fp , dt.template get<I>() ) ; _writeASCII< I+1 >( fp , dt ); }
596 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) >::type _writeASCII( FILE *fp , const VertexType &dt ) const {}
597 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) >::type _writeBinary( FILE *fp , const VertexType &dt ) const { this->template get<I>().writeBinary( fp , dt.template get<I>() ) ; _writeBinary< I+1 >( fp , dt ); }
598 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) >::type _writeBinary( FILE *fp , const VertexType &dt ) const {}
599
600 template< unsigned int I > static constexpr bool _IsStaticallyAllocated( void )
601 {
602 if constexpr( I<sizeof...(Factories) ) return FactoryType< I >::IsStaticallyAllocated() && _IsStaticallyAllocated< I+1 >();
603 else return true;
604 }
605
606 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type _plyStaticReadProperty ( unsigned int idx ) const;
607 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type _plyStaticWriteProperty( unsigned int idx ) const;
608 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , PlyProperty >::type _plyStaticReadProperty ( unsigned int idx ) const { MK_THROW( "read property out of bounds" ) ; return PlyProperty(); }
609 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , PlyProperty >::type _plyStaticWriteProperty( unsigned int idx ) const { MK_THROW( "write property out of bounds" ) ; return PlyProperty(); }
610
611 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , size_t >::type _bufferSize( void ) const { return this->template get<I>().bufferSize() + _bufferSize< I+1 >(); }
612 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , size_t >::type _bufferSize( void ) const { return 0; }
613 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) >::type _toBuffer( const VertexType &dt , Pointer( char ) buffer ) const { this->template get<I>().toBuffer( dt.template get<I>() , buffer ) ; _toBuffer< I+1 >( dt , buffer + this->template get<I>().bufferSize() ); }
614 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) >::type _toBuffer( const VertexType &dt , Pointer( char ) buffer ) const {}
615 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) >::type _fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const {this->template get<I>().fromBuffer( buffer , dt.template get<I>() ) ; _fromBuffer< I+1 >( buffer + this->template get<I>().bufferSize() , dt ); }
616 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) >::type _fromBuffer( ConstPointer( char ) buffer , VertexType &dt ) const {}
617
618 template< unsigned int I > typename std::enable_if< I!=sizeof...(Factories) , bool >::type _equal( const Factory &factory ) const { return _EqualFactories< FactoryType<I> , Factory >( this->template get< I >() , factory.template get<I>() ) && _equal< I+1 >( factory ); }
619 template< unsigned int I > typename std::enable_if< I==sizeof...(Factories) , bool >::type _equal( const Factory &factory ) const { return true; }
620 };
621 }
622
623 #include "VertexFactory.inl"
624 }
625
626 #endif // VERTEX_FACTORY_INCLUDED