Code

ab78c1651c53372eb9005d55520ba323c5929c58
[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>
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);
72 NRRectL *nr_rect_l_enlarge(NRRectL *d, int amount);
74 namespace NR {
76 /** A rectangle is always aligned to the X and Y axis.  This means it
77  * can be defined using only 4 coordinates, and determining
78  * intersection is very efficient.  The points inside a rectangle are
79  * min[dim] <= _pt[dim] <= max[dim].  Emptiness, however, is defined
80  * as having zero area, meaning an empty rectangle may still contain
81  * points.  Infinities are also permitted. */
82 class Rect {
83 public:
84     Rect() : _min(0.0, 0.0), _max(0.0, 0.0) {}
85     Rect(NRRect const &r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
86     Rect(Rect const &r) : _min(r._min), _max(r._max) {}
87     Rect(Point const &p0, Point const &p1);
89     Point const &min() const { return _min; }
90     Point const &max() const { return _max; }
92     /** returns the four corners of the rectangle in order
93      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
94     Point corner(unsigned i) const;
96     /** returns a vector from min to max. */
97     Point dimensions() const;
99     /** returns the midpoint of this rect. */
100     Point midpoint() const;
102     /** does this rectangle have zero area? */
103     bool isEmpty() const {
104         return isEmpty<X>() || isEmpty<Y>();
105     }
107     bool intersects(Rect const &r) const {
108         return intersects<X>(r) && intersects<Y>(r);
109     }
110     bool contains(Rect const &r) const {
111         return contains<X>(r) && contains<Y>(r);
112     }
113     bool contains(Point const &p) const {
114         return contains<X>(p) && contains<Y>(p);
115     }
117     double area() const {
118         return extent<X>() * extent<Y>();
119     }
121     double maxExtent() const {
122         return MAX(extent<X>(), extent<Y>());
123     }
125     double extent(Dim2 const axis) const {
126         switch (axis) {
127             case X: return extent<X>();
128             case Y: return extent<Y>();
129             default: g_error("invalid axis value %d", (int) axis); return 0;
130         };
131     }
133     double extent(unsigned i) const throw(std::out_of_range) {
134         switch (i) {
135             case 0: return extent<X>();
136             case 1: return extent<Y>();
137             default: throw std::out_of_range("Dimension out of range");
138         };
139     }
141     /**
142         \brief  Remove some precision from the Rect
143         \param  places  The number of decimal places left in the end
145         This function just calls round on the \c _min and \c _max points.
146     */
147     inline void round(int places = 0) {
148         _min.round(places);
149         _max.round(places);
150         return;
151     }
153     /** Translates the rectangle by p. */
154     void offset(Point p);
156     /** Makes this rectangle large enough to include the point p. */
157     void expandTo(Point p);
159     /** Makes this rectangle large enough to include the rectangle r. */
160     void expandTo(Rect const &r);
162     inline void move_left (gdouble by) {
163         _min[NR::X] += by;
164     }
165     inline void move_right (gdouble by) {
166         _max[NR::X] += by;
167     }
168     inline void move_top (gdouble by) {
169         _min[NR::Y] += by;
170     }
171     inline void move_bottom (gdouble by) {
172         _max[NR::Y] += by;
173     }
175     /** Returns the set of points shared by both rectangles. */
176     static Maybe<Rect> intersection(Rect const &a, Rect const &b);
178     /** Returns the smallest rectangle that encloses both rectangles. */
179     static Rect union_bounds(Rect const &a, Rect const &b);
181     /** Scales the rect by s, with origin at 0, 0 */
182     inline Rect operator*(double const s) const {
183         return Rect(s * min(), s * max());
184     }
186     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
187     inline Rect operator*(Matrix const m) const {
188         return Rect(_min * m, _max * m);
189     }
191     inline bool operator==(Rect const &in_rect) {
192         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
193     }
195     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
197 private:
199     template <NR::Dim2 axis>
200     double extent() const {
201         return _max[axis] - _min[axis];
202     }
204     template <Dim2 axis>
205     bool isEmpty() const {
206         return !( _min[axis] < _max[axis] );
207     }
209     template <Dim2 axis>
210     bool intersects(Rect const &r) const {
211         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
212     }
214     template <Dim2 axis>
215     bool contains(Rect const &r) const {
216         return contains(r._min) && contains(r._max);
217     }
219     template <Dim2 axis>
220     bool contains(Point const &p) const {
221         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
222     }
224     Point _min, _max;
226     /* evil, but temporary */
227     friend class Maybe<Rect>;
228 };
230 /** A function to print out the rectange if sent to an output
231     stream. */
232 inline std::ostream
233 &operator<<(std::ostream &out_file, NR::Rect const &in_rect)
235     out_file << "Rectangle:\n";
236     out_file << "\tMin Point -> " << in_rect.min() << "\n";
237     out_file << "\tMax Point -> " << in_rect.max() << "\n";
239     return out_file;
242 } /* namespace NR */
245 #endif /* !LIBNR_NR_RECT_H_SEEN */
247 /*
248   Local Variables:
249   mode:c++
250   c-file-style:"stroustrup"
251   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
252   indent-tabs-mode:nil
253   fill-column:99
254   End:
255 */
256 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :