mapnik/src/envelope.cpp

330 lines
7.2 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* Copyright (C) 2006 Artem Pavlenko
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
2005-06-14 17:06:59 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
2005-06-14 17:06:59 +02:00
//$Id: envelope.cpp 17 2005-03-08 23:58:43Z pavlenko $
#include <mapnik/envelope.hpp>
2005-06-14 17:06:59 +02:00
namespace mapnik
{
template <typename T>
Envelope<T>::Envelope()
2005-06-14 17:06:59 +02:00
:minx_(0),miny_(0),maxx_(-1),maxy_(-1) {}
template <typename T>
Envelope<T>::Envelope(T minx_,T miny_,T maxx_,T maxy_)
2005-06-14 17:06:59 +02:00
{
init(minx_,miny_,maxx_,maxy_);
}
template <typename T>
Envelope<T>::Envelope(const coord<T,2> &c0,const coord<T,2> &c1)
2005-06-14 17:06:59 +02:00
{
init(c0.x,c0.y,c1.x,c1.y);
}
template <typename T>
Envelope<T>::Envelope(const Envelope &rhs)
2005-06-14 17:06:59 +02:00
{
init(rhs.minx_,rhs.miny_,rhs.maxx_,rhs.maxy_);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::operator==(const Envelope<T>& other) const
2005-06-14 17:06:59 +02:00
{
return minx_==other.minx_ &&
miny_==other.miny_ &&
maxx_==other.maxx_ &&
maxy_==other.maxy_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
T Envelope<T>::minx() const
2005-06-14 17:06:59 +02:00
{
return minx_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
T Envelope<T>::maxx() const
2005-06-14 17:06:59 +02:00
{
return maxx_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
T Envelope<T>::miny() const
2005-06-14 17:06:59 +02:00
{
return miny_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
T Envelope<T>::maxy() const
2005-06-14 17:06:59 +02:00
{
return maxy_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
T Envelope<T>::width() const
2005-06-14 17:06:59 +02:00
{
return maxx_-minx_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
T Envelope<T>::height() const
2005-06-14 17:06:59 +02:00
{
return maxy_-miny_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
void Envelope<T>::width(T w)
2005-06-14 17:06:59 +02:00
{
T cx=center().x;
minx_=static_cast<T>(cx-w*0.5);
maxx_=static_cast<T>(cx+w*0.5);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
void Envelope<T>::height(T h)
2005-06-14 17:06:59 +02:00
{
T cy=center().y;
miny_=static_cast<T>(cy-h*0.5);
maxy_=static_cast<T>(cy+h*0.5);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
coord<T,2> Envelope<T>::center() const
2005-06-14 17:06:59 +02:00
{
return coord<T,2>(static_cast<T>(0.5*(minx_+maxx_)),
static_cast<T>(0.5*(miny_+maxy_)));
2005-06-14 17:06:59 +02:00
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
void Envelope<T>::expand_to_include(const coord<T,2>& c)
2005-06-14 17:06:59 +02:00
{
expand_to_include(c.x,c.y);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
void Envelope<T>::expand_to_include(T x,T y)
2005-06-14 17:06:59 +02:00
{
if (x<minx_) minx_=x;
if (x>maxx_) maxx_=x;
if (y<miny_) miny_=y;
if (y>maxy_) maxy_=y;
}
template <typename T>
void Envelope<T>::expand_to_include(const Envelope<T> &other)
2005-06-14 17:06:59 +02:00
{
if (other.minx_<minx_) minx_=other.minx_;
if (other.maxx_>maxx_) maxx_=other.maxx_;
if (other.miny_<miny_) miny_=other.miny_;
if (other.maxy_>maxy_) maxy_=other.maxy_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::contains(const coord<T,2> &c) const
2005-06-14 17:06:59 +02:00
{
return contains(c.x,c.y);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::contains(T x,T y) const
2005-06-14 17:06:59 +02:00
{
return x>=minx_ && x<=maxx_ && y>=miny_ && y<=maxy_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::contains(const Envelope<T> &other) const
2005-06-14 17:06:59 +02:00
{
return other.minx_>=minx_ &&
other.maxx_<=maxx_ &&
other.miny_>=miny_ &&
other.maxy_<=maxy_;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::intersects(const coord<T,2> &c) const
2005-06-14 17:06:59 +02:00
{
return intersects(c.x,c.y);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::intersects(T x,T y) const
2005-06-14 17:06:59 +02:00
{
return !(x>maxx_ || x<minx_ || y>maxy_ || y<miny_);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
bool Envelope<T>::intersects(const Envelope<T> &other) const
2005-06-14 17:06:59 +02:00
{
return !(other.minx_>maxx_ || other.maxx_<minx_ ||
other.miny_>maxy_ || other.maxy_<miny_);
2005-06-14 17:06:59 +02:00
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
Envelope<T> Envelope<T>::intersect(const EnvelopeType& other) const
2005-06-14 17:06:59 +02:00
{
if (intersects(other)) {
T x0=std::max(minx_,other.minx_);
T y0=std::max(miny_,other.miny_);
2005-06-14 17:06:59 +02:00
T x1=std::min(maxx_,other.maxx_);
T y1=std::min(maxy_,other.maxy_);
2005-06-14 17:06:59 +02:00
return Envelope<T>(x0,y0,x1,y1);
} else {
return Envelope<T>();
}
2005-06-14 17:06:59 +02:00
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
void Envelope<T>::re_center(T cx,T cy)
2005-06-14 17:06:59 +02:00
{
T dx=cx-center().x;
T dy=cy-center().y;
minx_+=dx;
miny_+=dy;
maxx_+=dx;
maxy_+=dy;
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
#endif
void Envelope<T>::init(T x0,T y0,T x1,T y1)
2005-06-14 17:06:59 +02:00
{
if (x0<x1)
{
minx_=x0;maxx_=x1;
}
else
{
minx_=x1;maxx_=x0;
}
if (y0<y1)
{
miny_=y0;maxy_=y1;
}
else
{
miny_=y1;maxy_=y0;
}
}
2006-10-16 15:31:52 +02:00
template <typename T>
Envelope<T>& Envelope<T>::operator+=(Envelope<T> const& other)
{
expand_to_include(other);
return *this;
}
template <typename T>
Envelope<T>& Envelope<T>::operator-=(Envelope<T> const& other)
{
// not sure what to do here. intersect?
return *this;
}
template <typename T>
Envelope<T>& Envelope<T>::operator*=(T t)
{
minx_ *= t;
miny_ *= t;
maxx_ *= t;
maxy_ *= t;
return *this;
}
2005-06-14 17:06:59 +02:00
2006-10-16 15:31:52 +02:00
template <typename T>
Envelope<T>& Envelope<T>::operator/=(T t)
{
minx_ /= t;
miny_ /= t;
maxx_ /= t;
maxy_ /= t;
return *this;
}
2005-06-14 17:06:59 +02:00
template class Envelope<int>;
template class Envelope<double>;
}