Code

add growBy and constructors from NRRect(L)
[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 struct NRRect;
29 struct NRRectL;
31 namespace NR {
32     struct Matrix;
34 /** A rectangle is always aligned to the X and Y axis.  This means it
35  * can be defined using only 4 coordinates, and determining
36  * intersection is very efficient.  The points inside a rectangle are
37  * min[dim] <= _pt[dim] <= max[dim].  A rectangle may be empty, in the
38  * sense of having zero area, but it will always contain at least one
39  * point.  Infinities are also permitted.
40  */
41 class Rect {
42 public:
43     Rect() : _min(-_inf(), -_inf()), _max(_inf(), _inf()) {}
44     Rect(Point const &p0, Point const &p1);
45     Rect(NRRect *r);
46     Rect(NRRectL *r);
48     Point const &min() const { return _min; }
49     Point const &max() const { return _max; }
51     /** returns the four corners of the rectangle in order
52      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
53     Point corner(unsigned i) const;
55     /** returns a vector from min to max. */
56     Point dimensions() const;
58     /** returns the midpoint of this rect. */
59     Point midpoint() const;
61     bool isEmpty(double epsilon=1e-6) const {
62         return isEmpty<X>(epsilon) || isEmpty<Y>(epsilon);
63     }
65     bool intersects(Rect const &r) const {
66         return intersects<X>(r) && intersects<Y>(r);
67     }
68     bool contains(Rect const &r) const {
69         return contains<X>(r) && contains<Y>(r);
70     }
71     bool contains(Point const &p) const {
72         return contains<X>(p) && contains<Y>(p);
73     }
75     double area() const {
76         return extent<X>() * extent<Y>();
77     }
79     double maxExtent() const {
80         return MAX(extent<X>(), extent<Y>());
81     }
83     double extent(Dim2 const axis) const {
84         switch (axis) {
85             case X: return extent<X>();
86             case Y: return extent<Y>();
87             default: g_error("invalid axis value %d", (int) axis); return 0;
88         };
89     }
91     double extent(unsigned i) const throw(std::out_of_range) {
92         switch (i) {
93             case 0: return extent<X>();
94             case 1: return extent<Y>();
95             default: throw std::out_of_range("Dimension out of range");
96         };
97     }
99     /**
100         \brief  Remove some precision from the Rect
101         \param  places  The number of decimal places left in the end
103         This function just calls round on the \c _min and \c _max points.
104     */
105     inline void round(int places = 0) {
106         _min.round(places);
107         _max.round(places);
108         return;
109     }
111     /** Translates the rectangle by p. */
112     void offset(Point p);
114     /** Makes this rectangle large enough to include the point p. */
115     void expandTo(Point p);
117     /** Makes this rectangle large enough to include the rectangle r. */
118     void expandTo(Rect const &r);
120     inline void move_left (gdouble by) {
121         _min[NR::X] += by;
122     }
123     inline void move_right (gdouble by) {
124         _max[NR::X] += by;
125     }
126     inline void move_top (gdouble by) {
127         _min[NR::Y] += by;
128     }
129     inline void move_bottom (gdouble by) {
130         _max[NR::Y] += by;
131     }
133     void growBy (gdouble by);
135     /** Scales the rect by s, with origin at 0, 0 */
136     inline Rect operator*(double const s) const {
137         return Rect(s * min(), s * max());
138     }
140     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
141     inline Rect operator*(Matrix const m) const {
142         return Rect(_min * m, _max * m);
143     }
145     inline bool operator==(Rect const &in_rect) {
146         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
147     }
149     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
151 private:
152     Rect(Nothing) : _min(1, 1), _max(-1, -1) {}
154     static double _inf() {
155         return std::numeric_limits<double>::infinity();
156     }
158     template <NR::Dim2 axis>
159     double extent() const {
160         return _max[axis] - _min[axis];
161     }
163     template <NR::Dim2 axis>
164     bool isEmpty(double epsilon) const {
165         return extent<axis>() < epsilon;
166     }
168     template <Dim2 axis>
169     bool intersects(Rect const &r) const {
170         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
171     }
173     template <Dim2 axis>
174     bool contains(Rect const &r) const {
175         return contains(r._min) && contains(r._max);
176     }
178     template <Dim2 axis>
179     bool contains(Point const &p) const {
180         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
181     }
183     Point _min, _max;
185     friend class MaybeStorage<Rect>;
186     friend Maybe<Rect> intersection(Maybe<Rect> const &, Maybe<Rect> const &);
187     friend Rect union_bounds(Rect const &, Rect const &);
188 };
190 template <>
191 class MaybeStorage<Rect> {
192 public:
193     MaybeStorage() : _rect(Nothing()) {}
194     MaybeStorage(Rect const &rect) : _rect(rect) {}
196     bool is_nothing() const {
197         return _rect._min[X] > _rect._max[X];
198     }
199     Rect const &value() const { return _rect; }
200     Rect &value() { return _rect; }
202 private:
203     Rect _rect;
204 };
206 /** Returns the set of points shared by both rectangles. */
207 Maybe<Rect> intersection(Maybe<Rect> const & a, Maybe<Rect> const & b);
209 /** Returns the smallest rectangle that encloses both rectangles. */
210 Rect union_bounds(Rect const &a, Rect const &b);
211 inline Rect union_bounds(Maybe<Rect> const & a, Rect const &b) {
212     if (a) {
213         return union_bounds(*a, b);
214     } else {
215         return b;
216     }
218 inline Rect union_bounds(Rect const &a, Maybe<Rect> const & b) {
219     if (b) {
220         return union_bounds(a, *b);
221     } else {
222         return a;
223     }
225 inline Maybe<Rect> union_bounds(Maybe<Rect> const & a, Maybe<Rect> const & b)
227     if (!a) {
228         return b;
229     } else if (!b) {
230         return a;
231     } else {
232         return union_bounds(*a, *b);
233     }
236 /** A function to print out the rectange if sent to an output
237     stream. */
238 inline std::ostream
239 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
241     out_file << "Rectangle:\n";
242     out_file << "\tMin Point -> " << in_rect.min() << "\n";
243     out_file << "\tMax Point -> " << in_rect.max() << "\n";
245     return out_file;
248 } /* namespace NR */
250 /* legacy rect stuff */
252 struct NRMatrix;
254 /* NULL rect is infinite */
256 struct NRRect {
257     NRRect() {}
258     NRRect(NR::Coord xmin, NR::Coord ymin, NR::Coord xmax, NR::Coord ymax)
259     : x0(xmin), y0(ymin), x1(xmin), y1(ymin)
260     {}
261     explicit NRRect(NR::Rect const &rect);
262     explicit NRRect(NR::Maybe<NR::Rect> const &rect);
263     operator NR::Maybe<NR::Rect>() const { return upgrade(); }
264     NR::Maybe<NR::Rect> upgrade() const;
266     NR::Coord x0, y0, x1, y1;
267 };
269 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
270 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
272 #define nr_rect_d_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
273 #define nr_rect_l_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
275 #define nr_rect_d_test_intersect(r0,r1) \
276         (!nr_rect_d_test_empty(r0) && !nr_rect_d_test_empty(r1) && \
277          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
278 #define nr_rect_l_test_intersect(r0,r1) \
279         (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
280          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
282 #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))))
283 #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))))
284 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
286 // returns minimal rect which covers all of r0 not covered by r1
287 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
289 // returns the area of r
290 NR::ICoord nr_rect_l_area(NRRectL *r);
292 /* NULL values are OK for r0 and r1, but not for d */
293 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
294 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
296 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
297 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
299 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
300 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
301 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
303 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
304 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m);
305 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
308 #endif /* !LIBNR_NR_RECT_H_SEEN */
310 /*
311   Local Variables:
312   mode:c++
313   c-file-style:"stroustrup"
314   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
315   indent-tabs-mode:nil
316   fill-column:99
317   End:
318 */
319 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :