root/trunk/src/envelope.cpp @ 789

Revision 789, 7.2 kB (checked in by artem, 20 months ago)

+ applied intersection patch from randomjunk - #127

Line 
1/*****************************************************************************
2 *
3 * This file is part of Mapnik (c++ mapping toolkit)
4 *
5 * Copyright (C) 2006 Artem Pavlenko
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 *****************************************************************************/
22//$Id: envelope.cpp 17 2005-03-08 23:58:43Z pavlenko $
23
24#include <mapnik/envelope.hpp>
25
26namespace mapnik
27{
28    template <typename T>
29    Envelope<T>::Envelope()
30        :minx_(0),miny_(0),maxx_(-1),maxy_(-1) {}
31
32    template <typename T>
33    Envelope<T>::Envelope(T minx_,T miny_,T maxx_,T maxy_)
34    {
35        init(minx_,miny_,maxx_,maxy_);
36    }
37
38    template <typename T>
39    Envelope<T>::Envelope(const coord<T,2> &c0,const coord<T,2> &c1)
40    {
41        init(c0.x,c0.y,c1.x,c1.y);
42    }
43
44    template <typename T>
45    Envelope<T>::Envelope(const Envelope &rhs)
46    {
47        init(rhs.minx_,rhs.miny_,rhs.maxx_,rhs.maxy_);
48    }
49
50    template <typename T>
51#if !defined(__SUNPRO_CC)
52    inline
53#endif
54    bool Envelope<T>::operator==(const Envelope<T>& other) const
55    {
56        return minx_==other.minx_ &&
57            miny_==other.miny_ &&
58            maxx_==other.maxx_ &&
59            maxy_==other.maxy_;
60    }
61
62    template <typename T>
63#if !defined(__SUNPRO_CC)
64    inline
65#endif
66    T Envelope<T>::minx() const
67    {
68        return minx_;
69    }
70
71    template <typename T>
72#if !defined(__SUNPRO_CC)
73    inline
74#endif
75    T Envelope<T>::maxx() const
76    {
77        return maxx_;
78    }
79
80    template <typename T>
81#if !defined(__SUNPRO_CC)
82    inline
83#endif
84    T Envelope<T>::miny() const
85    {
86        return miny_;
87    }
88
89    template <typename T>
90#if !defined(__SUNPRO_CC)
91    inline
92#endif
93    T Envelope<T>::maxy() const
94    {
95        return maxy_;
96    }
97
98    template <typename T>
99#if !defined(__SUNPRO_CC)
100    inline
101#endif
102    T Envelope<T>::width() const
103    {
104        return maxx_-minx_;
105    }
106
107    template <typename T>
108#if !defined(__SUNPRO_CC)
109    inline
110#endif
111    T Envelope<T>::height() const
112    {
113        return maxy_-miny_;
114    }
115
116    template <typename T>
117#if !defined(__SUNPRO_CC)
118    inline
119#endif
120    void Envelope<T>::width(T w)
121    {
122        T cx=center().x;
123        minx_=static_cast<T>(cx-w*0.5);
124        maxx_=static_cast<T>(cx+w*0.5);
125    }
126
127    template <typename T>
128#if !defined(__SUNPRO_CC)
129    inline
130#endif
131    void Envelope<T>::height(T h)
132    {
133        T cy=center().y;
134        miny_=static_cast<T>(cy-h*0.5);
135        maxy_=static_cast<T>(cy+h*0.5);
136    }
137
138    template <typename T>
139#if !defined(__SUNPRO_CC)
140    inline
141#endif
142    coord<T,2> Envelope<T>::center() const
143    {
144        return coord<T,2>(static_cast<T>(0.5*(minx_+maxx_)),
145                          static_cast<T>(0.5*(miny_+maxy_)));
146    }
147
148    template <typename T>
149#if !defined(__SUNPRO_CC)
150    inline
151#endif
152    void Envelope<T>::expand_to_include(const coord<T,2>& c)
153    {
154        expand_to_include(c.x,c.y);
155    }
156
157    template <typename T>
158#if !defined(__SUNPRO_CC)
159    inline
160#endif
161    void Envelope<T>::expand_to_include(T x,T y)
162    {
163        if (x<minx_) minx_=x;
164        if (x>maxx_) maxx_=x;
165        if (y<miny_) miny_=y;
166        if (y>maxy_) maxy_=y;
167    }
168
169    template <typename T>
170    void Envelope<T>::expand_to_include(const Envelope<T> &other)
171    {
172        if (other.minx_<minx_) minx_=other.minx_;
173        if (other.maxx_>maxx_) maxx_=other.maxx_;
174        if (other.miny_<miny_) miny_=other.miny_;
175        if (other.maxy_>maxy_) maxy_=other.maxy_;
176    }
177
178    template <typename T>
179#if !defined(__SUNPRO_CC)
180    inline
181#endif
182    bool Envelope<T>::contains(const coord<T,2> &c) const
183    {
184        return contains(c.x,c.y);
185    }
186
187    template <typename T>
188#if !defined(__SUNPRO_CC)
189    inline
190#endif
191    bool Envelope<T>::contains(T x,T y) const
192    {
193        return x>=minx_ && x<=maxx_ && y>=miny_ && y<=maxy_;
194    }
195
196    template <typename T>
197#if !defined(__SUNPRO_CC)
198    inline
199#endif
200    bool Envelope<T>::contains(const Envelope<T> &other) const
201    {
202        return other.minx_>=minx_ &&
203            other.maxx_<=maxx_ &&
204            other.miny_>=miny_ &&
205            other.maxy_<=maxy_;
206    }
207
208    template <typename T>
209#if !defined(__SUNPRO_CC)
210    inline
211#endif
212    bool Envelope<T>::intersects(const coord<T,2> &c) const
213    {
214        return intersects(c.x,c.y);
215    }
216
217    template <typename T>
218#if !defined(__SUNPRO_CC)
219    inline
220#endif
221    bool Envelope<T>::intersects(T x,T y) const
222    {
223        return !(x>maxx_ || x<minx_ || y>maxy_ || y<miny_);
224    }
225
226    template <typename T>
227#if !defined(__SUNPRO_CC)
228    inline
229#endif
230    bool Envelope<T>::intersects(const Envelope<T> &other) const
231    {
232        return !(other.minx_>maxx_ || other.maxx_<minx_ ||
233                 other.miny_>maxy_ || other.maxy_<miny_);
234    }
235
236    template <typename T>
237#if !defined(__SUNPRO_CC)
238    inline
239#endif
240    Envelope<T> Envelope<T>::intersect(const EnvelopeType& other) const
241    {
242        if (intersects(other)) {
243            T x0=std::max(minx_,other.minx_);
244            T y0=std::max(miny_,other.miny_);
245
246            T x1=std::min(maxx_,other.maxx_);
247            T y1=std::min(maxy_,other.maxy_);
248
249            return Envelope<T>(x0,y0,x1,y1);
250        } else {
251            return Envelope<T>();
252        }
253    }
254
255    template <typename T>
256#if !defined(__SUNPRO_CC)
257    inline
258#endif
259    void Envelope<T>::re_center(T cx,T cy)
260    {
261        T dx=cx-center().x;
262        T dy=cy-center().y;
263        minx_+=dx;
264        miny_+=dy;
265        maxx_+=dx;
266        maxy_+=dy;
267    }
268
269    template <typename T>
270#if !defined(__SUNPRO_CC)
271    inline
272#endif
273    void Envelope<T>::init(T x0,T y0,T x1,T y1)
274    {
275        if (x0<x1)
276        {
277            minx_=x0;maxx_=x1;
278        }
279        else
280        {
281            minx_=x1;maxx_=x0;
282        }
283        if (y0<y1)
284        {
285            miny_=y0;maxy_=y1;
286        }
287        else
288        {
289            miny_=y1;maxy_=y0;
290        }
291    }
292   
293    template <typename T>
294    Envelope<T>&  Envelope<T>::operator+=(Envelope<T> const& other)
295    {
296        expand_to_include(other);
297        return *this;
298    }
299   
300    template <typename T>   
301    Envelope<T>& Envelope<T>::operator-=(Envelope<T> const& other)
302    {
303        // not sure what to do here. intersect?
304        return *this;
305    }
306   
307    template <typename T>   
308    Envelope<T>& Envelope<T>::operator*=(T t)
309    {
310        minx_ *= t;
311        miny_ *= t;
312        maxx_ *= t;
313        maxy_ *= t;
314        return *this;
315    }
316
317    template <typename T>   
318    Envelope<T>& Envelope<T>::operator/=(T t)
319    {
320        minx_ /= t;
321        miny_ /= t;
322        maxx_ /= t;
323        maxy_ /= t;
324        return *this;
325    }
326   
327    template class Envelope<int>;
328    template class Envelope<double>;
329}
Note: See TracBrowser for help on using the browser.