7

I have a very simple question:

I have an organized point cloud stored in a pcl::PointCloud<pcl::PointXYZ> data structure.

If I didn't get it wrong, organized point clouds should be stored in a matrix-like structure.

So, my question is: is there a way to access this structure with row and column index? Instead of accessing it in the usual way, i.e., as a linear array.

To make an example:

//data structure
pcl::PointCloud<pcl::PointXYZ> cloud;

//linearized access
cloud[j + cols*i] = ....

//matrix-like access
cloud.at(i,j) = ...

Thanks.

Federico Nardi
  • 510
  • 7
  • 19
  • 3
    `cloud.at(column, row)` should have worked for you. Did you encounter any issues with it? Are you sure that your cloud is organized (`cloud.isOrganized()`)? – Mark Loyman Jun 19 '18 at 04:51

2 Answers2

6

You can accces the points with () operator

    //creating the cloud
    PointCloud<PointXYZ> organizedCloud;
    organizedCloud.width = 2;
    organizedCloud.height = 3;
    organizedCloud.is_dense = false;
    organizedCloud.points.resize(organizedCloud.height*organizedCloud.width);

    //setting random values
for(std::size_t i=0; i<organizedCloud.height; i++){
        for(std::size_t j=0; j<organizedCloud.width; j++){
            organizedCloud.at(i,j).x = 1024*rand() / (RAND_MAX + 1.0f);
            organizedCloud.at(i,j).y = 1024*rand() / (RAND_MAX + 1.0f);
            organizedCloud.at(i,j).z = 1024*rand() / (RAND_MAX + 1.0f);
        }
    }

    //display
    std::cout << "Organized Cloud" <<std:: endl; 

    for(std::size_t i=0; i<organizedCloud.height; i++){
        for(std::size_t j=0; j<organizedCloud.width; j++){
           std::cout << organizedCloud.at(i,j).x << organizedCloud.at(i,j).y  <<  organizedCloud.at(i,j).z << " - "; }
          std::cout << std::endl;
      }
badcode
  • 581
  • 1
  • 9
  • 28
  • 1
    Thanks for the reply, just what I was looking for. However the function signature is `const PointT & at (int column, int row) const`, so I think i and j should be switched, i.e. it should be `organizedCloud.at( j, i )` in your example. At least that's how it works in my test... – Germanunkol Apr 27 '21 at 07:55
-2

To access the points do the following:

// create the cloud as a pointer
pcl::PointCloud<pcl::PointXYZ> cloud(new pcl::PointCloud<pcl::PointXYZ>);

Let i be the element number which you want to access

cloud->points[i].x will give you the x-coordinate.

Similarly, cloud->points[i].y and cloud->points[i].z will give you the y and z coordinates.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 1
    That is not specific to organized pointclouds. The question was about organized clouds and their matrix like structure. – metsburg Dec 17 '18 at 14:33