/usr/include/vigra/coordinate_iterator.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | /************************************************************************/
/* */
/* Copyright 2011-2012 by Markus Nullmeier and Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_COORDINATE_ITERATOR_HXX
#define VIGRA_COORDINATE_ITERATOR_HXX
#include <complex>
#include "tuple.hxx"
#include "accessor.hxx"
#include "tinyvector.hxx"
#include "numerictraits.hxx"
#include "multi_iterator.hxx"
#include "multi_array.hxx"
namespace vigra {
template<unsigned N>
struct StridePair
{
typedef typename MultiArrayShape<N>::type index_type;
typedef TinyVector<double, N> coord_type;
typedef coord_type deref_type;
typedef StridePair type;
typedef StridePair stride_type;
typedef TinyVector<type, N> stride_array_type;
typedef TinyVector<index_type, N> shape_array_type;
typedef shape_array_type shape_type;
index_type index;
coord_type coord;
StridePair(const index_type & i) : index(i), coord(i) {}
StridePair(const coord_type & c) : index(), coord(c) {}
StridePair(const index_type & i, const coord_type & c)
: index (i), coord(c) {}
StridePair( MultiArrayIndex i, const coord_type & c)
: index(index_type(i)), coord(c) {}
StridePair() {}
// use just the coordinates for further processing ...
const coord_type & operator*() const
{
return this->coord;
}
void operator+=(const StridePair & x)
{
index += x.index;
coord += x.coord;
}
void operator-=(const StridePair & x)
{
index -= x.index;
coord -= x.coord;
}
StridePair operator+(const StridePair & x)
{
StridePair ret = *this;
ret += x;
return ret;
}
StridePair operator-(const StridePair & x)
{
StridePair ret = *this;
ret -= x;
return ret;
}
StridePair operator*(const StridePair & x)
{
StridePair ret = *this;
ret.index *= x.index;
ret.coord *= x.coord;
return ret;
}
StridePair operator/(const StridePair & x)
{
StridePair ret = *this;
ret.index /= x.index;
ret.coord /= x.coord;
return ret;
}
MultiArrayIndex & idx0()
{
return index[0];
}
const index_type & idx() const
{
return index;
}
double & dim0()
{
return coord[0];
}
double dim0() const
{
return coord[0];
}
};
template<unsigned M>
struct NumericTraits<StridePair<M> >
: public NumericTraits<typename StridePair<M>::index_type>
{};
template<unsigned N>
struct StridePairCoord : public TinyVector<double, N>
{
typedef TinyVector<double, N> entry_type;
StridePairCoord(const entry_type & c) : entry_type(c) {}
StridePairCoord() {}
double & dim0()
{
return (*this)[0];
}
double dim0() const
{
return (*this)[0];
}
};
template<unsigned M>
struct NumericTraits<StridePairCoord<M> >
: public NumericTraits<typename StridePairCoord<M>::entry_type>
{};
template<unsigned N>
struct StridePairDiff : public StridePairCoord<N>
{
MultiArrayIndex c;
typedef StridePairCoord<N> base_type;
StridePairDiff(MultiArrayIndex c_, const base_type & x)
: base_type(x), c(c_) {}
StridePairDiff(const base_type & x)
: base_type(x), c(0) {}
StridePairDiff(const TinyVector<double, N> & x)
: base_type(x), c(0) {}
StridePairDiff(const TinyVector<MultiArrayIndex, N> & x)
: base_type(x), c(0) {}
StridePairDiff() : c(0) {}
const base_type & base() const
{
return *this;
}
StridePairDiff operator*(const StridePairDiff & x)
{
StridePairDiff ret = base() * x.base();
ret.c = c * x.c;
return ret;
}
};
template<unsigned M>
struct NumericTraits<StridePairDiff<M> >
: public NumericTraits<StridePairCoord<M> >
{};
template<unsigned N, class T>
struct StridePairPointer : public StridePairCoord<N>
{
typedef const T* index_type;
typedef StridePairCoord<N> coord_type;
typedef typename coord_type::entry_type coord_num_type;
typedef StridePairPointer type;
typedef type deref_type;
typedef StridePairDiff<N> stride_type;
typedef TinyVector<stride_type, N> stride_array_type;
typedef typename MultiArrayShape<N>::type shape_array_type;
typedef shape_array_type shape_type;
index_type index;
StridePairPointer(const index_type & i, const coord_type & c)
: coord_type(c), index(i) {}
const type & operator*() const
{
return *this;
}
const T & value() const
{
return *index;
}
const coord_type & coord() const
{
return *this;
}
index_type & idx0()
{
return index;
}
const index_type & idx() const
{
return index;
}
void operator+=(stride_type x)
{
index += x.c;
coord_type::operator+=(x);
}
void operator-=(stride_type x)
{
index -= x.c;
coord_type::operator-=(x);
}
};
template<unsigned M, class T>
struct NumericTraits<StridePairPointer<M, T> >
: public NumericTraits<typename StridePairPointer<M, T>::coord_type>
{};
namespace detail {
template<class T, bool is_complex = NumericTraits<T>::isComplex::value,
bool is_vector = !NumericTraits<T>::isScalar::value>
struct weighted_abs
{
static double get(const T & x)
{
return x;
}
};
template<class T>
struct weighted_abs<T, true, false>
{
static double get(const T & x)
{
using std::abs;
return abs(x);
}
};
template<class T, bool is_complex>
struct weighted_abs<T, is_complex, true>
{
static double get(const T & x)
{
return x.magnitude();
}
};
template<class T>
struct accumulable_coord_access;
template<class T>
struct accumulable_value_access;
template<class T>
struct accumulable_weighted_access;
template<unsigned N, class T>
struct accumulable_coord_access<StridePairPointer<N, T> >
{
typedef StridePairPointer<N, T> accumulable_type;
typedef typename accumulable_type::coord_num_type type;
static const type & get(const accumulable_type & v) { return v.coord(); }
};
template<unsigned N, class T>
struct accumulable_value_access<StridePairPointer<N, T> >
{
typedef StridePairPointer<N, T> accumulable_type;
typedef T type;
static const type & get(const accumulable_type & v) { return v.value(); }
};
template<unsigned N, class T>
struct accumulable_weighted_access<StridePairPointer<N, T> >
{
typedef StridePairPointer<N, T> accumulable_type;
typedef typename accumulable_type::coord_num_type type;
static type get(const accumulable_type & v)
{
return weighted_abs<T>::get(v.value()) * v.coord();
}
};
template<class X>
void dismember(X & r, const X & x, unsigned i)
{
r[i] = x[i];
}
template<unsigned N>
void dismember(StridePair<N> & r, const StridePair<N> & x, unsigned i)
{
r.index[i] = x.index[i];
r.coord[i] = x.coord[i];
}
template<unsigned N>
void dismember(StridePairDiff<N> & r, const StridePairDiff<N> & x, unsigned i)
{
r.c = static_cast<MultiArrayIndex>(r[i] = x[i]);
}
template<unsigned N, class X>
TinyVector<X, N>
dismember(const X & x)
{
TinyVector<X, N> ret;
for (unsigned i = 0; i != N; ++i)
dismember(ret[i], x, i);
return ret;
}
template<unsigned N>
TinyVector<StridePairDiff<N>, N>
dismember(const TinyVector<MultiArrayIndex, N> & x,
const StridePairCoord<N> & y)
{
typedef StridePairDiff<N> type;
TinyVector<type, N> ret;
for (unsigned i = 0; i != N; ++i)
{
ret[i].c = x[i];
ret[i][i] = y[i];
}
return ret;
}
} // namespace detail
// A fake "pointer" for MultiIterator containing coordinates.
// Indices (or a pointer) cannot be circumvented in coordiante iterators,
// since floating point addition is not associative and
// iterator comparison is done via via '<' or '!='. Class CoordinateStride
// thus forwards iterator comparison to the index or pointer part
// of its template parameter S.
template<unsigned N, class S = StridePair<N> >
class CoordinateStride : protected S
{
public:
typedef MultiArrayIndex difference_type;
typedef typename S::stride_type stride_type;
typedef typename S::deref_type deref_type;
typedef CoordinateStride<N> type;
typedef typename S::coord_type coord_type;
typedef typename S::index_type index_type;
typedef typename S::shape_array_type shape_array_type;
protected:
double stride_0;
CoordinateStride(void*) {} // used MultiIterator ctor, unused.
public:
CoordinateStride(const S & x, double s0)
: S(x), stride_0(s0) {}
using S::operator*;
using S::idx0;
using S::idx;
using S::dim0;
using S::operator+=;
using S::operator-=;
void operator++()
{
++idx0();
dim0() += stride_0;
}
void operator--()
{
--idx0();
dim0() -= stride_0;
}
void operator+=(difference_type n)
{
idx0() += n;
dim0() += n * stride_0;
}
void operator-=(difference_type n)
{
idx0() -= n;
dim0() -= n * stride_0;
}
stride_type operator[](difference_type n) const
{
type ret = *this;
ret[0] += n;
return ret;
}
stride_type operator[](stride_type x) const
{
return *this + x;
}
// ... but use the idx() for comparisons:
bool operator!=(const CoordinateStride & y) const
{
return idx() != y.idx();
}
bool operator==(const CoordinateStride & y) const
{
if (stride_0 != y.stride_0)
return false;
return idx() == y.idx();
}
bool operator<(const CoordinateStride & y) const
{
return idx() < y.idx();
}
bool operator<=(const CoordinateStride & y) const
{
if (stride_0 == y.stride_0)
return true;
return *this < y;
}
bool operator>(const CoordinateStride & y) const
{
return y < *this;
}
bool operator>=(const CoordinateStride & y) const
{
if (stride_0 == y.stride_0)
return true;
return operator>(y);
}
friend std::ostream &
operator<<(std::ostream & os, const CoordinateStride & x)
{
os << "{" << x.stride_0 << ": " << static_cast<const S &>(x) << "}";
return os;
}
typedef MultiIterator<N, deref_type, const deref_type &, CoordinateStride>
iterator_type;
};
template <unsigned N, class S>
struct MultiIteratorStrideTraits<CoordinateStride<N, S> >
{
typedef typename S::stride_type stride_type;
typedef typename S::stride_array_type stride_array_type;
typedef typename S::shape_array_type shape_array_type;
static stride_array_type shift(const stride_array_type & s, unsigned d)
{
stride_array_type ret;
for (unsigned i = d; i != N; ++i)
ret[i - d] = s[i];
return ret;
}
};
template <unsigned N>
struct CoordinateMultiIterator : public CoordinateStride<N>::iterator_type
{
typedef CoordinateStride<N> ptr_type;
typedef typename ptr_type::iterator_type base_type;
typedef typename ptr_type::stride_type stride_type;
typedef typename ptr_type::shape_array_type shape_array_type;
typedef typename ptr_type::coord_type coord_type;
typedef typename ptr_type::index_type index_type;
CoordinateMultiIterator(const stride_type & origin,
const stride_type & stride,
const index_type & shape)
: base_type(ptr_type(origin, stride.dim0()),
detail::dismember<N>(stride),
detail::dismember<N>(shape)) {}
CoordinateMultiIterator(const base_type & x) : base_type(x) {}
};
namespace detail {
template<unsigned N>
struct CoordinateMultiRangeReturns
{
typedef CoordinateMultiIterator<N> iterator_type;
typedef typename iterator_type::coord_type coord_type;
typedef StridePair<N> pair_type;
typedef typename pair_type::type stride_type;
typedef typename pair_type::stride_array_type stride_array_type;
typedef typename AccessorTraits<coord_type>::default_const_accessor
access_type;
typedef triple<iterator_type, stride_array_type, access_type> type;
};
} // namespace detail
template <unsigned N>
typename detail::CoordinateMultiRangeReturns<N>::type
coordinateMultiRange(const typename MultiArrayShape<N>::type & shape,
const TinyVector<double, N> & stride
= TinyVector<double, N>(1.0),
const TinyVector<double, N> & origin
= TinyVector<double, N>(0.0))
{
typedef typename
detail::CoordinateMultiRangeReturns<N>::stride_type stride_type;
typedef typename
detail::CoordinateMultiRangeReturns<N>::access_type access_type;
return typename detail::CoordinateMultiRangeReturns<N>::type
(CoordinateMultiIterator<N>(stride_type(0, origin),
stride_type(1, stride),
shape),
detail::dismember<N>(stride_type(shape)),
access_type());
}
template <unsigned N, class T>
struct CombinedMultiIterator
: public CoordinateStride<N, StridePairPointer<N, T> >::iterator_type
{
typedef StridePairPointer<N, T> pair_type;
typedef CoordinateStride<N, pair_type> ptr_type;
typedef typename ptr_type::iterator_type base_type;
typedef typename ptr_type::stride_type stride_type;
typedef typename ptr_type::coord_type coord_type;
typedef typename pair_type::shape_array_type shape_array_type;
CombinedMultiIterator(const T* raw_pointer,
const stride_type & origin,
const TinyVector<MultiArrayIndex, N> & pointer_stride,
const stride_type & stride,
const shape_array_type & shape)
: base_type(ptr_type(pair_type(raw_pointer, origin), stride.dim0()),
detail::dismember<N>(pointer_stride, stride),
shape) {}
CombinedMultiIterator(const base_type & x) : base_type(x) {}
};
template<unsigned N, class T>
struct SrcCoordinateMultiArrayRangeReturns
{
typedef CombinedMultiIterator<N, T> iterator_type;
typedef typename iterator_type::coord_type coord_type;
typedef typename iterator_type::pair_type pair_type;
typedef typename iterator_type::ptr_type ptr_type;
typedef typename ptr_type::deref_type deref_type;
typedef typename iterator_type::stride_type stride_type;
typedef typename pair_type::stride_array_type stride_array_type;
typedef typename pair_type::shape_array_type shape_array_type;
typedef typename AccessorTraits<deref_type>::default_const_accessor
access_type;
typedef triple<iterator_type, stride_array_type, access_type> type;
};
// work around GCC 4.4.3 template argument deduction bug:
template<unsigned N>
struct CoordinateSteps
{
typedef const TinyVector<double, N> & type;
};
template <unsigned int N, class T, class StrideTag>
inline typename SrcCoordinateMultiArrayRangeReturns<N, T>::type
srcCoordinateMultiArrayRange(const MultiArrayView<N, T, StrideTag> & array,
typename CoordinateSteps<N>::type stride
= TinyVector<double, N>(1.0),
typename CoordinateSteps<N>::type origin
= TinyVector<double, N>(0.0))
{
typedef SrcCoordinateMultiArrayRangeReturns<N, T> returns;
typedef typename returns::type type;
typedef typename returns::stride_type stride_type;
typedef typename returns::access_type access_type;
typedef typename returns::iterator_type iterator_type;
typedef typename returns::shape_array_type shape_array_type;
shape_array_type shape = array.shape();
return type(iterator_type(array.traverser_begin().get(),
stride_type(origin),
array.stride(),
stride_type(stride),
shape),
detail::dismember<N>(stride_type(shape)),
access_type());
}
template <class VALUETYPE, class COORD>
struct AccessorCoordinatePair
{
typedef VALUETYPE value_type;
typedef COORD coord_type;
typedef AccessorCoordinatePair type;
value_type v;
const coord_type & c;
AccessorCoordinatePair(const value_type & v_, const coord_type & c_)
: v(v_), c(c_) {}
const value_type & value() const
{
return v;
}
const coord_type & coord() const
{
return c;
}
};
/** \brief Forward accessor to the value() part of the values an iterator
points to.
CoordinateConstValueAccessor is a accessor that forwards
the underlying accessor's operator() read functions.
It passes its arguments <em>by value</em>.
<b>\#include</b> \<vigra/coordinate_iterator.hxx\><br>
Namespace: vigra
*/
template <class Accessor, class COORD>
class CoordinateConstValueAccessor
{
public:
typedef typename Accessor::value_type forward_type;
typedef AccessorCoordinatePair<forward_type, COORD> value_type;
Accessor a;
CoordinateConstValueAccessor(const Accessor & a_) : a(a_) {}
/** Read the current data item.
*/
template <class ITERATOR>
value_type operator()(ITERATOR const & i) const
{
const typename ITERATOR::value_type & x = *i;
return value_type(a(&x.value()), x.coord());
}
/** Read the data item at an offset.
*/
template <class ITERATOR, class DIFFERENCE>
value_type operator()(ITERATOR const & i, DIFFERENCE const & diff) const
{
const typename ITERATOR::value_type & x = i[diff];
return value_type(a(&x.value()), x.coord());
}
};
template<unsigned N, class T, class Accessor>
struct SrcCoordinateMultiArrayRangeAccessorReturns
{
typedef CombinedMultiIterator<N, T> iterator_type;
typedef typename iterator_type::coord_type coord_type;
typedef typename iterator_type::pair_type pair_type;
typedef typename iterator_type::ptr_type ptr_type;
typedef typename ptr_type::deref_type deref_type;
typedef typename iterator_type::stride_type stride_type;
typedef typename pair_type::stride_array_type stride_array_type;
typedef typename pair_type::shape_array_type shape_array_type;
typedef CoordinateConstValueAccessor<Accessor, coord_type> access_type;
typedef triple<iterator_type, stride_array_type, access_type> type;
};
template <unsigned int N, class T, class StrideTag, class Access>
inline typename SrcCoordinateMultiArrayRangeAccessorReturns<N, T, Access>::type
srcCoordinateMultiArrayRangeAccessor(const MultiArrayView<N, T, StrideTag> &
array,
Access a,
typename CoordinateSteps<N>::type stride
= TinyVector<double, N>(1.0),
typename CoordinateSteps<N>::type origin
= TinyVector<double, N>(0.0))
{
typedef SrcCoordinateMultiArrayRangeAccessorReturns<N, T, Access> returns;
typedef typename returns::type type;
typedef typename returns::stride_type stride_type;
typedef typename returns::access_type access_type;
typedef typename returns::iterator_type iterator_type;
typedef typename returns::shape_array_type shape_array_type;
shape_array_type shape = array.shape();
return type(iterator_type(array.traverser_begin().get(),
stride_type(origin),
array.stride(),
stride_type(stride),
shape),
detail::dismember<N>(stride_type(shape)),
access_type(a));
}
} // namespace vigra
namespace std {
template <unsigned N>
ostream &
operator<<(ostream & os, const vigra::StridePair<N> & x)
{
os << "[" << x.index << ", " << x.coord << "]";
return os;
}
template <unsigned N>
ostream &
operator<<(ostream & os, const vigra::StridePairDiff<N> & x)
{
os << "<" << x.c << "; "
<< static_cast<vigra::StridePairCoord<N> >(x) << ">";
return os;
}
template <unsigned N, class T>
ostream &
operator<<(ostream & os, const vigra::StridePairPointer<N, T> & x)
{
os << "[" << x.value() << ", " << x.coord() << "]";
return os;
}
template <class VALUETYPE, class COORD>
ostream &
operator<<(ostream & os,
const vigra::AccessorCoordinatePair<VALUETYPE, COORD> & x)
{
os << "[" << x.value() << ", " << x.coord() << "]";
return os;
}
} // namespace std
#endif // VIGRA_COORDINATE_ITERATOR_HXX
|