如何对点云数据进行排序?

时间:2020-07-12 10:01:21

标签: c++ point-cloud-library

我想从点云数据中尝试优先级队列。我认为需要插入指向数组的指针。但是似乎没有办法。如何解决这个问题?谢谢~~

typedef struct tagNode 
{ 
    double point_X; 
    double point_Y;
    double point_Z;
} Node; 
struct classcomp 
{ 
   bool operator() (const Node& a, const Node& b) const 
   { 
       return a.point_X < b.point_X;            
   } 
}; 
int main(int argc, char** argv) 
{ 
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PCDReader reader;          
    reader.read<pcl::PointXYZ>("boudary.pcd", *cloud);      
    
    //Node arr[] = *cloud;
    //Node a[] = { {1.01, 55.456, 10.136}, {3.416, 33.231 , 15.121}, {2.564, 44.12491, 96.123}, {5.123, 11.123, 62.176}, {4.135, 22.456, 56.141} };
    priority_queue<Node, vector<Node>, classcomp> priQue; 
    for(unsigned int i = 0; i < sizeof(a)/sizeof(a[0]); ++i) 
    { 
        priQue.push(a[i]); 
    } 
    while(!priQue.empty()) 
    { 
        const Node& topNode = priQue.top(); 
        cout << "x:" << topNode.point_X << ", " << "y:" << topNode.point_Y << ", z:" << topNode.point_Z << endl;
        priQue.pop();       
    } 
    return 0; 
}

1 个答案:

答案 0 :(得分:0)

为什么需要定义自己的点类型? PCL已经有pcl::PointXYZcloud->points实际上是std::vector中的pcl::PointXYZ

所以您要寻找的是:

struct classcomp
{
    bool operator() (const pcl::PointXYZ& a, const pcl::PointXYZ& b) const
    {
        return a.x < b.x;
    }
};

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->points = { {1.01, 55.456, 10.136}, {3.416, 33.231 , 15.121}, {2.564, 44.12491, 96.123}, {5.123, 11.123, 62.176}, {4.135, 22.456, 56.141} };  

std::priority_queue<pcl::PointXYZ, vector<pcl::PointXYZ>, classcomp> priQue;            
for (unsigned int i = 0; i < cloud->size(); ++i)
    priQue.push(cloud->points[i]);
// Or:
// for (const auto& p: cloud->points)
//  priQue.push(p);

while (!priQue.empty())
{
    const pcl::PointXYZ& point = priQue.top();
    cout << "x:" << point.x << ", " << "y:" << point.y << ", z:" << point.z << endl;
    priQue.pop();
}

哪个提供给我们以下输出:

x:5.123, y:11.123, z:62.176
x:4.135, y:22.456, z:56.141
x:3.416, y:33.231, z:15.121
x:2.564, y:44.1249, z:96.123
x:1.01, y:55.456, z:10.136