36#include "DGtalCatch.h"
38#include <DGtal/kernel/SpaceND.h>
39#include <DGtal/kernel/domains/HyperRectDomain.h>
40#include <DGtal/kernel/domains/Linearizer.h>
56 template <
typename Domain,
int dimension>
73 const Point & extent )
79 multiplier *= extent[ k-1 ];
80 pos += multiplier * (
aPoint[ k ] - lowerBound[ k ] );
90 template <
typename Domain >
91 struct linearizer<
Domain, 1 >
97 const Point & lowerBound,
100 return aPoint[ 0 ] - lowerBound[ 0 ];
108 template <
typename Domain >
109 struct linearizer<
Domain, 2 >
115 const Point & lowerBound,
116 const Point & extent )
118 return (
aPoint[ 0 ] - lowerBound[ 0 ] ) + extent[ 0 ] *
119 (
aPoint[ 1 ] - lowerBound[ 1 ] );
127 template <
typename Domain >
128 struct linearizer<
Domain, 3 >
134 const Point & lowerBound,
135 const Point & extent )
138 Size multiplier = extent[ 0 ];
139 res += multiplier * (
aPoint[ 1 ] - lowerBound[ 1 ] );
140 multiplier *= extent[ 1 ];
141 res += multiplier * (
aPoint[ 2 ] - lowerBound[ 2 ] );
149template <
typename StorageOrder >
150struct PointConverter;
155 template <
typename TPo
int >
157 TPoint apply( TPoint
const&
aPoint )
166 template <
typename TPo
int >
168 TPoint apply( TPoint
const&
aPoint )
171 for (
auto i = 0 ; i < (int)TPoint::dimension ; ++i )
172 result[i] =
aPoint[ TPoint::dimension - i - 1 ];
178#define TEST_LINEARIZER( N , ORDER ) \
179TEST_CASE( "Testing Linearizer in dimension " #N " with " #ORDER, "[test][dim" #N "][" #ORDER "]" )\
182 typedef SpaceND<N> Space;\
183 typedef HyperRectDomain<Space> Domain;\
184 typedef Space::Point Point;\
186 typedef linearizer<Domain, N> RefLinearizer;\
187 typedef Linearizer<Domain, ORDER> NewLinearizer;\
189 typedef PointConverter<ORDER> RefConverter;\
191 std::size_t size = 1e3;\
194 for ( std::size_t i = 0 ; i < N ; ++i )\
195 lowerBound[i] = 1 + 7*i;\
197 std::size_t dim_size = std::size_t( std::pow( double(size), 1./N ) + 0.5 );\
199 for ( std::size_t i = 0; i < N ; ++i )\
200 upperBound[i] = lowerBound[i] + dim_size + i;\
202 Domain domain( lowerBound, upperBound );\
203 Point extent = upperBound - lowerBound + Point::diagonal(1);\
205 Point refLowerBound = RefConverter::apply(lowerBound);\
206 Point refExtent = RefConverter::apply(extent);\
208 SECTION( "Testing getIndex(Point, Point, Extent) syntax" )\
210 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
211 REQUIRE( RefLinearizer::apply( RefConverter::apply(*it), refLowerBound, refExtent ) == NewLinearizer::getIndex( *it, lowerBound, extent ) );\
214 SECTION( "Testing getIndex(Point, Extent) syntax" )\
216 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
217 REQUIRE( RefLinearizer::apply( RefConverter::apply(*it), refLowerBound, refExtent ) == NewLinearizer::getIndex( *it - lowerBound, extent ) );\
220 SECTION( "Testing getIndex(Point, Domain) syntax" )\
222 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
223 REQUIRE( RefLinearizer::apply( RefConverter::apply(*it), refLowerBound, refExtent ) == NewLinearizer::getIndex( *it, domain ) );\
226 SECTION( "Testing getPoint(Index, Point, Extent) syntax" )\
228 for ( std::size_t i = 0; i < domain.size(); ++i )\
229 REQUIRE( RefLinearizer::apply( RefConverter::apply( NewLinearizer::getPoint( i, lowerBound, extent ) ), refLowerBound, refExtent ) == i );\
232 SECTION( "Testing getPoint(Index, Extent) syntax" )\
234 for ( std::size_t i = 0; i < domain.size(); ++i )\
235 REQUIRE( RefLinearizer::apply( RefConverter::apply( NewLinearizer::getPoint( i, extent ) + lowerBound ), refLowerBound, refExtent ) == i );\
238 SECTION( "Testing getPoint(Index, Domain) syntax" )\
240 for ( std::size_t i = 0; i < domain.size(); ++i )\
241 REQUIRE( RefLinearizer::apply( RefConverter::apply( NewLinearizer::getPoint( i, domain ) ), refLowerBound, refExtent ) == i );\
245#define BENCH_LINEARIZER( N , ORDER ) \
246TEST_CASE( "Benchmarking Linearizer in dimension " #N " with " #ORDER, "[.bench][dim" #N "][" #ORDER "]" )\
249 typedef SpaceND<N> Space;\
250 typedef HyperRectDomain<Space> Domain;\
251 typedef Space::Point Point;\
253 typedef linearizer<Domain, N> RefLinearizer;\
254 typedef Linearizer<Domain, ORDER> NewLinearizer;\
256 typedef PointConverter<ORDER> RefConverter;\
258 std::size_t size = 1e8;\
261 for ( std::size_t i = 0 ; i < N ; ++i )\
262 lowerBound[i] = 1 + 7*i;\
264 std::size_t dim_size = std::size_t( std::pow( double(size), 1./N ) + 0.5 );\
266 for ( std::size_t i = 0; i < N ; ++i )\
267 upperBound[i] = lowerBound[i] + dim_size + i;\
269 Domain domain( lowerBound, upperBound );\
270 Point extent = upperBound - lowerBound + Point::diagonal(1);\
272 Point refLowerBound = RefConverter::apply(lowerBound);\
273 Point refExtent = RefConverter::apply(extent);\
275 std::size_t sum = 0;\
277 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
278 sum += RefLinearizer::apply( RefConverter::apply(*it), refLowerBound, refExtent );\
282 SECTION( "Benchmarking reference linearizer" )\
284 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
285 sum += RefLinearizer::apply( RefConverter::apply(*it), refLowerBound, refExtent );\
288 SECTION( "Benchmarking getIndex(Point, Point, Extent) syntax" )\
290 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
291 sum += NewLinearizer::getIndex( *it, lowerBound, extent );\
294 SECTION( "Benchmarking getIndex(Point, Extent) syntax" )\
296 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
297 sum += NewLinearizer::getIndex( *it, extent );\
300 SECTION( "Benchmarking getIndex(Point, Domain) syntax" )\
302 for ( Domain::ConstIterator it = domain.begin(), it_end = domain.end(); it != it_end ; ++it )\
303 sum += NewLinearizer::getIndex( *it, domain );\
306 SECTION( "Benchmarking getPoint(Index, Point, Extent) syntax" )\
308 for ( std::size_t i = 0; i < domain.size(); ++i )\
309 sum += NewLinearizer::getPoint( i, lowerBound, extent )[N-1];\
312 SECTION( "Benchmarking getPoint(Index, Extent) syntax" )\
314 for ( std::size_t i = 0; i < domain.size(); ++i )\
315 sum += NewLinearizer::getPoint( i, extent )[N-1];\
318 SECTION( "Benchmarking getPoint(Index, Domain) syntax" )\
320 for ( std::size_t i = 0; i < domain.size(); ++i )\
321 sum += NewLinearizer::getPoint( i, domain )[N-1];\
Space::Dimension Dimension
DGtal is the top-level namespace which contains all DGtal functions and types.
Tag (empty structure) specifying a col-major storage order.
Tag (empty structure) specifying a row-major storage order.
HalfEdgeDataStructure::Size Size
#define TEST_LINEARIZER(N, ORDER)
#define BENCH_LINEARIZER(N, ORDER)