pcl getting started note 1: pcl installation

preface

Recently entered the pit pcl,I'm going to record my learning process.

Preparation before installing pcl

This tutorial uses the precompiled package installation under windows. To compile the program smoothly, you need to install Microsoft's Visual Studio IDE and cmake. Both installation processes are very simple, and readers can baidu by themselves. cmae can also install the gui version.
pcl precompiled package link: https://pan.baidu.com/s/1hWi3_hELh1MVZihHV23juQ
Extraction code: ouzu
The author provides versions 1.8 and 1.12, which readers can choose to install. Each version of the folder has two files.

Installation steps

Double click the downloaded exe file as follows:

Click next

Click accept

As shown in the figure above, you need to add environment variables

Then select the installation location and wait for the installation to complete.

Check environment variables

You need to check after installation. The installed environment is as follows

Open the environment variables page to view. First, there will be a pcl in the system variable_ Root variable. The path of this variable is the pcl installation path.

The figure above shows the path to be added under the user variable.

test

Create a cpp file with the following code:

#include<iostream>
using namespace std;
#include<pcl/point_cloud.h>
#include<pcl/kdtree/kdtree_flann.h>
#include<vector>
#include<ctime>

int main(int argc,char** arv)
{
    //Random seed, the value in srand is the same every time, then the generated random number remains unchanged, otherwise it will change
    srand(time(NULL));
    //About initialization of point cloud data
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    cloud->width=1000;
    cloud->height=1;
    cloud->points.resize(cloud->width*cloud->height);
    for(size_t i=0;i<cloud->points.size();++i)
    {
        cloud->points[i].x=1024.0f*rand()/(RAND_MAX+1.0f);
        cloud->points[i].y=1024.0f*rand()/(RAND_MAX+1.0f);
        cloud->points[i].z=1024.0f*rand()/(RAND_MAX+1.0f);
    }
    //Create KD tree object
    pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
    //Set the search space of cloud
    kdtree.setInputCloud(cloud);
    //Define a query node and use it to query
    pcl::PointXYZ searchPoint;
    searchPoint.x=1024.0f*rand()/(RAND_MAX+1.0f);
    searchPoint.y=1024.0f*rand()/(RAND_MAX+1.0f);
    searchPoint.z=1024.0f*rand()/(RAND_MAX+1.0f);
    //k-nearest neighbor search
    int k=10;
    //Save the k-nearest neighbor index of query points
    vector<int> pointIdxNKNSearch(k);
    //Save the Euclidean distance corresponding to the k nearest neighbor
    vector<float> pointNKSquaredDistance(k);
    cout<<"k nearest neighbor search at ("<<searchPoint.x
    <<"  "<<searchPoint.y<<"  "<<searchPoint.z<<") with k="<<k<<endl;
    //Perform k-nearest neighbor search and print the corresponding information
    if(kdtree.nearestKSearch(searchPoint,k,pointIdxNKNSearch,pointNKSquaredDistance)>0)
    {
        cout<<"k nearest neighbor:"<<endl;
        for(size_t i=0;i<pointIdxNKNSearch.size();++i)
        {
            cout<<"    "<<cloud->points[pointIdxNKNSearch[i]].x
            <<" "<<cloud->points[pointIdxNKNSearch[i]].y
            <<" "<<cloud->points[pointIdxNKNSearch[i]].z
            <<" (squared distance:"<<pointNKSquaredDistance[i]<<")"<<endl;
        }
        //Nearest neighbor search method within radius r
        vector<int> pointIdxRadiusSearch;
        vector<float> pointRadiusSquaredDistance;
        float radius=256.0f*rand()/(RAND_MAX+1.0f);
        cout<<"Neighbors within radius search at ("<<searchPoint.x
        <<" "<<searchPoint.y
        <<" "<<searchPoint.z
        <<") with radius="<<radius<<endl;
        
        if(kdtree.radiusSearch(searchPoint,radius,pointIdxRadiusSearch,pointRadiusSquaredDistance)>0)
        {
            cout<<"radius search:"<<endl;
            for(size_t i=0;i<pointIdxRadiusSearch.size();++i)
            {
                cout<<"    "<<cloud->points[pointIdxRadiusSearch[i]].x
                <<" "<<cloud->points[pointIdxRadiusSearch[i]].y
                <<" "<<cloud->points[pointIdxRadiusSearch[i]].z
                <<" (squared distance:"<<pointRadiusSquaredDistance[i]<<")"<<endl;
            }
            
        }
        else
        {
            cout<<"no radius search"<<endl;
        }
    }
    return 0;
}

Create a CMakeLists.txt file with the following contents:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(kdtree_search)
find_package(PCL 1.12 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(kdtree_search kdtree_search.cpp)
target_link_libraries(kdtree_search ${PCL_LIBRARIES})

The two files are placed in the same folder, and then a build file is established in the same level directory to store the intermediate files generated by compilation. Then open cmd, cd to the build folder, and enter cmake in cmd.. after entering, find the sln suffix file in the build folder, and double-click vs to open it. In the build, click rebuild solution to start compiling. After compilation, enter the Debug folder to find the EXE executable. Then enter cmd cd into the Debug folder and enter the full name of exe executable file to run the program. The results of running the program are as follows:

At this point, the pcl installation is complete.

Tags: C++ Windows Autonomous vehicles

Posted on Sun, 19 Sep 2021 09:49:51 -0400 by statrat