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 <boost/optional.hpp>
26 #include <libnr/nr-point-matrix-ops.h>
27 #include <libnr/nr-forward.h>
29 namespace NR {
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 Point cornerFarthestFrom(Point const &p) const;
58 /** True iff either width or height is less than \a epsilon. */
59 bool isEmpty(double epsilon=1e-6) const {
60 return isEmpty<X>(epsilon) || isEmpty<Y>(epsilon);
61 }
63 bool intersects(Rect const &r) const {
64 return intersects<X>(r) && intersects<Y>(r);
65 }
66 bool contains(Rect const &r) const {
67 return contains<X>(r) && contains<Y>(r);
68 }
69 bool contains(Point const &p) const {
70 return contains<X>(p) && contains<Y>(p);
71 }
73 double area() const {
74 return extent<X>() * extent<Y>();
75 }
77 double maxExtent() const {
78 return MAX(extent<X>(), extent<Y>());
79 }
81 double extent(Dim2 const axis) const {
82 switch (axis) {
83 case X: return extent<X>();
84 case Y: return extent<Y>();
85 default: g_error("invalid axis value %d", (int) axis); return 0;
86 };
87 }
89 double extent(unsigned i) const throw(std::out_of_range) {
90 switch (i) {
91 case 0: return extent<X>();
92 case 1: return extent<Y>();
93 default: throw std::out_of_range("Dimension out of range");
94 };
95 }
97 /**
98 \brief Remove some precision from the Rect
99 \param places The number of decimal places left in the end
101 This function just calls round on the \c _min and \c _max points.
102 */
103 inline void round(int places = 0) {
104 _min.round(places);
105 _max.round(places);
106 return;
107 }
109 /** Translates the rectangle by p. */
110 void offset(Point p);
112 /** Makes this rectangle large enough to include the point p. */
113 void expandTo(Point p);
115 /** Makes this rectangle large enough to include the rectangle r. */
116 void expandTo(Rect const &r);
118 inline void move_left (gdouble by) {
119 _min[NR::X] += by;
120 }
121 inline void move_right (gdouble by) {
122 _max[NR::X] += by;
123 }
124 inline void move_top (gdouble by) {
125 _min[NR::Y] += by;
126 }
127 inline void move_bottom (gdouble by) {
128 _max[NR::Y] += by;
129 }
131 void growBy (gdouble by);
133 /** Scales the rect by s, with origin at 0, 0 */
134 inline Rect operator*(double const s) const {
135 return Rect(s * min(), s * max());
136 }
138 /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
139 inline Rect operator*(Matrix const m) const {
140 return Rect(_min * m, _max * m);
141 }
143 inline bool operator==(Rect const &in_rect) {
144 return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
145 }
147 friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
149 private:
150 // Rect(Nothing) : _min(1, 1), _max(-1, -1) {}
152 static double _inf() {
153 return std::numeric_limits<double>::infinity();
154 }
156 template <NR::Dim2 axis>
157 double extent() const {
158 return _max[axis] - _min[axis];
159 }
161 template <NR::Dim2 axis>
162 bool isEmpty(double epsilon) const {
163 return extent<axis>() < epsilon;
164 }
166 template <Dim2 axis>
167 bool intersects(Rect const &r) const {
168 return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
169 }
171 template <Dim2 axis>
172 bool contains(Rect const &r) const {
173 return contains(r._min) && contains(r._max);
174 }
176 template <Dim2 axis>
177 bool contains(Point const &p) const {
178 return p[axis] >= _min[axis] && p[axis] <= _max[axis];
179 }
181 Point _min, _max;
183 friend boost::optional<Rect> intersection(boost::optional<Rect> const &, boost::optional<Rect> const &);
184 friend Rect union_bounds(Rect const &, Rect const &);
185 };
187 /** Returns the set of points shared by both rectangles. */
188 boost::optional<Rect> intersection(boost::optional<Rect> const & a, boost::optional<Rect> const & b);
190 /** Returns the smallest rectangle that encloses both rectangles. */
191 Rect union_bounds(Rect const &a, Rect const &b);
192 inline Rect union_bounds(boost::optional<Rect> const & a, Rect const &b) {
193 if (a) {
194 return union_bounds(*a, b);
195 } else {
196 return b;
197 }
198 }
199 inline Rect union_bounds(Rect const &a, boost::optional<Rect> const & b) {
200 if (b) {
201 return union_bounds(a, *b);
202 } else {
203 return a;
204 }
205 }
206 inline boost::optional<Rect> union_bounds(boost::optional<Rect> const & a, boost::optional<Rect> const & b)
207 {
208 if (!a) {
209 return b;
210 } else if (!b) {
211 return a;
212 } else {
213 return union_bounds(*a, *b);
214 }
215 }
217 /** A function to print out the rectange if sent to an output
218 stream. */
219 inline std::ostream
220 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
221 {
222 out_file << "Rectangle:\n";
223 out_file << "\tMin Point -> " << in_rect.min() << "\n";
224 out_file << "\tMax Point -> " << in_rect.max() << "\n";
226 return out_file;
227 }
229 } /* namespace NR */
231 /* legacy rect stuff */
233 /* NULL rect is infinite */
235 struct NRRect {
236 NRRect()
237 : x0(0), y0(0), x1(0), y1(0)
238 {}
239 NRRect(NR::Coord xmin, NR::Coord ymin, NR::Coord xmax, NR::Coord ymax)
240 : x0(xmin), y0(ymin), x1(xmax), y1(ymax)
241 {}
242 explicit NRRect(NR::Rect const &rect);
243 explicit NRRect(boost::optional<NR::Rect> const &rect);
244 operator boost::optional<NR::Rect>() const { return upgrade(); }
245 boost::optional<NR::Rect> upgrade() const;
247 NR::Coord x0, y0, x1, y1;
248 };
250 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
251 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
253 /** "Empty" here includes the case of zero width or zero height. */
254 // TODO convert to static overloaded functions (pointer and ref) once performance can be tested:
255 #define nr_rect_d_test_empty_ptr(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
256 #define nr_rect_d_test_empty(r) NR_RECT_DFLS_TEST_EMPTY_REF(r)
258 // TODO convert to static overloaded functions (pointer and ref) once performance can be tested:
259 #define nr_rect_l_test_empty_ptr(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
260 #define nr_rect_l_test_empty(r) NR_RECT_DFLS_TEST_EMPTY_REF(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)))
266 // TODO convert to static overloaded functions (pointer and ref) once performance can be tested:
267 #define nr_rect_l_test_intersect_ptr(r0,r1) \
268 (!nr_rect_l_test_empty_ptr(r0) && !nr_rect_l_test_empty_ptr(r1) && \
269 !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
270 #define nr_rect_l_test_intersect(r0,r1) \
271 (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
272 !(!NR_RECT_DFLS_TEST_INTERSECT_REF(r0, r1)))
274 #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))))
275 #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))))
276 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
278 // returns minimal rect which covers all of r0 not covered by r1
279 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
281 // returns the area of r
282 NR::ICoord nr_rect_l_area(NRRectL *r);
284 /* NULL values are OK for r0 and r1, but not for d */
285 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
286 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
288 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
289 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
291 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
292 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
293 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
295 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
296 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const *m);
297 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
300 #endif /* !LIBNR_NR_RECT_H_SEEN */
302 /*
303 Local Variables:
304 mode:c++
305 c-file-style:"stroustrup"
306 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
307 indent-tabs-mode:nil
308 fill-column:99
309 End:
310 */
311 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :