Search code examples
c++functioninheritancepcapoint-cloud-library

C++ inheritance of private functions while using PCL


I've been trying to use the PCA module from PCL in C++, but it's been a pain. At one point I want to switch the current indices of points that need to be operated on using the setIndices() function, but to actually make the update there is a private inherited function that one HAS to use called initCompute() or else it doesn't change them (or at least that is how I understood it). Never the less, the code as is, doesn't update the indices for some reason. This is the documentation for the class and everything works, but I have no idea how to make a workaround for this function which they intended to be used for these purposes: http://docs.pointclouds.org/trunk/classpcl_1_1_p_c_a.html

How to deal with this? This is the error while compiling.

In function ‘void clustering(pcl::PointCloud<pcl::PointXYZ>::ConstPtr, pcl::PointCloud<pcl::PointXYZL>::Ptr, pcl::PointCloud<pcl::PointXYZRGB>::Ptr, pcl::PointCloud<pcl::PointXYZ>::Ptr, float)’:
/usr/include/pcl-1.7/pcl/common/impl/pca.hpp:64:1: error: ‘bool pcl::PCA<PointT>::initCompute() [with PointT = pcl::PointXYZ]’ is private
 pcl::PCA<PointT>::initCompute () 

This is the code:

pcl::PCA<pcl::PointXYZ>  cpca = new pcl::PCA<pcl::PointXYZ>;
cpca.setInputCloud(input);
std::cout << "We're now performing the cluster elimination!" << endl;
Eigen::Matrix3f pca_matrix; //serves to hold the eigenvectors, and never got updated...hence the couts for checking.

for (int i = 0; i < nclusters; ++i, n++)
{
    // the next two lines had to be done so, I found that in a forum, the library just behaves a bit strange.
    pcl::PointIndices::Ptr pi_ptr(new pcl::PointIndices);
    pi_ptr->indices = cluster_indices[i].indices;
    cout << "Current size is: " << pi_ptr->indices.size() << endl;//this shows different sizes on every turn
    //now can use pi_ptr
    cpca.setIndices(pi_ptr);
    pca_matrix = cpca.getEigenVectors();
    // but here I get the same vectors every time
    std::cout << "vector " << n << " " << pca_matrix(0,0) << " " << pca_matrix(0,1) << " " << pca_matrix(0,2) << endl;
    std::cout << "vector " << n << " " << pca_matrix(1,0) << " " << pca_matrix(1,1) << " " << pca_matrix(1,2) << endl;
    std::cout << "vector " << n << " " << pca_matrix(2,0) << " " << pca_matrix(2,1) << " " << pca_matrix(2,2) << endl;

Solution

  • Anyway, I got annoyed after a while and did the following. I created a pca object at the beginning of a for loop using a pointer, and then deleted it at the end of the loop with delete. It is some allocing and deallocing going on there which is most likely not optimal, but it did the trick. The PCA object itself was only 144 bytes large, cause it mostly uses pointers to address necessary elements.