| 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 |
#ifndef REGULAR_GRID_INCLUDED |
| 30 |
#define REGULAR_GRID_INCLUDED |
| 31 |
|
| 32 |
#include <type_traits> |
| 33 |
#include "Array.h" |
| 34 |
#include "Geometry.h" |
| 35 |
#include "MyMiscellany.h" |
| 36 |
|
| 37 |
namespace PoissonRecon |
| 38 |
{ |
| 39 |
|
| 40 |
template< typename ... Type > struct RegularGridDataType{}; |
| 41 |
|
| 42 |
template<> |
| 43 |
struct RegularGridDataType<> |
| 44 |
{ |
| 45 |
static inline void Write( FILE *fp , unsigned int dim , std::string name ); |
| 46 |
static inline bool Read( FILE *fp , unsigned int dim , std::string name ); |
| 47 |
}; |
| 48 |
|
| 49 |
template<> struct RegularGridDataType< char >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , 1 , Name ); } ; static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , 1 , Name ); } }; |
| 50 |
inline const std::string RegularGridDataType< char >::Name = "CHAR"; |
| 51 |
template<> struct RegularGridDataType< unsigned char >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , 1 , Name ); } static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , 1 , Name ); } }; |
| 52 |
inline const std::string RegularGridDataType< unsigned char >::Name = "UNSIGNED_CHAR"; |
| 53 |
template<> struct RegularGridDataType< int >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , 1 , Name ); } static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , 1 , Name ); } }; |
| 54 |
inline const std::string RegularGridDataType< int >::Name = "INT"; |
| 55 |
template<> struct RegularGridDataType< unsigned int >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , 1 , Name ); } static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , 1 , Name ); } }; |
| 56 |
inline const std::string RegularGridDataType< unsigned int >::Name = "UNSIGNED_INT"; |
| 57 |
template<> struct RegularGridDataType< float >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , 1 , Name ); } static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , 1 , Name ); } }; |
| 58 |
inline const std::string RegularGridDataType< float >::Name = "FLOAT"; |
| 59 |
template<> struct RegularGridDataType< double >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , 1 , Name ); } static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , 1 , Name ); } }; |
| 60 |
inline const std::string RegularGridDataType< double >::Name = "DOUBLE"; |
| 61 |
|
| 62 |
template< typename Real , unsigned int Dim > struct RegularGridDataType< Point< Real , Dim > >{ static const std::string Name ; static void Write( FILE *fp ){ RegularGridDataType<>::Write( fp , Dim , Name ); } static bool Read( FILE *fp ){ return RegularGridDataType<>::Read( fp , Dim , Name ); } }; |
| 63 |
template< typename Real , unsigned int Dim > const std::string RegularGridDataType< Point< Real , Dim > >::Name = RegularGridDataType< Real >::Name; |
| 64 |
|
| 65 |
|
| 66 |
template< typename DataType , unsigned int Dim > |
| 67 |
struct RegularGrid |
| 68 |
{ |
| 69 |
static bool ReadHeader( std::string fileName , unsigned int &dim , std::string &type ); |
| 70 |
|
| 71 |
RegularGrid( void ) : _values( NullPointer( DataType ) ){ for( unsigned int d=0 ; d<Dim ; d++ ) _res[d] = 0; } |
| 72 |
~RegularGrid( void ){ DeletePointer( _values ); } |
| 73 |
|
| 74 |
template< typename Real > static void Read( std::string fileName , unsigned int res[Dim] , Pointer( DataType ) &values , XForm< Real , Dim+1 > &gridToModel ); |
| 75 |
template< typename Real > static void Write( std::string fileName , const unsigned int res[Dim] , ConstPointer( DataType ) values , XForm< Real , Dim+1 > gridToModel ); |
| 76 |
|
| 77 |
template< typename Real > void read( std::string fileName , XForm< Real , Dim+1 > &gridToModel ); |
| 78 |
template< typename Real > void write( std::string fileName , XForm< Real , Dim+1 > gridToModel ) const; |
| 79 |
|
| 80 |
Pointer( DataType ) operator()( void ){ return _values; } |
| 81 |
ConstPointer( DataType ) operator()( void ) const { return _values; } |
| 82 |
|
| 83 |
DataType &operator[]( size_t idx ){ return _values[idx]; } |
| 84 |
const DataType &operator[]( size_t idx ) const { return _values[idx]; } |
| 85 |
|
| 86 |
// Take the Dim coordinates and transform them into an index |
| 87 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value , size_t >::type index( const Int coords[] ) const { return _index( coords , 0 ); } |
| 88 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value , size_t >::type index( Int coords[] ) const { return _index( coords , 0 ); } |
| 89 |
template< typename Int , typename ... Ints > typename std::enable_if< std::is_integral< Int >::value , size_t >::type index( Int coord , Ints ... coords ) const { static_assert( sizeof...(coords)+1==Dim , "[ERROR] number of coordinates does not match the number of dimensions" ) ; const Int c[] = { coord , coords ... } ; return index( c ); } |
| 90 |
|
| 91 |
// Take the index and transform it into the Dim coordinate |
| 92 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value >::type setIndex( size_t idx , Int coords[] ) const { _setIndex( idx , coords ); } |
| 93 |
|
| 94 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value >::type resize( const Int res[] ); |
| 95 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value >::type resize( Int res[] ); |
| 96 |
template< typename Int , typename ... Ints > typename std::enable_if< std::is_integral< Int >::value >::type resize( Int res , Ints ... ress ); |
| 97 |
|
| 98 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value , DataType & >::type operator()( const Int coords[] ) { return operator[]( index( coords ) ); } |
| 99 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value , DataType & >::type operator()( Int coords[] ) { return operator[]( index( coords ) ); } |
| 100 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value , const DataType & >::type operator()( const Int coords[] ) const { return operator[]( index( coords ) ); } |
| 101 |
template< typename Int > typename std::enable_if< std::is_integral< Int >::value , const DataType & >::type operator()( Int coords[] ) const { return operator[]( index( coords ) ); } |
| 102 |
template< typename Int , typename ... Ints > typename std::enable_if< std::is_integral< Int >::value , DataType & >::type operator()( Int coord , Ints ... coords ) { static_assert( sizeof...(coords)+1==Dim , "[ERROR] number of coordinates does not match the number of dimensions" ) ; const Int c[] = { coord , coords ... } ; return operator()( c ); } |
| 103 |
template< typename Int , typename ... Ints > typename std::enable_if< std::is_integral< Int >::value , const DataType & >::type operator()( Int coord , Ints ... coords ) const { static_assert( sizeof...(coords)+1==Dim , "[ERROR] number of coordinates does not match the number of dimensions" ) ; const Int c[] = { coord , coords ... } ; return operator()( c ); } |
| 104 |
|
| 105 |
template< typename Real > typename std::enable_if< !std::is_integral< Real >::value , ProjectiveData< Real , DataType > >::type operator()( Real coords[] ) { return _Sample( _res , coords , _values ); } |
| 106 |
template< typename Real > typename std::enable_if< !std::is_integral< Real >::value , ProjectiveData< Real , DataType > >::type operator()( const Real coords[] ) { return _Sample( _res , coords , _values ); } |
| 107 |
template< typename Real > typename std::enable_if< !std::is_integral< Real >::value , ProjectiveData< Real , DataType > >::type operator()( Real coords[] ) const { return _Sample( _res , coords , _values ); } |
| 108 |
template< typename Real > typename std::enable_if< !std::is_integral< Real >::value , ProjectiveData< Real , DataType > >::type operator()( const Real coords[] ) const { return _Sample( _res , coords , _values ); } |
| 109 |
template< typename Real , typename ... Reals > typename std::enable_if< !std::is_integral< Real >::value , ProjectiveData< Real , DataType > >::type operator()( Real coord , Reals ... coords ) { static_assert( sizeof...(coords)+1==Dim , "[ERROR] number of coordinates does not match the number of dimensions" ) ; const Real c[] = { coord , coords ... } ; return operator()( c ); } |
| 110 |
template< typename Real , typename ... Reals > typename std::enable_if< !std::is_integral< Real >::value , ProjectiveData< Real , DataType > >::type operator()( Real coord , Reals ... coords ) const { static_assert( sizeof...(coords)+1==Dim , "[ERROR] number of coordinates does not match the number of dimensions" ) ; const Real c[] = { coord , coords ... } ; return operator()( c ); } |
| 111 |
template< typename Real > ProjectiveData< Real , DataType > operator()( Point< Real , Dim > coords ) { return operator()( &coords[0] ); } |
| 112 |
template< typename Real > ProjectiveData< Real , DataType > operator()( Point< Real , Dim > coords ) const { return operator()( &coords[0] ); } |
| 113 |
|
| 114 |
template< typename Int > typename std::enable_if< std::is_integral< int >::value , bool >::type inBounds( const Int coords[] ) const { return _inBounds< Dim-1 >( coords ); } |
| 115 |
template< typename Int > typename std::enable_if< std::is_integral< int >::value , bool >::type inBounds( Int coords[] ) const { return _inBounds< Dim-1 >( coords ); } |
| 116 |
template< typename Int , typename ... Ints > typename std::enable_if< std::is_integral< int >::value , bool >::type inBounds( Ints ... coords ) const { static_assert( sizeof...(coords)+1==Dim , "[ERROR] number of coordinates does not match the number of dimensions" ) ; const Int c[] = { coords ... } ; return inBounds( c ); } |
| 117 |
|
| 118 |
const unsigned int *res( void ) const { return _res; } |
| 119 |
unsigned int res( unsigned int d ) const { return _res[d]; } |
| 120 |
size_t resolution( void ) const { return _Resolution< Dim >( _res ); } |
| 121 |
|
| 122 |
protected: |
| 123 |
template< unsigned int D=Dim > static typename std::enable_if< D==1 , size_t >::type _Resolution( const unsigned int res[] ) { return res[0]; } |
| 124 |
template< unsigned int D=Dim > static typename std::enable_if< D!=1 , size_t >::type _Resolution( const unsigned int res[] ) { return res[D-1] * _Resolution<D-1>(res); } |
| 125 |
|
| 126 |
template< typename Real , unsigned int D=Dim > static typename std::enable_if< D==1 , ProjectiveData< Real , DataType > >::type _Sample( const unsigned int res[] , const Real coords[] , ConstPointer( DataType ) values ); |
| 127 |
template< typename Real , unsigned int D=Dim > static typename std::enable_if< D!=1 , ProjectiveData< Real , DataType > >::type _Sample( const unsigned int res[] , const Real coords[] , ConstPointer( DataType ) values ); |
| 128 |
|
| 129 |
// _index( x[0] , ... , x[D-1] ) = x[0] + ... + x[D-1] * ( res[0] * ... * res[D-1] ) |
| 130 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==Dim-1 && D!=0 , size_t >::type _index( Int coords[] , size_t idx ) const { return _index< Int , D-1 >( coords , coords[D] ); } |
| 131 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=Dim-1 && D!=0 , size_t >::type _index( Int coords[] , size_t idx ) const { return _index< Int , D-1 >( coords , coords[D] + idx*_res[D] ); } |
| 132 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=Dim-1 && D==0 , size_t >::type _index( Int coords[] , size_t idx ) const { return coords[D] + idx*_res[D] ; } |
| 133 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==Dim-1 && D==0 , size_t >::type _index( Int coords[] , size_t idx ) const { return coords[D] ; } |
| 134 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==Dim-1 && D!=0 , size_t >::type _index( const Int coords[] , size_t idx ) const { return _index< Int , D-1 >( coords , coords[D] ); } |
| 135 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=Dim-1 && D!=0 , size_t >::type _index( const Int coords[] , size_t idx ) const { return _index< Int , D-1 >( coords , coords[D] + idx*_res[D] ); } |
| 136 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=Dim-1 && D==0 , size_t >::type _index( const Int coords[] , size_t idx ) const { return coords[D] + idx*_res[D] ; } |
| 137 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==Dim-1 && D==0 , size_t >::type _index( const Int coords[] , size_t idx ) const { return coords[D] ; } |
| 138 |
|
| 139 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=0 , size_t >::type _setIndex( size_t idx , Int coords[] ) const { idx = _setIndex< Int , D-1 >( idx , coords ) ; coords[D] = (Int)( idx % _res[D] ) ; return idx / _res[D]; } |
| 140 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==0 , size_t >::type _setIndex( size_t idx , Int coords[] ) const { coords[D] = (Int)( idx % _res[D] ) ; return idx / _res[D]; } |
| 141 |
|
| 142 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==0 , bool >::type _inBounds( Int coords[] ) const { return coords[0]>=0 && coords[0]<_res[0]; } |
| 143 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=0 , bool >::type _inBounds( Int coords[] ) const { return coords[D]>=0 && coords[D]<_res[D] && _inBounds< D-1 >( coords ); } |
| 144 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D==0 , bool >::type _inBounds( const Int coords[] ) const { return coords[0]>=0 && coords[0]<_res[0]; } |
| 145 |
template< typename Int , unsigned int D=Dim-1 > typename std::enable_if< D!=0 , bool >::type _inBounds( const Int coords[] ) const { return coords[D]>=0 && coords[D]<_res[D] && _inBounds< D-1 >( coords ); } |
| 146 |
|
| 147 |
|
| 148 |
unsigned int _res[Dim]; |
| 149 |
Pointer( DataType ) _values; |
| 150 |
}; |
| 151 |
#include "RegularGrid.inl" |
| 152 |
} |
| 153 |
|
| 154 |
#endif // REGULAR_GRID_INCLUDED |