Code

update 2geom (svn rev1433)
[inkscape.git] / src / 2geom / rect.h
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 #ifdef _2GEOM_D2  /*This is intentional: we don't actually want anyone to
39                     include this, other than D2.h.  If somone else tries, D2
40                     won't be defined.  If it is, this will already be included. */
41 #ifndef _2GEOM_RECT
42 #define _2GEOM_RECT
44 #include <2geom/matrix.h>
45 #include <boost/optional/optional.hpp>
47 namespace Geom {
49 typedef D2<Interval> Rect;
51 Rect unify(const Rect &, const Rect &);
53 template<>
54 class D2<Interval> {
55   private:
56     Interval f[2];
57   public:
58     D2<Interval>() { f[X] = f[Y] = Interval(0, 0); }
59     
60     D2<Interval>(Interval const &a, Interval const &b) {
61         f[X] = a;
62         f[Y] = b;
63     }
65     D2<Interval>(Point const & a, Point const & b) {
66         f[X] = Interval(a[X], b[X]);
67         f[Y] = Interval(a[Y], b[Y]);
68     }
70     inline Interval& operator[](unsigned i)              { return f[i]; }
71     inline Interval const & operator[](unsigned i) const { return f[i]; }
73     inline Point min() const { return Point(f[X].min(), f[Y].min()); }
74     inline Point max() const { return Point(f[X].max(), f[Y].max()); }
76     /** returns the four corners of the rectangle in positive order
77      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
78     Point corner(unsigned i) const {
79         switch(i % 4) {
80             case 0:  return Point(f[X].min(), f[Y].min());
81             case 1:  return Point(f[X].max(), f[Y].min());
82             case 2:  return Point(f[X].max(), f[Y].max());
83             default: return Point(f[X].min(), f[Y].max());
84         }
85     }
86         
87     //We should probably remove these - they're coord sys gnostic
88     inline double top() const { return f[Y].min(); }
89     inline double bottom() const { return f[Y].max(); }
90     inline double left() const { return f[X].min(); }
91     inline double right() const { return f[X].max(); }
92     
93     inline double width() const { return f[X].extent(); }
94     inline double height() const { return f[Y].extent(); }
96     /** returns a vector from min to max. */
97     inline Point dimensions() const { return Point(f[X].extent(), f[Y].extent()); }
98     inline Point midpoint() const { return Point(f[X].middle(), f[Y].middle()); }
100     inline double area() const { return f[X].extent() * f[Y].extent(); }
101     inline double maxExtent() const { return std::max(f[X].extent(), f[Y].extent()); }
103     inline bool isEmpty()                 const { 
104         return f[X].isEmpty()        && f[Y].isEmpty(); 
105     }
106     inline bool intersects(Rect const &r) const { 
107         return f[X].intersects(r[X]) && f[Y].intersects(r[Y]); 
108     }
109     inline bool contains(Rect const &r)   const { 
110         return f[X].contains(r[X]) && f[Y].contains(r[Y]); 
111     }
112     inline bool contains(Point const &p)  const {
113         return f[X].contains(p[X]) && f[Y].contains(p[Y]);
114     }
116     inline void expandTo(Point p)        { 
117         f[X].extendTo(p[X]);  f[Y].extendTo(p[Y]); 
118     }
119     inline void unionWith(Rect const &b) { 
120         f[X].unionWith(b[X]); f[Y].unionWith(b[Y]); 
121     }
123     inline void expandBy(double amnt)    { 
124         f[X].expandBy(amnt);  f[Y].expandBy(amnt); 
125     }
126     inline void expandBy(Point const p)  { 
127         f[X].expandBy(p[X]);  f[Y].expandBy(p[Y]); 
128     }
130     /** Transforms the rect by m. Note that it gives correct results only for scales and translates,
131         in the case of rotations, the area of the rect will grow as it cannot rotate. */
132     inline Rect operator*(Matrix const m) const { 
133         return unify(Rect(corner(0) * m, corner(2) * m),
134                      Rect(corner(1) * m, corner(3) * m));
135     }
136 };
138 inline Rect unify(Rect const & a, Rect const & b) {
139     return Rect(unify(a[X], b[X]), unify(a[Y], b[Y]));
142 inline Rect union_list(std::vector<Rect> const &r) {
143     if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
144     Rect ret = r[0];
145     for(unsigned i = 1; i < r.size(); i++)
146         ret.unionWith(r[i]);
147     return ret;
150 inline boost::optional<Rect> intersect(Rect const & a, Rect const & b) {
151     boost::optional<Interval> x = intersect(a[X], b[X]);
152     boost::optional<Interval> y = intersect(a[Y], b[Y]);
153     return x && y ? boost::optional<Rect>(Rect(*x, *y)) : boost::optional<Rect>();
156 inline
157 double distanceSq( Point const& p, Rect const& rect )
159         double dx = 0, dy = 0;
160         if ( p[X] < rect.left() )
161         {
162                 dx = p[X] - rect.left();
163         }
164         else if ( p[X] > rect.right() )
165         {
166                 dx = rect.right() - p[X];
167         }
168         if ( p[Y] < rect.top() )
169         {
170                 dy = rect.top() - p[Y];
171         }
172         else if (  p[Y] > rect.bottom() )
173         {
174                 dy = p[Y] - rect.bottom();
175         }
176         return dx*dx + dy*dy;
179 inline 
180 double distance( Point const& p, Rect const& rect )
182         return std::sqrt(distanceSq(p, rect));
186 } // end namespace Geom
188 #endif //_2GEOM_RECT
189 #endif //_2GEOM_D2
190 /*
191   Local Variables:
192   mode:c++
193   c-file-style:"stroustrup"
194   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
195   indent-tabs-mode:nil
196   fill-column:99
197   End:
198 */
199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :