expose an indexing operator on box2d to allow for easy pythonic access to bounds parts

This commit is contained in:
Dane Springmeyer 2010-09-16 14:41:29 +00:00
parent 1aa05e052d
commit e22c187e95
4 changed files with 42 additions and 2 deletions

View file

@ -231,6 +231,7 @@ void export_envelope()
.def(self * float()) // __mult__
.def(float() * self)
.def(self / float()) // __div__
.def("__getitem__",&box2d<double>::operator[])
.def_pickle(envelope_pickle_suite())
;
}

View file

@ -84,7 +84,8 @@ public:
box2d_type& operator+=(box2d_type const& other);
box2d_type& operator-=(box2d_type const& other);
box2d_type& operator*=(T);
box2d_type& operator/=(T);
box2d_type& operator/=(T);
T operator[](int index) const;
};
template <class charT,class traits,class T>

View file

@ -23,6 +23,9 @@
#include <mapnik/box2d.hpp>
// stl
#include <stdexcept>
namespace mapnik
{
template <typename T>
@ -339,6 +342,32 @@ box2d<T>& box2d<T>::operator/=(T t)
maxy_ = c.y + sy;
return *this;
}
template <typename T>
T box2d<T>::operator[] (int index) const
{
switch(index)
{
case 0:
return minx_;
case 1:
return miny_;
case 2:
return maxx_;
case 3:
return maxy_;
case -4:
return minx_;
case -3:
return miny_;
case -2:
return maxx_;
case -1:
return maxy_;
default:
throw std::out_of_range("index out of range, max value is 3, min value is -4 ");
}
}
template class box2d<int>;
template class box2d<double>;

View file

@ -482,7 +482,16 @@ def test_envelope_init():
eq_(e.maxx, 200)
eq_(e.maxy, 200)
eq_(e[0],100)
eq_(e[1],100)
eq_(e[2],200)
eq_(e[3],200)
eq_(e[0],e[-4])
eq_(e[1],e[-3])
eq_(e[2],e[-2])
eq_(e[3],e[-1])
c = e.center()
eq_(c.x, 150)