~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/libnr/nr-rect-l.h

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef SEEN_NR_RECT_L_H
2
 
#define SEEN_NR_RECT_L_H
3
 
 
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>
8
 
 
9
 
struct NRRectL {
10
 
    boost::optional<NR::Rect> upgrade() const;
11
 
    NR::ICoord x0, y0, x1, y1;
12
 
};
13
 
 
14
 
 
15
 
namespace NR {
16
 
 
17
 
 
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
 
    }
31
 
 
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
 
    }
56
 
 
57
 
    ICoord extent(Dim2 axis) const {
58
 
        switch (axis) {
59
 
        case X: return extent<X>();
60
 
        case Y: return extent<Y>();
61
 
        };
62
 
    }
63
 
 
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
 
    }
71
 
 
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);
77
 
 
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);
83
 
 
84
 
    /** Returns the smallest rectangle that encloses both rectangles. */
85
 
    static IRect union_bounds(const IRect &a, const IRect &b);
86
 
 
87
 
    bool operator==(const IRect &other) const {
88
 
        return (min() == other.min()) && (max() == other.max());
89
 
    }
90
 
 
91
 
    bool operator!=(const IRect &other) const {
92
 
        return (min() != other.min()) || (max() != other.max());
93
 
    }
94
 
 
95
 
private:
96
 
    IRect() {}
97
 
 
98
 
    template <NR::Dim2 axis>
99
 
    ICoord extent() const {
100
 
        return _max[axis] - _min[axis];
101
 
    }
102
 
 
103
 
    template <Dim2 axis>
104
 
    bool isEmpty() const {
105
 
        return !( _min[axis] < _max[axis] );
106
 
    }
107
 
 
108
 
    template <Dim2 axis>
109
 
    bool intersects(const IRect &r) const {
110
 
        return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
111
 
    }
112
 
 
113
 
    template <Dim2 axis>
114
 
    bool contains(const IRect &r) const {
115
 
        return contains(r._min) && contains(r._max);
116
 
    }
117
 
 
118
 
    template <Dim2 axis>
119
 
    bool contains(const IPoint &p) const {
120
 
        return p[axis] >= _min[axis] && p[axis] <= _max[axis];
121
 
    }
122
 
 
123
 
    IPoint _min, _max;
124
 
};
125
 
 
126
 
 
127
 
 
128
 
}  // namespace NR
129
 
 
130
 
#endif /* !SEEN_NR_RECT_L_H */
131
 
 
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:encoding=utf-8:textwidth=99 :