PCL Octree Cheat Sheet

The point cloud library (PCL) is a fantastic resource for working with point clouds, however it is a large library and it can take a while to effectively find your way around it. The octree construct is very useful for working with point clouds, but again it can take a while to learn how to interact with octrees. To help me to remember how to interact with them I will include some examples here:

To iterate across the nodes of an octree (depth first):

//
for (auto it = octree.begin(); it != octree.end(); ++it) {
	if (it.isBranchNode()) {
		cout << "branch" << std::endl;
	}

	if (it.isLeafNode()) {
		cout << "leaf" << std::endl;
	}
}
//

To iterate across the leaf nodes:

//
for (auto it = octree.leaf_begin(); it != octree.leaf_end(); ++it) {

}
//


Get a Voxel's Bounding Region given node iterator:

//
// Get iterator to first leaf
auto it = octree.leaf_begin();

Eigen::Vector3f voxel_min, voxel_max;
octree.getVoxelBounds(it, voxel_min, voxel_max);
//

Get point indices from leaf node iterator

//
auto it = octree.leaf_begin(); 

std::vector indices;
it.getLeafContainer().getPointIndices(indices);
//

Get point indices from leaf node pointer

//
std::vector indices;

if (node->getNodeType() == LEAF_NODE)
{
	// Nasty down-cast?
	auto* l = dynamic_cast ::LeafNode*>(node);
	(*l)->getPointIndices(indices);
}
//