core/vgl/vgl_window_scan_iterator.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_window_scan_iterator.h
00002 #ifndef vgl_window_scan_iterator_h_
00003 #define vgl_window_scan_iterator_h_
00004 //:
00005 // \file
00006 // \brief Iterator to scan rectangular windows
00007 // \author fsm
00008 // \verbatim
00009 //  Modifications
00010 //   Nov.2003 - Peter Vanroose - made class vgl_window_scan_iterator templated
00011 //   Nov.2003 - Peter Vanroose - made constructor more robust w.r.t. its input
00012 // \endverbatim
00013 
00014 #include <vcl_cmath.h>
00015 #include <vgl/vgl_region_scan_iterator.h>
00016 
00017 //: Iterator to scan rectangular windows
00018 //  This class is completely inlined.
00019 template <class T>
00020 class vgl_window_scan_iterator : public vgl_region_scan_iterator
00021 {
00022   int x1, y1, x2, y2;
00023  public:
00024 
00025   //: Define rectangular region to be [x1, x2] x [y1, y2]
00026   inline void set_window_corners(T x1_, T y1_, T x2_, T y2_)
00027   {
00028     // Make sure that the scan lines have positive x increment:
00029     if (x1_ > x2_) { T x=x1_; x1_=x2_; x2_=x; }
00030     x1 = (int) vcl_ceil (x1_);
00031     x2 = (int) vcl_floor(x2_);
00032     // subsequent scan lines need not have positive y increment:
00033     if (y1_ <= y2_) {
00034       y1 = (int) vcl_ceil (y1_);
00035       y2 = (int) vcl_floor(y2_);
00036     }
00037     else {
00038       y2 = (int) vcl_ceil (y2_);
00039       y1 = (int) vcl_floor(y1_);
00040     }
00041   }
00042 
00043   //: Define rectangular region to be [x-w, x+w] x [y-h, y+h]
00044   inline void set_window(T x, T y, T w, T h)
00045   { set_window_corners(x - w, y - h, x + w, y + h); }
00046 
00047   //: Define rectangular region to be [x-r, x+r] x [y-r, y+r]
00048   inline void set_window(T x, T y, T r)
00049   { set_window_corners(x - r, y - r, x + r, y + r); }
00050 
00051   //: makes uninitialized iterator.
00052   inline vgl_window_scan_iterator() {}
00053 
00054   //: region is [x1, x2] x [y1, y2].  No assumption about x1<x2 or y1<y2.
00055   inline vgl_window_scan_iterator(T x1_, T y1_, T x2_, T y2_)
00056   { set_window_corners(x1_, y1_, x2_, y2_); }
00057 
00058   int current_y;
00059 
00060   inline void reset() { current_y = y1<=y2 ? y1-1 : y1+1; }
00061   inline bool next () { return y1<=y2 ? ++current_y <= y2 : --current_y >= y2; }
00062   inline int  scany () const { return current_y; }
00063   inline int  startx() const { return x1; }
00064   inline int  endx  () const { return x2; }
00065 };
00066 
00067 #define VGL_WINDOW_SCAN_ITERATOR_INSTANTIATE(T) extern "please include <vgl/vgl_window_scan_iterator.txx> instead"
00068 
00069 #endif // vgl_window_scan_iterator_h_