| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 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 |
#ifndef DATA_STREAM_IMPLEMENTATION_INCLUDED |
| 30 |
#define DATA_STREAM_IMPLEMENTATION_INCLUDED |
| 31 |
|
| 32 |
#include <vector> |
| 33 |
#include "DataStream.h" |
| 34 |
#include "VertexFactory.h" |
| 35 |
#include "Ply.h" |
| 36 |
|
| 37 |
namespace PoissonRecon |
| 38 |
{ |
| 39 |
//////////////////////////////// |
| 40 |
// Vector-backed data streams // |
| 41 |
//////////////////////////////// |
| 42 |
template< typename Data > |
| 43 |
struct VectorBackedInputDataStream : public InputDataStream< Data > |
| 44 |
{ |
| 45 |
VectorBackedInputDataStream( const std::vector< Data > &data ) : _data(data) , _current(0) {} |
| 46 |
void reset( void ) { _current = 0; } |
| 47 |
|
| 48 |
bool read( Data &d ){ if( _current<_data.size() ){ d = _data[_current++] ; return true; } else return false; } |
| 49 |
|
| 50 |
protected: |
| 51 |
const std::vector< Data > &_data; |
| 52 |
size_t _current; |
| 53 |
}; |
| 54 |
|
| 55 |
template< typename Data > |
| 56 |
struct VectorBackedInputIndexedDataStream : public InputIndexedDataStream< Data > |
| 57 |
{ |
| 58 |
VectorBackedInputIndexedDataStream( const std::vector< std::pair< size_t , Data > > &data ) : _data(data) , _current(0) {} |
| 59 |
void reset( void ) { _current = 0; } |
| 60 |
|
| 61 |
bool read( size_t &idx , Data &d ) |
| 62 |
{ |
| 63 |
if( _current<_data.size() ) |
| 64 |
{ |
| 65 |
idx = _data[_current].first; |
| 66 |
d = _data[_current].second; |
| 67 |
_current++; |
| 68 |
return true; |
| 69 |
} |
| 70 |
else return false; |
| 71 |
} |
| 72 |
|
| 73 |
protected: |
| 74 |
const std::vector< std::pair< size_t , Data > > &_data; |
| 75 |
size_t _current; |
| 76 |
}; |
| 77 |
|
| 78 |
template< typename Data > |
| 79 |
struct VectorBackedOutputDataStream : public OutputDataStream< Data > |
| 80 |
{ |
| 81 |
VectorBackedOutputDataStream( std::vector< Data > &data ) : _data(data) {} |
| 82 |
size_t write( const Data &d ){ size_t idx = _data.size() ; _data.push_back(d) ; return idx; } |
| 83 |
size_t size( void ) const { return _data.size(); } |
| 84 |
protected: |
| 85 |
std::vector< Data > &_data; |
| 86 |
}; |
| 87 |
|
| 88 |
template< typename Data > |
| 89 |
struct VectorBackedOutputIndexedDataStream : public OutputIndexedDataStream< Data > |
| 90 |
{ |
| 91 |
VectorBackedOutputIndexedDataStream( std::vector< std::pair< size_t , Data > > &data ) : _data(data) {} |
| 92 |
size_t write( const size_t &idx , const Data &d ) |
| 93 |
{ |
| 94 |
size_t sz = _data.size(); |
| 95 |
_data.push_back( std::make_pair( idx , d ) ); |
| 96 |
return idx; |
| 97 |
} |
| 98 |
size_t size( void ) const { return _data.size(); } |
| 99 |
protected: |
| 100 |
std::vector< std::pair< size_t , Data > > &_data; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
//////////////////////////////////////////////////////////////////// |
| 105 |
// File-backed data stream (assumes Data is statically allocated) // |
| 106 |
//////////////////////////////////////////////////////////////////// |
| 107 |
template< typename Data > |
| 108 |
struct FileBackedInputDataStream : public InputDataStream< Data > |
| 109 |
{ |
| 110 |
// It is assumed that the file pointer was open for binary reading |
| 111 |
FileBackedInputDataStream( FILE *fp ) : _fp(fp) {} |
| 112 |
void reset( void ){ fseek( _fp , 0 , SEEK_SET ); } |
| 113 |
bool read( Data &d ){ return fread( &d , sizeof(Data) , 1 , _fp )==1; } |
| 114 |
|
| 115 |
protected: |
| 116 |
FILE *_fp; |
| 117 |
}; |
| 118 |
|
| 119 |
template< typename Data > |
| 120 |
struct FileBackedOutputDataStream : public OutputDataStream< Data > |
| 121 |
{ |
| 122 |
// It is assumed that the file pointer was open for binary writing |
| 123 |
FileBackedOutputDataStream( FILE *fp ) : _fp(fp) {} |
| 124 |
size_t write( const Data &d ){ fwrite( &d , sizeof(Data) , 1 , _fp ) ; return _sz++; } |
| 125 |
size_t size( void ) const { return _sz; } |
| 126 |
|
| 127 |
protected: |
| 128 |
size_t _sz = 0; |
| 129 |
FILE *_fp; |
| 130 |
}; |
| 131 |
|
| 132 |
///////////////////////////////////////////////////////////////////////////////////////////// |
| 133 |
// File-backed data stream specialized for vectors (assumes Data is statically allocated) // |
| 134 |
///////////////////////////////////////////////////////////////////////////////////////////// |
| 135 |
template< typename Data > |
| 136 |
struct FileBackedInputDataStream< std::vector< Data > > : public InputDataStream< std::vector< Data > > |
| 137 |
{ |
| 138 |
// It is assumed that the file pointer was open for binary reading |
| 139 |
FileBackedInputDataStream( FILE *fp ) : _fp(fp) {} |
| 140 |
void reset( void ){ fseek( _fp , 0 , SEEK_SET ); } |
| 141 |
bool read( std::vector< Data > &d ) |
| 142 |
{ |
| 143 |
unsigned int pSize; |
| 144 |
if( fread( &pSize , sizeof(unsigned int) , 1 , _fp )==1 ) |
| 145 |
{ |
| 146 |
d.resize( pSize ); |
| 147 |
if( fread( &d[0] , sizeof(Data) , pSize , _fp )==pSize ) return true; |
| 148 |
MK_THROW( "Failed to read polygon from file" ); |
| 149 |
return true; |
| 150 |
} |
| 151 |
else return false; |
| 152 |
} |
| 153 |
|
| 154 |
protected: |
| 155 |
FILE *_fp; |
| 156 |
}; |
| 157 |
|
| 158 |
template< typename Data > |
| 159 |
struct FileBackedOutputDataStream< std::vector< Data > > : public OutputDataStream< std::vector< Data > > |
| 160 |
{ |
| 161 |
// It is assumed that the file pointer was open for binary writing |
| 162 |
FileBackedOutputDataStream( FILE *fp ) : _fp(fp) {} |
| 163 |
size_t write( const std::vector< Data > &d ) |
| 164 |
{ |
| 165 |
unsigned int pSize = (unsigned int)d.size(); |
| 166 |
fwrite( &pSize , sizeof(unsigned int) , 1 , _fp ); |
| 167 |
fwrite( &d[0] , sizeof(Data) , pSize , _fp ); |
| 168 |
return _sz++; |
| 169 |
} |
| 170 |
size_t size( void ) const { return _sz; } |
| 171 |
|
| 172 |
protected: |
| 173 |
size_t _sz = 0; |
| 174 |
FILE *_fp; |
| 175 |
}; |
| 176 |
|
| 177 |
//////////////////////////////////////////////////////// |
| 178 |
// File-backed stream with data desribed by a factory // |
| 179 |
//////////////////////////////////////////////////////// |
| 180 |
template< typename Factory > |
| 181 |
struct FileBackedInputFactoryTypeStream : public InputDataStream< typename Factory::VertexType > |
| 182 |
{ |
| 183 |
using Data = typename Factory::VertexType; |
| 184 |
// It is assumed that the file pointer was open for binary reading |
| 185 |
FileBackedInputFactoryTypeStream( FILE *fp , const Factory &factory ) : _fp(fp) , _factory(factory) , _buffer( NewPointer< char >( _factory.bufferSize() ) ) , _bufferSize( _factory.bufferSize() ) {} |
| 186 |
~FileBackedInputFactoryTypeStream( void ){ DeletePointer( _buffer ); } |
| 187 |
void reset( void ){ fseek( _fp , 0 , SEEK_SET ); } |
| 188 |
bool read( Data &d ) |
| 189 |
{ |
| 190 |
if( fread( _buffer , sizeof(unsigned char) , _bufferSize , _fp )==_bufferSize ) |
| 191 |
{ |
| 192 |
_factory.fromBuffer( _buffer , d ); |
| 193 |
return true; |
| 194 |
} |
| 195 |
else return false; |
| 196 |
return _sz++; |
| 197 |
} |
| 198 |
|
| 199 |
protected: |
| 200 |
size_t _sz = 0; |
| 201 |
FILE *_fp; |
| 202 |
const Factory _factory; |
| 203 |
Pointer( char ) _buffer; |
| 204 |
const size_t _bufferSize; |
| 205 |
}; |
| 206 |
|
| 207 |
template< typename Factory > |
| 208 |
struct FileBackedInputIndexedFactoryTypeStream : public InputIndexedDataStream< typename Factory::VertexType > |
| 209 |
{ |
| 210 |
using Data = typename Factory::VertexType; |
| 211 |
// It is assumed that the file pointer was open for binary reading |
| 212 |
FileBackedInputIndexedFactoryTypeStream( FILE *fp , const Factory &factory ) : _fp(fp) , _factory(factory) , _buffer( NewPointer< char >( _factory.bufferSize() ) ) , _bufferSize( _factory.bufferSize() ) {} |
| 213 |
~FileBackedInputIndexedFactoryTypeStream( void ){ DeletePointer( _buffer ); } |
| 214 |
void reset( void ){ fseek( _fp , 0 , SEEK_SET ); } |
| 215 |
bool read( size_t &idx , Data &d ) |
| 216 |
{ |
| 217 |
if( fread( &idx , sizeof(size_t) , 1 , _fp )!=1 ) return false; |
| 218 |
if( fread( _buffer , sizeof(unsigned char) , _bufferSize , _fp )==_bufferSize ) |
| 219 |
{ |
| 220 |
_factory.fromBuffer( _buffer , d ); |
| 221 |
return true; |
| 222 |
} |
| 223 |
else return false; |
| 224 |
return _sz++; |
| 225 |
} |
| 226 |
|
| 227 |
protected: |
| 228 |
size_t _sz = 0; |
| 229 |
FILE *_fp; |
| 230 |
const Factory _factory; |
| 231 |
Pointer( char ) _buffer; |
| 232 |
const size_t _bufferSize; |
| 233 |
}; |
| 234 |
|
| 235 |
template< typename Factory > |
| 236 |
struct FileBackedOutputFactoryTypeStream : public OutputDataStream< typename Factory::VertexType > |
| 237 |
{ |
| 238 |
using Data = typename Factory::VertexType; |
| 239 |
// It is assumed that the file pointer was open for binary reading |
| 240 |
FileBackedOutputFactoryTypeStream( FILE *fp , const Factory &factory ) : _fp(fp) , _factory(factory) , _buffer( NewPointer< char >( _factory.bufferSize() ) ) , _bufferSize( _factory.bufferSize() ) {} |
| 241 |
~FileBackedOutputFactoryTypeStream( void ){ DeletePointer( _buffer ); } |
| 242 |
size_t write( const Data &d ) |
| 243 |
{ |
| 244 |
_factory.toBuffer( d , _buffer ); |
| 245 |
fwrite( _buffer , sizeof(unsigned char) , _bufferSize , _fp ); |
| 246 |
return _sz++; |
| 247 |
} |
| 248 |
size_t size( void ) const { return _sz; } |
| 249 |
protected: |
| 250 |
size_t _sz = 0; |
| 251 |
FILE *_fp; |
| 252 |
const Factory _factory; |
| 253 |
Pointer( char ) _buffer; |
| 254 |
const size_t _bufferSize; |
| 255 |
}; |
| 256 |
|
| 257 |
template< typename Factory > |
| 258 |
struct FileBackedOutputIndexedFactoryTypeStream : public OutputIndexedDataStream< typename Factory::VertexType > |
| 259 |
{ |
| 260 |
using Data = typename Factory::VertexType; |
| 261 |
// It is assumed that the file pointer was open for binary reading |
| 262 |
FileBackedOutputIndexedFactoryTypeStream( FILE *fp , const Factory &factory ) : _fp(fp) , _factory(factory) , _buffer( NewPointer< char >( _factory.bufferSize() ) ) , _bufferSize( _factory.bufferSize() ) {} |
| 263 |
~FileBackedOutputIndexedFactoryTypeStream( void ){ DeletePointer( _buffer ); } |
| 264 |
size_t write( const size_t &idx , const Data &d ) |
| 265 |
{ |
| 266 |
fwrite( &idx , sizeof(size_t) , 1 , _fp ); |
| 267 |
_factory.toBuffer( d , _buffer ); |
| 268 |
fwrite( _buffer , sizeof(unsigned char) , _bufferSize , _fp ); |
| 269 |
return _sz++; |
| 270 |
} |
| 271 |
size_t size( void ) const { return _sz; } |
| 272 |
protected: |
| 273 |
size_t _sz = 0; |
| 274 |
FILE *_fp; |
| 275 |
const Factory _factory; |
| 276 |
Pointer( char ) _buffer; |
| 277 |
const size_t _bufferSize; |
| 278 |
}; |
| 279 |
|
| 280 |
/////////////////////////////////////////////////////////////////////////////////// |
| 281 |
// File-backed data streams, with functionality for reading/writing from/to disk // |
| 282 |
/////////////////////////////////////////////////////////////////////////////////// |
| 283 |
|
| 284 |
template< typename Factory > |
| 285 |
struct ASCIIInputDataStream : public InputDataStream< typename Factory::VertexType > |
| 286 |
{ |
| 287 |
typedef typename Factory::VertexType Data; |
| 288 |
|
| 289 |
ASCIIInputDataStream( const char* fileName , const Factory &factory ); |
| 290 |
~ASCIIInputDataStream( void ); |
| 291 |
void reset( void ); |
| 292 |
bool read( Data &d ); |
| 293 |
|
| 294 |
protected: |
| 295 |
const Factory _factory; |
| 296 |
FILE *_fp; |
| 297 |
}; |
| 298 |
|
| 299 |
template< typename Factory > |
| 300 |
struct ASCIIOutputDataStream : public OutputDataStream< typename Factory::VertexType > |
| 301 |
{ |
| 302 |
typedef typename Factory::VertexType Data; |
| 303 |
|
| 304 |
ASCIIOutputDataStream( const char* fileName , const Factory &factory ); |
| 305 |
~ASCIIOutputDataStream( void ); |
| 306 |
size_t write( const Data &d ); |
| 307 |
size_t size( void ) const { return _sz; } |
| 308 |
|
| 309 |
protected: |
| 310 |
size_t _sz = 0; |
| 311 |
const Factory _factory; |
| 312 |
FILE *_fp; |
| 313 |
}; |
| 314 |
|
| 315 |
template< typename Factory > |
| 316 |
struct BinaryInputDataStream : public InputDataStream< typename Factory::VertexType > |
| 317 |
{ |
| 318 |
typedef typename Factory::VertexType Data; |
| 319 |
|
| 320 |
BinaryInputDataStream( const char* filename , const Factory &factory ); |
| 321 |
~BinaryInputDataStream( void ){ fclose( _fp ) , _fp=NULL; } |
| 322 |
void reset( void ); |
| 323 |
bool read( Data &d ); |
| 324 |
|
| 325 |
protected: |
| 326 |
const Factory _factory; |
| 327 |
FILE* _fp; |
| 328 |
}; |
| 329 |
|
| 330 |
template< typename Factory > |
| 331 |
struct BinaryOutputDataStream : public OutputDataStream< typename Factory::VertexType > |
| 332 |
{ |
| 333 |
typedef typename Factory::VertexType Data; |
| 334 |
|
| 335 |
BinaryOutputDataStream( const char* filename , const Factory &factory ); |
| 336 |
~BinaryOutputDataStream( void ){ fclose( _fp ) , _fp=NULL; } |
| 337 |
void reset( void ){ fseek( _fp , 0 , SEEK_SET ); } |
| 338 |
size_t write( const Data &d ); |
| 339 |
size_t size( void ) const { return _sz; } |
| 340 |
|
| 341 |
protected: |
| 342 |
size_t _sz = 0; |
| 343 |
const Factory _factory; |
| 344 |
FILE* _fp; |
| 345 |
}; |
| 346 |
|
| 347 |
//////////////////////////////////////////// |
| 348 |
// File-backed PLY-described data streams // |
| 349 |
//////////////////////////////////////////// |
| 350 |
|
| 351 |
template< typename Factory > |
| 352 |
struct PLYInputDataStream : public InputDataStream< typename Factory::VertexType > |
| 353 |
{ |
| 354 |
typedef typename Factory::VertexType Data; |
| 355 |
|
| 356 |
PLYInputDataStream( const char* fileName , const Factory &factory ); |
| 357 |
PLYInputDataStream( const char* fileName , const Factory &factory , size_t &count ); |
| 358 |
~PLYInputDataStream( void ); |
| 359 |
void reset( void ); |
| 360 |
bool read( Data &d ); |
| 361 |
|
| 362 |
protected: |
| 363 |
const Factory _factory; |
| 364 |
char* _fileName; |
| 365 |
PlyFile *_ply; |
| 366 |
std::vector< std::string > _elist; |
| 367 |
Pointer( char ) _buffer; |
| 368 |
|
| 369 |
size_t _pCount , _pIdx; |
| 370 |
void _free( void ); |
| 371 |
}; |
| 372 |
|
| 373 |
template< typename Factory > |
| 374 |
struct PLYOutputDataStream : public OutputDataStream< typename Factory::VertexType > |
| 375 |
{ |
| 376 |
typedef typename Factory::VertexType Data; |
| 377 |
|
| 378 |
PLYOutputDataStream( const char* fileName , const Factory &factory , size_t count , int fileType=PLY_BINARY_NATIVE ); |
| 379 |
~PLYOutputDataStream( void ); |
| 380 |
size_t write( const Data &d ); |
| 381 |
size_t size( void ) const { return _pIdx; } |
| 382 |
|
| 383 |
protected: |
| 384 |
const Factory _factory; |
| 385 |
PlyFile *_ply; |
| 386 |
size_t _pCount , _pIdx; |
| 387 |
Pointer( char ) _buffer; |
| 388 |
}; |
| 389 |
|
| 390 |
#include "DataStream.imp.inl" |
| 391 |
} |
| 392 |
|
| 393 |
#endif // DATA_STREAM_IMPLEMENTATION_INCLUDED |