| 1 |
/* |
| 2 |
Copyright (c) 2016, 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 WINDOW_INCLUDED |
| 30 |
#define WINDOW_INCLUDED |
| 31 |
|
| 32 |
#include <functional> |
| 33 |
#include "Array.h" |
| 34 |
#include "ParameterPack.h" |
| 35 |
#include "MultiThreading.h" |
| 36 |
|
| 37 |
namespace Window |
| 38 |
{ |
| 39 |
using namespace PoissonRecon; |
| 40 |
template< unsigned int Res , unsigned int ... Ress > |
| 41 |
constexpr unsigned int Size( void ) |
| 42 |
{ |
| 43 |
if constexpr( sizeof...(Ress)==0 ) return Res; |
| 44 |
else return Res * Size< Ress... >(); |
| 45 |
} |
| 46 |
|
| 47 |
template< unsigned int Dim , unsigned int Res > |
| 48 |
constexpr unsigned int IsotropicSize( void ) |
| 49 |
{ |
| 50 |
if constexpr( Dim==1 ) return Res; |
| 51 |
else return Res * IsotropicSize< Dim-1 , Res >(); |
| 52 |
} |
| 53 |
|
| 54 |
template< unsigned int Res , unsigned int ... Ress > |
| 55 |
struct Index |
| 56 |
{ |
| 57 |
template< unsigned int Idx , unsigned int ... Idxs > |
| 58 |
static constexpr unsigned int I( void ) |
| 59 |
{ |
| 60 |
static_assert( sizeof...(Ress)==sizeof...(Idxs) , "[ERROR] sizes don't match" ); |
| 61 |
if constexpr( sizeof...(Ress)==0 ) return Idx; |
| 62 |
else return Idx * Size< Ress ... >() + Index< Ress... >::template I< Idxs... >(); |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
template< unsigned int Dim , unsigned int Res > |
| 67 |
struct IsotropicIndex |
| 68 |
{ |
| 69 |
template< unsigned int Idx > |
| 70 |
static constexpr unsigned int I( void ) |
| 71 |
{ |
| 72 |
if constexpr( Dim==1 ) return Idx; |
| 73 |
else return Idx * IsotropicSize< Dim-1 , Res >() + IsotropicIndex< Dim-1 , Res >::template I< Idx >(); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
template< unsigned int Res , unsigned int ... Ress > |
| 78 |
unsigned int GetIndex( const unsigned int idx[] ) |
| 79 |
{ |
| 80 |
if constexpr( sizeof...(Ress)==0 ) return idx[0]; |
| 81 |
else return idx[0] * Size< Ress ... >() + GetIndex< Ress... >( idx+1 ); |
| 82 |
} |
| 83 |
|
| 84 |
template< unsigned int Res , unsigned int ... Ress > |
| 85 |
unsigned int GetIndex( const int idx[] ) |
| 86 |
{ |
| 87 |
if constexpr( sizeof...(Ress)==0 ) return idx[0]; |
| 88 |
else return idx[0] * Size< Ress ... >() + GetIndex< Ress... >( idx+1 ); |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
template< class Data , unsigned int ... Res > struct ConstSlice{}; |
| 93 |
|
| 94 |
template< class Data , unsigned int Res , unsigned int ... Ress > |
| 95 |
struct ConstSlice< Data , Res , Ress... > |
| 96 |
{ |
| 97 |
using data_type = Data; |
| 98 |
using data_reference_type = const Data &; |
| 99 |
using const_data_reference_type = const Data &; |
| 100 |
static constexpr unsigned int Size( void ){ return Window::Size< Res , Ress... >(); } |
| 101 |
|
| 102 |
ConstSlice( Pointer( Data ) d ) : data(d) {} |
| 103 |
ConstSlice( ConstPointer( Data ) d ) : data(d) {} |
| 104 |
|
| 105 |
std::conditional_t< sizeof...(Ress)==0 , data_reference_type , ConstSlice< Data , Ress... > > operator[]( int idx ) const |
| 106 |
{ |
| 107 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 108 |
else return ConstSlice< Data , Ress... >( data + Window::Size< Ress... >() * idx ); |
| 109 |
} |
| 110 |
data_reference_type operator()( const int idx[] ) const { return data[ GetIndex< Res , Ress... >( idx ) ]; } |
| 111 |
data_reference_type operator()( const unsigned int idx[] ) const { return data[ GetIndex< Res , Ress... >( idx ) ]; } |
| 112 |
ConstPointer( Data ) data; |
| 113 |
}; |
| 114 |
|
| 115 |
template< typename Data , unsigned Dim , unsigned int Res , unsigned int ... Ress > |
| 116 |
struct _IsotropicConstSlice{ using Type = std::conditional_t< Dim==1 , ConstSlice< Data , Res , Ress... > , _IsotropicConstSlice< Data , Dim-1 , Res , Res , Ress... > >; }; |
| 117 |
template< typename Data , unsigned int Dim , unsigned int Res > |
| 118 |
using IsotropicConstSlice = typename _IsotropicConstSlice< Data , Dim , Res >::Type; |
| 119 |
|
| 120 |
|
| 121 |
template< class Data , unsigned int ... Res > struct Slice{}; |
| 122 |
|
| 123 |
template< class Data , unsigned int Res , unsigned int ... Ress > |
| 124 |
struct Slice< Data , Res , Ress... > |
| 125 |
{ |
| 126 |
using data_type = Data; |
| 127 |
using data_reference_type = Data &; |
| 128 |
using const_data_reference_type = const Data &; |
| 129 |
static constexpr unsigned int Size( void ){ return Window::Size< Res , Ress... >(); } |
| 130 |
|
| 131 |
Slice( Pointer( Data ) d ) : data(d) {} |
| 132 |
std::conditional_t< sizeof...(Ress)==0 , data_reference_type , Slice< Data , Ress... > > operator[]( int idx ) |
| 133 |
{ |
| 134 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 135 |
else return Slice< Data , Ress... >( data + Window::Size< Ress... >() * idx ); |
| 136 |
} |
| 137 |
std::conditional_t< sizeof...(Ress)==0 , const_data_reference_type , ConstSlice< Data , Ress... > > operator[]( int idx ) const |
| 138 |
{ |
| 139 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 140 |
else return ConstSlice< Data , Ress... >( data + Window::Size< Ress... >() * idx ); |
| 141 |
} |
| 142 |
data_reference_type operator()( const int idx[] ) |
| 143 |
{ |
| 144 |
if constexpr( sizeof...(Ress)==0 ) return operator[]( idx[0] ); |
| 145 |
else return operator[]( idx[0] )( idx+1 ); |
| 146 |
} |
| 147 |
const_data_reference_type operator()( const int idx[] ) const |
| 148 |
{ |
| 149 |
if constexpr( sizeof...(Ress)==0 ) return operator[]( idx[0] ); |
| 150 |
else return operator[]( idx[0] )( idx+1 ); |
| 151 |
} |
| 152 |
data_reference_type operator()( const unsigned int idx[] ) |
| 153 |
{ |
| 154 |
if constexpr( sizeof...(Ress)==0 ) return operator[]( idx[0] ); |
| 155 |
else return operator[]( idx[0] )( idx+1 ); |
| 156 |
} |
| 157 |
const_data_reference_type operator()( const unsigned int idx[] ) const |
| 158 |
{ |
| 159 |
if constexpr( sizeof...(Ress)==0 ) return operator[]( idx[0] ); |
| 160 |
else return operator[]( idx[0] )( idx+1 ); |
| 161 |
} |
| 162 |
operator ConstSlice< Data , Res , Ress... >() const { return ConstSlice< Data , Res , Ress... >( ( ConstPointer( Data ) )data ); } |
| 163 |
Pointer( Data ) data; |
| 164 |
}; |
| 165 |
|
| 166 |
template< typename Data , unsigned Dim , unsigned int Res , unsigned int ... Ress > |
| 167 |
struct _IsotropicSlice{ using Type = std::conditional_t< Dim==1 , Slice< Data , Res , Ress... > , _IsotropicSlice< Data , Dim-1 , Res , Res , Ress... > >; }; |
| 168 |
template< typename Data , unsigned int Dim , unsigned int Res > |
| 169 |
using IsotropicSlice = typename _IsotropicSlice< Data , Dim , Res >::Type; |
| 170 |
|
| 171 |
|
| 172 |
template< class Data , unsigned int Res , unsigned int ... Ress > |
| 173 |
struct StaticWindow |
| 174 |
{ |
| 175 |
using const_window_slice_type = ConstSlice< Data , Res , Ress... >; |
| 176 |
using window_slice_type = Slice< Data , Res , Ress... >; |
| 177 |
using data_type = Data ; |
| 178 |
static constexpr unsigned int Size( void ){ return Window::Size< Res , Ress... >(); } |
| 179 |
|
| 180 |
std::conditional_t< sizeof...(Ress)==0 , Data & , Slice< Data , Ress... > > operator[]( int idx ) |
| 181 |
{ |
| 182 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 183 |
else return Slice< Data , Ress... >( GetPointer( data , Size() ) + Window::Size< Ress... >() * idx ); |
| 184 |
} |
| 185 |
|
| 186 |
std::conditional_t< sizeof...(Ress)==0 , const Data & , ConstSlice< Data , Ress... > > operator[]( int idx ) const |
| 187 |
{ |
| 188 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 189 |
else return ConstSlice< Data , Ress... >( ( ConstPointer( Data ) )GetPointer( data , Size() ) + Window::Size< Ress... >() * idx ); |
| 190 |
} |
| 191 |
|
| 192 |
Slice< Data , Res , Ress... > operator()( void ){ return Slice< Data , Res , Ress... >( GetPointer( data , Size() ) ); } |
| 193 |
|
| 194 |
ConstSlice< Data , Res , Ress... > operator()( void ) const { return ConstSlice< Data , Res , Ress... >( ( ConstPointer( Data ) )GetPointer( data , Size() ) ); } |
| 195 |
|
| 196 |
Data& operator()( const unsigned int idx[] ){ return (*this)()( idx ); } |
| 197 |
Data& operator()( const int idx[] ){ return (*this)()( idx ); } |
| 198 |
|
| 199 |
const Data& operator()( const unsigned int idx[] ) const { return data[ GetIndex< Res , Ress... >( idx ) ]; } |
| 200 |
const Data& operator()( const int idx[] ) const { return data[ GetIndex< Res , Ress... >( idx ) ]; } |
| 201 |
|
| 202 |
Data data[ Window::Size< Res , Ress... >() ]; |
| 203 |
}; |
| 204 |
|
| 205 |
template< typename Data , unsigned Dim , unsigned int Res , unsigned int ... Ress > |
| 206 |
struct _IsotropicStaticWindow{ using Type = std::conditional_t< Dim==1 , StaticWindow< Data , Res , Ress... > , _IsotropicStaticWindow< Data , Dim-1 , Res , Res , Ress... > >; }; |
| 207 |
template< typename Data , unsigned int Dim , unsigned int Res > |
| 208 |
using IsotropicStaticWindow = typename _IsotropicStaticWindow< Data , Dim , Res >::Type; |
| 209 |
|
| 210 |
|
| 211 |
template< class Data , unsigned int Res , unsigned int ... Ress > |
| 212 |
struct DynamicWindow |
| 213 |
{ |
| 214 |
using const_window_slice_type = ConstSlice< Data , Res , Ress... >; |
| 215 |
using window_slice_type = Slice< Data , Res , Ress... >; |
| 216 |
using data_type = Data; |
| 217 |
static constexpr unsigned int Size( void ){ return Window::Size< Res , Ress... >(); } |
| 218 |
|
| 219 |
std::conditional_t< sizeof...(Ress)==0 , Data & , Slice< Data , Ress... > > operator[]( int idx ) |
| 220 |
{ |
| 221 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 222 |
else return Slice< Data , Ress... >( data + Window::Size< Ress... >() * idx ); |
| 223 |
} |
| 224 |
|
| 225 |
std::conditional_t< sizeof...(Ress)==0 , const Data & , ConstSlice< Data , Ress... > > operator[]( int idx ) const |
| 226 |
{ |
| 227 |
if constexpr( sizeof...(Ress)==0 ) return data[idx]; |
| 228 |
else ConstSlice< Data , Ress... >( ( ConstPointer( Data ) )( data + Window::Size< Ress... >() * idx ) ); |
| 229 |
} |
| 230 |
|
| 231 |
Slice< Data , Res , Ress... > operator()( void ){ return Slice< Data , Res , Ress... >( data ); } |
| 232 |
ConstSlice< Data , Res , Ress... > operator()( void ) const { return ConstSlice< Data , Res , Ress... >( ( ConstPointer( Data ) )data ); } |
| 233 |
|
| 234 |
Data& operator()( const int idx[] ){ return (*this)()( idx ); } |
| 235 |
const Data& operator()( const int idx[] ) const { return (*this)()( idx ); } |
| 236 |
|
| 237 |
DynamicWindow( void ){ data = NewPointer< Data >( Size() ); } |
| 238 |
|
| 239 |
~DynamicWindow( void ){ DeletePointer( data ); } |
| 240 |
|
| 241 |
Pointer( Data ) data; |
| 242 |
}; |
| 243 |
|
| 244 |
template< typename Data , unsigned Dim , unsigned int Res , unsigned int ... Ress > |
| 245 |
struct _IsotropicDynamicWindow{ using Type = std::conditional_t< Dim==1 , DynamicWindow< Data , Res , Ress... > , _IsotropicDynamicWindow< Data , Dim-1 , Res , Res , Ress... > >; }; |
| 246 |
template< typename Data , unsigned int Dim , unsigned int Res > |
| 247 |
using IsotropicDynamicWindow = typename _IsotropicDynamicWindow< Data , Dim , Res >::Type; |
| 248 |
|
| 249 |
// Recursive loop iterations for processing window slices |
| 250 |
// WindowDimension: the the window slice |
| 251 |
// IterationDimensions: the number of dimensions to process |
| 252 |
// Res: the resolution of the window |
| 253 |
|
| 254 |
template< unsigned int WindowDimension , unsigned int IterationDimensions , unsigned int CurrentIteration > struct _Loop; |
| 255 |
|
| 256 |
template< unsigned int WindowDimension , unsigned int IterationDimensions=WindowDimension > |
| 257 |
struct Loop |
| 258 |
{ |
| 259 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 260 |
static void Run( int begin , int end , UpdateFunction updateState , ProcessFunction function , Windows ... w ) |
| 261 |
{ |
| 262 |
_Loop< WindowDimension , IterationDimensions , IterationDimensions >::Run( begin , end , updateState , function , w ... ); |
| 263 |
} |
| 264 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 265 |
static void Run( const int* begin , const int* end , UpdateFunction updateState , ProcessFunction function , Windows ... w ) |
| 266 |
{ |
| 267 |
_Loop< WindowDimension , IterationDimensions , IterationDimensions >::Run( begin , end , updateState , function , w ... ); |
| 268 |
} |
| 269 |
template< unsigned int ... Begin , unsigned int ... End , typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 270 |
static void Run( ParameterPack::UIntPack< Begin ... > begin , ParameterPack::UIntPack< End ... > end , UpdateFunction updateState , ProcessFunction function , Windows ... w ) |
| 271 |
{ |
| 272 |
_Loop< WindowDimension , IterationDimensions , IterationDimensions >::Run( begin , end , updateState , function , w ... ); |
| 273 |
} |
| 274 |
|
| 275 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 276 |
static void RunParallel( int begin , int end , UpdateFunction updateState , ProcessFunction function , Windows ... w ) |
| 277 |
{ |
| 278 |
_Loop< WindowDimension , IterationDimensions , IterationDimensions >::RunParallel( begin , end , updateState , function , w ... ); |
| 279 |
} |
| 280 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 281 |
static void RunParallel( const int* begin , const int* end , UpdateFunction updateState , ProcessFunction function , Windows ... w ) |
| 282 |
{ |
| 283 |
_Loop< WindowDimension , IterationDimensions , IterationDimensions >::RunParallel( begin , end , updateState , function , w ... ); |
| 284 |
} |
| 285 |
template< unsigned int ... Begin , unsigned int ... End , typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 286 |
static void RunParallel( ParameterPack::UIntPack< Begin ... > begin , ParameterPack::UIntPack< End ... > end , UpdateFunction updateState , ProcessFunction function , Windows ... w ) |
| 287 |
{ |
| 288 |
_Loop< WindowDimension , IterationDimensions , IterationDimensions >::RunParallel( begin , end , updateState , function , w ... ); |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
#include "Window.inl" |
| 293 |
} |
| 294 |
// Adding definitions to be consistent with old code |
| 295 |
namespace PoissonRecon |
| 296 |
{ |
| 297 |
template< typename Pack1 , typename Pack2 > struct WindowIndex; |
| 298 |
template< unsigned int ... Res , unsigned int ... Idx > |
| 299 |
struct WindowIndex< ParameterPack::UIntPack< Res... > , ParameterPack::UIntPack< Idx... > >{ static const unsigned int Index = Window::Index< Res... >::template I< Idx ... >(); }; |
| 300 |
|
| 301 |
template< int ... Values > |
| 302 |
using IntPack = ParameterPack::IntPack< Values... >; |
| 303 |
|
| 304 |
template< unsigned int ... Values > |
| 305 |
using UIntPack = ParameterPack::UIntPack< Values... >; |
| 306 |
|
| 307 |
template< unsigned int Dim , unsigned int Res > |
| 308 |
using IsotropicUIntPack = ParameterPack::IsotropicPack< unsigned int , Dim , Res >; |
| 309 |
|
| 310 |
template< unsigned int Dim > |
| 311 |
using ZeroUIntPack = IsotropicUIntPack< Dim , 0 >; |
| 312 |
|
| 313 |
template< typename Data , typename Pack > struct _WindowSlice; |
| 314 |
template< typename Data , unsigned int ... Res > struct _WindowSlice< Data , ParameterPack::UIntPack< Res... > >{ using type = Window::Slice< Data , Res... >; }; |
| 315 |
template< typename Data , typename Pack > using WindowSlice = typename _WindowSlice< Data , Pack >::type; |
| 316 |
|
| 317 |
template< typename Data , typename Pack > struct _ConstWindowSlice; |
| 318 |
template< typename Data , unsigned int ... Res > struct _ConstWindowSlice< Data , ParameterPack::UIntPack< Res... > >{ using type = Window::ConstSlice< Data , Res... >; }; |
| 319 |
template< typename Data , typename Pack > using ConstWindowSlice = typename _ConstWindowSlice< Data , Pack >::type; |
| 320 |
|
| 321 |
template< typename Data , typename Pack > struct StaticWindow; |
| 322 |
template< typename Data , unsigned int ... Res > |
| 323 |
struct StaticWindow< Data , ParameterPack::UIntPack< Res... > > : public Window::StaticWindow< Data , Res... >{}; |
| 324 |
|
| 325 |
template< typename Data , typename Pack > struct DynamicWindow; |
| 326 |
template< typename Data , unsigned int ... Res > |
| 327 |
struct DynamicWindow< Data , ParameterPack::UIntPack< Res... > > : public Window::DynamicWindow< Data , Res... >{}; |
| 328 |
|
| 329 |
template< typename Pack > struct WindowSize; |
| 330 |
template< unsigned int ... Res > struct WindowSize< ParameterPack::UIntPack< Res... > >{ static const unsigned int Size = Window::Size< Res... >(); }; |
| 331 |
|
| 332 |
template< unsigned ... Res > |
| 333 |
unsigned int GetWindowIndex( UIntPack< Res... > , const unsigned int idx[] ){ return Window::GetIndex< Res... >( idx ); } |
| 334 |
|
| 335 |
template< unsigned int WindowDimension > |
| 336 |
using WindowLoop = Window::Loop< WindowDimension >; |
| 337 |
} |
| 338 |
#endif // WINDOW_INCLUDED |