How to iterate over all leaf nodes of a specific parent node?

Skip to first unread message

Sebastian Eck

Jan 21, 2019, 4:10:52 PM1/21/19
to OctoMap developers and users discussion
Hello everyone,

I would like to multithread my octomap processing by running one thread per depth 2 node (assuming root node is depth 1 and its eight children are depth 2), so each thread could handle one eighth of the map. I know I could simply traverse the tree with nodeHasChildren and getNodeChild, but by design a node does not store any information about it's position. I need this information because I have to address adjacent nodes, thus I need some sort of iterator with an OcTreeKey to navigate to adjacent nodes. However I could not find a trivial way to achieve this. Does anybody know how to properly iterate over all descendants of a specific node so that I have an OcTreeKey for each traversed node? For clarification please see my code below:

octomap::OcTree* mapPtr;

void travTreeRec(octomap::OcTreeNode* nodePtr) {
if (mapPtr->nodeHasChildren(nodePtr)) {
    for (int i = 0; i < 8; ++i) {
if (mapPtr->nodeChildExists(nodePtr, i))    travTreeRec(mapPtr->getNodeChild(nodePtr, i));
} else if (mapPtr->isNodeOccupied(nodePtr)) {
    // node is an occupied leaf node, now find adjacent nodes

void* travTree(void* voidArgsPtr) {
((octomap::OcTreeNode*) voidArgsPtr);

int main(int argc, char **argv) {
= new octomap::OcTree("/home/fapsros/Downloads/");
::OcTreeNode* rootNodePtr = mapPtr->getRoot();
    pthread_t threads
for (int i = 0; i < 8; ++i) {
if (mapPtr->nodeChildExists(rootNodePtr, i)) {
        octomap::OcTreeNode* childNodePtr = mapPtr->getNodeChild(rootNodePtr, i);
(&threads[i], nullptr, travTree, (void*) childNodePtr);
(threads[i], nullptr);
return 0;


Thank you in advance
Reply all
Reply to author
0 new messages