Code

Convert union and intersection to use NR::Maybe<NR::Rect>
[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>
19 #include "libnr/nr-values.h"
20 #include <libnr/nr-coord.h>
21 #include <libnr/nr-i-coord.h>
22 #include <libnr/nr-dim2.h>
23 #include <libnr/nr-point.h>
24 #include <libnr/nr-maybe.h>
25 #include <libnr/nr-point-matrix-ops.h>
27 struct NRMatrix;
28 namespace NR {
29     struct Matrix;
30 }
32 /* NULL rect is infinite */
34 struct NRRect {
35     NR::Coord x0, y0, x1, y1;
36 };
38 inline bool empty(NRRect const &r)
39 {
40     return ( ( r.x0 > r.x1 ) ||
41              ( r.y0 > r.y1 ) );
42 }
44 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
45 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
47 #define nr_rect_d_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
48 #define nr_rect_l_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
50 #define nr_rect_d_test_intersect(r0,r1) \
51         (!nr_rect_d_test_empty(r0) && !nr_rect_d_test_empty(r1) && \
52          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
53 #define nr_rect_l_test_intersect(r0,r1) \
54         (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
55          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
57 #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))))
58 #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))))
59 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
61 // returns minimal rect which covers all of r0 not covered by r1
62 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
64 // returns the area of r
65 NR::ICoord nr_rect_l_area(NRRectL *r);
67 /* NULL values are OK for r0 and r1, but not for d */
68 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
69 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
71 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
72 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
74 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
75 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
76 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
78 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
79 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m);
80 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
82 namespace NR {
84 /** A rectangle is always aligned to the X and Y axis.  This means it
85  * can be defined using only 4 coordinates, and determining
86  * intersection is very efficient.  The points inside a rectangle are
87  * min[dim] <= _pt[dim] <= max[dim].  Emptiness, however, is defined
88  * as having zero area, meaning an empty rectangle may still contain
89  * points.  Infinities are also permitted. */
90 class Rect {
91 public:
92     Rect() : _min(0.0, 0.0), _max(0.0, 0.0) {}
93     Rect(NRRect const &r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
94     Rect(Rect const &r) : _min(r._min), _max(r._max) {}
95     Rect(Point const &p0, Point const &p1);
97     Point const &min() const { return _min; }
98     Point const &max() const { return _max; }
100     /** returns the four corners of the rectangle in order
101      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
102     Point corner(unsigned i) const;
104     /** returns a vector from min to max. */
105     Point dimensions() const;
107     /** returns the midpoint of this rect. */
108     Point midpoint() const;
110     /** does this rectangle have zero area? */
111     bool isEmpty() const {
112         return isEmpty<X>() || isEmpty<Y>();
113     }
115     bool intersects(Rect const &r) const {
116         return intersects<X>(r) && intersects<Y>(r);
117     }
118     bool contains(Rect const &r) const {
119         return contains<X>(r) && contains<Y>(r);
120     }
121     bool contains(Point const &p) const {
122         return contains<X>(p) && contains<Y>(p);
123     }
125     double area() const {
126         return extent<X>() * extent<Y>();
127     }
129     double maxExtent() const {
130         return MAX(extent<X>(), extent<Y>());
131     }
133     double extent(Dim2 const axis) const {
134         switch (axis) {
135             case X: return extent<X>();
136             case Y: return extent<Y>();
137             default: g_error("invalid axis value %d", (int) axis); return 0;
138         };
139     }
141     double extent(unsigned i) const throw(std::out_of_range) {
142         switch (i) {
143             case 0: return extent<X>();
144             case 1: return extent<Y>();
145             default: throw std::out_of_range("Dimension out of range");
146         };
147     }
149     /**
150         \brief  Remove some precision from the Rect
151         \param  places  The number of decimal places left in the end
153         This function just calls round on the \c _min and \c _max points.
154     */
155     inline void round(int places = 0) {
156         _min.round(places);
157         _max.round(places);
158         return;
159     }
161     /** Translates the rectangle by p. */
162     void offset(Point p);
164     /** Makes this rectangle large enough to include the point p. */
165     void expandTo(Point p);
167     /** Makes this rectangle large enough to include the rectangle r. */
168     void expandTo(Rect const &r);
170     inline void move_left (gdouble by) {
171         _min[NR::X] += by;
172     }
173     inline void move_right (gdouble by) {
174         _max[NR::X] += by;
175     }
176     inline void move_top (gdouble by) {
177         _min[NR::Y] += by;
178     }
179     inline void move_bottom (gdouble by) {
180         _max[NR::Y] += by;
181     }
183     /** Returns the set of points shared by both rectangles. */
184     static Maybe<Rect> intersection(Maybe<Rect> const &a, Maybe<Rect> const &b);
186     /** Returns the smallest rectangle that encloses both rectangles. */
187     static Maybe<Rect> union_bounds(Maybe<Rect> const &a, Maybe<Rect> const &b);
189     /** Scales the rect by s, with origin at 0, 0 */
190     inline Rect operator*(double const s) const {
191         return Rect(s * min(), s * max());
192     }
194     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
195     inline Rect operator*(Matrix const m) const {
196         return Rect(_min * m, _max * m);
197     }
199     inline bool operator==(Rect const &in_rect) {
200         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
201     }
203     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
205 private:
207     template <NR::Dim2 axis>
208     double extent() const {
209         return _max[axis] - _min[axis];
210     }
212     template <Dim2 axis>
213     bool isEmpty() const {
214         return !( _min[axis] < _max[axis] );
215     }
217     template <Dim2 axis>
218     bool intersects(Rect const &r) const {
219         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
220     }
222     template <Dim2 axis>
223     bool contains(Rect const &r) const {
224         return contains(r._min) && contains(r._max);
225     }
227     template <Dim2 axis>
228     bool contains(Point const &p) const {
229         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
230     }
232     Point _min, _max;
234     /* evil, but temporary */
235     friend class Maybe<Rect>;
236 };
238 /** A function to print out the rectange if sent to an output
239     stream. */
240 inline std::ostream
241 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
243     out_file << "Rectangle:\n";
244     out_file << "\tMin Point -> " << in_rect.min() << "\n";
245     out_file << "\tMax Point -> " << in_rect.max() << "\n";
247     return out_file;
250 } /* namespace NR */
253 #endif /* !LIBNR_NR_RECT_H_SEEN */
255 /*
256   Local Variables:
257   mode:c++
258   c-file-style:"stroustrup"
259   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
260   indent-tabs-mode:nil
261   fill-column:99
262   End:
263 */
264 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :