Code

update to latest 2geom (rev1497)
[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     /* The default constructor creates a rect that contains the point (0,0). Isn't it better to have it be empty?
59      * i.e.  f[X] = f[Y] = Interval(); .  Don't use Interval(+COORD_HUGE, -COORD_HUGE) !! because the arguments
60      * will be interchanged to Interval(-COORD_HUGE, +COORD_HUGE)!!
61      */
62     D2<Interval>() { f[X] = f[Y] = Interval(0, 0); }
63     
64     D2<Interval>(Interval const &a, Interval const &b) {
65         f[X] = a;
66         f[Y] = b;
67     }
69     D2<Interval>(Point const & a, Point const & b) {
70         f[X] = Interval(a[X], b[X]);
71         f[Y] = Interval(a[Y], b[Y]);
72     }
74     inline Interval& operator[](unsigned i)              { return f[i]; }
75     inline Interval const & operator[](unsigned i) const { return f[i]; }
77     inline Point min() const { return Point(f[X].min(), f[Y].min()); }
78     inline Point max() const { return Point(f[X].max(), f[Y].max()); }
80     /** returns the four corners of the rectangle in positive order
81      *  (clockwise if +Y is up, anticlockwise if +Y is down) */
82     Point corner(unsigned i) const {
83         switch(i % 4) {
84             case 0:  return Point(f[X].min(), f[Y].min());
85             case 1:  return Point(f[X].max(), f[Y].min());
86             case 2:  return Point(f[X].max(), f[Y].max());
87             default: return Point(f[X].min(), f[Y].max());
88         }
89     }
90         
91     //We should probably remove these - they're coord sys gnostic
92     inline double top() const { return f[Y].min(); }
93     inline double bottom() const { return f[Y].max(); }
94     inline double left() const { return f[X].min(); }
95     inline double right() const { return f[X].max(); }
96     
97     inline double width() const { return f[X].extent(); }
98     inline double height() const { return f[Y].extent(); }
100     /** returns a vector from min to max. */
101     inline Point dimensions() const { return Point(f[X].extent(), f[Y].extent()); }
102     inline Point midpoint() const { return Point(f[X].middle(), f[Y].middle()); }
104     inline double area() const { return f[X].extent() * f[Y].extent(); }
105     inline double maxExtent() const { return std::max(f[X].extent(), f[Y].extent()); }
107     inline bool isEmpty()                 const { 
108         return f[X].isEmpty()        && f[Y].isEmpty(); 
109     }
110     inline bool intersects(Rect const &r) const { 
111         return f[X].intersects(r[X]) && f[Y].intersects(r[Y]); 
112     }
113     inline bool contains(Rect const &r)   const { 
114         return f[X].contains(r[X]) && f[Y].contains(r[Y]); 
115     }
116     inline bool contains(Point const &p)  const {
117         return f[X].contains(p[X]) && f[Y].contains(p[Y]);
118     }
120     inline void expandTo(Point p)        { 
121         f[X].extendTo(p[X]);  f[Y].extendTo(p[Y]); 
122     }
123     inline void unionWith(Rect const &b) { 
124         f[X].unionWith(b[X]); f[Y].unionWith(b[Y]); 
125     }
127     inline void expandBy(double amnt)    { 
128         f[X].expandBy(amnt);  f[Y].expandBy(amnt); 
129     }
130     inline void expandBy(Point const p)  { 
131         f[X].expandBy(p[X]);  f[Y].expandBy(p[Y]); 
132     }
134     /** Transforms the rect by m. Note that it gives correct results only for scales and translates,
135         in the case of rotations, the area of the rect will grow as it cannot rotate. */
136     inline Rect operator*(Matrix const m) const { 
137         return unify(Rect(corner(0) * m, corner(2) * m),
138                      Rect(corner(1) * m, corner(3) * m));
139     }
140 };
142 inline Rect unify(Rect const & a, Rect const & b) {
143     return Rect(unify(a[X], b[X]), unify(a[Y], b[Y]));
146 inline Rect union_list(std::vector<Rect> const &r) {
147     if(r.empty()) return Rect(Interval(0,0), Interval(0,0));
148     Rect ret = r[0];
149     for(unsigned i = 1; i < r.size(); i++)
150         ret.unionWith(r[i]);
151     return ret;
154 inline boost::optional<Rect> intersect(Rect const & a, Rect const & b) {
155     boost::optional<Interval> x = intersect(a[X], b[X]);
156     boost::optional<Interval> y = intersect(a[Y], b[Y]);
157     return x && y ? boost::optional<Rect>(Rect(*x, *y)) : boost::optional<Rect>();
160 inline
161 double distanceSq( Point const& p, Rect const& rect )
163         double dx = 0, dy = 0;
164         if ( p[X] < rect.left() )
165         {
166                 dx = p[X] - rect.left();
167         }
168         else if ( p[X] > rect.right() )
169         {
170                 dx = rect.right() - p[X];
171         }
172         if ( p[Y] < rect.top() )
173         {
174                 dy = rect.top() - p[Y];
175         }
176         else if (  p[Y] > rect.bottom() )
177         {
178                 dy = p[Y] - rect.bottom();
179         }
180         return dx*dx + dy*dy;
183 inline 
184 double distance( Point const& p, Rect const& rect )
186         return std::sqrt(distanceSq(p, rect));
190 } // end namespace Geom
192 #endif //_2GEOM_RECT
193 #endif //_2GEOM_D2
194 /*
195   Local Variables:
196   mode:c++
197   c-file-style:"stroustrup"
198   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
199   indent-tabs-mode:nil
200   fill-column:99
201   End:
202 */
203 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :