test_mesh_projection.cpp
Go to the documentation of this file.
00001 
00063 #include <boost/program_options.hpp>
00064 
00065 #include <pcl/io/pcd_io.h>
00066 
00067 #include <cob_3d_mapping_common/stop_watch.h>
00068 #include <cob_3d_mapping_common/sensor_model.h>
00069 
00070 #include <cob_3d_meshing/mesh_types.h>
00071 #include <cob_3d_meshing/mesh_conversion.h>
00072 #include <cob_3d_meshing/mesh_decimation.h>
00073 
00074 
00075 //== GLOBAL VARS ===============================================================
00076 std::string file_in_;
00077 std::string file_out_;
00078 
00079 int readOptions(int argc, char** argv)
00080 {
00081   using namespace boost::program_options;
00082   options_description options("Options");
00083   options.add_options()
00084     ("help,h", "produce help message")
00085     ("in,i", value<std::string>(&file_in_), "input folder with data points")
00086     ("out,o", value<std::string>(&file_out_), "out file with data points")
00087     ;
00088 
00089   positional_options_description p_opt;
00090   p_opt.add("in", 1).add("out",1);
00091   variables_map vm;
00092   store(command_line_parser(argc, argv)
00093         .options(options).positional(p_opt).run(), vm);
00094   notify(vm);
00095 
00096   if(vm.count("help") || argc == 1)
00097   { std::cout << options << std::endl; return(-1); }
00098 
00099   return 0;
00100 }
00101 
00102 template<typename Vec3T>
00103 inline Eigen::Vector4f makeVector4f(const Vec3T& v, float last=1.0f)
00104 {
00105   return Eigen::Vector4f(v[0], v[1], v[2], last);
00106 }
00107 
00108 template<typename MeshT, typename SensorT = cob_3d_mapping::PrimeSense>
00109 class MeshProjection
00110 {
00111 private:
00112   typedef Eigen::Vector3f Vec3;
00113   typedef Eigen::Vector4f Vec4;
00114   typedef Eigen::Matrix4f Mat4;
00115   typedef typename SensorT::MatMap MatMap;
00116   typedef typename MeshT::VertexHandle VertexHandle;
00117   typedef typename MeshT::FaceHandle FaceHandle;
00118 
00119 public:
00120   void compute(const Mat4& tf_camera, int w, int h,
00121                const MeshT& mesh,
00122                std::vector<std::vector<FaceHandle> >& projection)
00123   {
00124     Mat4 proj = SensorT::tf_unit_cube() * tf_camera;
00125     ScanlinePolygonFill<FaceHandle> spf(w,h);
00126 
00127     std::map<VertexHandle, Vec3> transformed;
00128     typename MeshT::VertexIter v_it = mesh.vertices_begin();
00129     typename MeshT::VertexIter v_end = mesh.vertices_end();
00130     for(; v_it != v_end; ++v_it)
00131     {
00132       MeshT::Point p = mesh.point(v_it.handle());
00133       Vec4 v = proj * Vec4( p.x, p.y, p.z, 1.0);
00134       transformed[v_it.handle()] = (v/v(3)).head<3>();
00135     }
00136 
00137     typename MeshT::FaceIter f_it = mesh.faces_begin();
00138     typename MeshT::FaceIter f_end = mesh.faces_end();
00139     typename MeshT::FaceVertexIter fv_it;
00140     Vec3 p1, p2, p3, a, b, normal;
00141     for(; f_it != f_end; ++f_it)
00142     {
00143       fv_it = mesh.fv_iter(f_it.handle());
00144       p1 = transformed[fv_it.handle()]; ++fv_it;
00145       p2 = transformed[fv_it.handle()]; ++fv_it;
00146       p3 = transformed[fv_it.handle()];
00147 
00148       a = p2 - p1;
00149       b = p3 - p1;
00150       normal = (a.cross(b)).normalized();
00151       float d = p1.dot(normal);
00152       spf.addPolygon(f_it.handle(), normal, d);
00153       spf.addEdge(f_it.handle(), p2(0), p2(1), p1(0), p1(1));
00154       spf.addEdge(f_it.handle(), p3(0), p3(1), p2(0), p2(1));
00155       spf.addEdge(f_it.handle(), p1(0), p1(1), p3(0), p3(1));
00156     }
00157     spf.fill(projection);
00158   }
00159 };
00160 
00161 int main(int argc, char** argv)
00162 {
00163   typedef cob_3d_meshing::Mesh<> MeshT;
00164   typedef pcl::PointXYZRGB PointT;
00165 
00166   if(readOptions(argc, argv)<0) return 0;
00167 
00168   pcl::PointCloud<PointT>::Ptr input(new pcl::PointCloud<PointT>);
00169 
00170   pcl::PCDReader r;
00171   r.read(file_in_, *input);
00172 
00173   PrecisionStopWatch t;
00174 
00175   t.precisionStart();
00176   MeshT mesh;
00177   cob_3d_meshing::MeshConversion<>::fromPointCloud<PointT,MeshT>(input, mesh);
00178   cob_3d_meshing::MeshDecimation<MeshT>::quadratic(&mesh, 10000);
00179   std::cout << "MeshSimplification took "
00180             << t.precisionStop() << "s." << std::endl;
00181 
00182   
00183 
00184   return 0;
00185 }
00186 


cob_3d_meshing
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:04:03