1 /*
2 * rect.h - D2<Interval> specialization to Rect
3 *
4 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
13 *
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
19 *
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
24 *
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
28 *
29 */
31 /* Authors of original rect class:
32 * Lauris Kaplinski <lauris@kaplinski.com>
33 * Nathan Hurst <njh@mail.csse.monash.edu.au>
34 * bulia byak <buliabyak@users.sf.net>
35 * MenTaLguY <mental@rydia.net>
36 */
38 #include <2geom/d2.h>
40 #ifndef _2GEOM_RECT
41 #define _2GEOM_RECT
43 #include <2geom/matrix.h>
44 #include <boost/optional/optional.hpp>
46 namespace Geom {
48 typedef D2<Interval> Rect;
50 Rect unify(const Rect &, const Rect &);
52 template<>
53 class D2<Interval> {
54 private:
55 Interval f[2];
56 public:
57 /* The default constructor creates an empty rect, constructed of two empty Intervals. (users rely on this!)
58 */
59 D2<Interval>() { f[X] = f[Y] = Interval(); }
61 D2<Interval>(Interval const &a, Interval const &b) {
62 f[X] = a;
63 f[Y] = b;
64 }
66 D2<Interval>(Point const & a, Point const & b) {
67 f[X] = Interval(a[X], b[X]);
68 f[Y] = Interval(a[Y], b[Y]);
69 }
71 inline Interval& operator[](unsigned i) { return f[i]; }
72 inline Interval const & operator[](unsigned i) const { return f[i]; }
74 inline Point min() const { return Point(f[X].min(), f[Y].min()); }
75 inline Point max() const { return Point(f[X].max(), f[Y].max()); }
77 /** returns the four corners of the rectangle in positive order
78 * (clockwise if +Y is up, anticlockwise if +Y is down) */
79 Point corner(unsigned i) const {
80 switch(i % 4) {
81 case 0: return Point(f[X].min(), f[Y].min());
82 case 1: return Point(f[X].max(), f[Y].min());
83 case 2: return Point(f[X].max(), f[Y].max());
84 default: return Point(f[X].min(), f[Y].max());
85 }
86 }
88 //We should probably remove these - they're coord sys gnostic
89 inline double top() const { return f[Y].min(); }
90 inline double bottom() const { return f[Y].max(); }
91 inline double left() const { return f[X].min(); }
92 inline double right() const { return f[X].max(); }
94 inline double width() const { return f[X].extent(); }
95 inline double height() const { return f[Y].extent(); }
97 /** returns a vector from min to max. */
98 inline Point dimensions() const { return Point(f[X].extent(), f[Y].extent()); }
99 inline Point midpoint() const { return Point(f[X].middle(), f[Y].middle()); }
101 inline double area() const { return f[X].extent() * f[Y].extent(); }
102 inline double maxExtent() const { return std::max(f[X].extent(), f[Y].extent()); }
104 inline bool isEmpty() const {
105 return f[X].isEmpty() || f[Y].isEmpty();
106 }
107 inline bool intersects(Rect const &r) const {
108 return f[X].intersects(r[X]) && f[Y].intersects(r[Y]);
109 }
110 inline bool contains(Rect const &r) const {
111 return f[X].contains(r[X]) && f[Y].contains(r[Y]);
112 }
113 inline bool contains(Point const &p) const {
114 return f[X].contains(p[X]) && f[Y].contains(p[Y]);
115 }
117 inline void expandTo(Point p) {
118 f[X].extendTo(p[X]); f[Y].extendTo(p[Y]);
119 }
120 inline void unionWith(Rect const &b) {
121 f[X].unionWith(b[X]); f[Y].unionWith(b[Y]);
122 }
124 inline void expandBy(double amnt) {
125 f[X].expandBy(amnt); f[Y].expandBy(amnt);
126 }
127 inline void expandBy(Point const p) {
128 f[X].expandBy(p[X]); f[Y].expandBy(p[Y]);
129 }
131 /** Transforms the rect by m. Note that it gives correct results only for scales and translates,
132 in the case of rotations, the area of the rect will grow as it cannot rotate. */
133 inline Rect operator*(Matrix const m) const {
134 return unify(Rect(corner(0) * m, corner(2) * m),
135 Rect(corner(1) * m, corner(3) * m));
136 }
137 };
139 inline Rect unify(Rect const & a, Rect const & b) {
140 return Rect(unify(a[X], b[X]), unify(a[Y], b[Y]));
141 }
143 /** Returns the smallest rectangle that encloses both rectangles.
144 * An empty argument is assumed to be an empty rectangle */
145 inline boost::optional<Rect> unify(boost::optional<Rect> const & a, boost::optional<Rect> const & b) {
146 if (!a) {
147 return b;
148 } else if (!b) {
149 return a;
150 } else {
151 return unify(*a, *b);
152 }
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 boost::optional<Rect> intersect(Rect const & a, Rect const & b) {
164 boost::optional<Interval> x = intersect(a[X], b[X]);
165 boost::optional<Interval> y = intersect(a[Y], b[Y]);
166 return x && y ? boost::optional<Rect>(Rect(*x, *y)) : boost::optional<Rect>();
167 }
169 inline
170 double distanceSq( Point const& p, Rect const& rect )
171 {
172 double dx = 0, dy = 0;
173 if ( p[X] < rect.left() )
174 {
175 dx = p[X] - rect.left();
176 }
177 else if ( p[X] > rect.right() )
178 {
179 dx = rect.right() - p[X];
180 }
181 if ( p[Y] < rect.top() )
182 {
183 dy = rect.top() - p[Y];
184 }
185 else if ( p[Y] > rect.bottom() )
186 {
187 dy = p[Y] - rect.bottom();
188 }
189 return dx*dx + dy*dy;
190 }
192 inline
193 double distance( Point const& p, Rect const& rect )
194 {
195 return std::sqrt(distanceSq(p, rect));
196 }
199 } // end namespace Geom
201 #endif //_2GEOM_RECT
203 /*
204 Local Variables:
205 mode:c++
206 c-file-style:"stroustrup"
207 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
208 indent-tabs-mode:nil
209 fill-column:99
210 End:
211 */
212 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :