| 1 |
/* |
| 2 |
Copyright (c) 2019, 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 |
///////////////////////// |
| 30 |
// RegularGridTypeData // |
| 31 |
///////////////////////// |
| 32 |
|
| 33 |
void RegularGridDataType<>::Write( FILE *fp , unsigned int dim , std::string name ){ fprintf( fp , "%d %s\n" , (int)dim , name.c_str() ); } |
| 34 |
bool RegularGridDataType<>::Read( FILE *fp , unsigned int dim , std::string name ) |
| 35 |
{ |
| 36 |
char line[1024]; |
| 37 |
int d; |
| 38 |
if( fscanf( fp , " %d %s " , &d , line )!=2 ) return false; |
| 39 |
return d==dim && name==std::string(line); |
| 40 |
} |
| 41 |
|
| 42 |
///////////////// |
| 43 |
// RegularGrid // |
| 44 |
///////////////// |
| 45 |
|
| 46 |
template< typename DataType , unsigned int Dim > |
| 47 |
template< typename Int > |
| 48 |
typename std::enable_if< std::is_integral< Int >::value >::type RegularGrid< DataType , Dim >::resize( Int res[] ) |
| 49 |
{ |
| 50 |
if( _values ) DeletePointer( _values ); |
| 51 |
size_t resolution = 1; |
| 52 |
for( int d=0 ; d<Dim ; d++ ) _res[d] = (unsigned int)res[d] , resolution *= (size_t)res[d]; |
| 53 |
if( resolution ) _values = NewPointer< DataType >( resolution ); |
| 54 |
} |
| 55 |
|
| 56 |
template< typename DataType , unsigned int Dim > |
| 57 |
template< typename Int > |
| 58 |
typename std::enable_if< std::is_integral< Int >::value >::type RegularGrid< DataType , Dim >::resize( const Int res[] ) |
| 59 |
{ |
| 60 |
if( _values ) DeletePointer( _values ); |
| 61 |
size_t resolution = 1; |
| 62 |
for( int d=0 ; d<Dim ; d++ ) _res[d] = (unsigned int)res[d] , resolution *= (size_t)res[d]; |
| 63 |
if( resolution ) _values = NewPointer< DataType >( resolution ); |
| 64 |
} |
| 65 |
|
| 66 |
template< typename DataType , unsigned int Dim > |
| 67 |
template< typename Int , typename ... Ints > |
| 68 |
typename std::enable_if< std::is_integral< Int >::value >::type RegularGrid< DataType , Dim >::resize( Int res , Ints ... ress ) |
| 69 |
{ |
| 70 |
static_assert( sizeof...(ress)+1==Dim , "[ERROR] number of resolutions does not match the number of dimensions" ); |
| 71 |
const Int r[] = { res , ress ... }; |
| 72 |
return resize( r ); |
| 73 |
} |
| 74 |
|
| 75 |
template< typename DataType , unsigned int Dim > |
| 76 |
template< typename Real , unsigned int D > |
| 77 |
typename std::enable_if< D==1 , ProjectiveData< Real , DataType > >::type RegularGrid< DataType , Dim >::_Sample( const unsigned int res[] , const Real coords[] , ConstPointer( DataType ) values ) |
| 78 |
{ |
| 79 |
int iCoord1 = (int)floor(coords[0]) , iCoord2 = (int)floor(coords[0])+1; |
| 80 |
Real dx1 = (Real)( iCoord2 - coords[0] ) , dx2 = (Real)( coords[0] - iCoord1 ); |
| 81 |
ProjectiveData< Real , DataType > d; |
| 82 |
if( iCoord1>=0 && iCoord1<(int)res[0] ) d += ProjectiveData< Real , DataType >( values[ iCoord1 ] * dx1 , dx1 ); |
| 83 |
if( iCoord2>=0 && iCoord2<(int)res[0] ) d += ProjectiveData< Real , DataType >( values[ iCoord2 ] * dx2 , dx2 ); |
| 84 |
return d; |
| 85 |
} |
| 86 |
|
| 87 |
template< typename DataType , unsigned int Dim > |
| 88 |
template< typename Real , unsigned int D > |
| 89 |
typename std::enable_if< D!=1 , ProjectiveData< Real , DataType > >::type RegularGrid< DataType , Dim >::_Sample( const unsigned int res[] , const Real coords[] , ConstPointer( DataType ) values ) |
| 90 |
{ |
| 91 |
int iCoord1 = (int)floor(coords[D-1]) , iCoord2 = (int)floor(coords[D-1])+1; |
| 92 |
Real dx1 = (Real)( iCoord2 - coords[D-1] ) , dx2 = (Real)( coords[D-1] - iCoord1 ); |
| 93 |
ProjectiveData< Real , DataType > d; |
| 94 |
if( iCoord1>=0 && iCoord1<(int)res[D-1] ) d += _Sample< Real , D-1 >( res , coords , values + _Resolution< D-1 >(res) * iCoord1 ) * dx1; |
| 95 |
if( iCoord2>=0 && iCoord2<(int)res[D-1] ) d += _Sample< Real , D-1 >( res , coords , values + _Resolution< D-1 >(res) * iCoord2 ) * dx2; |
| 96 |
return d; |
| 97 |
} |
| 98 |
|
| 99 |
template< typename DataType , unsigned int Dim > |
| 100 |
bool RegularGrid< DataType , Dim >::ReadHeader( std::string fileName , unsigned int &dim , std::string &name ) |
| 101 |
{ |
| 102 |
FILE *fp = fopen( fileName.c_str() , "rb" ); |
| 103 |
if( !fp ) return false; |
| 104 |
else |
| 105 |
{ |
| 106 |
// Write the magic number |
| 107 |
int d; |
| 108 |
if( fscanf( fp , " G%d " , &d )!=1 || d!=Dim ){ fclose(fp) ; return false; } |
| 109 |
|
| 110 |
char line[1024]; |
| 111 |
if( fscanf( fp , " %d %s " , &d , line )!=2 ){ fclose(fp) ; return false; } |
| 112 |
dim = d , name =std::string( line ); |
| 113 |
fclose( fp ); |
| 114 |
} |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
template< typename DataType , unsigned int Dim > |
| 119 |
template< typename Real > |
| 120 |
void RegularGrid< DataType , Dim >::Write( std::string fileName , const unsigned int res[Dim] , ConstPointer( DataType ) values , XForm< Real , Dim+1 > gridToModel ) |
| 121 |
{ |
| 122 |
FILE *fp = fopen( fileName.c_str() , "wb" ); |
| 123 |
if( !fp ) MK_THROW( "Failed to open grid file for writing: " , fileName ); |
| 124 |
else |
| 125 |
{ |
| 126 |
// Write the magic number |
| 127 |
fprintf( fp , "G%d\n" , (int)Dim ); |
| 128 |
|
| 129 |
RegularGridDataType< DataType >::Write( fp ); |
| 130 |
|
| 131 |
// Write the dimensions |
| 132 |
for( int d=0 ; d<Dim ; d++ ) |
| 133 |
{ |
| 134 |
fprintf( fp , "%d" , (int)res[d] ); |
| 135 |
if( d==Dim-1 ) fprintf( fp , "\n" ); |
| 136 |
else fprintf( fp , " " ); |
| 137 |
} |
| 138 |
|
| 139 |
// Write the transformation |
| 140 |
for( int j=0 ; j<Dim+1 ; j++ ) for( int i=0 ; i<Dim+1 ; i++ ) |
| 141 |
{ |
| 142 |
fprintf( fp , "%f" , (float)gridToModel(i,j) ); |
| 143 |
if( i==Dim ) fprintf( fp , "\n" ); |
| 144 |
else fprintf( fp , " " ); |
| 145 |
} |
| 146 |
|
| 147 |
// Write the grid values |
| 148 |
fwrite( values , sizeof(DataType) , _Resolution(res) , fp ); |
| 149 |
fclose( fp ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
template< typename DataType , unsigned int Dim > |
| 155 |
template< typename Real > |
| 156 |
void RegularGrid< DataType , Dim >::write( std::string fileName , XForm< Real , Dim+1 > gridToModel ) const |
| 157 |
{ |
| 158 |
Write( fileName , _res , _values , gridToModel ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
template< typename DataType , unsigned int Dim > |
| 163 |
template< typename Real > |
| 164 |
void RegularGrid< DataType , Dim >::Read( std::string fileName , unsigned int res[Dim] , Pointer( DataType ) &values , XForm< Real , Dim+1 > &gridToModel ) |
| 165 |
{ |
| 166 |
FILE *fp = fopen( fileName.c_str() , "rb" ); |
| 167 |
if( !fp ) MK_THROW( "Failed to open grid file for reading: " , fileName ); |
| 168 |
else |
| 169 |
{ |
| 170 |
// Read the magic number |
| 171 |
{ |
| 172 |
int dim; |
| 173 |
if( fscanf( fp , " G%d " , &dim )!=1 ) MK_THROW( "Failed to read magic number: " , fileName ); |
| 174 |
if( dim!=Dim ) MK_THROW( "Dimensions don't match: " , Dim , " != " , dim ); |
| 175 |
} |
| 176 |
|
| 177 |
// Read the data type |
| 178 |
if( !RegularGridDataType< DataType >::Read( fp ) ) MK_THROW( "Failed to read type" ); |
| 179 |
|
| 180 |
// Read the dimensions |
| 181 |
{ |
| 182 |
int r; |
| 183 |
for( int d=0 ; d<Dim ; d++ ) |
| 184 |
{ |
| 185 |
if( fscanf( fp , " %d " , &r )!=1 ) MK_THROW( "Failed to read dimension[ " , d , " ]" ); |
| 186 |
res[d] = r; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// Read the transformation |
| 191 |
{ |
| 192 |
float x; |
| 193 |
for( int j=0 ; j<Dim+1 ; j++ ) for( int i=0 ; i<Dim+1 ; i++ ) |
| 194 |
{ |
| 195 |
if( fscanf( fp , " %f" , &x )!=1 ) MK_THROW( "Failed to read xForm( " , i , " , " , j , " )" ); |
| 196 |
gridToModel(i,j) = x; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Read through the end of the line |
| 201 |
{ |
| 202 |
char line[1024]; |
| 203 |
if( !fgets( line , sizeof(line)/sizeof(char) , fp ) ) MK_THROW( "Could not read end of line" ); |
| 204 |
} |
| 205 |
|
| 206 |
values = NewPointer< DataType >( _Resolution(res) ); |
| 207 |
|
| 208 |
// Write the grid values |
| 209 |
fread( values , sizeof(DataType) , _Resolution(res) , fp ); |
| 210 |
fclose( fp ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
template< typename DataType , unsigned int Dim > |
| 216 |
template< typename Real > |
| 217 |
void RegularGrid< DataType , Dim >::read( std::string fileName , XForm< Real , Dim+1 > &gridToModel ) |
| 218 |
{ |
| 219 |
Read( fileName , _res , _values , gridToModel ); |
| 220 |
} |