• Visualization


    CloudViewer

    The CloudViewer is a straight forward,simple piont cloud visualization,meant to get you up and viewing clouds in as little code as possible.

     1 #include <pcl/visualization/cloud_viewer.h>
     2 #include <iostream>
     3 #include <pcl/io/io.h>
     4 #include <pcl/io/pcd_io.h>
     5 
     6 int
     7 main()
     8 {
     9     pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    10     pcl::io::loadPCDFile("D:\pcd\rabbit.pcd", *cloud);
    11     pcl::visualization::CloudViewer viewer("Cloud-Viewer");
    12     viewer.showCloud(cloud);
    13     while (!viewer.wasStopped(100))
    14     {
    15     }
    16     return 0;
    17 }
    View Code

     PCLVisualizer

    PCLVisualizer is PCL’s full-featured visualisation class. While more complex to use than the CloudViewer, it is also more powerful, offering features such as displaying normals, drawing shapes and multiple viewports.

      1 /* author Geoffrey Biggs */
      2 
      3 #include <iostream>
      4 #include <thread>
      5 
      6 #include <pcl/common/common_headers.h>
      7 #include <pcl/features/normal_3d.h>
      8 #include <pcl/io/pcd_io.h>
      9 #include <pcl/visualization/pcl_visualizer.h>
     10 #include <pcl/console/parse.h>
     11 
     12 using namespace std::chrono_literals;
     13 
     14 // --------------
     15 // -----Help-----
     16 // --------------
     17 void
     18 printUsage(const char* progName)
     19 {
     20     std::cout << "
    
    Usage: " << progName << " [options]
    
    "
     21         << "Options:
    "
     22         << "-------------------------------------------
    "
     23         << "-h           this help
    "
     24         << "-s           Simple visualisation example
    "
     25         << "-r           RGB colour visualisation example
    "
     26         << "-c           Custom colour visualisation example
    "
     27         << "-n           Normals visualisation example
    "
     28         << "-a           Shapes visualisation example
    "
     29         << "-v           Viewports example
    "
     30         << "-i           Interaction Customization example
    "
     31         << "
    
    ";
     32 }
     33 
     34 
     35 pcl::visualization::PCLVisualizer::Ptr simpleVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
     36 {
     37     // --------------------------------------------
     38     // -----Open 3D viewer and add point cloud-----
     39     // --------------------------------------------
     40     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
     41     viewer->setBackgroundColor(0, 0, 0);
     42     viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
     43     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
     44     viewer->addCoordinateSystem(2.0);
     45     viewer->initCameraParameters();
     46     return (viewer);
     47 }
     48 
     49 
     50 pcl::visualization::PCLVisualizer::Ptr rgbVis(pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
     51 {
     52     // --------------------------------------------
     53     // -----Open 3D viewer and add point cloud-----
     54     // --------------------------------------------
     55     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
     56     viewer->setBackgroundColor(0, 0, 0);
     57     pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
     58     viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
     59     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
     60     viewer->addCoordinateSystem(1.0);
     61     viewer->initCameraParameters();
     62     return (viewer);
     63 }
     64 
     65 
     66 pcl::visualization::PCLVisualizer::Ptr customColourVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
     67 {
     68     // --------------------------------------------
     69     // -----Open 3D viewer and add point cloud-----
     70     // --------------------------------------------
     71     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
     72     viewer->setBackgroundColor(0, 0, 0);
     73     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0);
     74     viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "sample cloud");
     75     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
     76     viewer->addCoordinateSystem(1.0);
     77     viewer->initCameraParameters();
     78     return (viewer);
     79 }
     80 
     81 
     82 pcl::visualization::PCLVisualizer::Ptr normalsVis(
     83     pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals)
     84 {
     85     // --------------------------------------------------------
     86     // -----Open 3D viewer and add point cloud and normals-----
     87     // --------------------------------------------------------
     88     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
     89     viewer->setBackgroundColor(0, 0, 0);
     90     pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
     91     viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
     92     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
     93     viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals, 10, 0.05, "normals");
     94     viewer->addCoordinateSystem(1.0);
     95     viewer->initCameraParameters();
     96     return (viewer);
     97 }
     98 
     99 
    100 pcl::visualization::PCLVisualizer::Ptr shapesVis(pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
    101 {
    102     // --------------------------------------------
    103     // -----Open 3D viewer and add point cloud-----
    104     // --------------------------------------------
    105     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    106     viewer->setBackgroundColor(0, 0, 0);
    107     pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
    108     viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
    109     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
    110     viewer->addCoordinateSystem(1.0);
    111     viewer->initCameraParameters();
    112 
    113     //------------------------------------
    114     //-----Add shapes at cloud points-----
    115     //------------------------------------
    116     viewer->addLine<pcl::PointXYZRGB>(cloud->points[0],
    117         cloud->points[cloud->size() - 1], "line");
    118     //viewer->addSphere(cloud->points[0], 0.2, 0.5, 0.5, 0.0, "sphere");
    119 
    120     //---------------------------------------
    121     //-----Add shapes at other locations-----
    122     //---------------------------------------
    123     pcl::ModelCoefficients coeffs;
    124     coeffs.values.push_back(0.0);
    125     coeffs.values.push_back(0.0);
    126     coeffs.values.push_back(1.0);
    127     coeffs.values.push_back(0.0);
    128     viewer->addPlane(coeffs, "plane");
    129     coeffs.values.clear();
    130     coeffs.values.push_back(0.3);
    131     coeffs.values.push_back(0.3);
    132     coeffs.values.push_back(0.0);
    133     coeffs.values.push_back(0.0);
    134     coeffs.values.push_back(1.0);
    135     coeffs.values.push_back(0.0);
    136     coeffs.values.push_back(5.0);
    137     viewer->addCone(coeffs, "cone");
    138 
    139     return (viewer);
    140 }
    141 
    142 
    143 pcl::visualization::PCLVisualizer::Ptr viewportsVis(
    144     pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals1, pcl::PointCloud<pcl::Normal>::ConstPtr normals2)
    145 {
    146     // --------------------------------------------------------
    147     // -----Open 3D viewer and add point cloud and normals-----
    148     // --------------------------------------------------------
    149     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    150     viewer->initCameraParameters();
    151 
    152     int v1(0);
    153     viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1);
    154     viewer->setBackgroundColor(0, 0, 0, v1);
    155     viewer->addText("Radius: 0.01", 10, 10, "v1 text", v1);
    156     pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
    157     viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud1", v1);
    158 
    159     int v2(0);
    160     viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
    161     viewer->setBackgroundColor(0.3, 0.3, 0.3, v2);
    162     viewer->addText("Radius: 0.1", 10, 10, "v2 text", v2);
    163     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color(cloud, 0, 255, 0);
    164     viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "sample cloud2", v2);
    165 
    166     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud1");
    167     viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud2");
    168     viewer->addCoordinateSystem(1.0);
    169 
    170     //viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals1, 10, 0.05, "normals1", v1);
    171     //viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal>(cloud, normals2, 10, 0.05, "normals2", v2);
    172 
    173     return (viewer);
    174 }
    175 
    176 
    177 unsigned int text_id = 0;
    178 void keyboardEventOccurred(const pcl::visualization::KeyboardEvent &event,
    179     void* viewer_void)
    180 {
    181     pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (viewer_void);
    182     if (event.getKeySym() == "r" && event.keyDown())
    183     {
    184         std::cout << "r was pressed => removing all text" << std::endl;
    185 
    186         char str[512];
    187         for (unsigned int i = 0; i < text_id; ++i)
    188         {
    189             sprintf(str, "text#%03d", i);
    190             viewer->removeShape(str);
    191         }
    192         text_id = 0;
    193     }
    194 }
    195 
    196 void mouseEventOccurred(const pcl::visualization::MouseEvent &event,
    197     void* viewer_void)
    198 {
    199     pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (viewer_void);
    200     if (event.getButton() == pcl::visualization::MouseEvent::LeftButton &&
    201         event.getType() == pcl::visualization::MouseEvent::MouseButtonRelease)
    202     {
    203         std::cout << "Left mouse button released at position (" << event.getX() << ", " << event.getY() << ")" << std::endl;
    204 
    205         char str[512];
    206         sprintf(str, "text#%03d", text_id++);
    207         viewer->addText("clicked here", event.getX(), event.getY(), str);
    208     }
    209 }
    210 
    211 pcl::visualization::PCLVisualizer::Ptr interactionCustomizationVis()
    212 {
    213     pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    214     viewer->setBackgroundColor(0, 0, 0);
    215     viewer->addCoordinateSystem(1.0);
    216 
    217     viewer->registerKeyboardCallback(keyboardEventOccurred, (void*)viewer.get());
    218     viewer->registerMouseCallback(mouseEventOccurred, (void*)viewer.get());
    219 
    220     return (viewer);
    221 }
    222 
    223 
    224 // --------------
    225 // -----Main-----
    226 // --------------
    227 int
    228 main(int argc, char** argv)
    229 {
    230     bool simple(false), rgb(false), custom_c(false), normals(true),
    231         shapes(false), viewports(false), interaction_customization(false);
    232 
    233     // ------------------------------------
    234     // -----Create example point cloud-----
    235     // ------------------------------------
    236     pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);
    237     pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
    238     std::cout << "Generating example point clouds.
    
    ";
    239     // We're going to make an ellipse extruded along the z-axis. The colour for
    240     // the XYZRGB cloud will gradually go from red to green to blue.
    241     uint8_t r(255), g(15), b(15);
    242     for (float z(-1.0); z <= 1.0; z += 0.05)
    243     {
    244         for (float angle(0.0); angle <= 360.0; angle += 5.0)
    245         {
    246             pcl::PointXYZ basic_point;
    247             basic_point.x = 0.5 * cosf(pcl::deg2rad(angle));
    248             basic_point.y = sinf(pcl::deg2rad(angle));
    249             basic_point.z = z;
    250             basic_cloud_ptr->points.push_back(basic_point);
    251 
    252             pcl::PointXYZRGB point;
    253             point.x = basic_point.x;
    254             point.y = basic_point.y;
    255             point.z = basic_point.z;
    256             uint32_t rgb = (static_cast<uint32_t>(r) << 16 |
    257                 static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
    258             point.rgb = *reinterpret_cast<float*>(&rgb);
    259             point_cloud_ptr->points.push_back(point);
    260         }
    261         if (z < 0.0)
    262         {
    263             r -= 12;
    264             g += 12;
    265         }
    266         else
    267         {
    268             g -= 12;
    269             b += 12;
    270         }
    271     }
    272     basic_cloud_ptr->width = (int)basic_cloud_ptr->points.size();
    273     basic_cloud_ptr->height = 1;
    274     point_cloud_ptr->width = (int)point_cloud_ptr->points.size();
    275     point_cloud_ptr->height = 1;
    276 
    277     // ----------------------------------------------------------------
    278     // -----Calculate surface normals with a search radius of 0.05-----
    279     // ----------------------------------------------------------------
    280     pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> ne;
    281     ne.setInputCloud(point_cloud_ptr);
    282     pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
    283     ne.setSearchMethod(tree);
    284     pcl::PointCloud<pcl::Normal>::Ptr cloud_normals1(new pcl::PointCloud<pcl::Normal>);
    285     ne.setRadiusSearch(0.05);
    286     ne.compute(*cloud_normals1);
    287 
    288     // ---------------------------------------------------------------
    289     // -----Calculate surface normals with a search radius of 0.1-----
    290     // ---------------------------------------------------------------
    291     pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2(new pcl::PointCloud<pcl::Normal>);
    292     ne.setRadiusSearch(0.1);
    293     ne.compute(*cloud_normals2);
    294     // ---------------------------------------------------------------
    295 
    296     pcl::visualization::PCLVisualizer::Ptr viewer;
    297     if (simple)
    298     {
    299         viewer = simpleVis(basic_cloud_ptr);
    300     }
    301     else if (rgb)
    302     {
    303         viewer = rgbVis(point_cloud_ptr);
    304     }
    305     else if (custom_c)
    306     {
    307         viewer = customColourVis(basic_cloud_ptr);
    308     }
    309     else if (normals)
    310     {
    311         viewer = normalsVis(point_cloud_ptr, cloud_normals2);
    312     }
    313     else if (shapes)
    314     {
    315         viewer = shapesVis(point_cloud_ptr);
    316     }
    317     else if (viewports)
    318     {
    319         viewer = viewportsVis(point_cloud_ptr, cloud_normals1, cloud_normals2);
    320     }
    321     else if (interaction_customization)
    322     {
    323         viewer = interactionCustomizationVis();
    324     }
    325 
    326     //--------------------
    327     // -----Main loop-----
    328     //--------------------
    329     while (!viewer->wasStopped())
    330     {
    331         viewer->spinOnce(100);
    332         std::this_thread::sleep_for(100ms);
    333     }
    334 }
    View Code

     PCLPlotter

    PCLPlotter provides a very straightforward and easy interface for plotting graphs. One can visualize all sort of important plots - from polynomial functions to histograms - inside the library without going to any other software (like MATLAB).

      1 /* author Kripasindhu Sarkar */
      2 
      3 #include<pcl/visualization/pcl_plotter.h>
      4 
      5 #include<iostream>
      6 #include<vector>
      7 #include<utility>
      8 #include<math.h>  //for abs()
      9 
     10 using namespace std;
     11 using namespace pcl::visualization;
     12 
     13 void
     14 generateData(double *ax, double *acos, double *asin, int numPoints)
     15 {
     16     double inc = 7.5 / (numPoints - 1);
     17     for (int i = 0; i < numPoints; ++i)
     18     {
     19         ax[i] = i*inc;
     20         acos[i] = cos(i * inc);
     21         asin[i] = sin(i * inc);
     22     }
     23 }
     24 
     25 //.....................callback functions defining Y= f(X)....................
     26 double
     27 step(double val)
     28 {
     29     if (val > 0)
     30         return (double)(int)val;
     31     else
     32         return (double)((int)val - 1);
     33 }
     34 
     35 double
     36 identity1(double val)
     37 {
     38     return val;
     39 }
     40 //............................................................................
     41 
     42 int
     43 main(int argc, char * argv[])
     44 {
     45     //defining a plotter
     46     PCLPlotter *plotter = new PCLPlotter("My Plotter");
     47 
     48     //setting some properties
     49     plotter->setShowLegend(true);
     50 
     51     //generating point correspondances
     52     int numPoints = 69;
     53     double ax[100], acos[100], asin[100];
     54     generateData(ax, acos, asin, numPoints);
     55 
     56     //adding plot data
     57     plotter->addPlotData(ax, acos, numPoints, "cos");
     58     plotter->addPlotData(ax, asin, numPoints, "sin");
     59 
     60     //display for 2 seconds
     61     plotter->spinOnce(3000);
     62     plotter->clearPlots();
     63 
     64 
     65     //...................plotting implicit functions and custom callbacks....................
     66 
     67     //make a fixed axis
     68     plotter->setYRange(-10, 10);
     69 
     70     //defining polynomials
     71     vector<double> func1(1, 0);
     72     func1[0] = 1; //y = 1
     73     vector<double> func2(3, 0);
     74     func2[2] = 1; //y = x^2
     75 
     76     plotter->addPlotData(std::make_pair(func1, func2), -10, 10, "y = 1/x^2", 100, vtkChart::POINTS);
     77     plotter->spinOnce(2000);
     78 
     79     plotter->addPlotData(func2, -10, 10, "y = x^2");
     80     plotter->spinOnce(2000);
     81 
     82     //callbacks
     83     plotter->addPlotData(identity1, -10, 10, "identity");
     84     plotter->spinOnce(2000);
     85 
     86     plotter->addPlotData(abs, -10, 10, "abs");
     87     plotter->spinOnce(2000);
     88 
     89     plotter->addPlotData(step, -10, 10, "step", 100, vtkChart::POINTS);
     90     plotter->spinOnce(2000);
     91 
     92     plotter->clearPlots();
     93 
     94     //........................A simple animation..............................
     95     vector<double> fsq(3, 0);
     96     fsq[2] = -100; //y = x^2
     97     while (!plotter->wasStopped())
     98     {
     99         if (fsq[2] == 100) fsq[2] = -100;
    100         fsq[2]++;
    101         char str[50];
    102         sprintf(str, "y = %dx^2", (int)fsq[2]);
    103         plotter->addPlotData(fsq, -10, 10, str);
    104 
    105         plotter->spinOnce(100);
    106         plotter->clearPlots();
    107     }
    108 
    109     return 1;
    110 }
    View Code
  • 相关阅读:
    https证书设置以及设置301跳转
    SVN服务器从windows迁移至Linux
    禅道从windows迁移到linux
    gitlab安装和迁移
    java 枚举类型enum 的使用
    java中的日期注意事项
    C# using 三种使用方式 zhuan
    设计路径测试覆盖率与代码测试覆盖率
    Selenium API (C#) 转
    Selenium 2.0 WebDriver with Visual Studio, C#, & IE
  • 原文地址:https://www.cnblogs.com/larry-xia/p/11049288.html
Copyright © 2020-2023  润新知