1
/************************************************************************/
3
/* Copyright 1998-2003 by Hans Meine */
4
/* Cognitive Systems Group, University of Hamburg, Germany */
6
/* This file is part of the VIGRA computer vision library. */
7
/* The VIGRA Website is */
8
/* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */
9
/* Please direct questions, bug reports, and contributions to */
10
/* koethe@informatik.uni-hamburg.de or */
11
/* vigra@kogs1.informatik.uni-hamburg.de */
13
/* Permission is hereby granted, free of charge, to any person */
14
/* obtaining a copy of this software and associated documentation */
15
/* files (the "Software"), to deal in the Software without */
16
/* restriction, including without limitation the rights to use, */
17
/* copy, modify, merge, publish, distribute, sublicense, and/or */
18
/* sell copies of the Software, and to permit persons to whom the */
19
/* Software is furnished to do so, subject to the following */
22
/* The above copyright notice and this permission notice shall be */
23
/* included in all copies or substantial portions of the */
26
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
27
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
28
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
29
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
30
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
31
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
32
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
33
/* OTHER DEALINGS IN THE SOFTWARE. */
35
/************************************************************************/
37
#ifndef VIGRA_DIFF2D_HXX
38
#define VIGRA_DIFF2D_HXX
40
#include <cmath> // for sqrt()
42
#include "vigra/config.hxx"
43
#include "vigra/iteratortags.hxx"
44
#include "vigra/iteratortraits.hxx"
45
#include "vigra/iteratoradapter.hxx"
46
#include "vigra/tuple.hxx"
51
class Diff2DConstRowIteratorPolicy
54
typedef Diff BaseType;
55
typedef Diff value_type;
56
typedef typename Diff::MoveX difference_type;
57
typedef Diff const & reference;
58
typedef Diff index_reference;
59
typedef Diff const * pointer;
60
typedef std::random_access_iterator_tag iterator_category;
62
static void initialize(BaseType &) {}
64
static reference dereference(BaseType const & d)
67
static index_reference dereference(BaseType d, difference_type n)
73
static bool equal(BaseType const & d1, BaseType const & d2)
74
{ return d1.x == d2.x; }
76
static bool less(BaseType const & d1, BaseType const & d2)
77
{ return d1.x < d2.x; }
79
static difference_type difference(BaseType const & d1, BaseType const & d2)
80
{ return d1.x - d2.x; }
82
static void increment(BaseType & d)
85
static void decrement(BaseType & d)
88
static void advance(BaseType & d, difference_type n)
93
class Diff2DConstColumnIteratorPolicy
96
typedef Diff BaseType;
97
typedef Diff value_type;
98
typedef typename Diff::MoveY difference_type;
99
typedef Diff const & reference;
100
typedef Diff index_reference;
101
typedef Diff const * pointer;
102
typedef std::random_access_iterator_tag iterator_category;
104
static void initialize(BaseType & /*d*/) {}
106
static reference dereference(BaseType const & d)
109
static index_reference dereference(BaseType d, difference_type n)
115
static bool equal(BaseType const & d1, BaseType const & d2)
116
{ return d1.y == d2.y; }
118
static bool less(BaseType const & d1, BaseType const & d2)
119
{ return d1.y < d2.y; }
121
static difference_type difference(BaseType const & d1, BaseType const & d2)
122
{ return d1.y - d2.y; }
124
static void increment(BaseType & d)
127
static void decrement(BaseType & d)
130
static void advance(BaseType & d, difference_type n)
134
/** \addtogroup RangesAndPoints Two-dimensional Ranges and Points
136
Specify a 2D position, extent, or rectangle.
140
/********************************************************/
144
/********************************************************/
146
/** \brief Two dimensional difference vector.
148
This class acts primarily as a difference vector for specifying
149
pixel coordinates and region sizes. In addition, Diff2D fulfills
150
the requirements of an \ref ImageIterator, so that it can be used to
151
simulate an image whose pixels' values equal their coordinates. This
152
secondary usage is explained on page \ref CoordinateIterator.
154
Standard usage as a difference vector is mainly needed in the context
155
of images. For example, Diff2D may be used as an index for <TT>operator[]</TT>:
158
vigra::Diff2D location(...);
160
value = image[location];
163
This is especially important in connection with accessors, where the
164
offset variant of <TT>operator()</TT> takes only one offset object:
167
// accessor(iterator, dx, dy); is not allowed
168
value = accessor(iterator, vigra::Diff2D(dx, dy));
172
Diff2D is also returned by <TT>image.size()</TT>, so that we can create
173
new images by calculating their size using Diff2D's arithmetic
177
// create an image that is 10 pixels smaller in each direction
178
Image new_image(old_image.size() - Diff2D(10,10));
181
<b>\#include</b> "<a href="diff2d_8hxx-source.html">vigra/utilities.hxx</a>"<br>
187
/** The iterator's value type: a coordinate.
189
typedef Diff2D PixelType;
191
/** The iterator's value type: a coordinate.
193
typedef Diff2D value_type;
195
/** the iterator's reference type (return type of <TT>*iter</TT>)
197
typedef Diff2D const & reference;
199
/** the iterator's index reference type (return type of <TT>iter[diff]</TT>)
201
typedef Diff2D index_reference;
203
/** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
205
typedef Diff2D const * pointer;
207
/** the iterator's difference type (argument type of <TT>iter[diff]</TT>)
209
typedef Diff2D difference_type;
211
/** the iterator tag (image traverser)
213
typedef image_traverser_tag iterator_category;
215
/** The associated row iterator.
217
typedef IteratorAdaptor<Diff2DConstRowIteratorPolicy<Diff2D> > row_iterator;
219
/** The associated column iterator.
221
typedef IteratorAdaptor<Diff2DConstColumnIteratorPolicy<Diff2D> > column_iterator;
223
/** type of the iterator's x-navigator
226
/** type of the iterator's y-navigator
231
/** Default Constructor. Init iterator at position (0,0)
237
/** Construct at given position.
239
Diff2D(int ax, int ay)
243
/** Copy Constructor.
245
Diff2D(Diff2D const & v)
251
Diff2D & operator=(Diff2D const & v)
263
Diff2D operator-() const
265
return Diff2D(-x, -y);
268
/** Increase coordinate by specified offset.
270
Diff2D & operator+=(Diff2D const & offset)
277
/** Decrease coordinate by specified vector.
279
Diff2D & operator-=(Diff2D const & offset)
286
/** Create vector by scaling by factor.
288
Diff2D & operator*=(int factor)
295
/** Create vector by scaling by factor.
297
Diff2D & operator*=(double factor)
299
x = (int)(x * factor);
300
y = (int)(y * factor);
304
/** Create vector by scaling by 1/factor.
306
Diff2D & operator/=(int factor)
313
/** Create vector by scaling by 1/factor.
315
Diff2D & operator/=(double factor)
317
x = (int)(x / factor);
318
y = (int)(y / factor);
322
/** Create vector by scaling by factor.
324
Diff2D operator*(int factor) const
326
return Diff2D(x * factor, y * factor);
329
/** Create vector by scaling by factor.
331
Diff2D operator*(double factor) const
333
return Diff2D((int)(x * factor), (int)(y * factor));
336
/** Create vector by scaling by 1/factor.
338
Diff2D operator/(int factor) const
340
return Diff2D(x / factor, y / factor);
343
/** Create vector by scaling by 1/factor.
345
Diff2D operator/(double factor) const
347
return Diff2D((int)(x / factor), (int)(y / factor));
350
/** Calculate length of difference vector.
352
int squaredMagnitude() const
357
/** Calculate length of difference vector.
359
double magnitude() const
361
return VIGRA_CSTD::sqrt((double)squaredMagnitude());
366
bool operator==(Diff2D const & r) const
368
return (x == r.x) && (y == r.y);
373
bool operator!=(Diff2D const & r) const
375
return (x != r.x) || (y != r.y);
378
/** Used for both access to the current x-coordinate \em and
379
to specify that an iterator navigation command is to be
380
applied in x-direction. <br>
381
usage: <TT> x = diff2d.x </TT> (use \p Diff2D::x as component of difference vector) <br>
382
or <TT> ++diff.x </TT> (use Diff2D as iterator, move right)
385
/** Used for both access to the current y-coordinate \em and
386
to specify that an iterator navigation command is to be
387
applied in y-direction. <br>
388
usage: <TT> y = diff2d.y </TT> (use \p Diff2D::y as component of difference vector) <br>
389
or <TT> ++diff.y </TT> (use Diff2D as iterator, move right)
393
/** Access current coordinate.
395
reference operator*() const
400
/** Read coordinate at an offset.
402
index_reference operator()(int const & dx, int const & dy) const
404
return Diff2D(x + dx, y + dy);
407
/** Read coordinate at an offset.
409
index_reference operator[](Diff2D const & offset) const
411
return Diff2D(x + offset.x, y + offset.y);
414
/** Read vector components.
416
int operator[](int index) const
421
/** Access current coordinate.
423
pointer operator->() const
428
/** Get a row iterator at the current position.
430
row_iterator rowIterator() const
431
{ return row_iterator(*this); }
433
/** Get a column iterator at the current position.
435
column_iterator columnIterator() const
436
{ return column_iterator(*this); }
441
struct IteratorTraits<Diff2D >
443
typedef Diff2D Iterator;
444
typedef Iterator iterator;
445
typedef Iterator const_iterator;
446
// typedef multable_iterator; undefined
447
typedef iterator::iterator_category iterator_category;
448
typedef iterator::value_type value_type;
449
typedef iterator::reference reference;
450
typedef iterator::index_reference index_reference;
451
typedef iterator::pointer pointer;
452
typedef iterator::difference_type difference_type;
453
typedef iterator::row_iterator row_iterator;
454
typedef iterator::column_iterator column_iterator;
455
typedef StandardConstValueAccessor<Diff2D> DefaultAccessor;
456
typedef StandardConstValueAccessor<Diff2D> default_accessor;
457
typedef VigraTrueType hasConstantStrides;
462
/********************************************************/
466
/********************************************************/
468
/** \brief Two dimensional size object.
470
Specializes \ref Diff2D for the specification of a 2-dimensional
471
extent, in contrast to a point or position (for the latter
475
// create an image that is 10 pixels squared
476
Image new_image(Size2D(10,10));
479
<b>\#include</b> "<a href="diff2d_8hxx-source.html">vigra/utilities.hxx</a>"<br>
482
class Size2D : public Diff2D
485
/** Default Constructor. Init point at position (0,0)
490
/** Construct point at given position.
492
Size2D(int width, int height)
493
: Diff2D(width, height)
496
/** Copy Constructor.
498
Size2D(Size2D const & v)
502
/** Explicit conversion Constructor.
504
explicit Size2D(Diff2D const & v)
515
/** Query the height.
522
/** Change the width.
529
/** Change the height.
531
void setHeight(int h)
536
/** Returns width()*height(), the area of a rectangle of this size.
540
return width()*height();
545
Size2D & operator=(Diff2D const & v)
547
return static_cast<Size2D &>(Diff2D::operator=(v));
552
Size2D operator-() const
554
return Size2D(-x, -y);
557
/** Increase size by specified offset.
559
Size2D & operator+=(Diff2D const & offset)
561
return static_cast<Size2D &>(Diff2D::operator+=(offset));
564
/** Decrease size by specified offset.
566
Size2D & operator-=(Diff2D const & offset)
568
return static_cast<Size2D &>(Diff2D::operator-=(offset));
572
/********************************************************/
576
/********************************************************/
578
/** \brief Two dimensional point or position.
580
Specializes \ref Diff2D for the specification of a 2-dimensional
581
point or position, in contrast to an extent (for the latter
585
// access an image at a point
586
value = image[Point2D(10, 20)];
589
<b>\#include</b> "<a href="diff2d_8hxx-source.html">vigra/utilities.hxx</a>"<br>
592
class Point2D : public Diff2D
595
/** The iterator's value type: a coordinate.
597
typedef Point2D PixelType;
599
/** The iterator's value type: a coordinate.
601
typedef Point2D value_type;
603
/** the iterator's reference type (return type of <TT>*iter</TT>)
605
typedef Point2D const & reference;
607
/** the iterator's index reference type (return type of <TT>iter[diff]</TT>)
609
typedef Point2D index_reference;
611
/** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
613
typedef Point2D const * pointer;
615
/** Default Constructor. Init point at position (0,0)
620
/** Construct point at given position.
622
Point2D(int x, int y)
626
/** Copy Constructor.
628
Point2D(Point2D const & v)
632
/** Explicit conversion Constructor.
634
explicit Point2D(Diff2D const & v)
638
/** Query the points' x coordinate
645
/** Query the points' y coordinate
654
Point2D & operator=(Diff2D const & v)
656
return static_cast<Point2D &>(Diff2D::operator=(v));
661
Point2D operator-() const
663
return Point2D(-x, -y);
666
/** Increase point coordinates by specified offset.
668
Point2D & operator+=(Diff2D const & offset)
670
return static_cast<Point2D &>(Diff2D::operator+=(offset));
673
/** Decrease point coordinates by specified offset.
675
Point2D & operator-=(Diff2D const & offset)
677
return static_cast<Point2D &>(Diff2D::operator-=(offset));
680
/** Access current point coordinate.
682
reference operator*() const
687
/** Read point coordinate at an offset.
689
index_reference operator()(int const & dx, int const & dy) const
691
return Point2D(x + dx, y + dy);
694
/** Read point coordinate at an offset.
696
index_reference operator[](Diff2D const & offset) const
698
return Point2D(x + offset.x, y + offset.y);
701
/** Access current point coordinate.
703
pointer operator->() const
709
/** Create vector by subtracting specified offset.
711
inline Diff2D operator-(Diff2D const &a, Diff2D const &b)
713
return Diff2D(a.x - b.x, a.y - b.y);
716
/** Create size by subtracting specified offset.
718
inline Size2D operator-(Size2D const & s, Diff2D const &offset)
720
return Size2D(s.x - offset.x, s.y - offset.y);
723
/** Calculate size of rect between two points.
725
inline Point2D operator-(Point2D const & s, Diff2D const & offset)
727
return Point2D(s.x - offset.x, s.y - offset.y);
730
/** The difference of two points is a size
732
inline Size2D operator-(Point2D const & s, Point2D const & p)
734
return Size2D(s.x - p.x, s.y - p.y);
737
/** Create vector by adding specified offset.
739
inline Diff2D operator+(Diff2D const &a, Diff2D const &b)
741
return Diff2D(a.x + b.x, a.y + b.y);
744
/** Create size by adding specified offset.
746
inline Size2D operator+(Size2D const &a, Diff2D const &b)
748
return Size2D(a.x + b.x, a.y + b.y);
751
/** Create point by adding specified offset.
753
inline Point2D operator+(Point2D const &a, Diff2D const &b)
755
return Point2D(a.x + b.x, a.y + b.y);
758
/** Add size and point
760
inline Point2D operator+(Size2D const & s, Point2D const & p)
762
return Point2D(s.x + p.x, s.y + p.y);
765
inline Point2D operator*(Point2D l, double r)
771
inline Point2D operator*(double l, Point2D r)
777
inline Size2D operator*(Size2D l, double r)
783
inline Size2D operator*(double l, Size2D r)
789
inline Point2D operator/(Point2D l, double r)
795
inline Size2D operator/(Size2D l, double r)
801
inline Point2D operator*(Point2D l, int r)
807
inline Point2D operator*(int l, Point2D r)
813
inline Size2D operator*(Size2D l, int r)
819
inline Size2D operator*(int l, Size2D r)
825
inline Point2D operator/(Point2D l, int r)
831
inline Size2D operator/(Size2D l, int r)
838
/********************************************************/
842
/********************************************************/
844
/** \brief Two dimensional rectangle.
846
This class stores a 2-dimensional rectangular range or region. Thus,
847
it follows the VIGRA convention that the upper left corner is inside
848
the rectangle, while the lower right is 1 pixel to the right and below the
849
last pixel in the rectangle.
851
A major advantage of this class is that it can be constructed from either
852
a pair of \ref Point2D, or from a \ref Point2D and an extend
853
(\ref Size2D). Rect2D overloads operators |=, &=, |, & to realize set
854
union (in the sense of a minimal bounding rectangle) and set intersection.
857
Rect2D r1(Point2D(0,0), Point2D(10, 20)),
858
r2(Point2D(10, 15), Size2D(20, 20));
861
Rect2D r3 = r1 | r2; // upper left is (0,0), lower right is (30, 35)
862
assert(r3.contains(r2));
863
assert(!r3.contains(p));
865
r3 |= p; // lower right now (30,101) so that p is inside r3
866
assert(r3.contains(p));
869
<b>\#include</b> "<a href="diff2d_8hxx-source.html">vigra/utilities.hxx</a>"<br>
874
Point2D upperLeft_, lowerRight_;
877
/** Construct a null rectangle (isEmpty() will return true)
882
/** Construct a rectangle representing the given range
883
* (lowerRight is considered to be outside the rectangle as
884
* usual in the VIGRA)
886
Rect2D(Point2D const &upperLeft, Point2D const &lowerRight)
887
: upperLeft_(upperLeft), lowerRight_(lowerRight)
890
/** Construct a rectangle representing the given range
892
Rect2D(int left, int top, int right, int bottom)
893
: upperLeft_(left, top), lowerRight_(right, bottom)
896
/** Construct a rectangle of given position and size
898
Rect2D(Point2D const &upperLeft, Size2D const &size)
899
: upperLeft_(upperLeft), lowerRight_(upperLeft + size)
902
/** Construct a rectangle of given size at position (0,0)
904
explicit Rect2D(Size2D const &size)
905
: lowerRight_(Point2D(size))
908
/** Return the first point (scan-order wise) which is
909
* considered to be "in" the rectangle.
911
Point2D const & upperLeft() const
916
/** Return the first point to the right and below the
919
Point2D const & lowerRight() const
924
/** Change upperLeft() without changing lowerRight(), which
925
* will change the size most probably.
927
void setUpperLeft(Point2D const &ul)
932
/** Change lowerRight() without changing upperLeft(), which
933
* will change the size most probably.
935
void setLowerRight(Point2D const &lr)
940
/** Move the whole rectangle so that the given point will be
941
* upperLeft() afterwards.
943
void moveTo(Point2D const &newUpperLeft)
945
lowerRight_ += newUpperLeft - upperLeft_;
946
upperLeft_ = newUpperLeft;
949
/** Move the whole rectangle so that upperLeft() will become
950
* Point2D(left, top) afterwards.
952
void moveTo(int left, int top)
954
moveTo(Point2D(left, top));
957
/** Move the whole rectangle by the given 2D offset.
959
void moveBy(Diff2D const &offset)
961
upperLeft_ += offset;
962
lowerRight_ += offset;
965
/** Move the whole rectangle by the given x- and y-offsets.
967
void moveBy(int xOffset, int yOffset)
969
moveBy(Diff2D(xOffset, yOffset));
972
/** Return the left coordinate of this rectangle.
979
/** Return the top coordinate of this rectangle.
986
/** Return the right coordinate of this rectangle. That is the
987
* first column to the right of the rectangle.
991
return lowerRight_.x;
994
/** Return the bottom coordinate of this rectangle. That is the
995
* first row below the rectangle.
999
return lowerRight_.y;
1002
/** Determine and return the width of this rectangle. It might be
1003
* zero or even negative, and if so, isEmpty() will return true.
1007
return lowerRight_.x - upperLeft_.x;
1010
/** Determine and return the height of this rectangle. It might be
1011
* zero or even negative, and if so, isEmpty() will return true.
1015
return lowerRight_.y - upperLeft_.y;
1018
/** Determine and return the area of this rectangle. That is, if
1019
* this rect isEmpty(), returns zero, otherwise returns
1022
long long area() const
1024
return isEmpty() ? 0LL : (long long)width()*(long long)height();
1027
/** Determine and return the size of this rectangle. The width
1028
* and/or height might be zero or even negative, and if so,
1029
* isEmpty() will return true.
1033
return lowerRight_ - upperLeft_;
1036
/** Resize this rectangle to the given extents. This will move
1037
* the lower right corner only.
1039
void setSize(Size2D const &size)
1041
lowerRight_ = upperLeft_ + size;
1044
/** Resize this rectangle to the given extents. This will move
1045
* the lower right corner only.
1047
void setSize(int width, int height)
1049
lowerRight_ = upperLeft_ + Size2D(width, height);
1052
/** Increase the size of the rectangle by the given offset. This
1053
* will move the lower right corner only. (If any of offset's
1054
* components is negative, the rectangle will get smaller
1057
void addSize(Size2D const &offset)
1059
lowerRight_ += offset;
1062
/** Adds a border of the given width around the rectangle. That
1063
* means, upperLeft()'s components are moved by -borderWidth
1064
* and lowerRight()'s by borderWidth. (If borderWidth is
1065
* negative, the rectangle will get smaller accordingly.)
1067
void addBorder(int borderWidth)
1069
upperLeft_ += Diff2D(-borderWidth, -borderWidth);
1070
lowerRight_ += Diff2D(borderWidth, borderWidth);
1073
/** Adds a border with possibly different widths in x- and
1074
* y-directions around the rectangle. That means, each x
1075
* component is moved borderWidth pixels and each y component
1076
* is moved borderHeight pixels to the outside. (If
1077
* borderWidth is negative, the rectangle will get smaller
1080
void addBorder(int borderWidth, int borderHeight)
1082
upperLeft_ += Diff2D(-borderWidth, -borderHeight);
1083
lowerRight_ += Diff2D(borderWidth, borderHeight);
1087
bool operator==(Rect2D const &r) const
1089
return (upperLeft_ == r.upperLeft_) && (lowerRight_ == r.lowerRight_);
1092
/// inequality check
1093
bool operator!=(Rect2D const &r) const
1095
return (upperLeft_ != r.upperLeft_) || (lowerRight_ != r.lowerRight_);
1098
/** Return whether this rectangle is considered empty. It is
1099
* non-empty if both coordinates of the lower right corner are
1100
* greater than the corresponding coordinate of the upper left
1101
* corner. Uniting an empty rectangle with something will return
1102
* the bounding rectangle of the 'something', intersecting with an
1103
* empty rectangle will yield again an empty rectangle.
1105
bool isEmpty() const
1107
return ((lowerRight_.x <= upperLeft_.x) ||
1108
(lowerRight_.y <= upperLeft_.y));
1111
/** Return whether this rectangle contains the given point. That
1112
* is, if the point lies within the valid range of an
1113
* ImageIterator walking from upperLeft() to lowerRight()
1114
* (excluding the latter).
1116
bool contains(Point2D const &p) const
1118
return ((upperLeft_.x <= p.x) &&
1119
(upperLeft_.y <= p.y) &&
1120
(p.x < lowerRight_.x) &&
1121
(p.y < lowerRight_.y));
1124
/** Return whether this rectangle contains the given
1125
* one. <tt>r1.contains(r2)</tt> returns the same as
1126
* <tt>r1 == (r1|r2)</tt> (but is of course more
1127
* efficient). That also means, a rectangle (even an empty one!)
1128
* contains() any empty rectangle.
1130
bool contains(Rect2D const &r) const
1132
return r.isEmpty() ||
1133
(contains(r.upperLeft()) && contains(r.lowerRight()-Diff2D(1,1)));
1136
/** Return whether this rectangle overlaps with the given
1137
* one. <tt>r1.intersects(r2)</tt> returns the same as
1138
* <tt>!(r1&r2).isEmpty()</tt> (but is of course much more
1141
bool intersects(Rect2D const &r) const
1143
return ((r.upperLeft_.x < lowerRight_.x) &&
1144
(upperLeft_.x < r.lowerRight_.x) &&
1145
(r.upperLeft_.y < lowerRight_.y) &&
1146
(upperLeft_.y < r.lowerRight_.y))
1150
/** Modifies this rectangle by including the given point. The
1151
* result is the bounding rectangle of the rectangle and the
1152
* point. If isEmpty returns true, the union will be a
1153
* rectangle containing only the given point.
1155
Rect2D &operator|=(Point2D const &p)
1160
lowerRight_ = p + Diff2D(1, 1);
1164
if(p.x < upperLeft_.x)
1166
if(p.y < upperLeft_.y)
1168
if(lowerRight_.x <= p.x)
1169
lowerRight_.x = p.x + 1;
1170
if(lowerRight_.y <= p.y)
1171
lowerRight_.y = p.y + 1;
1176
/** Returns the union of this rectangle and the given
1177
* point. The result is the bounding rectangle of the
1178
* rectangle and the point. If isEmpty returns true, the union
1179
* will be a rectangle containing only the given point.
1181
Rect2D operator|(Point2D const &p) const
1183
Rect2D result(*this);
1188
/** Modifies this rectangle by uniting it with the given
1189
* one. The result is the bounding rectangle of both
1190
* rectangles. If one of the rectangles isEmpty(), the union
1191
* will be the other one.
1193
Rect2D &operator|=(Rect2D const &r)
1198
return operator=(r);
1200
if(r.upperLeft_.x < upperLeft_.x)
1201
upperLeft_.x = r.upperLeft_.x;
1202
if(r.upperLeft_.y < upperLeft_.y)
1203
upperLeft_.y = r.upperLeft_.y;
1204
if(lowerRight_.x < r.lowerRight_.x)
1205
lowerRight_.x = r.lowerRight_.x;
1206
if(lowerRight_.y < r.lowerRight_.y)
1207
lowerRight_.y = r.lowerRight_.y;
1211
/** Returns the union of this rectangle and the given one. The
1212
* result is the bounding rectangle of both rectangles. If one
1213
* of the rectangles isEmpty(), the union will be the other
1216
Rect2D operator|(Rect2D const &r) const
1218
Rect2D result(*this);
1223
/** Modifies this rectangle by intersecting it with the given
1224
* point. The result is the bounding rect of the point (with
1225
* width and height equal to 1) if it was contained in the
1226
* original rect, or an empty rect otherwise.
1228
Rect2D &operator&=(Point2D const &p)
1233
lowerRight_ = p + Diff2D(1, 1);
1236
lowerRight_ = upperLeft_;
1240
/** Intersects this rectangle with the given point. The result
1241
* is the bounding rect of the point (with width and height
1242
* equal to 1) if it was contained in the original rect, or an
1243
* empty rect otherwise.
1245
Rect2D operator&(Point2D const &p) const
1247
Rect2D result(*this);
1252
/** Modifies this rectangle by intersecting it with the given
1253
* one. The result is the maximal rectangle contained in both
1254
* original ones. Intersecting with an empty rectangle will
1255
* yield again an empty rectangle.
1257
Rect2D &operator&=(Rect2D const &r)
1262
return operator=(r);
1264
if(upperLeft_.x < r.upperLeft_.x)
1265
upperLeft_.x = r.upperLeft_.x;
1266
if(upperLeft_.y < r.upperLeft_.y)
1267
upperLeft_.y = r.upperLeft_.y;
1268
if(r.lowerRight_.x < lowerRight_.x)
1269
lowerRight_.x = r.lowerRight_.x;
1270
if(r.lowerRight_.y < lowerRight_.y)
1271
lowerRight_.y = r.lowerRight_.y;
1275
/** Intersects this rectangle with the given one. The result
1276
* is the maximal rectangle contained in both original ones.
1277
* Intersecting with an empty rectangle will yield again an
1280
Rect2D operator&(Rect2D const &r) const
1282
Rect2D result(*this);
1287
/** create iterators to iterate over this ROI
1289
* @param image iterators to source image
1291
* @return iterator set, to iterate over this ROI
1293
template <typename ImgIter, typename ImgAccessor>
1294
vigra::triple<ImgIter, ImgIter, ImgAccessor>
1295
apply(vigra::triple<ImgIter, ImgIter, ImgAccessor> image) const
1297
return vigra::make_triple(image.first + upperLeft_, image.first + lowerRight_, image.third);
1300
/** create iterators to iterate over the ROI
1302
* @param image iterators to source image (the source image should
1303
* cover the whole ROI)
1305
* @return iterator set, to iterate over this ROI
1307
template <typename ImgIter, typename ImgAccessor>
1308
std::pair<ImgIter, ImgAccessor>
1309
apply(std::pair<ImgIter, ImgAccessor> image) const
1311
return std::make_pair(image.first + upperLeft_, image.second);
1316
/********************************************************/
1320
/********************************************************/
1322
/** @deprecated use \ref vigra::Diff2D instead
1327
Dist2D(int the_width, int the_height)
1332
Dist2D(Dist2D const & s)
1337
Dist2D & operator=(Dist2D const & s)
1347
Dist2D & operator+=(Dist2D const & s)
1355
Dist2D operator+(Dist2D const & s) const
1364
{ return Diff2D(width, height); }
1373
* Output a \ref vigra::Diff2D as a tuple.
1374
* Example Diff2D(-12, 13) -> "(-12, 13)"
1377
std::ostream & operator<<(std::ostream & o, vigra::Diff2D const & d)
1379
o << '(' << d.x << ", " << d.y << ')';
1384
* Output a \ref vigra::Size2D.
1385
* Example Size2D(100, 200) -> "(100x200)"
1388
std::ostream &operator <<(std::ostream &s, vigra::Size2D const &d)
1390
s << '(' << d.x << 'x' << d.y << ')';
1395
* Output a description of a \ref vigra::Rect2D.
1396
* Example Rect2D(10, 10, 30, 20) -> "[(10, 10) to (30, 20) = (20x10)]"
1399
std::ostream &operator <<(std::ostream &s, vigra::Rect2D const &r)
1401
s << "[" << r.upperLeft() << " to " << r.lowerRight()
1402
<< " = " << r.size() << "]";
1406
} // namespace vigra
1408
#endif // VIGRA_DIFF2D_HXX