36 #ifndef VIGRA_COORDINATE_ITERATOR_HXX
37 #define VIGRA_COORDINATE_ITERATOR_HXX
42 #include "accessor.hxx"
43 #include "tinyvector.hxx"
44 #include "numerictraits.hxx"
45 #include "multi_iterator.hxx"
46 #include "multi_array.hxx"
53 typedef typename MultiArrayShape<N>::type index_type;
54 typedef TinyVector<double, N> coord_type;
55 typedef coord_type deref_type;
56 typedef StridePair type;
57 typedef StridePair stride_type;
58 typedef TinyVector<type, N> stride_array_type;
59 typedef TinyVector<index_type, N> shape_array_type;
60 typedef shape_array_type shape_type;
65 StridePair(
const index_type & i) : index(i), coord(i) {}
66 StridePair(
const coord_type & c) : index(), coord(c) {}
67 StridePair(
const index_type & i,
const coord_type & c)
68 : index (i), coord(c) {}
70 : index(index_type(i)), coord(c) {}
74 const coord_type & operator*()
const
89 StridePair
operator+(
const StridePair & x)
91 StridePair ret = *
this;
95 StridePair
operator-(
const StridePair & x)
97 StridePair ret = *
this;
101 StridePair operator*(
const StridePair & x)
103 StridePair ret = *
this;
104 ret.index *= x.index;
105 ret.coord *= x.coord;
108 StridePair operator/(
const StridePair & x)
110 StridePair ret = *
this;
111 ret.index /= x.index;
112 ret.coord /= x.coord;
120 const index_type & idx()
const
135 struct NumericTraits<StridePair<M> >
136 :
public NumericTraits<typename StridePair<M>::index_type>
140 struct StridePairCoord :
public TinyVector<double, N>
142 typedef TinyVector<double, N> entry_type;
144 StridePairCoord(
const entry_type & c) : entry_type(c) {}
157 struct NumericTraits<StridePairCoord<M> >
158 :
public NumericTraits<typename StridePairCoord<M>::entry_type>
162 struct StridePairDiff :
public StridePairCoord<N>
166 typedef StridePairCoord<N> base_type;
168 : base_type(x), c(c_) {}
169 StridePairDiff(
const base_type & x)
170 : base_type(x), c(0) {}
171 StridePairDiff(
const TinyVector<double, N> & x)
172 : base_type(x), c(0) {}
173 StridePairDiff(
const TinyVector<MultiArrayIndex, N> & x)
174 : base_type(x), c(0) {}
175 StridePairDiff() : c(0) {}
177 const base_type & base()
const
181 StridePairDiff operator*(
const StridePairDiff & x)
183 StridePairDiff ret = base() * x.base();
190 struct NumericTraits<StridePairDiff<M> >
191 :
public NumericTraits<StridePairCoord<M> >
194 template<
unsigned N,
class T>
195 struct StridePairPointer :
public StridePairCoord<N>
197 typedef const T* index_type;
198 typedef StridePairCoord<N> coord_type;
199 typedef typename coord_type::entry_type coord_num_type;
200 typedef StridePairPointer type;
201 typedef type deref_type;
202 typedef StridePairDiff<N> stride_type;
203 typedef TinyVector<stride_type, N> stride_array_type;
204 typedef typename MultiArrayShape<N>::type shape_array_type;
205 typedef shape_array_type shape_type;
209 StridePairPointer(
const index_type & i,
const coord_type & c)
210 : coord_type(c), index(i) {}
212 const type & operator*()
const
216 const T & value()
const
220 const coord_type & coord()
const
229 const index_type & idx()
const
246 template<
unsigned M,
class T>
247 struct NumericTraits<StridePairPointer<M, T> >
248 :
public NumericTraits<typename StridePairPointer<M, T>::coord_type>
253 template<class T, bool is_complex = NumericTraits<T>::isComplex::value,
254 bool is_vector = !NumericTraits<T>::isScalar::value>
257 static double get(
const T & x)
264 struct weighted_abs<T, true, false>
266 static double get(
const T & x)
273 template<
class T,
bool is_complex>
274 struct weighted_abs<T, is_complex, true>
276 static double get(
const T & x)
278 return x.magnitude();
283 struct accumulable_coord_access;
285 struct accumulable_value_access;
287 struct accumulable_weighted_access;
289 template<
unsigned N,
class T>
290 struct accumulable_coord_access<StridePairPointer<N, T> >
292 typedef StridePairPointer<N, T> accumulable_type;
293 typedef typename accumulable_type::coord_num_type type;
294 static const type &
get(
const accumulable_type & v) {
return v.coord(); }
297 template<
unsigned N,
class T>
298 struct accumulable_value_access<StridePairPointer<N, T> >
300 typedef StridePairPointer<N, T> accumulable_type;
302 static const type &
get(
const accumulable_type & v) {
return v.value(); }
305 template<
unsigned N,
class T>
306 struct accumulable_weighted_access<StridePairPointer<N, T> >
308 typedef StridePairPointer<N, T> accumulable_type;
309 typedef typename accumulable_type::coord_num_type type;
310 static type
get(
const accumulable_type & v)
317 void dismember(X & r,
const X & x,
unsigned i)
322 void dismember(StridePair<N> & r,
const StridePair<N> & x,
unsigned i)
324 r.index[i] = x.index[i];
325 r.coord[i] = x.coord[i];
328 void dismember(StridePairDiff<N> & r,
const StridePairDiff<N> & x,
unsigned i)
333 template<
unsigned N,
class X>
335 dismember(
const X & x)
337 TinyVector<X, N> ret;
338 for (
unsigned i = 0; i != N; ++i)
339 dismember(ret[i], x, i);
343 TinyVector<StridePairDiff<N>, N>
344 dismember(
const TinyVector<MultiArrayIndex, N> & x,
345 const StridePairCoord<N> & y)
347 typedef StridePairDiff<N> type;
348 TinyVector<type, N> ret;
349 for (
unsigned i = 0; i != N; ++i)
365 template<
unsigned N,
class S = Str
idePair<N> >
366 class CoordinateStride :
protected S
370 typedef typename S::stride_type stride_type;
371 typedef typename S::deref_type deref_type;
372 typedef CoordinateStride<N> type;
373 typedef typename S::coord_type coord_type;
374 typedef typename S::index_type index_type;
375 typedef typename S::shape_array_type shape_array_type;
380 CoordinateStride(
void*) {}
383 CoordinateStride(
const S & x,
double s0)
384 : S(x), stride_0(s0) {}
408 dim0() += n * stride_0;
413 dim0() -= n * stride_0;
416 stride_type operator[](difference_type n)
const
423 stride_type operator[](stride_type x)
const
429 bool operator!=(
const CoordinateStride & y)
const
431 return idx() != y.idx();
433 bool operator==(
const CoordinateStride & y)
const
435 if (stride_0 != y.stride_0)
437 return idx() == y.idx();
439 bool operator<(
const CoordinateStride & y)
const
441 return idx() < y.idx();
444 bool operator<=(
const CoordinateStride & y)
const
446 if (stride_0 == y.stride_0)
450 bool operator>(
const CoordinateStride & y)
const
454 bool operator>=(
const CoordinateStride & y)
const
456 if (stride_0 == y.stride_0)
461 friend std::ostream &
462 operator<<(std::ostream & os,
const CoordinateStride & x)
464 os <<
"{" << x.stride_0 <<
": " <<
static_cast<const S &
>(x) <<
"}";
468 typedef MultiIterator<N, deref_type, const deref_type &, CoordinateStride>
472 template <
unsigned N,
class S>
473 struct MultiIteratorStrideTraits<CoordinateStride<N, S> >
475 typedef typename S::stride_type stride_type;
476 typedef typename S::stride_array_type stride_array_type;
477 typedef typename S::shape_array_type shape_array_type;
478 static stride_array_type shift(
const stride_array_type & s,
unsigned d)
480 stride_array_type ret;
481 for (
unsigned i = d; i != N; ++i)
487 template <
unsigned N>
488 struct CoordinateMultiIterator :
public CoordinateStride<N>::iterator_type
490 typedef CoordinateStride<N> ptr_type;
491 typedef typename ptr_type::iterator_type base_type;
492 typedef typename ptr_type::stride_type stride_type;
493 typedef typename ptr_type::shape_array_type shape_array_type;
494 typedef typename ptr_type::coord_type coord_type;
495 typedef typename ptr_type::index_type index_type;
497 CoordinateMultiIterator(
const stride_type & origin,
498 const stride_type & stride,
499 const index_type & shape)
501 : base_type(ptr_type(origin, stride.dim0()),
502 detail::dismember<N>(stride),
503 detail::dismember<N>(shape)) {}
505 CoordinateMultiIterator(
const base_type & x) : base_type(x) {}
511 struct CoordinateMultiRangeReturns
513 typedef CoordinateMultiIterator<N> iterator_type;
514 typedef typename iterator_type::coord_type coord_type;
515 typedef StridePair<N> pair_type;
516 typedef typename pair_type::type stride_type;
517 typedef typename pair_type::stride_array_type stride_array_type;
519 typedef typename AccessorTraits<coord_type>::default_const_accessor
521 typedef triple<iterator_type, stride_array_type, access_type> type;
526 template <
unsigned N>
527 typename detail::CoordinateMultiRangeReturns<N>::type
528 coordinateMultiRange(
const typename MultiArrayShape<N>::type & shape,
529 const TinyVector<double, N> & stride
530 = TinyVector<double, N>(1.0),
531 const TinyVector<double, N> & origin
532 = TinyVector<double, N>(0.0))
535 detail::CoordinateMultiRangeReturns<N>::stride_type stride_type;
537 detail::CoordinateMultiRangeReturns<N>::access_type access_type;
539 return typename detail::CoordinateMultiRangeReturns<N>::type
540 (CoordinateMultiIterator<N>(stride_type(0, origin),
541 stride_type(1, stride),
543 detail::dismember<N>(stride_type(shape)),
547 template <
unsigned N,
class T>
548 struct CombinedMultiIterator
549 :
public CoordinateStride<N, StridePairPointer<N, T> >::iterator_type
551 typedef StridePairPointer<N, T> pair_type;
552 typedef CoordinateStride<N, pair_type> ptr_type;
553 typedef typename ptr_type::iterator_type base_type;
554 typedef typename ptr_type::stride_type stride_type;
555 typedef typename ptr_type::coord_type coord_type;
556 typedef typename pair_type::shape_array_type shape_array_type;
558 CombinedMultiIterator(
const T* raw_pointer,
559 const stride_type & origin,
560 const TinyVector<MultiArrayIndex, N> & pointer_stride,
561 const stride_type & stride,
562 const shape_array_type & shape)
564 : base_type(ptr_type(pair_type(raw_pointer, origin), stride.dim0()),
565 detail::dismember<N>(pointer_stride, stride),
568 CombinedMultiIterator(
const base_type & x) : base_type(x) {}
571 template<
unsigned N,
class T>
572 struct SrcCoordinateMultiArrayRangeReturns
574 typedef CombinedMultiIterator<N, T> iterator_type;
575 typedef typename iterator_type::coord_type coord_type;
576 typedef typename iterator_type::pair_type pair_type;
577 typedef typename iterator_type::ptr_type ptr_type;
578 typedef typename ptr_type::deref_type deref_type;
579 typedef typename iterator_type::stride_type stride_type;
580 typedef typename pair_type::stride_array_type stride_array_type;
581 typedef typename pair_type::shape_array_type shape_array_type;
583 typedef typename AccessorTraits<deref_type>::default_const_accessor
585 typedef triple<iterator_type, stride_array_type, access_type> type;
590 struct CoordinateSteps
592 typedef const TinyVector<double, N> & type;
595 template <
unsigned int N,
class T,
class Str
ideTag>
596 inline typename SrcCoordinateMultiArrayRangeReturns<N, T>::type
597 srcCoordinateMultiArrayRange(
const MultiArrayView<N, T, StrideTag> & array,
598 typename CoordinateSteps<N>::type stride
599 = TinyVector<double, N>(1.0),
600 typename CoordinateSteps<N>::type origin
601 = TinyVector<double, N>(0.0))
603 typedef SrcCoordinateMultiArrayRangeReturns<N, T> returns;
604 typedef typename returns::type type;
605 typedef typename returns::stride_type stride_type;
606 typedef typename returns::access_type access_type;
607 typedef typename returns::iterator_type iterator_type;
608 typedef typename returns::shape_array_type shape_array_type;
610 shape_array_type shape = array.shape();
611 return type(iterator_type(array.traverser_begin().get(),
616 detail::dismember<N>(stride_type(shape)),
620 template <
class VALUETYPE,
class COORD>
621 struct AccessorCoordinatePair
623 typedef VALUETYPE value_type;
624 typedef COORD coord_type;
625 typedef AccessorCoordinatePair type;
628 const coord_type & c;
630 AccessorCoordinatePair(
const value_type & v_,
const coord_type & c_)
633 const value_type & value()
const
637 const coord_type & coord()
const
653 template <
class Accessor,
class COORD>
657 typedef typename Accessor::value_type forward_type;
658 typedef AccessorCoordinatePair<forward_type, COORD> value_type;
663 template <
class ITERATOR>
666 const typename ITERATOR::value_type & x = *i;
667 return value_type(a(&x.value()), x.coord());
671 template <
class ITERATOR,
class DIFFERENCE>
672 value_type
operator()(ITERATOR
const & i, DIFFERENCE
const & diff)
const
674 const typename ITERATOR::value_type & x = i[diff];
675 return value_type(a(&x.value()), x.coord());
679 template<
unsigned N,
class T,
class Accessor>
680 struct SrcCoordinateMultiArrayRangeAccessorReturns
682 typedef CombinedMultiIterator<N, T> iterator_type;
683 typedef typename iterator_type::coord_type coord_type;
684 typedef typename iterator_type::pair_type pair_type;
685 typedef typename iterator_type::ptr_type ptr_type;
686 typedef typename ptr_type::deref_type deref_type;
687 typedef typename iterator_type::stride_type stride_type;
688 typedef typename pair_type::stride_array_type stride_array_type;
689 typedef typename pair_type::shape_array_type shape_array_type;
691 typedef CoordinateConstValueAccessor<Accessor, coord_type> access_type;
692 typedef triple<iterator_type, stride_array_type, access_type> type;
695 template <
unsigned int N,
class T,
class Str
ideTag,
class Access>
696 inline typename SrcCoordinateMultiArrayRangeAccessorReturns<N, T, Access>::type
697 srcCoordinateMultiArrayRangeAccessor(
const MultiArrayView<N, T, StrideTag> &
700 typename CoordinateSteps<N>::type stride
701 = TinyVector<double, N>(1.0),
702 typename CoordinateSteps<N>::type origin
703 = TinyVector<double, N>(0.0))
705 typedef SrcCoordinateMultiArrayRangeAccessorReturns<N, T, Access> returns;
706 typedef typename returns::type type;
707 typedef typename returns::stride_type stride_type;
708 typedef typename returns::access_type access_type;
709 typedef typename returns::iterator_type iterator_type;
710 typedef typename returns::shape_array_type shape_array_type;
712 shape_array_type shape = array.shape();
713 return type(iterator_type(array.traverser_begin().get(),
718 detail::dismember<N>(stride_type(shape)),
726 template <
unsigned N>
728 operator<<(ostream & os, const vigra::StridePair<N> & x)
730 os <<
"[" << x.index <<
", " << x.coord <<
"]";
734 template <
unsigned N>
736 operator<<(ostream & os, const vigra::StridePairDiff<N> & x)
738 os <<
"<" << x.c <<
"; "
739 <<
static_cast<vigra::StridePairCoord<N>
>(x) <<
">";
743 template <
unsigned N,
class T>
745 operator<<(ostream & os, const vigra::StridePairPointer<N, T> & x)
747 os <<
"[" << x.value() <<
", " << x.coord() <<
"]";
751 template <
class VALUETYPE,
class COORD>
754 const vigra::AccessorCoordinatePair<VALUETYPE, COORD> & x)
756 os <<
"[" << x.value() <<
", " << x.coord() <<
"]";
762 #endif // VIGRA_COORDINATE_ITERATOR_HXX
value_type operator()(ITERATOR const &i, DIFFERENCE const &diff) const
Definition: coordinate_iterator.hxx:672
Diff2D operator-(Diff2D const &a, Diff2D const &b)
Definition: diff2d.hxx:711
Diff2D operator+(Diff2D const &a, Diff2D const &b)
Definition: diff2d.hxx:739
std::ptrdiff_t MultiArrayIndex
Definition: multi_shape.hxx:55
FFTWComplex< R > & operator-=(FFTWComplex< R > &a, const FFTWComplex< R > &b)
subtract-assignment
Definition: fftw3.hxx:867
bool operator<=(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
less or equal
Definition: fixedpoint.hxx:521
FFTWComplex< R > & operator+=(FFTWComplex< R > &a, const FFTWComplex< R > &b)
add-assignment
Definition: fftw3.hxx:859
value_type operator()(ITERATOR const &i) const
Definition: coordinate_iterator.hxx:664
vigra::MultiArrayView< N, T, Stride >::const_reference get(vigra::MultiArrayView< N, T, Stride > const &pmap, typename vigra::MultiArrayView< N, T, Stride >::difference_type const &k)
Read the value at key k in property map pmap (API: boost).
Definition: multi_gridgraph.hxx:2859
bool operator!=(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
not equal
Definition: fftw3.hxx:841
bool operator==(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
equal
Definition: fftw3.hxx:825
bool operator<(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
less than
Definition: fixedpoint.hxx:512
bool operator>=(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
greater or equal
Definition: fixedpoint.hxx:539
FFTWComplex< R >::NormType abs(const FFTWComplex< R > &a)
absolute value (= magnitude)
Definition: fftw3.hxx:1002
ostream & operator<<(ostream &s, const vigra::MultiArrayView< 2, T, C > &m)
Definition: matrix.hxx:2322
Forward accessor to the value() part of the values an iterator points to.
Definition: coordinate_iterator.hxx:654
bool operator>(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
greater
Definition: fixedpoint.hxx:530