Code

Separate NRRect and NR::Rect a bit further; the goal is to get to the point where...
[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].  Emptiness, however, is defined
35  * as having zero area, meaning an empty rectangle may still contain
36  * points.  Infinities are also permitted. */
37 class Rect {
38 public:
39     Rect() : _min(-_inf(), -_inf()), _max(_inf(), _inf()) {}
40     Rect(Point const &p0, Point const &p1);
42     Point const &min() const { return _min; }
43     Point const &max() const { return _max; }
45     /** returns the four corners of the rectangle in order
46      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
47     Point corner(unsigned i) const;
49     /** returns a vector from min to max. */
50     Point dimensions() const;
52     /** returns the midpoint of this rect. */
53     Point midpoint() const;
55     /** does this rectangle have zero area? */
56     bool isEmpty() const {
57         return isEmpty<X>() || isEmpty<Y>();
58     }
60     bool intersects(Rect const &r) const {
61         return intersects<X>(r) && intersects<Y>(r);
62     }
63     bool contains(Rect const &r) const {
64         return contains<X>(r) && contains<Y>(r);
65     }
66     bool contains(Point const &p) const {
67         return contains<X>(p) && contains<Y>(p);
68     }
70     double area() const {
71         return extent<X>() * extent<Y>();
72     }
74     double maxExtent() const {
75         return MAX(extent<X>(), extent<Y>());
76     }
78     double extent(Dim2 const axis) const {
79         switch (axis) {
80             case X: return extent<X>();
81             case Y: return extent<Y>();
82             default: g_error("invalid axis value %d", (int) axis); return 0;
83         };
84     }
86     double extent(unsigned i) const throw(std::out_of_range) {
87         switch (i) {
88             case 0: return extent<X>();
89             case 1: return extent<Y>();
90             default: throw std::out_of_range("Dimension out of range");
91         };
92     }
94     /**
95         \brief  Remove some precision from the Rect
96         \param  places  The number of decimal places left in the end
98         This function just calls round on the \c _min and \c _max points.
99     */
100     inline void round(int places = 0) {
101         _min.round(places);
102         _max.round(places);
103         return;
104     }
106     /** Translates the rectangle by p. */
107     void offset(Point p);
109     /** Makes this rectangle large enough to include the point p. */
110     void expandTo(Point p);
112     /** Makes this rectangle large enough to include the rectangle r. */
113     void expandTo(Rect const &r);
115     inline void move_left (gdouble by) {
116         _min[NR::X] += by;
117     }
118     inline void move_right (gdouble by) {
119         _max[NR::X] += by;
120     }
121     inline void move_top (gdouble by) {
122         _min[NR::Y] += by;
123     }
124     inline void move_bottom (gdouble by) {
125         _max[NR::Y] += by;
126     }
128     /** Returns the set of points shared by both rectangles. */
129     static Maybe<Rect> intersection(Maybe<Rect> const &a, Maybe<Rect> const &b);
131     /** Returns the smallest rectangle that encloses both rectangles. */
132     static Maybe<Rect> union_bounds(Maybe<Rect> const &a, Maybe<Rect> const &b)
133     {
134         if (!a) {
135             return b;
136         } else if (!b) {
137             return a;
138         } else {
139             return union_bounds(*a, *b);
140         }
141     }
142     static Rect union_bounds(Maybe<Rect> const &a, Rect const &b) {
143         if (a) {
144             return union_bounds(*a, b);
145         } else {
146             return b;
147         }
148     }
149     static Rect union_bounds(Rect const &a, Maybe<Rect> const &b) {
150         if (b) {
151             return union_bounds(a, *b);
152         } else {
153             return a;
154         }
155     }
156     static Rect union_bounds(Rect const &a, Rect const &b);
158     /** Scales the rect by s, with origin at 0, 0 */
159     inline Rect operator*(double const s) const {
160         return Rect(s * min(), s * max());
161     }
163     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
164     inline Rect operator*(Matrix const m) const {
165         return Rect(_min * m, _max * m);
166     }
168     inline bool operator==(Rect const &in_rect) {
169         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
170     }
172     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
174 private:
175     static double _inf() {
176         return std::numeric_limits<double>::infinity();
177     }
179     template <NR::Dim2 axis>
180     double extent() const {
181         return _max[axis] - _min[axis];
182     }
184     template <Dim2 axis>
185     bool isEmpty() const {
186         return !( _min[axis] < _max[axis] );
187     }
189     template <Dim2 axis>
190     bool intersects(Rect const &r) const {
191         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
192     }
194     template <Dim2 axis>
195     bool contains(Rect const &r) const {
196         return contains(r._min) && contains(r._max);
197     }
199     template <Dim2 axis>
200     bool contains(Point const &p) const {
201         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
202     }
204     Point _min, _max;
206     /* evil, but temporary */
207     friend class Maybe<Rect>;
208 };
210 /** A function to print out the rectange if sent to an output
211     stream. */
212 inline std::ostream
213 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
215     out_file << "Rectangle:\n";
216     out_file << "\tMin Point -> " << in_rect.min() << "\n";
217     out_file << "\tMax Point -> " << in_rect.max() << "\n";
219     return out_file;
222 } /* namespace NR */
224 /* legacy rect stuff */
226 struct NRMatrix;
228 /* NULL rect is infinite */
230 struct NRRect {
231     NRRect() {}
232     NRRect(NR::Coord xmin, NR::Coord ymin, NR::Coord xmax, NR::Coord ymax)
233     : x0(xmin), y0(ymin), x1(xmin), y1(ymin)
234     {}
235     explicit NRRect(NR::Rect const &rect);
236     explicit NRRect(NR::Maybe<NR::Rect> const &rect);
237     operator NR::Maybe<NR::Rect>() const { return upgrade(); }
238     NR::Maybe<NR::Rect> upgrade() const;
240     NR::Coord x0, y0, x1, y1;
241 };
243 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
244 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
246 #define nr_rect_d_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
247 #define nr_rect_l_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
249 #define nr_rect_d_test_intersect(r0,r1) \
250         (!nr_rect_d_test_empty(r0) && !nr_rect_d_test_empty(r1) && \
251          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
252 #define nr_rect_l_test_intersect(r0,r1) \
253         (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
254          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
256 #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))))
257 #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))))
258 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
260 // returns minimal rect which covers all of r0 not covered by r1
261 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
263 // returns the area of r
264 NR::ICoord nr_rect_l_area(NRRectL *r);
266 /* NULL values are OK for r0 and r1, but not for d */
267 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
268 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
270 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
271 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
273 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
274 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
275 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
277 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
278 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m);
279 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
282 #endif /* !LIBNR_NR_RECT_H_SEEN */
284 /*
285   Local Variables:
286   mode:c++
287   c-file-style:"stroustrup"
288   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
289   indent-tabs-mode:nil
290   fill-column:99
291   End:
292 */
293 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :