36 #ifndef VIGRA_MULTI_SHAPE_HXX 37 #define VIGRA_MULTI_SHAPE_HXX 39 #include "multi_fwd.hxx" 40 #include <sys/types.h> 41 #include "tinyvector.hxx" 42 #include "array_vector.hxx" 43 #include "numerictraits.hxx" 72 struct IsMultiband : VigraFalseType{
76 struct IsMultiband<Multiband<T> > : VigraTrueType{
86 struct NumericTraits<Singleband<T> >
87 :
public NumericTraits<T>
91 struct NumericTraits<Multiband<T> >
93 typedef Multiband<T> Type;
100 typedef Type ValueType;
102 typedef typename NumericTraits<T>::isIntegral isIntegral;
103 typedef VigraFalseType isScalar;
104 typedef typename NumericTraits<T>::isSigned isSigned;
105 typedef typename NumericTraits<T>::isSigned isOrdered;
106 typedef typename NumericTraits<T>::isSigned isComplex;
149 inline TinyVector <MultiArrayIndex, N>
150 defaultStride(
const TinyVector <MultiArrayIndex, N> &shape)
152 TinyVector <MultiArrayIndex, N> ret;
154 for (
int i = 1; i < (int)N; ++i)
155 ret [i] = ret [i-1] * shape [i-1];
162 inline TinyVector <MultiArrayIndex, N>
163 defaultMultibandStride(
const TinyVector <MultiArrayIndex, N> &shape)
165 TinyVector <MultiArrayIndex, N> ret;
167 for (
int i = 0; i < (int)N-1; ++i)
169 int j = (i + int(N - 1)) % N;
170 ret [i] = ret [j] * shape [j];
182 struct ResolveMultiband
185 typedef StridedArrayTag Stride;
186 static const bool value =
false;
189 static TinyVector <MultiArrayIndex, N>
190 defaultStride(
const TinyVector <MultiArrayIndex, N> &shape)
192 return vigra::detail::defaultStride(shape);
197 struct ResolveMultiband<Singleband<T> >
200 typedef StridedArrayTag Stride;
201 static const bool value =
false;
204 static TinyVector <MultiArrayIndex, N>
205 defaultStride(
const TinyVector <MultiArrayIndex, N> &shape)
207 return vigra::detail::defaultStride(shape);
212 struct ResolveMultiband<Multiband<T> >
215 typedef StridedArrayTag Stride;
216 static const bool value =
true;
219 static TinyVector <MultiArrayIndex, N>
220 defaultStride(
const TinyVector <MultiArrayIndex, N> &shape)
222 return vigra::detail::defaultMultibandStride(shape);
227 struct ResolveChunkedMemory
233 struct ResolveChunkedMemory<ChunkedMemory<T> >
243 template <
unsigned int N>
244 class MultiArrayShape
268 template <
unsigned int N,
class T =
int>
272 static Shape defaultShape()
275 res.template subarray<0,5>() = ChunkShape<5,T>::defaultShape();
281 struct ChunkShape<0, T>
283 static Shape1 defaultShape()
290 struct ChunkShape<1, T>
292 static Shape1 defaultShape()
299 struct ChunkShape<2, T>
301 static Shape2 defaultShape()
303 return Shape2(1 << 9, 1 << 9);
308 struct ChunkShape<3, T>
310 static Shape3 defaultShape()
312 return Shape3(1 << 6, 1 << 6, 1 << 6);
317 struct ChunkShape<4, T>
319 static Shape4 defaultShape()
321 return Shape4(1 << 6, 1 << 6, 1 << 4, 1 << 2);
326 struct ChunkShape<5, T>
328 static Shape5 defaultShape()
330 return Shape5(1 << 6, 1 << 6, 1 << 4, 1 << 2, 1 << 2);
349 struct CoordinateToScanOrder
351 template <
int N,
class D1,
class D2,
class D3,
class D4>
356 return coordinate[N-K] + shape[N-K] * CoordinateToScanOrder<K-1>::exec(shape, coordinate);
361 struct CoordinateToScanOrder<1>
363 template <
int N,
class D1,
class D2,
class D3,
class D4>
368 return coordinate[N-1];
381 struct ScanOrderToCoordinate
388 result[N-K] = (d % shape[N-K]);
389 ScanOrderToCoordinate<K-1>::exec(d / shape[N-K], shape, result);
394 struct ScanOrderToCoordinate<1>
415 struct ScanOrderToOffset
422 return stride[N-K] * (d % shape[N-K]) +
423 ScanOrderToOffset<K-1>::exec(d / shape[N-K], shape, stride);
428 struct ScanOrderToOffset<1>
435 return stride[N-1] * d;
449 struct CoordinatesToOffest
455 return stride[0] * x;
461 return stride[0] * x + stride[1] * y;
466 struct CoordinatesToOffest<UnstridedArrayTag>
478 return x + stride[1] * y;
492 struct RelativeToAbsoluteCoordinate
498 RelativeToAbsoluteCoordinate<M-1>::exec(shape, coord);
500 coord[M] += shape[M];
505 struct RelativeToAbsoluteCoordinate<0>
512 coord[0] += shape[0];
529 template <
unsigned int N,
unsigned int DIMENSION=N-1>
530 struct BorderTypeImpl
534 static unsigned int exec(shape_type
const & point, shape_type
const & shape)
536 unsigned int res = BorderTypeImpl<N, DIMENSION-1>::exec(point, shape);
537 if(point[DIMENSION] == 0)
538 res |= (1 << 2*DIMENSION);
539 if(point[DIMENSION] == shape[DIMENSION]-1)
540 res |= (2 << 2*DIMENSION);
545 template <
unsigned int N>
546 struct BorderTypeImpl<N, 0>
549 static const unsigned int DIMENSION = 0;
551 static unsigned int exec(shape_type
const & point, shape_type
const & shape)
553 unsigned int res = 0;
554 if(point[DIMENSION] == 0)
555 res |= (1 << 2*DIMENSION);
556 if(point[DIMENSION] == shape[DIMENSION]-1)
557 res |= (2 << 2*DIMENSION);
578 template <
unsigned int Level>
579 struct MakeDirectArrayNeighborhood
581 template <
class Array>
582 static void offsets(Array & a)
584 typedef typename Array::value_type Shape;
589 MakeDirectArrayNeighborhood<Level-1>::offsets(a);
594 template <
class Array>
595 static void exists(Array & a,
unsigned int borderType)
597 a.push_back((borderType & (1 << 2*Level)) == 0);
598 MakeDirectArrayNeighborhood<Level-1>::exists(a, borderType);
599 a.push_back((borderType & (2 << 2*Level)) == 0);
604 struct MakeDirectArrayNeighborhood<0>
606 template <
class Array>
607 static void offsets(Array & a)
609 typedef typename Array::value_type Shape;
618 template <
class Array>
619 static void exists(Array & a,
unsigned int borderType)
621 a.push_back((borderType & 1) == 0);
622 a.push_back((borderType & 2) == 0);
627 template <
unsigned int Level>
628 struct MakeIndirectArrayNeighborhood
630 template <
class Array,
class Shape>
631 static void offsets(Array & a, Shape point,
bool isCenter =
true)
634 MakeIndirectArrayNeighborhood<Level-1>::offsets(a, point,
false);
636 MakeIndirectArrayNeighborhood<Level-1>::offsets(a, point, isCenter);
638 MakeIndirectArrayNeighborhood<Level-1>::offsets(a, point,
false);
641 template <
class Array>
642 static void exists(Array & a,
unsigned int borderType,
bool isCenter =
true)
644 if((borderType & (1 << 2*Level)) == 0)
645 MakeIndirectArrayNeighborhood<Level-1>::exists(a, borderType,
false);
647 MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
649 MakeIndirectArrayNeighborhood<Level-1>::exists(a, borderType, isCenter);
651 if((borderType & (2 << 2*Level)) == 0)
652 MakeIndirectArrayNeighborhood<Level-1>::exists(a, borderType,
false);
654 MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
657 template <
class Array>
658 static void markOutside(Array & a)
661 MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
662 MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
663 MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
669 struct MakeIndirectArrayNeighborhood<0>
671 template <
class Array,
class Shape>
672 static void offsets(Array & a, Shape point,
bool isCenter =
true)
685 template <
class Array>
686 static void exists(Array & a,
unsigned int borderType,
bool isCenter =
true)
688 a.push_back((borderType & 1) == 0);
693 a.push_back((borderType & 2) == 0);
696 template <
class Array>
697 static void markOutside(Array & a)
712 template <
class Shape>
718 enum { N = Shape::static_size };
720 neighborOffsets.clear();
723 MakeDirectArrayNeighborhood<N-1>::offsets(neighborOffsets);
728 MakeIndirectArrayNeighborhood<N-1>::offsets(neighborOffsets, point);
731 unsigned int borderTypeCount = 1 << 2*N;
732 neighborExists.resize(borderTypeCount);
734 for(
unsigned int k=0; k<borderTypeCount; ++k)
736 neighborExists[k].clear();
739 MakeDirectArrayNeighborhood<N-1>::exists(neighborExists[k], k);
743 MakeIndirectArrayNeighborhood<N-1>::exists(neighborExists[k], k);
754 #endif // VIGRA_MULTI_SHAPE_HXX MultiArrayShape< 3 >::type Shape3
shape type for MultiArray<3, T>
Definition: multi_shape.hxx:255
std::ptrdiff_t MultiArrayIndex
Definition: multi_fwd.hxx:60
Definition: accessor.hxx:43
Definition: array_vector.hxx:58
Definition: multi_fwd.hxx:63
TinyVector< MultiArrayIndex, N > type
Definition: multi_shape.hxx:250
Wrapper for fixed size vectors.
Definition: tinyvector.hxx:621
Class for fixed size vectors.This class contains an array of size SIZE of the specified VALUETYPE...
Definition: accessor.hxx:940
MultiArrayShape< 5 >::type Shape5
shape type for MultiArray<5, T>
Definition: multi_shape.hxx:257
MultiArrayShape< 2 >::type Shape2
shape type for MultiArray<2, T>
Definition: multi_shape.hxx:254
MultiArrayShape< 1 >::type Shape1
shape type for MultiArray<1, T>
Definition: multi_shape.hxx:253
Base class for fixed size vectors.
Definition: tinyvector.hxx:82
use only direct neighbors
Definition: multi_fwd.hxx:187
NeighborhoodType
Choose the neighborhood system in a dimension-independent way.
Definition: multi_fwd.hxx:186
MultiArrayShape< 4 >::type Shape4
shape type for MultiArray<4, T>
Definition: multi_shape.hxx:256