36#ifndef VIGRA_OVERLAPPED_BLOCKS_HXX
37#define VIGRA_OVERLAPPED_BLOCKS_HXX
42#include <vigra/multi_array.hxx>
43#include <vigra/multi_array_chunked.hxx>
48namespace overlapped_blocks_detail
52std::pair<Shape, Shape> blockBoundsAt(
const Shape& coordinates,
const Shape& global_shape,
const Shape& block_shape)
55 for(
int i = 0; i != Shape::static_size; ++i)
57 block_begin[i] = coordinates[i] * block_shape[i];
58 vigra_assert(block_begin[i] < global_shape[i],
"block coordinates out of bounds");
61 for(
int i = 0; i != Shape::static_size; ++i)
63 block_end[i] = std::min(block_begin[i] + block_shape[i], global_shape[i]);
65 return std::make_pair(block_begin, block_end);
69std::pair<Shape, Shape> overlapBoundsAt(
const std::pair<Shape, Shape>& block_bounds,
const Shape& global_shape,
70 const Shape& overlap_before,
const Shape& overlap_after)
72 Shape overlapped_block_begin = block_bounds.first;
73 Shape overlapped_block_end = block_bounds.second;
74 for(
int i = 0; i != Shape::static_size; ++i)
76 if(overlapped_block_begin[i] >= overlap_before[i])
77 overlapped_block_begin[i] -= overlap_before[i];
79 overlapped_block_begin[i] = 0;
81 if(overlapped_block_end[i] <= global_shape[i] - overlap_after[i])
82 overlapped_block_end[i] += overlap_after[i];
84 overlapped_block_end[i] = global_shape[i];
86 return std::make_pair(overlapped_block_begin, overlapped_block_end);
90Shape blocksShape(
const Shape& global_shape,
const Shape& block_shape)
93 for(
int i = 0; i != Shape::static_size; ++i)
95 result[i] = global_shape[i] / block_shape[i];
96 if(block_shape[i] * result[i] != global_shape[i])
105template <
class Shape>
107within(
const Shape& coordinates,
const std::pair<Shape, Shape>& bounds)
112template <
class ArrayType>
113struct OverlappingBlock;
115template <
class ArrayType>
118template <
unsigned int N,
class T,
class S>
119struct OverlappingBlock<MultiArrayView<N, T, S> >
121 typedef typename MultiArrayView<N, T, S>::difference_type Shape;
123 MultiArrayView<N, T, S> block;
124 std::pair<Shape, Shape> inner_bounds;
127template <
unsigned int N,
class T,
class S>
128class Overlaps<MultiArrayView<N, T, S> >
131 typedef MultiArrayView<N, T, S> View;
132 typedef typename View::difference_type Shape;
136 Shape overlap_before;
139 Overlaps(View view,
const Shape& block_shape,
const Shape& overlap_before,
const Shape& overlap_after)
141 block_shape(block_shape),
142 overlap_before(overlap_before),
143 overlap_after(overlap_after)
145 OverlappingBlock<View> operator[](
const Shape& coordinates)
const
147 using namespace overlapped_blocks_detail;
148 std::pair<Shape, Shape> block_bounds = blockBoundsAt(coordinates, view.shape(), block_shape);
149 std::pair<Shape, Shape> overlap_bounds = overlapBoundsAt(block_bounds, view.shape(), overlap_before, overlap_after);
151 OverlappingBlock<View> result;
152 result.block = view.subarray(overlap_bounds.first, overlap_bounds.second);
153 result.inner_bounds = std::make_pair(block_bounds.first - overlap_bounds.first, block_bounds.second - overlap_bounds.first);
158 using namespace overlapped_blocks_detail;
159 return blocksShape(view.shape(), block_shape);
163template <
unsigned int N,
class T>
164struct OverlappingBlock<ChunkedArray<N, T> >
168 MultiArray<N, T> block;
169 std::pair<Shape, Shape> inner_bounds;
172template <
unsigned int N,
class T>
173class Overlaps<ChunkedArray<N, T> >
176 typedef ChunkedArray<N, T> Array;
181 Shape overlap_before;
184 Overlaps(
const Array& array,
const Shape& block_shape,
const Shape& overlap_before,
const Shape& overlap_after)
186 block_shape(block_shape),
187 overlap_before(overlap_before),
188 overlap_after(overlap_after)
191 OverlappingBlock<Array> operator[](
const Shape& coordinates)
const
193 using namespace overlapped_blocks_detail;
194 std::pair<Shape, Shape> block_bounds = blockBoundsAt(coordinates, array.shape(), block_shape);
195 std::pair<Shape, Shape> overlap_bounds = overlapBoundsAt(block_bounds, array.shape(), overlap_before, overlap_after);
197 OverlappingBlock<Array> result;
198 result.block.reshape(overlap_bounds.second - overlap_bounds.first);
199 array.checkoutSubarray(overlap_bounds.first, result.block);
200 result.inner_bounds = std::make_pair(block_bounds.first - overlap_bounds.first, block_bounds.second - overlap_bounds.first);
206 using namespace overlapped_blocks_detail;
207 return blocksShape(array.shape(), block_shape);
TinyVector< MultiArrayIndex, N > type
Definition multi_shape.hxx:272
bool allLessEqual(TinyVectorBase< V1, SIZE, D1, D2 > const &l, TinyVectorBase< V2, SIZE, D3, D4 > const &r)
pointwise less-equal
Definition tinyvector.hxx:1399
bool allLess(TinyVectorBase< V1, SIZE, D1, D2 > const &l, TinyVectorBase< V2, SIZE, D3, D4 > const &r)
pointwise less-than
Definition tinyvector.hxx:1375