Code

doc
[inkscape.git] / src / libnr / nr-rect.h
1 #ifndef LIBNR_NR_RECT_H_SEEN
2 #define LIBNR_NR_RECT_H_SEEN
4 /** \file
5  * Definitions of NRRect and NR::Rect types, and some associated functions \& macros.
6  */
7 /*
8  * Authors:
9  *   Lauris Kaplinski <lauris@kaplinski.com>
10  *   Nathan Hurst <njh@mail.csse.monash.edu.au>
11  *   MenTaLguY <mental@rydia.net>
12  *
13  * This code is in public domain
14  */
17 #include <stdexcept>
18 #include <limits>
20 #include "libnr/nr-values.h"
21 #include <libnr/nr-coord.h>
22 #include <libnr/nr-i-coord.h>
23 #include <libnr/nr-dim2.h>
24 #include <libnr/nr-point.h>
25 #include <libnr/nr-maybe.h>
26 #include <libnr/nr-point-matrix-ops.h>
28 namespace NR {
29     struct Matrix;
31 /** A rectangle is always aligned to the X and Y axis.  This means it
32  * can be defined using only 4 coordinates, and determining
33  * intersection is very efficient.  The points inside a rectangle are
34  * min[dim] <= _pt[dim] <= max[dim].  A rectangle may be empty, in the
35  * sense of having zero area, but it will always contain at least one
36  * point.  Infinities are also permitted.
37  */
38 class Rect {
39 public:
40     Rect() : _min(-_inf(), -_inf()), _max(_inf(), _inf()) {}
41     Rect(Point const &p0, Point const &p1);
43     Point const &min() const { return _min; }
44     Point const &max() const { return _max; }
46     /** returns the four corners of the rectangle in order
47      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
48     Point corner(unsigned i) const;
50     /** returns a vector from min to max. */
51     Point dimensions() const;
53     /** returns the midpoint of this rect. */
54     Point midpoint() const;
56     /** True iff either width or height is less than \a epsilon. */
57     bool isEmpty(double epsilon=1e-6) const {
58         return isEmpty<X>(epsilon) || isEmpty<Y>(epsilon);
59     }
61     bool intersects(Rect const &r) const {
62         return intersects<X>(r) && intersects<Y>(r);
63     }
64     bool contains(Rect const &r) const {
65         return contains<X>(r) && contains<Y>(r);
66     }
67     bool contains(Point const &p) const {
68         return contains<X>(p) && contains<Y>(p);
69     }
71     double area() const {
72         return extent<X>() * extent<Y>();
73     }
75     double maxExtent() const {
76         return MAX(extent<X>(), extent<Y>());
77     }
79     double extent(Dim2 const axis) const {
80         switch (axis) {
81             case X: return extent<X>();
82             case Y: return extent<Y>();
83             default: g_error("invalid axis value %d", (int) axis); return 0;
84         };
85     }
87     double extent(unsigned i) const throw(std::out_of_range) {
88         switch (i) {
89             case 0: return extent<X>();
90             case 1: return extent<Y>();
91             default: throw std::out_of_range("Dimension out of range");
92         };
93     }
95     /**
96         \brief  Remove some precision from the Rect
97         \param  places  The number of decimal places left in the end
99         This function just calls round on the \c _min and \c _max points.
100     */
101     inline void round(int places = 0) {
102         _min.round(places);
103         _max.round(places);
104         return;
105     }
107     /** Translates the rectangle by p. */
108     void offset(Point p);
110     /** Makes this rectangle large enough to include the point p. */
111     void expandTo(Point p);
113     /** Makes this rectangle large enough to include the rectangle r. */
114     void expandTo(Rect const &r);
116     inline void move_left (gdouble by) {
117         _min[NR::X] += by;
118     }
119     inline void move_right (gdouble by) {
120         _max[NR::X] += by;
121     }
122     inline void move_top (gdouble by) {
123         _min[NR::Y] += by;
124     }
125     inline void move_bottom (gdouble by) {
126         _max[NR::Y] += by;
127     }
129     void growBy (gdouble by);
131     /** Scales the rect by s, with origin at 0, 0 */
132     inline Rect operator*(double const s) const {
133         return Rect(s * min(), s * max());
134     }
136     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
137     inline Rect operator*(Matrix const m) const {
138         return Rect(_min * m, _max * m);
139     }
141     inline bool operator==(Rect const &in_rect) {
142         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
143     }
145     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
147 private:
148     Rect(Nothing) : _min(1, 1), _max(-1, -1) {}
150     static double _inf() {
151         return std::numeric_limits<double>::infinity();
152     }
154     template <NR::Dim2 axis>
155     double extent() const {
156         return _max[axis] - _min[axis];
157     }
159     template <NR::Dim2 axis>
160     bool isEmpty(double epsilon) const {
161         return extent<axis>() < epsilon;
162     }
164     template <Dim2 axis>
165     bool intersects(Rect const &r) const {
166         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
167     }
169     template <Dim2 axis>
170     bool contains(Rect const &r) const {
171         return contains(r._min) && contains(r._max);
172     }
174     template <Dim2 axis>
175     bool contains(Point const &p) const {
176         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
177     }
179     Point _min, _max;
181     friend class MaybeStorage<Rect>;
182     friend Maybe<Rect> intersection(Maybe<Rect> const &, Maybe<Rect> const &);
183     friend Rect union_bounds(Rect const &, Rect const &);
184 };
186 template <>
187 class MaybeStorage<Rect> {
188 public:
189     MaybeStorage() : _rect(Nothing()) {}
190     MaybeStorage(Rect const &rect) : _rect(rect) {}
192     bool is_nothing() const {
193         return _rect._min[X] > _rect._max[X];
194     }
195     Rect const &value() const { return _rect; }
196     Rect &value() { return _rect; }
198 private:
199     Rect _rect;
200 };
202 /** Returns the set of points shared by both rectangles. */
203 Maybe<Rect> intersection(Maybe<Rect> const & a, Maybe<Rect> const & b);
205 /** Returns the smallest rectangle that encloses both rectangles. */
206 Rect union_bounds(Rect const &a, Rect const &b);
207 inline Rect union_bounds(Maybe<Rect> const & a, Rect const &b) {
208     if (a) {
209         return union_bounds(*a, b);
210     } else {
211         return b;
212     }
214 inline Rect union_bounds(Rect const &a, Maybe<Rect> const & b) {
215     if (b) {
216         return union_bounds(a, *b);
217     } else {
218         return a;
219     }
221 inline Maybe<Rect> union_bounds(Maybe<Rect> const & a, Maybe<Rect> const & b)
223     if (!a) {
224         return b;
225     } else if (!b) {
226         return a;
227     } else {
228         return union_bounds(*a, *b);
229     }
232 /** A function to print out the rectange if sent to an output
233     stream. */
234 inline std::ostream
235 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
237     out_file << "Rectangle:\n";
238     out_file << "\tMin Point -> " << in_rect.min() << "\n";
239     out_file << "\tMax Point -> " << in_rect.max() << "\n";
241     return out_file;
244 } /* namespace NR */
246 /* legacy rect stuff */
248 struct NRMatrix;
250 /* NULL rect is infinite */
252 struct NRRect {
253     NRRect() {}
254     NRRect(NR::Coord xmin, NR::Coord ymin, NR::Coord xmax, NR::Coord ymax)
255     : x0(xmin), y0(ymin), x1(xmin), y1(ymin)
256     {}
257     explicit NRRect(NR::Rect const &rect);
258     explicit NRRect(NR::Maybe<NR::Rect> const &rect);
259     operator NR::Maybe<NR::Rect>() const { return upgrade(); }
260     NR::Maybe<NR::Rect> upgrade() const;
262     NR::Coord x0, y0, x1, y1;
263 };
265 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
266 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
268 /** "Empty" here includes the case of zero width or zero height. */
269 #define nr_rect_d_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
270 #define nr_rect_l_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
272 #define nr_rect_d_test_intersect(r0,r1) \
273         (!nr_rect_d_test_empty(r0) && !nr_rect_d_test_empty(r1) && \
274          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
275 #define nr_rect_l_test_intersect(r0,r1) \
276         (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
277          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
279 #define nr_rect_d_point_d_test_inside(r,p) ((p) && (!(r) || (!NR_RECT_DF_TEST_EMPTY(r) && NR_RECT_DF_POINT_DF_TEST_INSIDE(r,p))))
280 #define nr_rect_l_point_l_test_inside(r,p) ((p) && (!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_POINT_LS_TEST_INSIDE(r,p))))
281 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
283 // returns minimal rect which covers all of r0 not covered by r1
284 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
286 // returns the area of r
287 NR::ICoord nr_rect_l_area(NRRectL *r);
289 /* NULL values are OK for r0 and r1, but not for d */
290 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
291 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
293 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
294 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
296 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
297 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
298 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
300 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
301 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m);
302 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
305 #endif /* !LIBNR_NR_RECT_H_SEEN */
307 /*
308   Local Variables:
309   mode:c++
310   c-file-style:"stroustrup"
311   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
312   indent-tabs-mode:nil
313   fill-column:99
314   End:
315 */
316 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :