1 /**
2 * \file
3 * \brief D2<Interval> specialization to Rect
4 */
5 /*
6 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
15 *
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
26 *
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
30 *
31 */
33 /* Authors of original rect class:
34 * Lauris Kaplinski <lauris@kaplinski.com>
35 * Nathan Hurst <njh@mail.csse.monash.edu.au>
36 * bulia byak <buliabyak@users.sf.net>
37 * MenTaLguY <mental@rydia.net>
38 */
40 #include <2geom/d2.h>
42 #ifndef _2GEOM_RECT
43 #define _2GEOM_RECT
45 #include <2geom/matrix.h>
46 #include <boost/optional/optional.hpp>
48 namespace Geom {
49 /** D2<Interval> specialization to Rect */
50 typedef D2<Interval> Rect;
51 class OptRect;
53 Rect unify(const Rect &, const Rect &);
54 /**
55 * %Rect class.
56 * The Rect class is actually a specialisation of D2<Interval>.
57 *
58 */
59 template<>
60 class D2<Interval> {
61 private:
62 Interval f[2];
63 public:
64 /** Best not to use this constructor, do not rely on what it initializes the object to.
65 *The default constructor creates a rect of default intervals.
66 */
67 D2<Interval>() { f[X] = f[Y] = Interval(); }
69 public:
70 D2<Interval>(Interval const &a, Interval const &b) {
71 f[X] = a;
72 f[Y] = b;
73 }
75 D2<Interval>(Point const & a, Point const & b) {
76 f[X] = Interval(a[X], b[X]);
77 f[Y] = Interval(a[Y], b[Y]);
78 }
80 inline Interval& operator[](unsigned i) { return f[i]; }
81 inline Interval const & operator[](unsigned i) const { return f[i]; }
83 inline Point min() const { return Point(f[X].min(), f[Y].min()); }
84 inline Point max() const { return Point(f[X].max(), f[Y].max()); }
86 /** Returns the four corners of the rectangle in positive order
87 * (clockwise if +Y is up, anticlockwise if +Y is down) */
88 Point corner(unsigned i) const {
89 switch(i % 4) {
90 case 0: return Point(f[X].min(), f[Y].min());
91 case 1: return Point(f[X].max(), f[Y].min());
92 case 2: return Point(f[X].max(), f[Y].max());
93 default: return Point(f[X].min(), f[Y].max());
94 }
95 }
97 //We should probably remove these - they're coord sys gnostic
98 inline double top() const { return f[Y].min(); }
99 inline double bottom() const { return f[Y].max(); }
100 inline double left() const { return f[X].min(); }
101 inline double right() const { return f[X].max(); }
103 inline double width() const { return f[X].extent(); }
104 inline double height() const { return f[Y].extent(); }
106 /** Returns a vector from min to max. */
107 inline Point dimensions() const { return Point(f[X].extent(), f[Y].extent()); }
108 inline Point midpoint() const { return Point(f[X].middle(), f[Y].middle()); }
110 /**
111 * \brief Compute the area of this rectangle.
112 *
113 * Note that a zero area rectangle is not empty - just as the interval [0,0] contains one point, the rectangle [0,0] x [0,0] contains 1 point and no area.
114 * \retval For a valid return value, the rect must be tested for emptyness first.
115 */
116 inline double area() const { return f[X].extent() * f[Y].extent(); }
117 inline bool hasZeroArea(double eps = EPSILON) const { return (area() <= eps); }
119 inline double maxExtent() const { return std::max(f[X].extent(), f[Y].extent()); }
120 inline double minExtent() const { return std::min(f[X].extent(), f[Y].extent()); }
122 // inline bool isEmpty() const {
123 // return f[X].isEmpty() || f[Y].isEmpty();
124 // }
125 inline bool intersects(Rect const &r) const {
126 return f[X].intersects(r[X]) && f[Y].intersects(r[Y]);
127 }
128 inline bool contains(Rect const &r) const {
129 return f[X].contains(r[X]) && f[Y].contains(r[Y]);
130 }
131 inline bool contains(Point const &p) const {
132 return f[X].contains(p[X]) && f[Y].contains(p[Y]);
133 }
135 inline void expandTo(Point p) {
136 f[X].extendTo(p[X]); f[Y].extendTo(p[Y]);
137 }
138 inline void unionWith(Rect const &b) {
139 f[X].unionWith(b[X]); f[Y].unionWith(b[Y]);
140 }
141 void unionWith(OptRect const &b);
143 inline void expandBy(double amnt) {
144 f[X].expandBy(amnt); f[Y].expandBy(amnt);
145 }
146 inline void expandBy(Point const p) {
147 f[X].expandBy(p[X]); f[Y].expandBy(p[Y]);
148 }
149 };
151 inline Rect unify(Rect const & a, Rect const & b) {
152 return Rect(unify(a[X], b[X]), unify(a[Y], b[Y]));
153 }
155 inline Rect union_list(std::vector<Rect> const &r) {
156 if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
157 Rect ret = r[0];
158 for(unsigned i = 1; i < r.size(); i++)
159 ret.unionWith(r[i]);
160 return ret;
161 }
163 inline
164 double distanceSq( Point const& p, Rect const& rect )
165 {
166 double dx = 0, dy = 0;
167 if ( p[X] < rect.left() )
168 {
169 dx = p[X] - rect.left();
170 }
171 else if ( p[X] > rect.right() )
172 {
173 dx = rect.right() - p[X];
174 }
175 if ( p[Y] < rect.top() )
176 {
177 dy = rect.top() - p[Y];
178 }
179 else if ( p[Y] > rect.bottom() )
180 {
181 dy = p[Y] - rect.bottom();
182 }
183 return dx*dx + dy*dy;
184 }
186 /**
187 * Returns the smallest distance between p and rect.
188 */
189 inline
190 double distance( Point const& p, Rect const& rect )
191 {
192 return std::sqrt(distanceSq(p, rect));
193 }
195 /**
196 * The OptRect class can represent and empty Rect and non-empty Rects.
197 * If OptRect is not empty, it means that both X and Y intervals are not empty.
198 *
199 */
200 class OptRect : public boost::optional<Rect> {
201 public:
202 OptRect() : boost::optional<Rect>() {};
203 OptRect(Rect const &a) : boost::optional<Rect>(a) {};
205 /**
206 * Creates an empty OptRect when one of the argument intervals is empty.
207 */
208 OptRect(OptInterval const &x_int, OptInterval const &y_int) {
209 if (x_int && y_int) {
210 *this = Rect(*x_int, *y_int);
211 }
212 // else, stay empty.
213 }
215 /**
216 * Check whether this OptRect is empty or not.
217 */
218 inline bool isEmpty() const { return (*this == false); };
220 /**
221 * If \c this is empty, copy argument \c b. Otherwise, union with it (and do nothing when \c b is empty)
222 */
223 inline void unionWith(OptRect const &b) {
224 if (b) {
225 if (*this) { // check that we are not empty
226 (**this)[X].unionWith((*b)[X]);
227 (**this)[Y].unionWith((*b)[Y]);
228 } else {
229 *this = b;
230 }
231 }
232 }
233 };
236 /**
237 * Returns the smallest rectangle that encloses both rectangles.
238 * An empty argument is assumed to be an empty rectangle
239 */
240 inline OptRect unify(OptRect const & a, OptRect const & b) {
241 if (!a) {
242 return b;
243 } else if (!b) {
244 return a;
245 } else {
246 return unify(*a, *b);
247 }
248 }
250 inline OptRect intersect(Rect const & a, Rect const & b) {
251 return OptRect(intersect(a[X], b[X]), intersect(a[Y], b[Y]));
252 }
254 inline void Rect::unionWith(OptRect const &b) {
255 if (b) {
256 unionWith(*b);
257 }
258 }
260 } // end namespace Geom
262 #endif //_2GEOM_RECT
264 /*
265 Local Variables:
266 mode:c++
267 c-file-style:"stroustrup"
268 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
269 indent-tabs-mode:nil
270 fill-column:99
271 End:
272 */
273 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :