gpt4 book ai didi

c++ - 使用一些 map 的点云中的有序点

转载 作者:行者123 更新时间:2023-11-28 05:22:24 24 4
gpt4 key购买 nike

我正在寻找容器,以按其 3D 位置对点云中的点进行排序。基本上我需要一张 map 来按其位置(首先是 X,然后是 Y,最后是 Z)来排序和查找点(具有一些属性 ej:RGB)。

关键位置X、Y、Z存储为std::vector<int>我要存储的值是其他 std::vector<int>

我认为执行此操作的自然类型是 std::map

但我不确定如何为该 map 构建 key 。

以及如何获得 iterator按 X,Y,Z 排序。

最佳答案

我认为您应该做的第一件事就是不要将您的积分存储为 std::vector<int>。到具有固定大小存储的;使用 std::vector始终存储在编译时已知的固定数量的值是过大的,并且会引入显着的每点内存开销,而没有任何补偿好处。

鉴于此,我会这样做:

#include <iostream>
#include <map>
#include <vector>

class Point
{
public:
Point() : x(0), y(0), z(0) {/* empty */}
Point(int xa, int ya, int za) : x(xa), y(ya), z(za) {/* empty */}

bool operator < (const Point & rhs) const
{
return (x < rhs.x) ||
((x == rhs.x) && (y < rhs.y)) ||
((x == rhs.x) && (y == rhs.y) && (z < rhs.z));
}

bool operator == (const Point & rhs) const
{
return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
}

int x, y, z;
};


int main(int, char **)
{
std::map<Point, std::vector<int> > some_points;
some_points[Point(1,2,3)] = std::vector<int>();
some_points[Point(3,2,1)] = std::vector<int>();
some_points[Point(1,3,2)] = std::vector<int>();

for (std::map<Point, std::vector<int> >::iterator iter = some_points.begin(); iter != some_points.end(); iter++)
{
const Point & key = iter->first;
const std::vector<int> & val = iter->second;
std::cout << "x=" << key.x << " y=" << key.y << " z=" << key.z << " --> vector with " << val.size() << " elements" << std::endl;
}

return 0;
}

关于c++ - 使用一些 map 的点云中的有序点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com