scanline_polygon_fill.h
Go to the documentation of this file.
00001 
00063 #ifndef SCANLINE_POLYGON_FILL_H
00064 #define SCANLINE_POLYGON_FILL_H
00065 
00066 namespace cob_3d_mapping
00067 {
00068   class ScanlinePolygon
00069   {
00070   public:
00071     ScanlinePolygon(const Eigen::Vector3f& n_, float d_)
00072     {
00073       float invN2 = 1.0f / n_(2);
00074       c(0) = d_ * invN2;
00075       c(1) = n_(0) * invN2;
00076       c(2) = n_(1) * invN2;
00077     }
00078 
00079     inline float intersection(float x, float y) const { return c(0) - c(1)*x - c(2)*y; }
00080 
00081     Eigen::Vector3f c;
00082   };
00083 
00084   /* data structure used by ScanlinePolygonFill
00085    * represents a line of a 2D polygon
00086    */
00087   template<typename T = int>
00088   class ScanlineEdge
00089   {
00090   public:
00091     ScanlineEdge(T id_, float x1, float y1, float x2, float y2)
00092       : id(id_)
00093       , x1_(x1)
00094       , y1_(y1)
00095       , x2_(x2)
00096       , y2_(y2)
00097     {
00098       if(y1>y2) { ymax = y1; ymin = y2; }
00099       else { ymax = y2; ymin = y1; }
00100       mInv = (x2-x1)/(y2-y1);
00101       t = mInv*y1/x1;
00102     }
00103 
00104     inline float intersection(float y) const { return mInv*(y-y1_)+x1_; }
00105 
00106     T id;
00107     float ymin;
00108     float ymax;
00109     float mInv;
00110     float t;
00111     float x1_;
00112     float y1_;
00113     float x2_;
00114     float y2_;
00115   };
00116 
00117   template<typename T>
00118   inline const bool operator< (const ScanlineEdge<T>& lhs,
00119                                const ScanlineEdge<T>& rhs)
00120   { return lhs.ymin < rhs.ymin; }
00121 
00122   template<typename T>
00123   inline const bool operator> (const ScanlineEdge<T>& lhs,
00124                                const ScanlineEdge<T>& rhs)
00125   { return  operator< (rhs, lhs); }
00126 
00127   template<typename T>
00128   inline const bool operator<= (const ScanlineEdge<T>& lhs,
00129                                 const ScanlineEdge<T>& rhs)
00130   { return !operator> (lhs, rhs); }
00131 
00132   template<typename T>
00133   inline const bool operator>= (const ScanlineEdge<T>& lhs,
00134                                 const ScanlineEdge<T>& rhs)
00135   { return !operator< (lhs, rhs); }
00136 
00137 
00138   /* Algorithm for rasterization of 2D images consisting of polygon lines
00139    * UPDATE: now modified to perform x,y,z clipping
00140    * Source: http://en.wikipedia.org/wiki/Flood_fill
00141    *         common practice in computer graphics
00142    * Output: image of which each pixel consists of a vector of ids
00143    */
00144   template<typename T = int>
00145   class ScanlinePolygonFill
00146   {
00147   public:
00148     ScanlinePolygonFill(int width, int height)
00149       : w(width)
00150       , h(height)
00151       , xmin(-1.0f)
00152       , xmax( 1.0f)
00153       , ymin(-1.0f)
00154       , ymax( 1.0f)
00155       , zmin(-1.0f)
00156       , zmax( 1.0f)
00157       , zthr( 0.2f)
00158     { }
00159 
00160     inline void addEdge(T id, float x1, float y1, float x2, float y2)
00161     {
00162       yque.push_back(ScanlineEdge<T>(id,x1,y1,x2,y2));
00163     }
00164 
00165     inline bool addPolygon(T id, const Eigen::Vector3f& normal, float d)
00166     {
00167       /*std::cout << "addPolygon: " << id <<": "<<normal(0)
00168         <<" x + "<<normal(1)<<" y + "<<normal(2)<<" z = "<<d<<std::endl;*/
00169       return polys.insert( std::make_pair(id, ScanlinePolygon(normal,d)) ).second;
00170     }
00171 
00172     inline int x2w(float x) const
00173     {
00174       return int((x - xmin) / (xmax - xmin) * float(w));
00175     }
00176 
00177     inline int y2h(float y) const
00178     {
00179       return int((y - ymin) / (ymax - ymin) * float(h));
00180     }
00181 
00182     // only draws lines of polygons
00183     void draw(std::vector<std::vector<T> >& out);
00184 
00185     // fills areas of polygons
00186     void fill(std::vector<std::vector<T> >& out);
00187 
00188   private:
00189     int w;
00190     int h;
00191     float xmin;
00192     float xmax;
00193     float ymin;
00194     float ymax;
00195     float zmin;
00196     float zmax;
00197     float zthr;
00198     std::list<ScanlineEdge<T> > yque;
00199     std::map<T,ScanlinePolygon> polys;
00200   };
00201 }
00202 
00203 #include "cob_3d_fov_segmentation/impl/scanline_polygon_fill.hpp"
00204 
00205 #endif


cob_3d_fov_segmentation
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:04:19