GSoC2011SfM
0.1
Google Summer of Code 2011: Structure from motion
|
00001 00002 #include <pcl/io/vtk_io.h> 00003 #include <iostream> 00004 00005 #include "Visualizer.h" 00006 #include "PCL_mapping.h" 00007 #include "PointOfView.h" 00008 #include "Camera.h" 00009 00010 using cv::Vec3d; 00011 using cv::Mat; 00012 00013 namespace OpencvSfM{ 00014 Visualizer::Visualizer( std::string name ) 00015 :viewer( new pcl::visualization::PCLVisualizer ( name ) ) 00016 { 00017 viewer->setBackgroundColor ( 0, 0, 0 ); 00018 viewer->addCoordinateSystem ( 0.1 ); 00019 viewer->initCameraParameters ( ); 00020 } 00021 00022 void Visualizer::addCamera( const PointOfView& camera, 00023 std::string name, int viewport ) 00024 { 00025 //First get the focal of camera: 00026 const cv::Ptr<Camera> cam = camera.getIntraParameters( ); 00027 double focal = cam->getFocal( ); 00028 //Now get the corners in camera's coordinates: 00029 std::vector<cv::Vec2d> corners, real_coord; 00030 corners.push_back( cv::Vec2d( 0, 0 ) ); 00031 corners.push_back( cv::Vec2d( cam->getImgWidth(), 00032 cam->getImgHeight()) ); 00033 00034 real_coord = cam->pixelToNormImageCoordinates( corners ); 00035 00036 //create the mesh: 00037 std::vector<Vec3d> camera_mesh; 00038 camera_mesh.push_back( Vec3d( 00039 real_coord[ 0 ][ 0 ], real_coord[ 0 ][ 1 ], 0 ) );//bottom left corner 00040 camera_mesh.push_back( Vec3d( 00041 real_coord[ 1 ][ 0 ], real_coord[ 0 ][ 1 ], 0 ) );//bottom right corner 00042 camera_mesh.push_back( Vec3d( 00043 real_coord[ 1 ][ 0 ], real_coord[ 1 ][ 1 ], 0 ) );//top right corner 00044 camera_mesh.push_back( Vec3d( 00045 real_coord[ 0 ][ 0 ], real_coord[ 1 ][ 1 ], 0 ) );//top left corner 00046 camera_mesh.push_back( Vec3d( 00047 0, 0, focal ) );//camera's origin 00048 00049 //move theses points to the world coordinates: 00050 for( unsigned int i = 0; i < camera_mesh.size( ); ++i ) 00051 { 00052 //translate to the correct location: 00053 camera_mesh[ i ] = ( Vec3d ) ( Mat ) ( ( Mat )( camera_mesh[ i ] ) - 00054 camera.getTranslationVector( ) ); 00055 //rotate the ith point: 00056 camera_mesh[ i ] = ( Vec3d ) ( Mat ) ( ( Mat )( camera_mesh[ i ] ).t( ) * 00057 camera.getRotationMatrix( ) ); 00058 } 00059 pcl::PointCloud<pcl::PointXYZ>::Ptr camera_cloud ( 00060 new pcl::PointCloud<pcl::PointXYZ> ); 00061 mapping::convert_OpenCV_vector( camera_mesh, *camera_cloud ); 00062 00063 //now create the mesh himself: 00064 std::vector<pcl::Vertices> vertices; 00065 pcl::Vertices vertice; 00066 vertice.vertices.push_back( 0 ); 00067 vertice.vertices.push_back( 1 ); 00068 vertice.vertices.push_back( 2 ); 00069 vertice.vertices.push_back( 3 ); 00070 vertices.push_back( vertice ); vertice.vertices.clear( ); 00071 vertice.vertices.push_back( 0 ); 00072 vertice.vertices.push_back( 1 ); 00073 vertice.vertices.push_back( 4 ); 00074 vertices.push_back( vertice ); vertice.vertices.clear( ); 00075 vertice.vertices.push_back( 1 ); 00076 vertice.vertices.push_back( 2 ); 00077 vertice.vertices.push_back( 4 ); 00078 vertices.push_back( vertice ); vertice.vertices.clear( ); 00079 vertice.vertices.push_back( 2 ); 00080 vertice.vertices.push_back( 3 ); 00081 vertice.vertices.push_back( 4 ); 00082 vertices.push_back( vertice ); vertice.vertices.clear( ); 00083 vertice.vertices.push_back( 3 ); 00084 vertice.vertices.push_back( 0 ); 00085 vertice.vertices.push_back( 4 ); 00086 vertices.push_back( vertice ); vertice.vertices.clear( ); 00087 00088 pcl::PolygonMesh triangles; 00089 sensor_msgs::PointCloud2 msg; 00090 pcl::toROSMsg( *camera_cloud, msg ); 00091 triangles.cloud = msg; 00092 triangles.polygons = vertices; 00093 00094 pcl::io::saveVTKFile ( "meshCam.vtk", triangles ); 00095 //now add mesh to the viewer: 00096 viewer->addPolygonMesh( triangles, name, viewport ); 00097 viewer->setShapeRenderingProperties( 00098 pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 1.5, name ); 00099 00100 //viewer->addPointCloud<pcl::PointXYZ>( camera_cloud, name ); 00101 //viewer->setPointCloudRenderingProperties ( 00102 // pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, name ); 00103 } 00104 void Visualizer::add3DPoints( const std::vector<cv::Vec3d>& points, 00105 std::string name, int viewport ) 00106 { 00107 if(points.size() == 0) 00108 return; 00109 pcl::PointCloud< pcl::PointXYZ >::Ptr my_cloud ( 00110 new pcl::PointCloud< pcl::PointXYZ > ); 00111 mapping::convert_OpenCV_vector( points, *my_cloud ); 00112 00113 pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> 00114 single_color( my_cloud, 0, 255, 0 ); 00115 viewer->addPointCloud<pcl::PointXYZ> ( my_cloud, single_color, 00116 name, viewport ); 00117 viewer->setPointCloudRenderingProperties ( 00118 pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, name, viewport ); 00119 00120 pcl::PolygonMesh triangles; 00121 sensor_msgs::PointCloud2 msg; 00122 pcl::toROSMsg( *my_cloud, msg ); 00123 triangles.cloud = msg; 00124 pcl::io::saveVTKFile ( ((std::string)"test_")+name+((std::string)".vtk"), triangles ); 00125 00126 } 00127 void Visualizer::add3DPointsColored( const std::vector<cv::Vec3d>& points, 00128 const std::vector<unsigned int>& colors, std::string name, int viewport ) 00129 { 00130 if(points.size() == 0) 00131 return; 00132 00133 // -------------------------------------------- 00134 // -----Open 3D viewer and add point cloud----- 00135 // -------------------------------------------- 00136 00137 00138 pcl::PointCloud< pcl::PointXYZRGB >::Ptr my_cloud ( 00139 new pcl::PointCloud< pcl::PointXYZRGB > ); 00140 //process point per point (can't convert directly...) 00141 unsigned int max_size = points.size(); 00142 for(unsigned int i = 0; i<max_size; i++) 00143 { 00144 pcl::PointXYZRGB p; 00145 p.data[0] = points[i].val[0]; 00146 p.data[1] = points[i].val[1]; 00147 p.data[2] = points[i].val[2]; 00148 p.rgb = *((float*)&(colors[i])); 00149 my_cloud->push_back(p); 00150 } 00151 pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(my_cloud); 00152 viewer->addPointCloud<pcl::PointXYZRGB> (my_cloud, rgb, name, viewport ); 00153 viewer->setPointCloudRenderingProperties ( 00154 pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, name, viewport ); 00155 00156 pcl::PolygonMesh triangles; 00157 sensor_msgs::PointCloud2 msg; 00158 pcl::toROSMsg( *my_cloud, msg ); 00159 triangles.cloud = msg; 00160 00161 pcl::io::saveVTKFile ( ((std::string)"test_")+name+((std::string)".vtk"), triangles ); 00162 00163 } 00164 00165 void Visualizer::runInteract( ) 00166 { 00167 viewer->spin( ); 00168 } 00169 }