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))))
59 /* NULL values are OK for r0 and r1, but not for d */
60 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
61 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
63 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
64 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
66 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
67 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
68 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
70 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
71 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NRMatrix const *m);
73 namespace NR {
75 /** A rectangle is always aligned to the X and Y axis. This means it
76 * can be defined using only 4 coordinates, and determining
77 * intersection is very efficient. The points inside a rectangle are
78 * min[dim] <= _pt[dim] <= max[dim]. Emptiness, however, is defined
79 * as having zero area, meaning an empty rectangle may still contain
80 * points. Infinities are also permitted. */
81 class Rect {
82 public:
83 Rect(NRRect const &r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
84 Rect(Rect const &r) : _min(r._min), _max(r._max) {}
85 Rect(Point const &p0, Point const &p1);
87 Point const &min() const { return _min; }
88 Point const &max() const { return _max; }
90 /** returns the four corners of the rectangle in order
91 * (clockwise if +Y is up, anticlockwise if +Y is down) */
92 Point corner(unsigned i) const;
94 /** returns a vector from min to max. */
95 Point dimensions() const;
97 /** returns the midpoint of this rect. */
98 Point midpoint() const;
100 /** does this rectangle have zero area? */
101 bool isEmpty() const {
102 return isEmpty<X>() || isEmpty<Y>();
103 }
105 bool intersects(Rect const &r) const {
106 return intersects<X>(r) && intersects<Y>(r);
107 }
108 bool contains(Rect const &r) const {
109 return contains<X>(r) && contains<Y>(r);
110 }
111 bool contains(Point const &p) const {
112 return contains<X>(p) && contains<Y>(p);
113 }
115 double area() const {
116 return extent<X>() * extent<Y>();
117 }
119 double maxExtent() const {
120 return MAX(extent<X>(), extent<Y>());
121 }
123 double extent(Dim2 const axis) const {
124 switch (axis) {
125 case X: return extent<X>();
126 case Y: return extent<Y>();
127 default: g_error("invalid axis value %d", (int) axis); return 0;
128 };
129 }
131 double extent(unsigned i) const throw(std::out_of_range) {
132 switch (i) {
133 case 0: return extent<X>();
134 case 1: return extent<Y>();
135 default: throw std::out_of_range("Dimension out of range");
136 };
137 }
139 /**
140 \brief Remove some precision from the Rect
141 \param places The number of decimal places left in the end
143 This function just calls round on the \c _min and \c _max points.
144 */
145 inline void round(int places = 0) {
146 _min.round(places);
147 _max.round(places);
148 return;
149 }
151 /** Translates the rectangle by p. */
152 void offset(Point p);
154 /** Makes this rectangle large enough to include the point p. */
155 void expandTo(Point p);
157 /** Makes this rectangle large enough to include the rectangle r. */
158 void expandTo(Rect const &r);
160 inline void move_left (gdouble by) {
161 _min[NR::X] += by;
162 }
163 inline void move_right (gdouble by) {
164 _max[NR::X] += by;
165 }
166 inline void move_top (gdouble by) {
167 _min[NR::Y] += by;
168 }
169 inline void move_bottom (gdouble by) {
170 _max[NR::Y] += by;
171 }
173 /** Returns the set of points shared by both rectangles. */
174 static Maybe<Rect> intersection(Rect const &a, Rect const &b);
176 /** Returns the smallest rectangle that encloses both rectangles. */
177 static Rect union_bounds(Rect const &a, Rect const &b);
179 /** Scales the rect by s, with origin at 0, 0 */
180 inline Rect operator*(double const s) const {
181 return Rect(s * min(), s * max());
182 }
184 /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
185 inline Rect operator*(Matrix const m) const {
186 return Rect(_min * m, _max * m);
187 }
189 inline bool operator==(Rect const &in_rect) {
190 return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
191 }
193 friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
195 private:
196 Rect() {}
198 template <NR::Dim2 axis>
199 double extent() const {
200 return _max[axis] - _min[axis];
201 }
203 template <Dim2 axis>
204 bool isEmpty() const {
205 return !( _min[axis] < _max[axis] );
206 }
208 template <Dim2 axis>
209 bool intersects(Rect const &r) const {
210 return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
211 }
213 template <Dim2 axis>
214 bool contains(Rect const &r) const {
215 return contains(r._min) && contains(r._max);
216 }
218 template <Dim2 axis>
219 bool contains(Point const &p) const {
220 return p[axis] >= _min[axis] && p[axis] <= _max[axis];
221 }
223 Point _min, _max;
225 /* evil, but temporary */
226 friend class Maybe<Rect>;
227 };
229 /** A function to print out the rectange if sent to an output
230 stream. */
231 inline std::ostream
232 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
233 {
234 out_file << "Rectangle:\n";
235 out_file << "\tMin Point -> " << in_rect.min() << "\n";
236 out_file << "\tMax Point -> " << in_rect.max() << "\n";
238 return out_file;
239 }
241 } /* namespace NR */
244 #endif /* !LIBNR_NR_RECT_H_SEEN */
246 /*
247 Local Variables:
248 mode:c++
249 c-file-style:"stroustrup"
250 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
251 indent-tabs-mode:nil
252 fill-column:99
253 End:
254 */
255 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :