Code

Unit test cleanup.
[inkscape.git] / src / libnr / nr-rect-l.h
1 #ifndef SEEN_NR_RECT_L_H
2 #define SEEN_NR_RECT_L_H
4 #include <libnr/nr-i-coord.h>
5 #include <boost/optional.hpp>
6 #include <libnr/nr-rect.h>
7 #include <libnr/nr-point-l.h>
9 struct NRRectL {
10     boost::optional<NR::Rect> upgrade() const;
11     NR::ICoord x0, y0, x1, y1;
12 };
15 namespace NR {
18 class IRect {
19 public:
20     IRect(const NRRectL& r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
21     IRect(const IRect& r) : _min(r._min), _max(r._max) {}
22     IRect(const IPoint &p0, const IPoint &p1) : _min(p0), _max(p1) {}
23     
24     /** as not all Rects are representable by IRects this gives the smallest IRect that contains
25      * r. */
26     IRect(const Rect& r);
27     
28     operator Rect() {
29         return Rect(Point(_min), Point(_max));
30     }
32     const IPoint &min() const { return _min; }
33     const IPoint &max() const { return _max; }
34     
35     /** returns a vector from min to max. */
36     IPoint dimensions() const;
37     
38     /** does this rectangle have zero area? */
39     bool isEmpty() const {
40         return isEmpty<X>() && isEmpty<Y>();
41     }
42     
43     bool intersects(const IRect &r) const {
44         return intersects<X>(r) && intersects<Y>(r);
45     }
46     bool contains(const IRect &r) const {
47         return contains<X>(r) && contains<Y>(r);
48     }
49     bool contains(const IPoint &p) const {
50         return contains<X>(p) && contains<Y>(p);
51     }
52     
53     ICoord maxExtent() const {
54         return MAX(extent<X>(), extent<Y>());
55     }
57     ICoord extent(Dim2 axis) const {
58         switch (axis) {
59         case X: return extent<X>();
60         case Y: return extent<Y>();
61         };
62     }
64     ICoord extent(unsigned i) const throw(std::out_of_range) {
65         switch (i) {
66         case 0: return extent<X>();
67         case 1: return extent<Y>();
68         default: throw std::out_of_range("Dimension out of range");
69         };
70     }
72     /** Translates the rectangle by p. */
73     void offset(IPoint p);
74         
75     /** Makes this rectangle large enough to include the point p. */
76     void expandTo(IPoint p);
78     /** Makes this rectangle large enough to include the rectangle r. */
79     void expandTo(const IRect &r);
80         
81     /** Returns the set of points shared by both rectangles. */
82     static boost::optional<IRect> intersection(const IRect &a, const IRect &b);
84     /** Returns the smallest rectangle that encloses both rectangles. */
85     static IRect union_bounds(const IRect &a, const IRect &b);
87     bool operator==(const IRect &other) const {
88         return (min() == other.min()) && (max() == other.max());
89     }
91     bool operator!=(const IRect &other) const {
92         return (min() != other.min()) || (max() != other.max());
93     }
95 private:
96     IRect() {}
98     template <NR::Dim2 axis>
99     ICoord extent() const {
100         return _max[axis] - _min[axis];
101     }
103     template <Dim2 axis>
104     bool isEmpty() const {
105         return !( _min[axis] < _max[axis] );
106     }
108     template <Dim2 axis>
109     bool intersects(const IRect &r) const {
110         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
111     }
113     template <Dim2 axis>
114     bool contains(const IRect &r) const {
115         return contains(r._min) && contains(r._max);
116     }
118     template <Dim2 axis>
119     bool contains(const IPoint &p) const {
120         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
121     }
123     IPoint _min, _max;
124 };
128 }  // namespace NR
130 #endif /* !SEEN_NR_RECT_L_H */
132 /*
133   Local Variables:
134   mode:c++
135   c-file-style:"stroustrup"
136   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
137   indent-tabs-mode:nil
138   fill-column:99
139   End:
140 */
141 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :