add operators for computing the envelope of a transformed box2d

(cherry picked from commit 9ad342cbed150948561c08e98957bc014dedc7f3)
This commit is contained in:
Mickey Rose 2012-03-06 18:12:45 +01:00
parent 291bc9b8ba
commit 46c2d1c710
2 changed files with 56 additions and 3 deletions

View file

@ -33,6 +33,12 @@
// stl
#include <iomanip>
// agg
// forward declare so that apps using mapnik do not need agg headers
namespace agg {
struct trans_affine;
}
namespace mapnik {
/*!
@ -56,6 +62,7 @@ public:
box2d(T minx,T miny,T maxx,T maxy);
box2d(const coord<T,2>& c0,const coord<T,2>& c1);
box2d(const box2d_type& rhs);
box2d(const box2d_type& rhs, const agg::trans_affine& tr);
T minx() const;
T miny() const;
T maxx() const;
@ -88,6 +95,10 @@ public:
box2d_type& operator*=(T);
box2d_type& operator/=(T);
T operator[](int index) const;
// compute the bounding box of this one transformed
box2d_type operator* (agg::trans_affine const& tr) const;
box2d_type& operator*=(agg::trans_affine const& tr);
};
template <class charT,class traits,class T>
@ -98,9 +109,9 @@ operator << (std::basic_ostream<charT,traits>& out,
std::basic_ostringstream<charT,traits> s;
s.copyfmt(out);
s.width(0);
s <<"box2d(" << std::setprecision(16)
<< e.minx() << "," << e.miny() <<","
<< e.maxx() << "," << e.maxy() <<")";
s << "box2d(" << std::setprecision(16)
<< e.minx() << ',' << e.miny() << ','
<< e.maxx() << ',' << e.maxy() << ')';
out << s.str();
return out;
}

View file

@ -31,6 +31,9 @@
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp>
// agg
#include "agg_trans_affine.h"
namespace mapnik
{
template <typename T>
@ -61,6 +64,22 @@ box2d<T>::box2d(const box2d &rhs)
init(rhs.minx_,rhs.miny_,rhs.maxx_,rhs.maxy_);
}*/
template <typename T>
box2d<T>::box2d(const box2d_type &rhs, const agg::trans_affine& tr)
{
double x0 = rhs.minx_, y0 = rhs.miny_;
double x1 = rhs.maxx_, y1 = rhs.miny_;
double x2 = rhs.maxx_, y2 = rhs.maxy_;
double x3 = rhs.minx_, y3 = rhs.maxy_;
tr.transform(&x0, &y0);
tr.transform(&x1, &y1);
tr.transform(&x2, &y2);
tr.transform(&x3, &y3);
init(x0, y0, x2, y2);
expand_to_include(x1, y1);
expand_to_include(x3, y3);
}
template <typename T>
#if !defined(__SUNPRO_CC)
inline
@ -437,6 +456,29 @@ T box2d<T>::operator[] (int index) const
}
}
template <typename T>
box2d<T> box2d<T>::operator*(agg::trans_affine const& tr) const
{
return box2d<T>(*this, tr);
}
template <typename T>
box2d<T>& box2d<T>::operator*=(agg::trans_affine const& tr)
{
double x0 = minx_, y0 = miny_;
double x1 = maxx_, y1 = miny_;
double x2 = maxx_, y2 = maxy_;
double x3 = minx_, y3 = maxy_;
tr.transform(&x0, &y0);
tr.transform(&x1, &y1);
tr.transform(&x2, &y2);
tr.transform(&x3, &y3);
init(x0, y0, x2, y2);
expand_to_include(x1, y1);
expand_to_include(x3, y3);
return *this;
}
template class box2d<int>;
template class box2d<double>;
}