Code

Use subdirectories with icon sizes.
[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>
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>
28 #include <2geom/rect.h>
30 namespace NR {
32 /** A rectangle is always aligned to the X and Y axis.  This means it
33  * can be defined using only 4 coordinates, and determining
34  * intersection is very efficient.  The points inside a rectangle are
35  * min[dim] <= _pt[dim] <= max[dim].  A rectangle may be empty, in the
36  * sense of having zero area, but it will always contain at least one
37  * point.  Infinities are also permitted.
38  */
39 class Rect {
40 public:
41     Rect() : _min(-_inf(), -_inf()), _max(_inf(), _inf()) {}
42     Rect(Point const &p0, Point const &p1);
44     Point const &min() const { return _min; }
45     Point const &max() const { return _max; }
47     /** returns the four corners of the rectangle in order
48      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
49     Point corner(unsigned i) const;
51     /** returns a vector from min to max. */
52     Point dimensions() const;
54     /** returns the midpoint of this rect. */
55     Point midpoint() const;
57     Point cornerFarthestFrom(Point const &p) const;
59     /** True iff either width or height is less than \a epsilon. */
60     bool isEmpty(double epsilon=1e-6) const {
61         return isEmpty<X>(epsilon) || isEmpty<Y>(epsilon);
62     }
64     bool intersects(Rect const &r) const {
65         return intersects<X>(r) && intersects<Y>(r);
66     }
67     bool contains(Rect const &r) const {
68         return contains<X>(r) && contains<Y>(r);
69     }
70     bool contains(Point const &p) const {
71         return contains<X>(p) && contains<Y>(p);
72     }
74     double area() const {
75         return extent<X>() * extent<Y>();
76     }
78     double maxExtent() const {
79         return MAX(extent<X>(), extent<Y>());
80     }
82     double extent(Dim2 const axis) const {
83         switch (axis) {
84             case X: return extent<X>();
85             case Y: return extent<Y>();
86             default: g_error("invalid axis value %d", (int) axis); return 0;
87         };
88     }
90     double extent(unsigned i) const throw(std::out_of_range) {
91         switch (i) {
92             case 0: return extent<X>();
93             case 1: return extent<Y>();
94             default: throw std::out_of_range("Dimension out of range");
95         };
96     }
98     /**
99         \brief  Remove some precision from the Rect
100         \param  places  The number of decimal places left in the end
102         This function just calls round on the \c _min and \c _max points.
103     */
104     inline void round(int places = 0) {
105         _min.round(places);
106         _max.round(places);
107         return;
108     }
110     /** Translates the rectangle by p. */
111     void offset(Point p);
113     /** Makes this rectangle large enough to include the point p. */
114     void expandTo(Point p);
116     /** Makes this rectangle large enough to include the rectangle r. */
117     void expandTo(Rect const &r);
119     inline void move_left (gdouble by) {
120         _min[NR::X] += by;
121     }
122     inline void move_right (gdouble by) {
123         _max[NR::X] += by;
124     }
125     inline void move_top (gdouble by) {
126         _min[NR::Y] += by;
127     }
128     inline void move_bottom (gdouble by) {
129         _max[NR::Y] += by;
130     }
132     void growBy (gdouble by);
134     /** Scales the rect by s, with origin at 0, 0 */
135     inline Rect operator*(double const s) const {
136         return Rect(s * min(), s * max());
137     }
139     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
140     inline Rect operator*(Matrix const m) const {
141         return Rect(_min * m, _max * m);
142     }
144     inline bool operator==(Rect const &in_rect) {
145         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
146     }
148     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
150 private:
151 //    Rect(Nothing) : _min(1, 1), _max(-1, -1) {}
153     static double _inf() {
154         return std::numeric_limits<double>::infinity();
155     }
157     template <NR::Dim2 axis>
158     double extent() const {
159         return _max[axis] - _min[axis];
160     }
162     template <NR::Dim2 axis>
163     bool isEmpty(double epsilon) const {
164         return extent<axis>() < epsilon;
165     }
167     template <Dim2 axis>
168     bool intersects(Rect const &r) const {
169         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
170     }
172     template <Dim2 axis>
173     bool contains(Rect const &r) const {
174         return contains(r._min) && contains(r._max);
175     }
177     template <Dim2 axis>
178     bool contains(Point const &p) const {
179         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
180     }
182     Point _min, _max;
184     friend boost::optional<Rect> intersection(boost::optional<Rect> const &, boost::optional<Rect> const &);
185     friend Rect union_bounds(Rect const &, Rect const &);
186 };
188 /** Returns the set of points shared by both rectangles. */
189 boost::optional<Rect> intersection(boost::optional<Rect> const & a, boost::optional<Rect> const & b);
191 /** Returns the smallest rectangle that encloses both rectangles. */
192 Rect union_bounds(Rect const &a, Rect const &b);
193 inline Rect union_bounds(boost::optional<Rect> const & a, Rect const &b) {
194     if (a) {
195         return union_bounds(*a, b);
196     } else {
197         return b;
198     }
200 inline Rect union_bounds(Rect const &a, boost::optional<Rect> const & b) {
201     if (b) {
202         return union_bounds(a, *b);
203     } else {
204         return a;
205     }
207 inline boost::optional<Rect> union_bounds(boost::optional<Rect> const & a, boost::optional<Rect> const & b)
209     if (!a) {
210         return b;
211     } else if (!b) {
212         return a;
213     } else {
214         return union_bounds(*a, *b);
215     }
218 /** A function to print out the rectange if sent to an output
219     stream. */
220 inline std::ostream
221 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
223     out_file << "Rectangle:\n";
224     out_file << "\tMin Point -> " << in_rect.min() << "\n";
225     out_file << "\tMax Point -> " << in_rect.max() << "\n";
227     return out_file;
230 } /* namespace NR */
232 /* legacy rect stuff */
234 /* NULL rect is infinite */
236 struct NRRect {
237     NRRect()
238     : x0(0), y0(0), x1(0), y1(0)
239     {}
240     NRRect(NR::Coord xmin, NR::Coord ymin, NR::Coord xmax, NR::Coord ymax)
241     : x0(xmin), y0(ymin), x1(xmax), y1(ymax)
242     {}
243     explicit NRRect(NR::Rect const &rect);
244     explicit NRRect(boost::optional<NR::Rect> const &rect);
245     operator boost::optional<NR::Rect>() const { return upgrade(); }
246     boost::optional<NR::Rect> upgrade() const;
247     explicit NRRect(Geom::OptRect const &rect);
248     operator Geom::OptRect() const { return upgrade_2geom(); }
249     Geom::OptRect upgrade_2geom() const;
251     NR::Coord x0, y0, x1, y1;
252 };
254 #define nr_rect_d_set_empty(r) (*(r) = NR_RECT_EMPTY)
255 #define nr_rect_l_set_empty(r) (*(r) = NR_RECT_L_EMPTY)
257 /** "Empty" here includes the case of zero width or zero height. */
258 // TODO convert to static overloaded functions (pointer and ref) once performance can be tested:
259 #define nr_rect_d_test_empty_ptr(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
260 #define nr_rect_d_test_empty(r) NR_RECT_DFLS_TEST_EMPTY_REF(r)
262 // TODO convert to static overloaded functions (pointer and ref) once performance can be tested:
263 #define nr_rect_l_test_empty_ptr(r) ((r) && NR_RECT_DFLS_TEST_EMPTY(r))
264 #define nr_rect_l_test_empty(r) NR_RECT_DFLS_TEST_EMPTY_REF(r)
266 #define nr_rect_d_test_intersect(r0,r1) \
267         (!nr_rect_d_test_empty(r0) && !nr_rect_d_test_empty(r1) && \
268          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
270 // TODO convert to static overloaded functions (pointer and ref) once performance can be tested:
271 #define nr_rect_l_test_intersect_ptr(r0,r1) \
272         (!nr_rect_l_test_empty_ptr(r0) && !nr_rect_l_test_empty_ptr(r1) && \
273          !((r0) && (r1) && !NR_RECT_DFLS_TEST_INTERSECT(r0, r1)))
274 #define nr_rect_l_test_intersect(r0,r1) \
275         (!nr_rect_l_test_empty(r0) && !nr_rect_l_test_empty(r1) && \
276          !(!NR_RECT_DFLS_TEST_INTERSECT_REF(r0, r1)))
278 #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))))
279 #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))))
280 #define nr_rect_l_test_inside(r,x,y) ((!(r) || (!NR_RECT_DFLS_TEST_EMPTY(r) && NR_RECT_LS_TEST_INSIDE(r,x,y))))
282 // returns minimal rect which covers all of r0 not covered by r1
283 NRRectL *nr_rect_l_subtract(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
285 // returns the area of r
286 NR::ICoord nr_rect_l_area(NRRectL *r);
288 /* NULL values are OK for r0 and r1, but not for d */
289 NRRect *nr_rect_d_intersect(NRRect *d, NRRect const *r0, NRRect const *r1);
290 NRRectL *nr_rect_l_intersect(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
292 NRRect *nr_rect_d_union(NRRect *d, NRRect const *r0, NRRect const *r1);
293 NRRectL *nr_rect_l_union(NRRectL *d, NRRectL const *r0, NRRectL const *r1);
295 NRRect *nr_rect_union_pt(NRRect *dst, NR::Point const &p);
296 NRRect *nr_rect_d_union_xy(NRRect *d, NR::Coord x, NR::Coord y);
297 NRRectL *nr_rect_l_union_xy(NRRectL *d, NR::ICoord x, NR::ICoord y);
299 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const &m);
300 NRRect *nr_rect_d_matrix_transform(NRRect *d, NRRect const *s, NR::Matrix const *m);
301 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
304 #endif /* !LIBNR_NR_RECT_H_SEEN */
306 /*
307   Local Variables:
308   mode:c++
309   c-file-style:"stroustrup"
310   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
311   indent-tabs-mode:nil
312   fill-column:99
313   End:
314 */
315 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :