Code

it seems we don't have "required" anymore
[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);
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() : _min(0.0, 0.0), _max(0.0, 0.0) {}
84     Rect(NRRect const &r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
85     Rect(Rect const &r) : _min(r._min), _max(r._max) {}
86     Rect(Point const &p0, Point const &p1);
88     Point const &min() const { return _min; }
89     Point const &max() const { return _max; }
91     /** returns the four corners of the rectangle in order
92      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
93     Point corner(unsigned i) const;
95     /** returns a vector from min to max. */
96     Point dimensions() const;
98     /** returns the midpoint of this rect. */
99     Point midpoint() const;
101     /** does this rectangle have zero area? */
102     bool isEmpty() const {
103         return isEmpty<X>() || isEmpty<Y>();
104     }
106     bool intersects(Rect const &r) const {
107         return intersects<X>(r) && intersects<Y>(r);
108     }
109     bool contains(Rect const &r) const {
110         return contains<X>(r) && contains<Y>(r);
111     }
112     bool contains(Point const &p) const {
113         return contains<X>(p) && contains<Y>(p);
114     }
116     double area() const {
117         return extent<X>() * extent<Y>();
118     }
120     double maxExtent() const {
121         return MAX(extent<X>(), extent<Y>());
122     }
124     double extent(Dim2 const axis) const {
125         switch (axis) {
126             case X: return extent<X>();
127             case Y: return extent<Y>();
128             default: g_error("invalid axis value %d", (int) axis); return 0;
129         };
130     }
132     double extent(unsigned i) const throw(std::out_of_range) {
133         switch (i) {
134             case 0: return extent<X>();
135             case 1: return extent<Y>();
136             default: throw std::out_of_range("Dimension out of range");
137         };
138     }
140     /**
141         \brief  Remove some precision from the Rect
142         \param  places  The number of decimal places left in the end
144         This function just calls round on the \c _min and \c _max points.
145     */
146     inline void round(int places = 0) {
147         _min.round(places);
148         _max.round(places);
149         return;
150     }
152     /** Translates the rectangle by p. */
153     void offset(Point p);
155     /** Makes this rectangle large enough to include the point p. */
156     void expandTo(Point p);
158     /** Makes this rectangle large enough to include the rectangle r. */
159     void expandTo(Rect const &r);
161     inline void move_left (gdouble by) {
162         _min[NR::X] += by;
163     }
164     inline void move_right (gdouble by) {
165         _max[NR::X] += by;
166     }
167     inline void move_top (gdouble by) {
168         _min[NR::Y] += by;
169     }
170     inline void move_bottom (gdouble by) {
171         _max[NR::Y] += by;
172     }
174     /** Returns the set of points shared by both rectangles. */
175     static Maybe<Rect> intersection(Rect const &a, Rect const &b);
177     /** Returns the smallest rectangle that encloses both rectangles. */
178     static Rect union_bounds(Rect const &a, Rect const &b);
180     /** Scales the rect by s, with origin at 0, 0 */
181     inline Rect operator*(double const s) const {
182         return Rect(s * min(), s * max());
183     }
185     /** Transforms the rect by m. Note that it gives correct results only for scales and translates */
186     inline Rect operator*(Matrix const m) const {
187         return Rect(_min * m, _max * m);
188     }
190     inline bool operator==(Rect const &in_rect) {
191         return ((this->min() == in_rect.min()) && (this->max() == in_rect.max()));
192     }
194     friend inline std::ostream &operator<<(std::ostream &out_file, NR::Rect const &in_rect);
196 private:
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)
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;
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 :