Search code examples
c++templatespoint-cloud-librarypoint-clouds

Class templating to process pcl::PointCloud objects with different point types


I am trying to use template classes to use my functions regardless of the point type. I read the “Writing a new PCL class” tutorial but I have not got it. I will share the simplest class where I am trying to use this technique. Its only function is to create the KDtree of a pointcloud in the correct point of the execution of a parent tree of processes.

KdtreeBuilder_Process.h

#ifndef KDTREEBUILDER_PROCESS_H 
#define KDTREEBUILDER_PROCESS_H 
#include "ProcessManager/ProcessConcurrent.h" //Parent class 
#include <pcl/kdtree/kdtree_flann.h>

class KdtreeBuilder_Process:public ProcessConcurrent 
{ 
public: 
    KdtreeBuilder_Process(pcl::PointCloud<pcl::PointXYZ>::Ptr inputCloud,pcl::KdTree<pcl::PointXYZ>::Ptr cloudKdtree); 
    virtual void run(); //method that executed when the process starts 
private: 

    pcl::PointCloud<pcl::PointXYZ>::Ptr mInputCloud; 
    pcl::KdTree<pcl::PointXYZ>::Ptr mCloudKdtree; 
}; 

#endif // KDTREEBUILDER_PROCESS_H 

KdtreeBuilder_Process.cpp

#include "KdtreeBuilder_Process.h" 

KdtreeBuilder_Process::KdtreeBuilder_Process(pcl::PointCloud<pcl::PointXYZ>::Ptr inputCloud,pcl::KdTree<pcl::PointXYZ>::Ptr cloudKdtree): 
    mInputCloud(inputCloud),mCloudKdtree(cloudKdtree) 
{ 

} 

void KdtreeBuilder_Process::run(){ 
    mCloudKdtree->setInputCloud(mInputCloud); 
} 

My intention is to be able to use this class with any point type that contains XYZ coordinates

Thanks for your support. BR


Solution

  • I solve the issue. Here is the final solution using only header file:

    KdtreeBuilder_Process.h

    #ifndef KDTREEBUILDER_PROCESS_H
    #define KDTREEBUILDER_PROCESS_H
    #include "ProcessManager/ProcessConcurrent.h"
    #include "PointDefinitions.h"
    #include <pcl/kdtree/kdtree_flann.h>
    #include <QDebug>
    
    template<class PointType>
    class KdtreeBuilder_Process:public ProcessConcurrent
    {
        typedef typename pcl::PointCloud<PointType>::Ptr PointCloudPtr;
        typedef typename pcl::KdTree<PointType>::Ptr KdTreePtr;
    
    public:
    
        KdtreeBuilder_Process(PointCloudPtr inputCloud,KdTreePtr cloudKdtree): mInputCloud(inputCloud), mCloudKdtree(cloudKdtree) { }
            virtual void run(){
                mCloudKdtree->setInputCloud(mInputCloud);
            }
        private:
    
            PointCloudPtr mInputCloud;
            KdTreePtr mCloudKdtree;
    };
    
    #endif // KDTREEBUILDER_PROCESS_H