This octree keeps a vector of point indices within its leaf nodes.
The resolution parameter describes the length of the smallest voxels at lowest octree level.
float resolution = 128.0f;
pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree (resolution);
octree.setInputCloud (cloud);
octree.addPointsFromInputCloud ();
// K nearest neighbor search
int K = 10;
std::vector<int> pointIdxNKNSearch;
std::vector<float> pointNKNSquaredDistance;
std::cout << "K nearest neighbor search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with K=" << K << std::endl;
if (octree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
{
for (std::size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
std::cout << " " << (*cloud)[ pointIdxNKNSearch[i] ].x
<< " " << (*cloud)[ pointIdxNKNSearch[i] ].y
<< " " << (*cloud)[ pointIdxNKNSearch[i] ].z
<< " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
}
Its search results are written to two separate vectors describing point indices and squares search point distances.
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
float radius = 256.0f * rand () / (RAND_MAX + 1.0f);
std::cout << "Neighbors within radius search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with radius=" << radius << std::endl;
if (octree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
{
for (std::size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
std::cout << " " << (*cloud)[ pointIdxRadiusSearch[i] ].x
<< " " << (*cloud)[ pointIdxRadiusSearch[i] ].y
<< " " << (*cloud)[ pointIdxRadiusSearch[i] ].z
<< " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
}
Neighbors within voxel search at (974.82 188.793 138.779)
903.656 82.8158 162.392
1007.34 191.035 61.7727
896.88 155.711 58.1942
K nearest neighbor search at (974.82 188.793 138.779)with K=10
903.656 82.8158 162.392 (squared distance: 16853.1)
903.18 247.058 54.3528 (squared distance: 15655)
861.595 149.96 135.199 (squared distance: 14340.7)
896.88 155.711 58.1942 (squared distance: 13663)
995.889 116.224 219.077 (squared distance: 12157.9)
885.852 238.41 160.966 (squared distance: 10869.5)
900.807 220.317 77.1432 (squared distance: 10270.7)
1002.46 117.236 184.594 (squared distance: 7983.59)
1007.34 191.035 61.7727 (squared distance: 6992.54)
930.13 223.335 174.763 (squared distance: 4485.15)
Neighbors within radius search at (974.82 188.793 138.779)with radius=109.783
1007.34 191.035 61.7727 (squared distance: 6992.54)
900.807 220.317 77.1432 (squared distance: 10270.7)
885.852 238.41 160.966 (squared distance: 10869.5)
1002.46 117.236 184.594 (squared distance: 7983.59)
930.13 223.335 174.763 (squared distance: 4485.15)