6ae1e4e9948bbb18a409d1e85a7b9ae7d090023a
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 class EmptyRectangle : public std::logic_error {
32 public:
33 EmptyRectangle() : logic_error("Attempt to create empty rectangle") {}
34 };
36 /** A rectangle is always aligned to the X and Y axis. This means it
37 * can be defined using only 4 coordinates, and determining
38 * intersection is very efficient. The points inside a rectangle are
39 * min[dim] <= _pt[dim] <= max[dim]. Emptiness, in the sense of having
40 * a zero area, is not permitted. Infinities are, however. */
41 class Rect {
42 public:
43 Rect() : _min(-_inf(), -_inf()), _max(_inf(), _inf()) {}
44 Rect(Point const &p0, Point const &p1);
46 Point const &min() const { return _min; }
47 Point const &max() const { return _max; }
49 /** returns the four corners of the rectangle in order
50 * (clockwise if +Y is up, anticlockwise if +Y is down) */
51 Point corner(unsigned i) const;
53 /** returns a vector from min to max. */
54 Point dimensions() const;
56 /** returns the midpoint of this rect. */
57 Point midpoint() const;
59 bool intersects(Rect const &r) const {
60 return intersects<X>(r) && intersects<Y>(r);
61 }
62 bool contains(Rect const &r) const {
63 return contains<X>(r) && contains<Y>(r);
64 }
65 bool contains(Point const &p) const {
66 return contains<X>(p) && contains<Y>(p);
67 }
69 double area() const {
70 return extent<X>() * extent<Y>();
71 }
73 double maxExtent() const {
74 return MAX(extent<X>(), extent<Y>());
75 }
77 double extent(Dim2 const axis) const {
78 switch (axis) {
79 case X: return extent<X>();
80 case Y: return extent<Y>();
81 default: g_error("invalid axis value %d", (int) axis); return 0;
82 };
83 }
85 double extent(unsigned i) const throw(std::out_of_range) {
86 switch (i) {
87 case 0: return extent<X>();
88 case 1: return extent<Y>();
89 default: throw std::out_of_range("Dimension out of range");
90 };
91 }
93 /**
94 \brief Remove some precision from the Rect
95 \param places The number of decimal places left in the end
97 This function just calls round on the \c _min and \c _max points.
98 */
99 inline void round(int places = 0) {
100 _min.round(places);
101 _max.round(places);
102 return;
103 }
105 /** Translates the rectangle by p. */
106 void offset(Point p);
108 /** Makes this rectangle large enough to include the point p. */
109 void expandTo(Point p);
111 /** Makes this rectangle large enough to include the rectangle r. */
112 void expandTo(Rect const &r);
114 inline void move_left (gdouble by) {
115 _min[NR::X] += by;
116 }
117 inline void move_right (gdouble by) {
118 _max[NR::X] += by;
119 }
120 inline void move_top (gdouble by) {
121 _min[NR::Y] += by;
122 }
123 inline void move_bottom (gdouble by) {
124 _max[NR::Y] += by;
125 }
127 /** Scales the rect by s, with origin at 0, 0 */
128 inline Rect operator*(double const s) const {
129 return Rect(s * min(), s * max());
130 }
132 /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
133 inline Rect operator*(Matrix const m) const {
134 return Rect(_min * m, _max * m);
135 }
137 inline bool operator==(Rect const &in_rect) {
138 return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
139 }
141 friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
143 private:
144 Rect(Nothing) : _min(1, 1), _max(-1, -1) {}
146 static double _inf() {
147 return std::numeric_limits<double>::infinity();
148 }
150 template <NR::Dim2 axis>
151 double extent() const {
152 return _max[axis] - _min[axis];
153 }
155 template <Dim2 axis>
156 bool intersects(Rect const &r) const {
157 return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
158 }
160 template <Dim2 axis>
161 bool contains(Rect const &r) const {
162 return contains(r._min) && contains(r._max);
163 }
165 template <Dim2 axis>
166 bool contains(Point const &p) const {
167 return p[axis] >= _min[axis] && p[axis] <= _max[axis];
168 }
170 Point _min, _max;
172 friend class MaybeStorage<Rect>;
173 friend Maybe<Rect> intersection(Maybe<Rect> const &, Maybe<Rect> const &);
174 friend Rect union_bounds(Rect const &, Rect const &);
175 };
177 template <>
178 class MaybeStorage<Rect> {
179 public:
180 MaybeStorage() : _rect(Nothing()) {}
181 MaybeStorage(Rect const &rect) : _rect(rect) {}
183 bool is_nothing() const {
184 return _rect._min[X] > _rect._max[X];
185 }
186 Rect const &value() const { return _rect; }
187 Rect &value() { return _rect; }
189 private:
190 Rect _rect;
191 };
193 /** Returns the set of points shared by both rectangles. */
194 Maybe<Rect> intersection(Maybe<Rect> const & a, Maybe<Rect> const & b);
196 /** Returns the smallest rectangle that encloses both rectangles. */
197 Rect union_bounds(Rect const &a, Rect const &b);
198 inline Rect union_bounds(Maybe<Rect> const & a, Rect const &b) {
199 if (a) {
200 return union_bounds(*a, b);
201 } else {
202 return b;
203 }
204 }
205 inline Rect union_bounds(Rect const &a, Maybe<Rect> const & b) {
206 if (b) {
207 return union_bounds(a, *b);
208 } else {
209 return a;
210 }
211 }
212 inline Maybe<Rect> union_bounds(Maybe<Rect> const & a, Maybe<Rect> const & b)
213 {
214 if (!a) {
215 return b;
216 } else if (!b) {
217 return a;
218 } else {
219 return union_bounds(*a, *b);
220 }
221 }
223 /** A function to print out the rectange if sent to an output
224 stream. */
225 inline std::ostream
226 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
227 {
228 out_file << "Rectangle:\n";
229 out_file << "\tMin Point -> " << in_rect.min() << "\n";
230 out_file << "\tMax Point -> " << in_rect.max() << "\n";
232 return out_file;
233 }
235 } /* namespace NR */
237 /* legacy rect stuff */
239 struct NRMatrix;
241 /* NULL rect is infinite */
243 struct NRRect {
244 NRRect() {}
245 NRRect(NR::Coord xmin, NR::Coord ymin, NR::Coord xmax, NR::Coord ymax)
246 : x0(xmin), y0(ymin), x1(xmin), y1(ymin)
247 {}
248 explicit NRRect(NR::Rect const &rect);
249 explicit NRRect(NR::Maybe<NR::Rect> const &rect);
250 operator NR::Maybe<NR::Rect>() const { return upgrade(); }
251 NR::Maybe<NR::Rect> upgrade() const;
253 NR::Coord x0, y0, x1, y1;
254 };
256 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
257 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
259 #define nr_rect_d_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
260 #define nr_rect_l_test_empty(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
262 #define nr_rect_d_test_intersect(r0,r1) \
263 (!nr_rect_d_test_empty(r0) && !nr_rect_d_test_empty(r1) && \
264 !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
265 #define nr_rect_l_test_intersect(r0,r1) \
266 (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
267 !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
269 #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))))
270 #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))))
271 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
273 // returns minimal rect which covers all of r0 not covered by r1
274 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
276 // returns the area of r
277 NR::ICoord nr_rect_l_area(NRRectL *r);
279 /* NULL values are OK for r0 and r1, but not for d */
280 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
281 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
283 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
284 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
286 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
287 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
288 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
290 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
291 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m);
292 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
295 #endif /* !LIBNR_NR_RECT_H_SEEN */
297 /*
298 Local Variables:
299 mode:c++
300 c-file-style:"stroustrup"
301 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
302 indent-tabs-mode:nil
303 fill-column:99
304 End:
305 */
306 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :