projection.hpp
Go to the documentation of this file.
00001 
00063 #ifndef COB_3D_PROJECTION_HPP
00064 #define COB_3D_PROJECTION_HPP
00065 
00066 #include "cob_3d_fov_segmentation/scanline_polygon_fill.h"
00067 
00068 namespace cob_3d_mapping
00069 {
00070   template<typename SensorT>
00071   void PerspectiveProjection<SensorT>::compute(
00072     const Mat4& tf_camera, int w, int h,
00073     const std::vector<cob_3d_mapping::Polygon::Ptr>& polygons,
00074     std::vector<std::vector<int> >& projection)
00075   {
00076     // create transformation matrix from sensor position and camera parameters
00077     Mat4 proj = SensorT::tf_unit_cube() * tf_camera;
00078 
00079     // transform polygons to clipping space and reject outliers
00080     // ( space: x,y,z E [-1,1] )
00081     ScanlinePolygonFill<> spf(w,h);
00082     Vec4 projected;
00083     float xprev, yprev, xcurr, ycurr;
00084     for(int i=0; i<polygons.size(); ++i)
00085     {
00086       int id = polygons[i]->id_;
00087       std::vector<std::vector<Vec3> > contours = polygons[i]->getContours3D();
00088 
00089       Vec4 p1 = proj * makeVector4f( contours[0][1] );
00090       Vec4 p2 = proj * makeVector4f( contours[0][2] );
00091       Vec4 p3 = proj * makeVector4f( contours[0][0] );
00092 
00093       p1 /= p1(3);
00094       p2 /= p2(3);
00095       p3 /= p3(3);
00096       Vec3 a = p2.head<3>() - p1.head<3>();
00097       Vec3 b = p3.head<3>() - p1.head<3>();
00098       Vec3 normal = (a.cross(b)).normalized();
00099       float d = p1.head<3>().dot(normal);
00100       spf.addPolygon(id, normal, d);
00101 
00102       for(int c=0; c<contours.size(); ++c)
00103       {
00104         std::vector<Vec3>* ptr_c = &contours[c];
00105         projected = proj * makeVector4f( (*ptr_c)[ ptr_c->size()-1 ] );
00106         float inv_w = 1.0f / projected(3);
00107         xprev = projected(0) * inv_w;
00108         yprev = projected(1) * inv_w;
00109 
00110         for(int p=0; p<ptr_c->size(); ++p)
00111         {
00112           projected = proj * makeVector4f((*ptr_c)[p]);
00113           inv_w = 1.0f / projected(3);
00114           xcurr = projected(0) * inv_w;
00115           ycurr = projected(1) * inv_w;
00116           spf.addEdge(id, xprev, yprev, xcurr, ycurr);
00117           xprev = xcurr;
00118           yprev = ycurr;
00119         }
00120       }
00121     }
00122     // rasterize remaining polygons using scanline rendering algorithm
00123     spf.fill(projection);
00124   }
00125 }
00126 
00127 #endif


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