1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/**
* \file
* \brief Axis-aligned rectangle
*//*
* Authors:
* Michael Sloan <mgsloan@gmail.com>
* Krzysztof Kosiński <tweenk.pl@gmail.com>
* Copyright 2007-2011 Authors
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, output to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* Authors of original rect class:
* Lauris Kaplinski <lauris@kaplinski.com>
* Nathan Hurst <njh@mail.csse.monash.edu.au>
* bulia byak <buliabyak@users.sf.net>
* MenTaLguY <mental@rydia.net>
*/
#ifndef LIB2GEOM_SEEN_RECT_H
#define LIB2GEOM_SEEN_RECT_H
#include <boost/optional.hpp>
#include <2geom/affine.h>
#include <2geom/interval.h>
#include <2geom/int-rect.h>
namespace Geom {
/**
* @brief Axis aligned, non-empty rectangle.
* @ingroup Primitives
*/
class Rect
: public GenericRect<Coord>
{
typedef GenericRect<Coord> Base;
public:
/// @name Create rectangles.
/// @{
/** @brief Create a rectangle that contains only the point at (0,0). */
Rect() {}
/** @brief Create a rectangle from X and Y intervals. */
Rect(Interval const &a, Interval const &b) : Base(a,b) {}
/** @brief Create a rectangle from two points. */
Rect(Point const &a, Point const &b) : Base(a,b) {}
Rect(Coord x0, Coord y0, Coord x1, Coord y1) : Base(x0, y0, x1, y1) {}
Rect(Base const &b) : Base(b) {}
Rect(IntRect const &ir) : Base(ir.min(), ir.max()) {}
/// @}
/// @name Inspect dimensions.
/// @{
/** @brief Check whether the rectangle has zero area up to specified tolerance.
* @param eps Maximum value of the area to consider empty
* @return True if rectangle has an area smaller than tolerance, false otherwise */
bool hasZeroArea(Coord eps = EPSILON) const { return (area() <= eps); }
/// @}
/// @name Test other rectangles and points for inclusion.
/// @{
/** @brief Check whether the interiors of the rectangles have any common points. */
bool interiorIntersects(Rect const &r) const {
return f[X].interiorIntersects(r[X]) && f[Y].interiorIntersects(r[Y]);
}
/** @brief Check whether the interior includes the given point. */
bool interiorContains(Point const &p) const {
return f[X].interiorContains(p[X]) && f[Y].interiorContains(p[Y]);
}
/** @brief Check whether the interior includes all points in the given rectangle.
* Interior of the rectangle is the entire rectangle without its borders. */
bool interiorContains(Rect const &r) const {
return f[X].interiorContains(r[X]) && f[Y].interiorContains(r[Y]);
}
inline bool interiorContains(OptRect const &r) const;
/// @}
/// @name Rounding to integer coordinates
/// @{
/** @brief Return the smallest integer rectangle which contains this one. */
IntRect roundOutwards() const {
IntRect ir(f[X].roundOutwards(), f[Y].roundOutwards());
return ir;
}
/** @brief Return the largest integer rectangle which is contained in this one. */
OptIntRect roundInwards() const {
OptIntRect oir(f[X].roundInwards(), f[Y].roundInwards());
return oir;
}
/// @}
/// @name Operators
/// @{
Rect &operator*=(Affine const &m);
bool operator==(IntRect const &ir) const {
return f[X] == ir[X] && f[Y] == ir[Y];
}
bool operator==(Rect const &other) const {
return Base::operator==(other);
}
/// @}
};
/**
* @brief Axis-aligned rectangle that can be empty.
* @ingroup Primitives
*/
class OptRect
: public GenericOptRect<Coord>
{
typedef GenericOptRect<Coord> Base;
public:
OptRect() : Base() {}
OptRect(Rect const &a) : Base(a) {}
OptRect(Point const &a, Point const &b) : Base(a, b) {}
OptRect(Coord x0, Coord y0, Coord x1, Coord y1) : Base(x0, y0, x1, y1) {}
OptRect(OptInterval const &x_int, OptInterval const &y_int) : Base(x_int, y_int) {}
OptRect(Base const &b) : Base(b) {}
OptRect(IntRect const &r) : Base(Rect(r)) {}
OptRect(OptIntRect const &r) : Base() {
if (r) *this = Rect(*r);
}
// actually, the only reason we have this class, instead of typedefing
// to GenericOptRect<Coord>, are the above constructors
bool operator==(OptRect const &other) const {
return Base::operator==(other);
}
bool operator==(Rect const &other) const {
return Base::operator==(other);
}
};
Coord distanceSq(Point const &p, Rect const &rect);
Coord distance(Point const &p, Rect const &rect);
inline bool Rect::interiorContains(OptRect const &r) const {
return !r || interiorContains(static_cast<Rect const &>(*r));
}
// the functions below do not work when defined generically
inline OptRect operator&(Rect const &a, Rect const &b) {
OptRect ret(a);
ret.intersectWith(b);
return ret;
}
inline OptRect intersect(Rect const &a, Rect const &b) {
return a & b;
}
inline OptRect intersect(OptRect const &a, OptRect const &b) {
return a & b;
}
inline Rect unify(Rect const &a, Rect const &b) {
return a | b;
}
inline OptRect unify(OptRect const &a, OptRect const &b) {
return a | b;
}
/** @brief Union a list of rectangles
* @deprecated Use OptRect::from_range instead */
inline Rect union_list(std::vector<Rect> const &r) {
if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
Rect ret = r[0];
for(unsigned i = 1; i < r.size(); i++)
ret.unionWith(r[i]);
return ret;
}
} // end namespace Geom
#endif // LIB2GEOM_SEEN_RECT_H
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|