b6918c4931b77e8730b3f4c33add4b79e6d8800d
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);
188 static Rect union_bounds(Rect const &a, Rect const &b);
190 /** Scales the rect by s, with origin at 0, 0 */
191 inline Rect operator*(double const s) const {
192 return Rect(s * min(), s * max());
193 }
195 /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
196 inline Rect operator*(Matrix const m) const {
197 return Rect(_min * m, _max * m);
198 }
200 inline bool operator==(Rect const &in_rect) {
201 return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
202 }
204 friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
206 private:
208 template <NR::Dim2 axis>
209 double extent() const {
210 return _max[axis] - _min[axis];
211 }
213 template <Dim2 axis>
214 bool isEmpty() const {
215 return !( _min[axis] < _max[axis] );
216 }
218 template <Dim2 axis>
219 bool intersects(Rect const &r) const {
220 return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
221 }
223 template <Dim2 axis>
224 bool contains(Rect const &r) const {
225 return contains(r._min) && contains(r._max);
226 }
228 template <Dim2 axis>
229 bool contains(Point const &p) const {
230 return p[axis] >= _min[axis] && p[axis] <= _max[axis];
231 }
233 Point _min, _max;
235 /* evil, but temporary */
236 friend class Maybe<Rect>;
237 };
239 /** A function to print out the rectange if sent to an output
240 stream. */
241 inline std::ostream
242 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
243 {
244 out_file << "Rectangle:\n";
245 out_file << "\tMin Point -> " << in_rect.min() << "\n";
246 out_file << "\tMax Point -> " << in_rect.max() << "\n";
248 return out_file;
249 }
251 } /* namespace NR */
254 #endif /* !LIBNR_NR_RECT_H_SEEN */
256 /*
257 Local Variables:
258 mode:c++
259 c-file-style:"stroustrup"
260 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
261 indent-tabs-mode:nil
262 fill-column:99
263 End:
264 */
265 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :