| 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 |
////////////////////////// |
| 30 |
// ASCIIInputDataStream // |
| 31 |
////////////////////////// |
| 32 |
template< typename Factory > |
| 33 |
ASCIIInputDataStream< Factory >::ASCIIInputDataStream( const char* fileName , const Factory &factory ) : _factory( factory ) |
| 34 |
{ |
| 35 |
_fp = fopen( fileName , "r" ); |
| 36 |
if( !_fp ) MK_THROW( "Failed to open file for reading: %s" , fileName ); |
| 37 |
} |
| 38 |
|
| 39 |
template< typename Factory > |
| 40 |
ASCIIInputDataStream< Factory >::~ASCIIInputDataStream( void ) |
| 41 |
{ |
| 42 |
fclose( _fp ); |
| 43 |
_fp = NULL; |
| 44 |
} |
| 45 |
|
| 46 |
template< typename Factory > |
| 47 |
void ASCIIInputDataStream< Factory >::reset( void ) { fseek( _fp , 0 , SEEK_SET ); } |
| 48 |
|
| 49 |
template< typename Factory > |
| 50 |
bool ASCIIInputDataStream< Factory >::read( Data &d ){ return _factory.readASCII( _fp , d ); } |
| 51 |
|
| 52 |
/////////////////////////// |
| 53 |
// ASCIIOutputDataStream // |
| 54 |
/////////////////////////// |
| 55 |
template< typename Factory > |
| 56 |
ASCIIOutputDataStream< Factory >::ASCIIOutputDataStream( const char* fileName , const Factory &factory ) : _factory( factory ) |
| 57 |
{ |
| 58 |
_fp = fopen( fileName , "w" ); |
| 59 |
if( !_fp ) MK_THROW( "Failed to open file for writing: %s" , fileName ); |
| 60 |
} |
| 61 |
|
| 62 |
template< typename Factory > |
| 63 |
ASCIIOutputDataStream< Factory >::~ASCIIOutputDataStream( void ) |
| 64 |
{ |
| 65 |
fclose( _fp ); |
| 66 |
_fp = NULL; |
| 67 |
} |
| 68 |
|
| 69 |
template< typename Factory > |
| 70 |
size_t ASCIIOutputDataStream< Factory >::write( const Data &d ){ _factory.writeASCII( _fp , d ) ; return _sz++; } |
| 71 |
|
| 72 |
/////////////////////////// |
| 73 |
// BinaryInputDataStream // |
| 74 |
/////////////////////////// |
| 75 |
template< typename Factory > |
| 76 |
BinaryInputDataStream< Factory >::BinaryInputDataStream( const char* fileName , const Factory &factory ) : _factory(factory) |
| 77 |
{ |
| 78 |
_fp = fopen( fileName , "rb" ); |
| 79 |
if( !_fp ) MK_THROW( "Failed to open file for reading: %s" , fileName ); |
| 80 |
} |
| 81 |
|
| 82 |
template< typename Factory > |
| 83 |
void BinaryInputDataStream< Factory >::reset( void ) { fseek( _fp , 0 , SEEK_SET ); } |
| 84 |
|
| 85 |
template< typename Factory > |
| 86 |
bool BinaryInputDataStream< Factory >::read( Data &d ){ return _factory.readBinary( _fp , d ); } |
| 87 |
|
| 88 |
//////////////////////////// |
| 89 |
// BinaryOutputDataStream // |
| 90 |
//////////////////////////// |
| 91 |
template< typename Factory > |
| 92 |
BinaryOutputDataStream< Factory >::BinaryOutputDataStream( const char* fileName , const Factory &factory ) : _factory(factory) |
| 93 |
{ |
| 94 |
_fp = fopen( fileName , "wb" ); |
| 95 |
if( !_fp ) MK_THROW( "Failed to open file for writing: %s" , fileName ); |
| 96 |
} |
| 97 |
|
| 98 |
template< typename Factory > |
| 99 |
size_t BinaryOutputDataStream< Factory >::write( const Data &d ){ _factory.writeBinary( _fp , d ); return _sz++; } |
| 100 |
|
| 101 |
//////////////////////// |
| 102 |
// PLYInputDataStream // |
| 103 |
//////////////////////// |
| 104 |
template< typename Factory > |
| 105 |
PLYInputDataStream< Factory >::PLYInputDataStream( const char* fileName , const Factory &factory , size_t &count ) : _factory(factory) |
| 106 |
{ |
| 107 |
_fileName = new char[ strlen( fileName )+1 ]; |
| 108 |
strcpy( _fileName , fileName ); |
| 109 |
_ply = NULL; |
| 110 |
if( factory.bufferSize() ) _buffer = NewPointer< char >( factory.bufferSize() ); |
| 111 |
else _buffer = NullPointer( char ); |
| 112 |
reset(); |
| 113 |
count = _pCount; |
| 114 |
} |
| 115 |
|
| 116 |
template< typename Factory > |
| 117 |
PLYInputDataStream< Factory >::PLYInputDataStream( const char* fileName , const Factory &factory ) : _factory(factory) |
| 118 |
{ |
| 119 |
_fileName = new char[ strlen( fileName )+1 ]; |
| 120 |
strcpy( _fileName , fileName ); |
| 121 |
_ply = NULL; |
| 122 |
if( factory.bufferSize() ) _buffer = NewPointer< char >( factory.bufferSize() ); |
| 123 |
else _buffer = NullPointer( char ); |
| 124 |
reset(); |
| 125 |
} |
| 126 |
|
| 127 |
template< typename Factory > |
| 128 |
void PLYInputDataStream< Factory >::reset( void ) |
| 129 |
{ |
| 130 |
int fileType; |
| 131 |
float version; |
| 132 |
if( _ply ) _free(); |
| 133 |
_ply = PlyFile::Read( _fileName, _elist, fileType, version ); |
| 134 |
if( !_ply ) MK_THROW( "Failed to open ply file for reading: " , _fileName ); |
| 135 |
|
| 136 |
bool foundData = false; |
| 137 |
for( int i=0 ; i<_elist.size() ; i++ ) |
| 138 |
{ |
| 139 |
std::string &elem_name = _elist[i]; |
| 140 |
|
| 141 |
if( elem_name=="vertex" ) |
| 142 |
{ |
| 143 |
size_t num_elems; |
| 144 |
std::vector< PlyProperty > plist = _ply->get_element_description( elem_name , num_elems ); |
| 145 |
if( !plist.size() ) MK_THROW( "Failed to get description for \"" , elem_name , "\"" ); |
| 146 |
|
| 147 |
foundData = true; |
| 148 |
_pCount = num_elems , _pIdx = 0; |
| 149 |
|
| 150 |
bool* properties = new bool[ _factory.plyReadNum() ]; |
| 151 |
for( unsigned int i=0 ; i<_factory.plyReadNum() ; i++ ) |
| 152 |
{ |
| 153 |
PlyProperty prop; |
| 154 |
if constexpr( Factory::IsStaticallyAllocated() ) prop = _factory.plyStaticReadProperty(i); |
| 155 |
else prop = _factory.plyReadProperty(i); |
| 156 |
if( !_ply->get_property( elem_name , &prop ) ) properties[i] = false; |
| 157 |
else properties[i] = true; |
| 158 |
} |
| 159 |
bool valid = _factory.plyValidReadProperties( properties ); |
| 160 |
delete[] properties; |
| 161 |
if( !valid ) MK_THROW( "Failed to validate properties in file" ); |
| 162 |
} |
| 163 |
} |
| 164 |
if( !foundData ) MK_THROW( "Could not find data in ply file" ); |
| 165 |
} |
| 166 |
|
| 167 |
template< typename Factory > |
| 168 |
void PLYInputDataStream< Factory >::_free( void ){ delete _ply; } |
| 169 |
|
| 170 |
template< typename Factory > |
| 171 |
PLYInputDataStream< Factory >::~PLYInputDataStream( void ) |
| 172 |
{ |
| 173 |
_free(); |
| 174 |
if( _fileName ) delete[] _fileName , _fileName = NULL; |
| 175 |
DeletePointer( _buffer ); |
| 176 |
} |
| 177 |
|
| 178 |
template< typename Factory > |
| 179 |
bool PLYInputDataStream< Factory >::read( Data &d ) |
| 180 |
{ |
| 181 |
if( _pIdx<_pCount ) |
| 182 |
{ |
| 183 |
if constexpr( Factory::IsStaticallyAllocated() ) _ply->get_element( (void *)&d ); |
| 184 |
else |
| 185 |
{ |
| 186 |
_ply->get_element( PointerAddress( _buffer ) ); |
| 187 |
_factory.fromBuffer( _buffer , d ); |
| 188 |
} |
| 189 |
_pIdx++; |
| 190 |
return true; |
| 191 |
} |
| 192 |
else return false; |
| 193 |
} |
| 194 |
|
| 195 |
///////////////////////// |
| 196 |
// PLYOutputDataStream // |
| 197 |
///////////////////////// |
| 198 |
template< typename Factory > |
| 199 |
PLYOutputDataStream< Factory >::PLYOutputDataStream( const char* fileName , const Factory &factory , size_t count , int fileType ) : _factory(factory) |
| 200 |
{ |
| 201 |
float version; |
| 202 |
std::vector< std::string > elem_names = { std::string( "vertex" ) }; |
| 203 |
_ply = PlyFile::Write( fileName , elem_names , fileType , version ); |
| 204 |
if( !_ply ) MK_THROW( "Failed to open ply file for writing: " , fileName ); |
| 205 |
|
| 206 |
_pIdx = 0; |
| 207 |
_pCount = count; |
| 208 |
_ply->element_count( "vertex" , _pCount ); |
| 209 |
for( unsigned int i=0 ; i<_factory.plyWriteNum() ; i++ ) |
| 210 |
{ |
| 211 |
PlyProperty prop; |
| 212 |
if constexpr( Factory::IsStaticallyAllocated() ) prop = _factory.plyStaticWriteProperty(i); |
| 213 |
else prop = _factory.plyWriteProperty(i); |
| 214 |
_ply->describe_property( "vertex" , &prop ); |
| 215 |
} |
| 216 |
_ply->header_complete(); |
| 217 |
_ply->put_element_setup( "vertex" ); |
| 218 |
if( _factory.bufferSize() ) _buffer = NewPointer< char >( _factory.bufferSize() ); |
| 219 |
else _buffer = NullPointer( char ); |
| 220 |
} |
| 221 |
|
| 222 |
template< typename Factory > |
| 223 |
PLYOutputDataStream< Factory >::~PLYOutputDataStream( void ) |
| 224 |
{ |
| 225 |
if( _pIdx!=_pCount ) MK_THROW( "Streamed points not equal to total count: " , _pIdx , " != " , _pCount ); |
| 226 |
delete _ply; |
| 227 |
DeletePointer( _buffer ); |
| 228 |
} |
| 229 |
|
| 230 |
template< typename Factory > |
| 231 |
size_t PLYOutputDataStream< Factory >::write( const Data &d ) |
| 232 |
{ |
| 233 |
if( _pIdx==_pCount ) MK_THROW( "Trying to add more points than total: " , _pIdx , " < " , _pCount ); |
| 234 |
if constexpr( Factory::IsStaticallyAllocated() ) _ply->put_element( (void *)&d ); |
| 235 |
else |
| 236 |
{ |
| 237 |
_factory.toBuffer( d , _buffer ); |
| 238 |
_ply->put_element( PointerAddress( _buffer ) ); |
| 239 |
} |
| 240 |
return _pIdx++; |
| 241 |
} |
| 242 |
|