Debian Codegraph @

src/thirdparty/PoissonRecon/PointPartition.inl

raw
1 /*
2 Copyright (c) 2023, 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
29 std::string FileDir( std::string dir , std::string header )
30 {
31 if( dir.back()!=FileSeparator ) dir += std::string(1,FileSeparator);
32 return dir + header;
33 }
34
35 std::string FileDir( std::string dir , std::string header , unsigned int clientIndex )
36 {
37 std::stringstream sStream;
38 sStream << header << "_" << clientIndex;
39 return FileDir( dir , sStream.str() );
40 }
41
42 std::string FileName( std::string dir , unsigned int slab , unsigned int slabs , unsigned int filesPerDir )
43 {
44 if( filesPerDir<=1 ) MK_THROW( "Need at least two files per directory" );
45
46 if( !dir.length() ) dir = std::string( "." );
47 if( dir.back()!=FileSeparator ) dir.push_back( FileSeparator );
48
49 std::vector< unsigned int > factors;
50 factors.push_back(slab);
51 while( slabs>filesPerDir )
52 {
53 factors.push_back( (slab / filesPerDir) % filesPerDir );
54 slab /= filesPerDir;
55 slabs /= filesPerDir;
56 }
57
58 std::stringstream sStream;
59 sStream << dir;
60
61 for( unsigned int i=(unsigned int)factors.size()-1 ; i!=0 ; i-- ) sStream << factors[i] << std::string(1,FileSeparator);
62 sStream << factors[0] << ".points";
63 return sStream.str();
64 }
65
66 std::string FileName( std::string dir , std::string header , unsigned int slab , unsigned int slabs , unsigned int filesPerDir )
67 {
68 return FileName( FileDir( dir , header ) , slab , slabs , filesPerDir );
69 }
70
71 std::string FileName( std::string dir , std::string header , unsigned int clientIndex , unsigned int slab , unsigned int slabs , unsigned int filesPerDir )
72 {
73 return FileName( FileDir( dir , header , clientIndex ) , slab , slabs , filesPerDir );
74 }
75
76 std::string PointSetInfoName( std::string dir , std::string header )
77 {
78 if( dir.back()==FileSeparator ) return dir + header + std::string( ".psi" );
79 else return dir + std::string(1,FileSeparator) + header + std::string( ".psi" );
80 }
81
82
83 //////////////////
84 // PointSetInfo //
85 //////////////////
86 template< typename Real , unsigned int Dim >
87 PointSetInfo< Real , Dim >::PointSetInfo( void ) : modelToUnitCube( XForm< Real , Dim+1 >::Identity() ) , filesPerDir(-1) {}
88
89 template< typename Real , unsigned int Dim >
90 PointSetInfo< Real , Dim >::PointSetInfo( unsigned int slabs ) : modelToUnitCube( XForm< Real , Dim+1 >::Identity() ) , filesPerDir(-1)
91 {
92 pointsPerSlab.resize( slabs , 0 );
93 }
94
95 template< typename Real , unsigned int Dim >
96 PointSetInfo< Real , Dim >::PointSetInfo( BinaryStream &stream )
97 {
98 if( !stream.read( header ) ) MK_THROW( "Failed to read header" );
99 if( !stream.read( modelToUnitCube ) ) MK_THROW( "Failed to read model-to-unit-cube transform" );
100 if( !stream.read( pointsPerSlab ) ) MK_THROW( "Failed to read points-per-slab" );
101 {
102 size_t sz;
103 if( !stream.read( sz ) ) MK_THROW( "Failed to read number of auxiliary properties" );
104 auxiliaryProperties.resize(sz);
105 for( size_t i=0 ; i<sz ; i++ ) auxiliaryProperties[i].read( stream );
106 }
107 if( !stream.read( filesPerDir ) ) MK_THROW( "Failed to read files-per-directory" );
108 }
109
110 template< typename Real , unsigned int Dim >
111 void PointSetInfo< Real , Dim >::write( BinaryStream &stream ) const
112 {
113 stream.write( header );
114 stream.write( modelToUnitCube );
115 stream.write( pointsPerSlab );
116 {
117 size_t sz = auxiliaryProperties.size();
118 stream.write( sz );
119 for( size_t i=0 ; i<sz ; i++ ) auxiliaryProperties[i].write( stream );
120 }
121 stream.write( filesPerDir );
122 }
123
124 void RemovePointSlabDirs( std::string dir ){ std::filesystem::remove_all( dir ); }
125 void CreatePointSlabDirs( std::string dir , unsigned int count , unsigned int filesPerDir )
126 {
127 if( filesPerDir<=1 ) MK_THROW( "Need at least two files per directory" );
128 if( !dir.length() ) dir = std::string( "." );
129 if( dir.back()!=FileSeparator ) dir += std::string(1,FileSeparator);
130
131 try{ std::filesystem::create_directories( dir ); }
132 catch( ... ){ MK_THROW( "Failed to create directory: " , dir ); }
133
134 unsigned int depth = 0;
135 {
136 size_t _filesPerDir = filesPerDir;
137 while( count>_filesPerDir ) _filesPerDir *= filesPerDir , depth++;
138 }
139
140 auto _exp = []( unsigned int filesPerDir , unsigned int depth )
141 {
142 unsigned int e = 1;
143 for( unsigned int d=0 ; d<depth ; d++ ) e *= filesPerDir;
144 return e;
145 };
146
147 std::function< void ( std::string , unsigned int , unsigned int , unsigned int ) > MakeDirs = [&]( std::string dir , unsigned int count , unsigned int depth , unsigned int filesPerDir )
148 {
149 if( depth )
150 {
151 unsigned int _filesPerDir = _exp( filesPerDir , depth );
152 for( unsigned int i=0 ; i*_filesPerDir<count ; i++ )
153 {
154 std::stringstream sStream;
155 sStream << dir << i << FileSeparator;
156 std::string _dir = sStream.str();
157 try{ std::filesystem::create_directories( _dir ); }
158 catch( ... ){ MK_THROW( "Failed to create directory: " , _dir ); }
159 MakeDirs( _dir , std::min< unsigned int >( count-(i*_filesPerDir) , _filesPerDir ) , depth-1 , filesPerDir );
160 }
161 }
162 };
163 MakeDirs( dir , count , depth , filesPerDir );
164 }
165 ///////////////
166 // Partition //
167 ///////////////
168 // Energy:
169 // An object supporting bool operator < ( const Energy & ) const;
170 template< typename Energy >
171 struct _DynamicProgrammingPartition
172 {
173 // EnergyFunctor:
174 // A functor that that takes the start and end of the range and returns the associated energy
175 // MergeFunctor:
176 // A functor that takes two energies and turns them into one
177 template< typename EnergyFunctor , typename MergeFunctor >
178 _DynamicProgrammingPartition( const EnergyFunctor &energy , const MergeFunctor &merge , unsigned int slabs , unsigned int interiorBoundaries )
179 {
180 _solutions.resize( slabs+1 );
181 for( unsigned int start=0 ; start<_solutions.size() ; start++ )
182 {
183 _solutions[start].resize( slabs+1 );
184 for( unsigned int end=start+1 ; end<_solutions[start].size() ; end++ ) _solutions[start][end].resize( interiorBoundaries+1 );
185 }
186 _energies.resize( slabs+1 );
187 for( unsigned int i=0 ; i<=slabs ; i++ )
188 {
189 _energies[i].resize( slabs+1 );
190 for( unsigned int j=i ; j<=slabs ; j++ ) _energies[i][j] = energy( i , j );
191 }
192 _getSolution( merge , 0 , slabs , interiorBoundaries , Energy() );
193 }
194
195 std::pair< Energy , std::vector< unsigned int > > solution( void ) const
196 {
197 std::pair< Energy , std::vector< unsigned int > > sol;
198 unsigned int start = 0 , end = (unsigned int)_solutions[0].size()-1;
199 unsigned int interiorBoundaries = (unsigned int)_solutions[start][end].size()-1;
200 sol.first = _solutions[0].back().back().e;
201 while( _solutions[start][end][interiorBoundaries].idx!=-1 )
202 {
203 sol.second.push_back( _solutions[start][end][interiorBoundaries].idx );
204 start = _solutions[start][end][interiorBoundaries].idx;
205 interiorBoundaries--;
206 }
207 return sol;
208 }
209
210 template< typename EnergyFunctor , typename MergeFunctor >
211 static std::pair< Energy , std::vector< unsigned int > > Partition( const EnergyFunctor &energy , const MergeFunctor &merge , unsigned int slabs , unsigned int interiorBoundaries )
212 {
213 return _DynamicProgrammingPartition( energy , merge , slabs , interiorBoundaries ).solution();
214 }
215
216 protected:
217 struct _Solution
218 {
219 unsigned int idx;
220 Energy e;
221 _Solution( void ) : idx(-1){}
222 };
223
224
225 // A solution is indexed by the start index, end index, and the number of interior boundaries
226 // The extent is defined by [start,end)
227 std::vector< std::vector< std::vector< _Solution > > > _solutions;
228 std::vector< std::vector< Energy > > _energies;
229 template< typename MergeFunctor >
230 Energy _getSolution( const MergeFunctor &merge , unsigned int start , unsigned int end , unsigned int interiorBoundaries , Energy minEnergy )
231 {
232 // Assuming that:
233 // start<end
234 // interiorBoundaries+1<(end-start)
235
236 // If we have a solution for this range, return it
237 if( _solutions[start][end][interiorBoundaries].idx!=-1 ) return _solutions[start][end][interiorBoundaries].e;
238
239 // If the range does not need to be partitioned
240 else if( interiorBoundaries==0 ) return ( _solutions[start][end][interiorBoundaries].e = _energies[start][end] );
241
242 // Otherwise
243 else
244 {
245 // 1. Split [start,end) into [start,_start) and [_start,end)
246 // 2. Find interiorBoundaries-1 interior boundaries for [_start,end)
247 // 3. Merge
248 unsigned int minIndex = -1;
249 unsigned int _interiorBoundaries = interiorBoundaries-1;
250 for( unsigned int _start=start+1 ; _start<end ; _start++ )
251 {
252 if( _interiorBoundaries<(end-_start) )
253 {
254 if( !(minEnergy<_energies[start][_start]) )
255 {
256 Energy e = merge( _energies[start][_start] , _getSolution( merge , _start , end , _interiorBoundaries , minEnergy ) );
257 if( minIndex==-1 || e<minEnergy ) minEnergy = e , minIndex = _start;
258 }
259 }
260 }
261 if( minIndex==-1 ) MK_THROW( "Could not find a solution: [ " , start , " , " , end , " ) " , interiorBoundaries );
262 _solutions[start][end][interiorBoundaries].e = minEnergy;
263 _solutions[start][end][interiorBoundaries].idx = minIndex;
264 return minEnergy;
265 }
266 }
267 };
268 Partition::Partition( void ) : _slabSizes(1,0) , _starts(0) {}
269
270 Partition::Partition( unsigned int dCount , const std::vector< size_t > &slabSizes ) : _slabSizes( slabSizes )
271 {
272 unsigned int sCount = (unsigned int)_slabSizes.size();
273 _starts.resize( dCount-1 );
274 for( unsigned int i=1 ; i<dCount ; i++ ) _starts[i-1] = ( sCount * i ) / dCount;
275 }
276
277 #ifdef ADAPTIVE_PADDING
278 void Partition::optimize( bool useMax )
279 #else // !ADAPTIVE_PADDING
280 void Partition::optimize( bool useMax , unsigned int padSize )
281 #endif // ADAPTIVE_PADDING
282 {
283 #ifdef ADAPTIVE_PADDING
284 #else // !ADAPTIVE_PADDING
285 auto paddedStart = [&]( unsigned int start ){ return start>padSize ? (start-padSize) : 0; };
286 auto paddedEnd = [&]( unsigned int end ){ return ( end+padSize<=_slabSizes.size() ) ? (end+padSize) : (unsigned int)_slabSizes.size(); };
287 #endif // ADAPTIVE_PADDING
288
289 struct L2Energy
290 {
291 double e;
292 L2Energy( void ) : e( std::numeric_limits<double>::infinity() ) {}
293 L2Energy( double e ) : e(e) {}
294 bool operator < ( const L2Energy &energy ) const { return e<energy.e; }
295 };
296 struct MaxEnergy
297 {
298 std::vector< double > e;
299 MaxEnergy( void ){}
300 MaxEnergy( double e ) { this->e.resize(1,e); }
301 bool operator < ( const MaxEnergy &energy ) const
302 {
303 for( unsigned int i=0 ; i<e.size() && i<energy.e.size() ; i++ )
304 {
305 if ( e[i]<energy.e[i] ) return true;
306 else if( e[i]>energy.e[i] ) return false;
307 }
308 return false;
309 }
310 };
311
312 auto energy = [&]( unsigned int start , unsigned int end )
313 {
314 double e = 0;
315 #ifdef ADAPTIVE_PADDING
316 for( unsigned int i=start ; i<end ; i++ ) e += _slabSizes[i];
317 #else // !ADAPTIVE_PADDING
318 for( unsigned int i=paddedStart(start) ; i<paddedEnd(end) ; i++ ) e += _slabSizes[i];
319 #endif // ADAPTIVE_PADDING
320 return e*e;
321 };
322 auto l2Energy = [&]( unsigned int start , unsigned int end ){ return L2Energy( energy(start,end) ); };
323 auto maxEnergy = [&]( unsigned int start , unsigned int end ){ return MaxEnergy( energy(start,end) ); };
324
325 auto l2Merge = []( L2Energy e1 , L2Energy e2 ){ return L2Energy( e1.e + e2.e ); };
326 auto maxMerge = []( MaxEnergy e1 , MaxEnergy e2 )
327 {
328 MaxEnergy e;
329 e.e.reserve( e1.e.size() + e2.e.size() );
330 for( unsigned int i=0 ; i<e1.e.size() ; i++ ) e.e.push_back( e1.e[i] );
331 for( unsigned int i=0 ; i<e2.e.size() ; i++ ) e.e.push_back( e2.e[i] );
332 std::sort( e.e.begin() , e.e.end() , []( double e1 , double e2 ){ return e1>e2; } );
333 return e;
334 };
335
336 if( useMax ) _starts = _DynamicProgrammingPartition< MaxEnergy >::Partition( maxEnergy , maxMerge , (unsigned)_slabSizes.size() , (unsigned int)_starts.size() ).second;
337 else _starts = _DynamicProgrammingPartition< L2Energy >::Partition( l2Energy , l2Merge , (unsigned)_slabSizes.size() , (unsigned int)_starts.size() ).second;
338 }
339
340 #ifdef ADAPTIVE_PADDING
341 std::pair< unsigned int , unsigned int > Partition::range( unsigned int i ) const
342 #else // !ADAPTIVE_PADDING
343 std::pair< unsigned int , unsigned int > Partition::range( unsigned int i , unsigned int padSize ) const
344 #endif // ADAPTIVE_PADDING
345 {
346 unsigned int begin = i==0 ? 0 : _starts[i-1];
347 unsigned int end = i==_starts.size() ? (unsigned int)_slabSizes.size() : _starts[i];
348 #ifdef ADAPTIVE_PADDING
349 #else // !ADAPTIVE_PADDING
350 if( begin<padSize ) begin = 0;
351 else begin -= padSize;
352 if( end+padSize>_slabSizes.size() ) end = (unsigned int)_slabSizes.size();
353 else end += padSize;
354 #endif // ADAPTIVE_PADDING
355 return std::pair< unsigned int , unsigned int >( begin , end );
356 }
357
358 #ifdef ADAPTIVE_PADDING
359 size_t Partition::size( unsigned int i ) const
360 #else // !ADAPTIVE_PADDING
361 size_t Partition::size( unsigned int i , unsigned int padSize ) const
362 #endif // ADAPTIVE_PADDING
363 {
364 if( i>_starts.size() ) MK_THROW( "Index out of bounds: 0 <= " , i , " <= " , _starts.size() );
365 #ifdef ADAPTIVE_PADDING
366 std::pair< unsigned int , unsigned int > r = range( i );
367 #else // !ADAPTIVE_PADDING
368 std::pair< unsigned int , unsigned int > r = range( i , padSize );
369 #endif // ADAPTIVE_PADDING
370 size_t count = 0;
371 for( unsigned int j=r.first ; j<r.second ; j++ ) count += _slabSizes[j];
372 return count;
373 }
374
375 size_t Partition::size( void ) const
376 {
377 size_t count = 0;
378 #ifdef ADAPTIVE_PADDING
379 for( unsigned int i=0 ; i<=_starts.size() ; i++ ) count += size(i);
380 #else // !ADAPTIVE_PADDING
381 for( unsigned int i=0 ; i<=_starts.size() ; i++ ) count += size(i,0);
382 #endif // ADAPTIVE_PADDING
383 return count;
384 }
385
386 #ifdef ADAPTIVE_PADDING
387 void Partition::printDistribution( void ) const
388 #else // !ADAPTIVE_PADDING
389 void Partition::printDistribution( unsigned int padSize ) const
390 #endif // ADAPTIVE_PADDING
391 {
392 for( unsigned int i=0 ; i<=_starts.size() ; i++ )
393 {
394 #ifdef ADAPTIVE_PADDING
395 std::pair< unsigned int , unsigned int > r = range(i);
396 std::cout << "Slab[ " << i << " ] [ " << r.first << " , " << r.second << " ) " << size(i) << std::endl;
397 #else // !ADAPTIVE_PADDING
398 std::pair< unsigned int , unsigned int > r = range(i,0) , _r = range( i , padSize );
399 if( padSize )
400 std::cout << "Slab[ " << i << " ] [ " << r.first << " , " << r.second << " ) [ " << _r.first << " , " << _r.second << " ) " << size(i,0) << " " << size(i,padSize) << std::endl;
401 else
402 std::cout << "Slab[ " << i << " ] [ " << r.first << " , " << r.second << " ) " << size(i,0) << std::endl;
403 #endif // ADAPTIVE_PADDING
404 }
405 }
406
407 #ifdef ADAPTIVE_PADDING
408 double Partition::l2Energy( void ) const
409 #else // !ADAPTIVE_PADDING
410 double Partition::l2Energy( unsigned int padSize ) const
411 #endif // ADAPTIVE_PADDING
412 {
413 double e = 0;
414 for( unsigned int i=0 ; i<=_starts.size() ; i++ )
415 {
416 #ifdef ADAPTIVE_PADDING
417 double d = (double)size(i);
418 #else // !ADAPTIVE_PADDING
419 double d = (double)size(i,padSize);
420 #endif // ADAPTIVE_PADDING
421 e += d*d;
422 }
423 return sqrt(e);
424 }
425
426 #ifdef ADAPTIVE_PADDING
427 double Partition::maxEnergy( void ) const
428 #else // !ADAPTIVE_PADDING
429 double Partition::maxEnergy( unsigned int padSize ) const
430 #endif // ADAPTIVE_PADDING
431 {
432 double e = 0;
433 #ifdef ADAPTIVE_PADDING
434 for( unsigned int i=0 ; i<=_starts.size() ; i++ ) e = std::max< double >( e , (double)size(i) );
435 #else // !ADAPTIVE_PADDING
436 for( unsigned int i=0 ; i<=_starts.size() ; i++ ) e = std::max< double >( e , (double)size(i,padSize) );
437 #endif // ADAPTIVE_PADDING
438 return e;
439 }
440
441 unsigned int Partition::slabs( void ) const { return (unsigned int)_slabSizes.size(); }
442
443 unsigned int Partition::partitions( void ) const{ return (unsigned int)_starts.size()+1; }
444
445 ///////////////////////////////
446 // Read/Write Ply Properties //
447 ///////////////////////////////
448 long ReadPLYProperties( FILE *fp , std::vector< PlyProperty > &properties )
449 {
450 size_t sz;
451 if( fread( &sz , sizeof( size_t ) , 1 , fp )!=1 ) MK_THROW( "Failed to read property size" );
452 properties.resize( sz );
453 FileStream fs(fp);
454 for( size_t i=0 ; i<sz ; i++ ) properties[i].read( fs );
455 return ftell( fp );
456 }
457
458 long ReadPLYProperties( const char *fileName , std::vector< PlyProperty > &properties )
459 {
460 FILE *fp = fopen( fileName , "rb" );
461 if( !fp ) MK_THROW( "Could not open file for reading: " , fileName );
462 long pos = ReadPLYProperties( fp , properties );
463 fclose( fp );
464 return pos;
465 }
466
467 long WritePLYProperties( FILE *fp , const std::vector< PlyProperty > &properties )
468 {
469 size_t sz = properties.size();
470 fwrite( &sz , sizeof( size_t ) , 1 , fp );
471 FileStream fs(fp);
472 for( size_t i=0 ; i<sz ; i++ ) properties[i].write( fs );
473 return ftell( fp );
474 }
475
476 long WritePLYProperties( const char *fileName , const std::vector< PlyProperty > &properties )
477 {
478 FILE *fp = fopen( fileName , "wb" );
479 if( !fp ) MK_THROW( "Could not open file for writing: " , fileName );
480 long pos = WritePLYProperties( fp , properties );
481 fclose( fp );
482 return pos;
483 }
484
485 ///////////////////////////////////
486 // BufferedBinaryInputDataStream //
487 ///////////////////////////////////
488 template< typename InputFactory >
489 BufferedBinaryInputDataStream< InputFactory >::BufferedBinaryInputDataStream( const char *fileName , const InputFactory &factory , size_t bufferSize ) : _factory(factory) , _bufferSize(bufferSize) , _current(0) , _inBuffer(0)
490 {
491
492 if( !_bufferSize )
493 {
494 MK_WARN_ONCE( "BufferSize cannot be zero , setting to one" );
495 _bufferSize = 1;
496 }
497 _elementSize = _factory.bufferSize();
498 _buffer = AllocPointer< char >( _elementSize*_bufferSize );
499 _fp = fopen( fileName , "rb" );
500 if( !_fp ) MK_THROW( "Could not open file for reading: " , fileName );
501 std::vector< PlyProperty > properties;
502 _inset = ReadPLYProperties( _fp , properties );
503 }
504
505 template< typename InputFactory >
506 BufferedBinaryInputDataStream< InputFactory >::~BufferedBinaryInputDataStream( void )
507 {
508 FreePointer( _buffer );
509 fclose( _fp );
510 }
511
512 template< typename InputFactory >
513 void BufferedBinaryInputDataStream< InputFactory >::reset( void )
514 {
515 fseek( _fp , _inset , SEEK_SET );
516 _current = 0;
517 _inBuffer = 0;
518 }
519
520 template< typename InputFactory >
521 bool BufferedBinaryInputDataStream< InputFactory >::read( Data &d )
522 {
523 if( _current==_inBuffer )
524 {
525 _inBuffer = fread( _buffer , _elementSize , _bufferSize , _fp );
526 _current = 0;
527 }
528 if( !_inBuffer ) return false;
529
530 _factory.fromBuffer( _buffer + _elementSize*_current , d );
531 _current++;
532
533 return true;
534 }
535
536 ////////////////////////////////////
537 // BufferedBinaryOutputDataStream //
538 ////////////////////////////////////
539
540 template< typename OutputFactory >
541 BufferedBinaryOutputDataStream< OutputFactory >::BufferedBinaryOutputDataStream( const char *fileName , const OutputFactory &factory , size_t bufferSize ) : _factory(factory) , _bufferSize(bufferSize) , _current(0)
542 {
543 if( !_bufferSize )
544 {
545 MK_WARN_ONCE( "BufferSize cannot be zero , setting to one" );
546 _bufferSize = 1;
547 }
548 _elementSize = _factory.bufferSize();
549 _buffer = AllocPointer< char >( _elementSize*_bufferSize );
550 _fp = fopen( fileName , "wb" );
551 if( !_fp ) MK_THROW( "Could not open file for writing: " , fileName );
552 std::vector< PlyProperty > properties( factory.plyWriteNum() );
553 for( unsigned int i=0 ; i<factory.plyWriteNum() ; i++ ) properties[i] = factory.plyWriteProperty(i);
554 _inset = WritePLYProperties( _fp , properties );
555 }
556
557 template< typename OutputFactory >
558 BufferedBinaryOutputDataStream< OutputFactory >::~BufferedBinaryOutputDataStream( void )
559 {
560 if( _current ) fwrite( _buffer , _elementSize , _current , _fp );
561 FreePointer( _buffer );
562 fclose( _fp );
563 }
564
565 template< typename OutputFactory >
566 void BufferedBinaryOutputDataStream< OutputFactory >::reset( void )
567 {
568 fseek( _fp , _inset , SEEK_SET );
569 _current = 0;
570 }
571
572 template< typename OutputFactory >
573 size_t BufferedBinaryOutputDataStream< OutputFactory >::write( const Data &d )
574 {
575 if( _current==_bufferSize )
576 {
577 fwrite( _buffer , _elementSize , _bufferSize , _fp );
578 _current = 0;
579 }
580 _factory.toBuffer( d , _buffer + _elementSize*_current );
581 return _current++;
582 }
583
584 template< typename OutputFactory >
585 size_t BufferedBinaryOutputDataStream< OutputFactory >::size( void ) const { return _current; }