https://svn.lrde.epita.fr/svn/oln/trunk/milena
Index: ChangeLog
from Nicolas Ballas <ballas(a)lrde.epita.fr>
Clean up the line piter code.
* tests/line_piter.cc: Update the test.
* mln/core/line_piter.hh: Clean up.
mln/core/line_piter.hh | 35 ++++++++++++++++----------------
tests/line_piter.cc | 52 +++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 63 insertions(+), 24 deletions(-)
Index: tests/line_piter.cc
--- tests/line_piter.cc (revision 1878)
+++ tests/line_piter.cc (working copy)
@@ -30,22 +30,62 @@
* \brief Tests on mln::line_piter.
*/
+//FIXME: replace by the necessary include
+#include <mln/core/image1d.hh>
#include <mln/core/image2d.hh>
+#include <mln/core/image3d.hh>
#include <mln/core/line_piter.hh>
int main()
{
using namespace mln;
- box2d b(make::point2d(1,2), make::point2d(5,8));
const unsigned border = 2;
- image2d<int> f(b, border);
- image2d<int>::line_piter p(f.domain());
+ /// Test with image 1d
+ {
+ box1d b1(make::point1d(5), make::point1d(42));
+ image1d<int> f1(b1, border);
+ image1d<int>::line_piter p1(f1.domain());
+ for_all(p1)
+ {
+ mln_assertion(p1[0] == 5);
+ std::cout << p1 <<std::endl;
+ }
+ }
+
+
+ /// Test with image 2d
+ {
+ box2d b2(make::point2d(1,2), make::point2d(5,8));
+ image2d<int> f2(b2, border);
+
+ image2d<int>::line_piter p2(f2.domain());
int i = 1;
- for_all(p)
+ for_all(p2)
{
- mln_assertion(p[1] == 0 && p[0] == i++);
- std::cout << p <<std::endl;
+ mln_assertion(p2[1] == 2 && p2[0] == i++);
+ std::cout << p2 <<std::endl;
+ }
}
+
+ /// Test with image 3d
+ {
+ box3d b3(make::point3d(1,2,3), make::point3d(5,8,7));
+ image3d<int> f3(b3, border);
+
+ image3d<int>::line_piter p3(f3.domain());
+ int i = 1;
+ int j = 2;
+ for_all(p3)
+ {
+ mln_assertion( p3[0] == i && p3[1] == j && p3[2] == 3);
+ std::cout << p3 << std::endl;
+ if (i++ == 5)
+ i = 1;
+ if (j++ == 8)
+ j = 2;
+ }
+ }
+
}
Index: mln/core/line_piter.hh
--- mln/core/line_piter.hh (revision 1878)
+++ mln/core/line_piter.hh (working copy)
@@ -46,7 +46,8 @@
* The parameter \c P is the type of points.
*/
template <typename P>
- class line_piter_ : public internal::point_iterator_base_< P, line_piter_<P>
>
+ class line_piter_ :
+ public internal::point_iterator_base_< P, line_piter_<P> >
{
typedef line_piter_<P> self_;
typedef internal::point_iterator_base_< P, self_ > super_;
@@ -84,7 +85,8 @@
private:
const box_<P>& b_;
- P p_, nop_;
+ P p_;
+ bool is_valid_;
};
@@ -98,14 +100,6 @@
line_piter_<P>::line_piter_(const box_<P>& b)
: b_(b)
{
- nop_ = b_.pmax();
- if (dim == 1)
- ++nop_[0];
- else
- {
- nop_[0] = 0;
- ++nop_[1];
- }
invalidate();
}
@@ -113,7 +107,7 @@
inline
line_piter_<P>::operator P() const
{
- return p_;
+ return to_point();
}
template <typename P>
@@ -121,6 +115,7 @@
const P&
line_piter_<P>::to_point() const
{
+ mln_precondition(is_valid());
return p_;
}
@@ -129,8 +124,10 @@
mln_coord(P)
line_piter_<P>::operator[](unsigned i) const
{
+ mln_precondition(is_valid());
+ mln_precondition(i < dim);
+
mln_invariant(p_[dim - 1] == b_.pmin()[dim - 1]);
- assert(i < dim);
return p_[i];
}
@@ -139,7 +136,7 @@
bool
line_piter_<P>::is_valid() const
{
- return p_ != nop_;
+ return is_valid_;
}
template <typename P>
@@ -147,7 +144,7 @@
void
line_piter_<P>::invalidate()
{
- p_ = nop_;
+ is_valid_= false;
}
template <typename P>
@@ -156,6 +153,7 @@
line_piter_<P>::start()
{
p_ = b_.pmin();
+ is_valid_ = true;
}
template <typename P>
@@ -163,17 +161,18 @@
void
line_piter_<P>::next_()
{
+ mln_precondition(is_valid());
+
+ // Do we want this run for image in 3d?
for (int c = dim - 2; c >= 0; --c)
{
if (p_[c] != b_.pmax()[c])
- {
++p_[c];
- break;
- }
+ else
p_[c] = b_.pmin()[c];
}
if (p_ == b_.pmin())
- p_ = nop_;
+ invalidate();
}