| 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 |
|
| 29 |
namespace VertexFactory |
| 30 |
{ |
| 31 |
int ToPlyType( TypeOnDisk typeOnDisk ) |
| 32 |
{ |
| 33 |
switch( typeOnDisk ) |
| 34 |
{ |
| 35 |
case TypeOnDisk::CHAR: return PLY::Type< char >(); |
| 36 |
case TypeOnDisk::UCHAR: return PLY::Type< unsigned char >(); |
| 37 |
case TypeOnDisk::INT: return PLY::Type< int >(); |
| 38 |
case TypeOnDisk::UINT: return PLY::Type< unsigned int >(); |
| 39 |
case TypeOnDisk::FLOAT: return PLY::Type< float >(); |
| 40 |
case TypeOnDisk::DOUBLE: return PLY::Type< double >(); |
| 41 |
case TypeOnDisk::INT_8: return PLY::Type< int8_t >(); |
| 42 |
case TypeOnDisk::UINT_8: return PLY::Type< uint8_t >(); |
| 43 |
case TypeOnDisk::INT_16: return PLY::Type< int16_t >(); |
| 44 |
case TypeOnDisk::UINT_16: return PLY::Type< uint16_t >(); |
| 45 |
case TypeOnDisk::INT_32: return PLY::Type< int32_t >(); |
| 46 |
case TypeOnDisk::UINT_32: return PLY::Type< uint32_t >(); |
| 47 |
case TypeOnDisk::INT_64: return PLY::Type< int64_t >(); |
| 48 |
case TypeOnDisk::UINT_64: return PLY::Type< uint64_t >(); |
| 49 |
default: MK_THROW( "Unrecognized type: " , typeOnDisk ); |
| 50 |
} |
| 51 |
return -1; |
| 52 |
} |
| 53 |
|
| 54 |
TypeOnDisk FromPlyType( int plyType ) |
| 55 |
{ |
| 56 |
switch( plyType ) |
| 57 |
{ |
| 58 |
case PLY_INT: return TypeOnDisk::INT; |
| 59 |
case PLY_UINT: return TypeOnDisk::UINT; |
| 60 |
case PLY_CHAR: return TypeOnDisk::CHAR; |
| 61 |
case PLY_UCHAR: return TypeOnDisk::UCHAR; |
| 62 |
case PLY_FLOAT: return TypeOnDisk::FLOAT; |
| 63 |
case PLY_DOUBLE: return TypeOnDisk::DOUBLE; |
| 64 |
case PLY_INT_8: return TypeOnDisk::INT_8; |
| 65 |
case PLY_UINT_8: return TypeOnDisk::UINT_8; |
| 66 |
case PLY_INT_16: return TypeOnDisk::INT_16; |
| 67 |
case PLY_UINT_16: return TypeOnDisk::UINT_16; |
| 68 |
case PLY_INT_32: return TypeOnDisk::INT_32; |
| 69 |
case PLY_UINT_32: return TypeOnDisk::UINT_32; |
| 70 |
case PLY_INT_64: return TypeOnDisk::INT_64; |
| 71 |
case PLY_UINT_64: return TypeOnDisk::UINT_64; |
| 72 |
case PLY_FLOAT_32: return TypeOnDisk::FLOAT; |
| 73 |
case PLY_FLOAT_64: return TypeOnDisk::DOUBLE; |
| 74 |
default: MK_THROW( "Unrecognized type: " , plyType ); |
| 75 |
} |
| 76 |
return TypeOnDisk::UNKNOWN; |
| 77 |
} |
| 78 |
|
| 79 |
template< typename Type > TypeOnDisk GetTypeOnDisk( void ) |
| 80 |
{ |
| 81 |
if constexpr( std::is_same< Type , char >::value ) return TypeOnDisk::CHAR; |
| 82 |
else if constexpr( std::is_same< Type , unsigned char >::value ) return TypeOnDisk::UCHAR; |
| 83 |
else if constexpr( std::is_same< Type , int >::value ) return TypeOnDisk::INT; |
| 84 |
else if constexpr( std::is_same< Type , unsigned int >::value ) return TypeOnDisk::UINT; |
| 85 |
else if constexpr( std::is_same< Type , int8_t >::value ) return TypeOnDisk::INT_8; |
| 86 |
else if constexpr( std::is_same< Type , uint8_t >::value ) return TypeOnDisk::UINT_8; |
| 87 |
else if constexpr( std::is_same< Type , int16_t >::value ) return TypeOnDisk::INT_16; |
| 88 |
else if constexpr( std::is_same< Type , uint16_t >::value ) return TypeOnDisk::UINT_16; |
| 89 |
else if constexpr( std::is_same< Type , int32_t >::value ) return TypeOnDisk::INT_32; |
| 90 |
else if constexpr( std::is_same< Type , uint32_t >::value ) return TypeOnDisk::UINT_32; |
| 91 |
else if constexpr( std::is_same< Type , int64_t >::value ) return TypeOnDisk::INT_64; |
| 92 |
else if constexpr( std::is_same< Type , uint64_t >::value ) return TypeOnDisk::UINT_64; |
| 93 |
else if constexpr( std::is_same< Type , float >::value ) return TypeOnDisk::FLOAT; |
| 94 |
else if constexpr( std::is_same< Type , double >::value ) return TypeOnDisk::DOUBLE; |
| 95 |
else MK_THROW( "Unrecognized type" ); |
| 96 |
return TypeOnDisk::UNKNOWN; |
| 97 |
} |
| 98 |
|
| 99 |
template< typename Real > |
| 100 |
template< typename Type > |
| 101 |
bool VertexIO< Real >::_ReadBinary( FILE *fp , Real &r ) |
| 102 |
{ |
| 103 |
Type t; |
| 104 |
if( fread( &t , sizeof(Type) , 1 , fp )!=1 ) return false; |
| 105 |
r = (Real)t; |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
template< typename Real > |
| 110 |
template< typename Type > |
| 111 |
void VertexIO< Real >::_WriteBinary( FILE *fp , Real r ){ Type t = (Type)r ; fwrite( &t , sizeof(Type) , 1 , fp ); } |
| 112 |
|
| 113 |
template< typename Real > |
| 114 |
bool VertexIO< Real >::ReadASCII( FILE *fp , TypeOnDisk , Real &s ) |
| 115 |
{ |
| 116 |
double d; |
| 117 |
if( fscanf( fp , " %lf" , &d )!=1 ) return false; |
| 118 |
s = (Real)d; |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
template< typename Real > |
| 123 |
bool VertexIO< Real >::ReadBinary( FILE *fp , TypeOnDisk typeOnDisk , Real &s ) |
| 124 |
{ |
| 125 |
if( TypeOnDisk()==typeOnDisk ) return fread( &s , sizeof(Real) , 1 , fp )==1; |
| 126 |
switch( typeOnDisk ) |
| 127 |
{ |
| 128 |
case TypeOnDisk::CHAR: return _ReadBinary< char >( fp , s ); |
| 129 |
case TypeOnDisk::UCHAR: return _ReadBinary< unsigned char >( fp , s ); |
| 130 |
case TypeOnDisk::INT: return _ReadBinary< int >( fp , s ); |
| 131 |
case TypeOnDisk::UINT: return _ReadBinary< unsigned int >( fp , s ); |
| 132 |
case TypeOnDisk::FLOAT: return _ReadBinary< float >( fp , s ); |
| 133 |
case TypeOnDisk::DOUBLE: return _ReadBinary< double >( fp , s ); |
| 134 |
case TypeOnDisk::INT_8: return _ReadBinary< int8_t >( fp , s ); |
| 135 |
case TypeOnDisk::UINT_8: return _ReadBinary< uint8_t >( fp , s ); |
| 136 |
case TypeOnDisk::INT_16: return _ReadBinary< int16_t >( fp , s ); |
| 137 |
case TypeOnDisk::UINT_16: return _ReadBinary< uint16_t >( fp , s ); |
| 138 |
case TypeOnDisk::INT_32: return _ReadBinary< int32_t >( fp , s ); |
| 139 |
case TypeOnDisk::UINT_32: return _ReadBinary< uint32_t >( fp , s ); |
| 140 |
case TypeOnDisk::INT_64: return _ReadBinary< int64_t >( fp , s ); |
| 141 |
case TypeOnDisk::UINT_64: return _ReadBinary< uint64_t >( fp , s ); |
| 142 |
default: MK_THROW( "Unrecognized type: " , typeOnDisk ); |
| 143 |
} |
| 144 |
return true; |
| 145 |
} |
| 146 |
template< typename Real > |
| 147 |
void VertexIO< Real >::WriteASCII( FILE *fp , TypeOnDisk typeOnDisk , const Real &s ) |
| 148 |
{ |
| 149 |
switch( typeOnDisk ) |
| 150 |
{ |
| 151 |
case TypeOnDisk::CHAR: fprintf( fp , " %d" , ( char)s ) ; break; |
| 152 |
case TypeOnDisk::UCHAR: fprintf( fp , " %u" , (unsigned char)s ) ; break; |
| 153 |
case TypeOnDisk::INT: fprintf( fp , " %d" , ( int)s ) ; break; |
| 154 |
case TypeOnDisk::UINT: fprintf( fp , " %u" , (unsigned int)s ) ; break; |
| 155 |
case TypeOnDisk::FLOAT: fprintf( fp , " %f" , ( float)s ) ; break; |
| 156 |
case TypeOnDisk::DOUBLE: fprintf( fp , " %f" , ( double)s ) ; break; |
| 157 |
case TypeOnDisk::INT_8: fprintf( fp , " %" PRId8 , ( int8_t)s ) ; break; |
| 158 |
case TypeOnDisk::UINT_8: fprintf( fp , " %" PRIu8 , ( uint8_t)s ) ; break; |
| 159 |
case TypeOnDisk::INT_16: fprintf( fp , " %" PRId16 , ( int16_t)s ) ; break; |
| 160 |
case TypeOnDisk::UINT_16: fprintf( fp , " %" PRIu16 , ( uint16_t)s ) ; break; |
| 161 |
case TypeOnDisk::INT_32: fprintf( fp , " %" PRId32 , ( int32_t)s ) ; break; |
| 162 |
case TypeOnDisk::UINT_32: fprintf( fp , " %" PRIu32 , ( uint32_t)s ) ; break; |
| 163 |
case TypeOnDisk::INT_64: fprintf( fp , " %" PRId64 , ( int64_t)s ) ; break; |
| 164 |
case TypeOnDisk::UINT_64: fprintf( fp , " %" PRIu64 , ( uint64_t)s ) ; break; |
| 165 |
default: MK_THROW( "Unrecongized type: " , typeOnDisk ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
template< typename Real > |
| 170 |
void VertexIO< Real >::WriteBinary( FILE *fp , TypeOnDisk typeOnDisk , const Real &s ) |
| 171 |
{ |
| 172 |
if( TypeOnDisk()==typeOnDisk ) fwrite( &s , sizeof(Real) , 1 , fp ); |
| 173 |
switch( typeOnDisk ) |
| 174 |
{ |
| 175 |
case TypeOnDisk::CHAR: _WriteBinary< char >( fp , s ) ; break; |
| 176 |
case TypeOnDisk::UCHAR: _WriteBinary< unsigned char >( fp , s ) ; break; |
| 177 |
case TypeOnDisk::INT: _WriteBinary< int >( fp , s ) ; break; |
| 178 |
case TypeOnDisk::UINT: _WriteBinary< unsigned int >( fp , s ) ; break; |
| 179 |
case TypeOnDisk::FLOAT: _WriteBinary< float >( fp , s ) ; break; |
| 180 |
case TypeOnDisk::DOUBLE: _WriteBinary< double >( fp , s ) ; break; |
| 181 |
case TypeOnDisk::INT_8: _WriteBinary< int8_t >( fp , s ) ; break; |
| 182 |
case TypeOnDisk::UINT_8: _WriteBinary< uint8_t >( fp , s ) ; break; |
| 183 |
case TypeOnDisk::INT_16: _WriteBinary< int16_t >( fp , s ) ; break; |
| 184 |
case TypeOnDisk::UINT_16: _WriteBinary< uint16_t >( fp , s ) ; break; |
| 185 |
case TypeOnDisk::INT_32: _WriteBinary< int32_t >( fp , s ) ; break; |
| 186 |
case TypeOnDisk::UINT_32: _WriteBinary< uint32_t >( fp , s ) ; break; |
| 187 |
case TypeOnDisk::INT_64: _WriteBinary< int64_t >( fp , s ) ; break; |
| 188 |
case TypeOnDisk::UINT_64: _WriteBinary< uint64_t >( fp , s ) ; break; |
| 189 |
default: MK_THROW( "Unrecongized type: " , typeOnDisk ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
template< typename Real > |
| 194 |
bool VertexIO< Real >::ReadASCII( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , Real *s ) |
| 195 |
{ |
| 196 |
for( size_t i=0 ; i<sz ; i++ ) if( !ReadASCII( fp , typeOnDisk , s[i] ) ) return false; |
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
template< typename Real > |
| 201 |
bool VertexIO< Real >::ReadBinary( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , Real *s ) |
| 202 |
{ |
| 203 |
if( TypeOnDisk()==typeOnDisk ) return fread( s , sizeof(Real) , sz , fp )==sz; |
| 204 |
else for( size_t i=0 ; i<sz ; i++ ) if( !ReadBinary( fp , typeOnDisk , s[i] ) ) return false; |
| 205 |
return true; |
| 206 |
} |
| 207 |
template< typename Real > |
| 208 |
void VertexIO< Real >::WriteASCII( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , const Real *s ) |
| 209 |
{ |
| 210 |
for( size_t i=0 ; i<sz ; i++ ) WriteASCII( fp , typeOnDisk , s[i] ); |
| 211 |
} |
| 212 |
|
| 213 |
template< typename Real > |
| 214 |
void VertexIO< Real >::WriteBinary( FILE *fp , TypeOnDisk typeOnDisk , size_t sz , const Real *s ) |
| 215 |
{ |
| 216 |
if( TypeOnDisk()==typeOnDisk ) fwrite( s , sizeof(Real) , sz , fp ); |
| 217 |
else for( size_t i=0 ; i<sz ; i++ ) WriteBinary( fp , typeOnDisk , s[i] ); |
| 218 |
} |
| 219 |
|
| 220 |
///////////////////// |
| 221 |
// PositionFactory // |
| 222 |
///////////////////// |
| 223 |
template< typename Real , unsigned int Dim > |
| 224 |
PlyProperty PositionFactory< Real , Dim >::plyReadProperty( unsigned int idx ) const |
| 225 |
{ |
| 226 |
if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 227 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 228 |
} |
| 229 |
template< typename Real , unsigned int Dim > |
| 230 |
PlyProperty PositionFactory< Real , Dim >::plyWriteProperty( unsigned int idx ) const |
| 231 |
{ |
| 232 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 233 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 234 |
} |
| 235 |
|
| 236 |
template< typename Real , unsigned int Dim > |
| 237 |
PlyProperty PositionFactory< Real , Dim >::plyStaticReadProperty( unsigned int idx ) const |
| 238 |
{ |
| 239 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 240 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 241 |
} |
| 242 |
template< typename Real , unsigned int Dim > |
| 243 |
PlyProperty PositionFactory< Real , Dim >::plyStaticWriteProperty( unsigned int idx ) const |
| 244 |
{ |
| 245 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 246 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 247 |
} |
| 248 |
|
| 249 |
template< typename Real , unsigned int Dim > |
| 250 |
std::string PositionFactory< Real , Dim >::_PlyName( unsigned int idx ) |
| 251 |
{ |
| 252 |
static const std::string names[] = { "x" , "y" , "z" }; |
| 253 |
return names[idx]; |
| 254 |
} |
| 255 |
|
| 256 |
/////////////////// |
| 257 |
// NormalFactory // |
| 258 |
/////////////////// |
| 259 |
template< typename Real , unsigned int Dim > |
| 260 |
PlyProperty NormalFactory< Real , Dim >::plyReadProperty( unsigned int idx ) const |
| 261 |
{ |
| 262 |
if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 263 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 264 |
} |
| 265 |
template< typename Real , unsigned int Dim > |
| 266 |
PlyProperty NormalFactory< Real , Dim >::plyWriteProperty( unsigned int idx ) const |
| 267 |
{ |
| 268 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 269 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 270 |
} |
| 271 |
|
| 272 |
template< typename Real , unsigned int Dim > |
| 273 |
PlyProperty NormalFactory< Real , Dim >::plyStaticReadProperty( unsigned int idx ) const |
| 274 |
{ |
| 275 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 276 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 277 |
} |
| 278 |
template< typename Real , unsigned int Dim > |
| 279 |
PlyProperty NormalFactory< Real , Dim >::plyStaticWriteProperty( unsigned int idx ) const |
| 280 |
{ |
| 281 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 282 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 283 |
} |
| 284 |
|
| 285 |
template< typename Real , unsigned int Dim > |
| 286 |
std::string NormalFactory< Real , Dim >::_PlyName( unsigned int idx ) |
| 287 |
{ |
| 288 |
static const std::string names[] = { "nx" , "ny" , "nz" }; |
| 289 |
return names[idx]; |
| 290 |
} |
| 291 |
|
| 292 |
//////////////////// |
| 293 |
// TextureFactory // |
| 294 |
//////////////////// |
| 295 |
template< typename Real , unsigned int Dim > |
| 296 |
PlyProperty TextureFactory< Real , Dim >::plyReadProperty( unsigned int idx ) const |
| 297 |
{ |
| 298 |
if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 299 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 300 |
} |
| 301 |
template< typename Real , unsigned int Dim > |
| 302 |
PlyProperty TextureFactory< Real , Dim >::plyWriteProperty( unsigned int idx ) const |
| 303 |
{ |
| 304 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 305 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 306 |
} |
| 307 |
|
| 308 |
template< typename Real , unsigned int Dim > |
| 309 |
PlyProperty TextureFactory< Real , Dim >::plyStaticReadProperty( unsigned int idx ) const |
| 310 |
{ |
| 311 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 312 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 313 |
} |
| 314 |
template< typename Real , unsigned int Dim > |
| 315 |
PlyProperty TextureFactory< Real , Dim >::plyStaticWriteProperty( unsigned int idx ) const |
| 316 |
{ |
| 317 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 318 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 319 |
} |
| 320 |
|
| 321 |
template< typename Real , unsigned int Dim > |
| 322 |
std::string TextureFactory< Real , Dim >::_PlyName( unsigned int idx ) |
| 323 |
{ |
| 324 |
static const std::string names[] = { "u" , "v" , "w" }; |
| 325 |
return names[idx]; |
| 326 |
} |
| 327 |
|
| 328 |
///////////////////// |
| 329 |
// RGBColorFactory // |
| 330 |
///////////////////// |
| 331 |
template< typename Real > |
| 332 |
PlyProperty RGBColorFactory< Real >::plyReadProperty( unsigned int idx ) const |
| 333 |
{ |
| 334 |
if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 335 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 336 |
} |
| 337 |
template< typename Real > |
| 338 |
PlyProperty RGBColorFactory< Real >::plyWriteProperty( unsigned int idx ) const |
| 339 |
{ |
| 340 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 341 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 342 |
} |
| 343 |
|
| 344 |
template< typename Real > |
| 345 |
PlyProperty RGBColorFactory< Real >::plyStaticReadProperty( unsigned int idx ) const |
| 346 |
{ |
| 347 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 348 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + (idx%3)*sizeof(Real) ) ); |
| 349 |
} |
| 350 |
template< typename Real > |
| 351 |
PlyProperty RGBColorFactory< Real >::plyStaticWriteProperty( unsigned int idx ) const |
| 352 |
{ |
| 353 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 354 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 355 |
} |
| 356 |
|
| 357 |
template< typename Real > |
| 358 |
std::string RGBColorFactory< Real >::_PlyName( unsigned int idx ) |
| 359 |
{ |
| 360 |
static const std::string names[] = { "red" , "green" , "blue" , "r" , "g" , "b" }; |
| 361 |
return names[idx]; |
| 362 |
} |
| 363 |
|
| 364 |
////////////////////// |
| 365 |
// RGBAColorFactory // |
| 366 |
////////////////////// |
| 367 |
template< typename Real > |
| 368 |
PlyProperty RGBAColorFactory< Real >::plyReadProperty( unsigned int idx ) const |
| 369 |
{ |
| 370 |
if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 371 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 372 |
} |
| 373 |
|
| 374 |
template< typename Real > |
| 375 |
PlyProperty RGBAColorFactory< Real >::plyWriteProperty( unsigned int idx ) const |
| 376 |
{ |
| 377 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 378 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 379 |
} |
| 380 |
|
| 381 |
template< typename Real > |
| 382 |
PlyProperty RGBAColorFactory< Real >::plyStaticReadProperty( unsigned int idx ) const |
| 383 |
{ |
| 384 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 385 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + (idx%4)*sizeof(Real) ) ); |
| 386 |
} |
| 387 |
|
| 388 |
template< typename Real > |
| 389 |
PlyProperty RGBAColorFactory< Real >::plyStaticWriteProperty( unsigned int idx ) const |
| 390 |
{ |
| 391 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 392 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , (int)( offsetof( VertexType , coords ) + idx*sizeof(Real) ) ); |
| 393 |
} |
| 394 |
|
| 395 |
template< typename Real > |
| 396 |
std::string RGBAColorFactory< Real >::_PlyName( unsigned int idx ) |
| 397 |
{ |
| 398 |
static const std::string names[] = { "red" , "green" , "blue" , "alpha" , "r" , "g" , "b" , "a" }; |
| 399 |
return names[idx]; |
| 400 |
} |
| 401 |
|
| 402 |
////////////////// |
| 403 |
// ValueFactory // |
| 404 |
////////////////// |
| 405 |
template< typename Real > |
| 406 |
PlyProperty ValueFactory< Real >::plyReadProperty( unsigned int idx ) const |
| 407 |
{ |
| 408 |
if( idx>= plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 409 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 410 |
} |
| 411 |
|
| 412 |
template< typename Real > |
| 413 |
PlyProperty ValueFactory< Real >::plyWriteProperty( unsigned int idx ) const |
| 414 |
{ |
| 415 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 416 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 417 |
} |
| 418 |
|
| 419 |
template< typename Real > |
| 420 |
PlyProperty ValueFactory< Real >::plyStaticReadProperty( unsigned int idx ) const |
| 421 |
{ |
| 422 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 423 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , 0 ); |
| 424 |
} |
| 425 |
|
| 426 |
template< typename Real > |
| 427 |
PlyProperty ValueFactory< Real >::plyStaticWriteProperty( unsigned int idx ) const |
| 428 |
{ |
| 429 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 430 |
return PlyProperty( _PlyName(idx) , ToPlyType( _typeOnDisk ) , PLY::Type< Real >() , 0 ); |
| 431 |
} |
| 432 |
|
| 433 |
template< typename Real > |
| 434 |
std::string ValueFactory< Real >::_PlyName( unsigned int idx ) |
| 435 |
{ |
| 436 |
static const std::string names[] = { "value" }; |
| 437 |
return names[idx]; |
| 438 |
} |
| 439 |
|
| 440 |
//////////////////// |
| 441 |
// DynamicFactory // |
| 442 |
//////////////////// |
| 443 |
template< typename Real > |
| 444 |
DynamicFactory< Real >::DynamicFactory( const std::vector< std::pair< std::string , TypeOnDisk > > &namesAndTypesOnDisk ) : _namesAndTypesOnDisk(namesAndTypesOnDisk) |
| 445 |
{ |
| 446 |
_realTypeOnDisk = true; |
| 447 |
for( unsigned int i=0 ; i<_namesAndTypesOnDisk.size() ; i++ ) _realTypeOnDisk &= GetTypeOnDisk< Real>()!=_namesAndTypesOnDisk[i].second; |
| 448 |
} |
| 449 |
template< typename Real > |
| 450 |
DynamicFactory< Real >::DynamicFactory( const std::vector< PlyProperty > &plyProperties ) |
| 451 |
{ |
| 452 |
for( int i=0 ; i<plyProperties.size() ; i++ ) |
| 453 |
if( !plyProperties[i].is_list ) _namesAndTypesOnDisk.push_back( std::pair< std::string , TypeOnDisk >( plyProperties[i].name , FromPlyType( plyProperties[i].external_type ) ) ); |
| 454 |
else MK_WARN( "List property not supported: " , plyProperties[i].name ); |
| 455 |
_realTypeOnDisk = true; |
| 456 |
for( unsigned int i=0 ; i<_namesAndTypesOnDisk.size() ; i++ ) _realTypeOnDisk &= GetTypeOnDisk< Real>()!=_namesAndTypesOnDisk[i].second; |
| 457 |
} |
| 458 |
|
| 459 |
template< typename Real > |
| 460 |
bool DynamicFactory< Real >::readASCII( FILE *fp , VertexType &dt ) const |
| 461 |
{ |
| 462 |
for( unsigned int i=0 ; i<dt.dim() ; i++ ) if( !VertexIO< Real >::ReadASCII( fp , _namesAndTypesOnDisk[i].second , dt[i] ) ) return false; |
| 463 |
return true; |
| 464 |
} |
| 465 |
template< typename Real > |
| 466 |
bool DynamicFactory< Real >::readBinary( FILE *fp , VertexType &dt ) const |
| 467 |
{ |
| 468 |
if( _realTypeOnDisk ) |
| 469 |
{ |
| 470 |
if( fread( &dt[0] , sizeof(Real) , dt.dim() , fp )!=dt.dim() ) return false; |
| 471 |
} |
| 472 |
else |
| 473 |
{ |
| 474 |
for( unsigned int i=0 ; i<dt.dim() ; i++ ) if( !VertexIO< Real >::ReadBinary( fp , _namesAndTypesOnDisk[i].second , dt[i] ) ) return false; |
| 475 |
} |
| 476 |
return true; |
| 477 |
} |
| 478 |
template< typename Real > |
| 479 |
void DynamicFactory< Real >::writeASCII( FILE *fp , const VertexType &dt ) const |
| 480 |
{ |
| 481 |
for( unsigned int i=0 ; i<dt.dim() ; i++ ) VertexIO< Real >::WriteASCII( fp , _namesAndTypesOnDisk[i].second , dt[i] ); |
| 482 |
} |
| 483 |
template< typename Real > |
| 484 |
void DynamicFactory< Real >::writeBinary( FILE *fp , const VertexType &dt ) const |
| 485 |
{ |
| 486 |
if( _realTypeOnDisk ) fwrite( &dt[0] , sizeof(Real) , dt.dim() , fp ); |
| 487 |
else for( unsigned int i=0 ; i<dt.dim() ; i++ ) VertexIO< Real >::WriteBinary( fp , _namesAndTypesOnDisk[i].second , dt[i] ); |
| 488 |
} |
| 489 |
|
| 490 |
template< typename Real > |
| 491 |
PlyProperty DynamicFactory< Real >::plyReadProperty( unsigned int idx ) const |
| 492 |
{ |
| 493 |
if( idx>=plyReadNum() ) MK_THROW( "read property out of bounds" ); |
| 494 |
return PlyProperty( _namesAndTypesOnDisk[idx].first , ToPlyType( _namesAndTypesOnDisk[idx].second ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 495 |
} |
| 496 |
template< typename Real > |
| 497 |
PlyProperty DynamicFactory< Real >::plyWriteProperty( unsigned int idx ) const |
| 498 |
{ |
| 499 |
if( idx>=plyWriteNum() ) MK_THROW( "write property out of bounds" ); |
| 500 |
return PlyProperty( _namesAndTypesOnDisk[idx].first , ToPlyType( _namesAndTypesOnDisk[idx].second ) , PLY::Type< Real >() , sizeof(Real)*idx ); |
| 501 |
} |
| 502 |
|
| 503 |
template< typename Real > |
| 504 |
bool DynamicFactory< Real >::operator == ( const DynamicFactory< Real > &factory ) const |
| 505 |
{ |
| 506 |
if( size()!=factory.size() ) return false; |
| 507 |
for( int i=0 ; i<size() ; i++ ) if( _namesAndTypesOnDisk[i].first!=factory._namesAndTypesOnDisk[i].first || _namesAndTypesOnDisk[i].second!=factory._namesAndTypesOnDisk[i].second ) return false; |
| 508 |
return true; |
| 509 |
} |
| 510 |
|
| 511 |
///////////// |
| 512 |
// Factory // |
| 513 |
///////////// |
| 514 |
template< typename Real , typename ... Factories > |
| 515 |
template< unsigned int I > |
| 516 |
typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type Factory< Real , Factories ... >::_plyReadProperty( unsigned int idx , size_t offset ) const |
| 517 |
{ |
| 518 |
if( idx<this->template get<I>().plyReadNum() ) |
| 519 |
{ |
| 520 |
PlyProperty prop = this->template get<I>().plyReadProperty(idx); |
| 521 |
prop.offset += (int)offset; |
| 522 |
return prop; |
| 523 |
} |
| 524 |
else return _plyReadProperty<I+1>( idx - this->template get<I>().plyReadNum() , offset + this->template get<I>().bufferSize() ); |
| 525 |
} |
| 526 |
|
| 527 |
template< typename Real , typename ... Factories > |
| 528 |
template< unsigned int I > |
| 529 |
typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type Factory< Real , Factories ... >::_plyWriteProperty( unsigned int idx , size_t offset ) const |
| 530 |
{ |
| 531 |
if( idx<this->template get<I>().plyWriteNum() ) |
| 532 |
{ |
| 533 |
PlyProperty prop = this->template get<I>().plyWriteProperty(idx); |
| 534 |
prop.offset += (int)offset; |
| 535 |
return prop; |
| 536 |
} |
| 537 |
else return _plyWriteProperty<I+1>( idx - this->template get<I>().plyWriteNum() , offset + this->template get<I>().bufferSize() ); |
| 538 |
} |
| 539 |
|
| 540 |
template< typename Real , typename ... Factories > |
| 541 |
template< unsigned int I > |
| 542 |
typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type Factory< Real , Factories ... >::_plyStaticReadProperty( unsigned int idx ) const |
| 543 |
{ |
| 544 |
if( idx<this->template get<I>().plyReadNum() ) |
| 545 |
{ |
| 546 |
VertexType v; |
| 547 |
PlyProperty prop = this->template get<I>().plyStaticReadProperty( idx ); |
| 548 |
prop.offset += (int)( (size_t)&v.template get<I>() - (size_t)&v ); |
| 549 |
return prop; |
| 550 |
} |
| 551 |
else return _plyStaticReadProperty<I+1>( idx - this->template get<I>().plyReadNum() ); |
| 552 |
} |
| 553 |
|
| 554 |
template< typename Real , typename ... Factories > |
| 555 |
template< unsigned int I > |
| 556 |
typename std::enable_if< I!=sizeof...(Factories) , PlyProperty >::type Factory< Real , Factories ... >::_plyStaticWriteProperty( unsigned int idx ) const |
| 557 |
{ |
| 558 |
if( idx<this->template get<I>().plyWriteNum() ) |
| 559 |
{ |
| 560 |
VertexType v; |
| 561 |
PlyProperty prop = this->template get<I>().plyStaticWriteProperty( idx ); |
| 562 |
prop.offset += (int)( (size_t)&v.template get<I>() - (size_t)&v ); |
| 563 |
return prop; |
| 564 |
} |
| 565 |
else return _plyStaticWriteProperty<I+1>( idx - this->template get<I>().plyWriteNum() ) ; |
| 566 |
} |
| 567 |
} |