Skip to content

Commit 4673d2f

Browse files
committed
search: add optional found_depth return by pointer
Add an optional found_depth return by pointer. If the found_depth pointer is set, the depth the search stopped at will be set to what found_depth points to. This way the caller can determine if the search ended at the requested depth if that is important to the caller.
1 parent b46d262 commit 4673d2f

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

octomap/include/octomap/OcTreeBaseImpl.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,26 @@ namespace octomap {
183183
/**
184184
* Search node at specified depth given a 3d point (depth=0: search full tree depth).
185185
* You need to check if the returned node is NULL, since it can be in unknown space.
186+
* If the caller needs to know the found node depth, it can be returned by setting found_depth.
186187
* @return pointer to node if found, NULL otherwise
187188
*/
188-
NODE* search(double x, double y, double z, unsigned int depth = 0) const;
189+
NODE* search(double x, double y, double z, unsigned int depth = 0, unsigned int* found_depth = nullptr) const;
189190

190191
/**
191192
* Search node at specified depth given a 3d point (depth=0: search full tree depth)
192193
* You need to check if the returned node is NULL, since it can be in unknown space.
194+
* If the caller needs to know the found node depth, it can be returned by setting found_depth.
193195
* @return pointer to node if found, NULL otherwise
194196
*/
195-
NODE* search(const point3d& value, unsigned int depth = 0) const;
197+
NODE* search(const point3d& value, unsigned int depth = 0, unsigned int* found_depth = nullptr) const;
196198

197199
/**
198200
* Search a node at specified depth given an addressing key (depth=0: search full tree depth)
199201
* You need to check if the returned node is NULL, since it can be in unknown space.
202+
* If the caller needs to know the found node depth, it can be returned by setting found_depth.
200203
* @return pointer to node if found, NULL otherwise
201204
*/
202-
NODE* search(const OcTreeKey& key, unsigned int depth = 0) const;
205+
NODE* search(const OcTreeKey& key, unsigned int depth = 0, unsigned int* found_depth = nullptr) const;
203206

204207
/**
205208
* Delete a node (if exists) given a 3d point. Will always

octomap/include/octomap/OcTreeBaseImpl.hxx

+15-6
Original file line numberDiff line numberDiff line change
@@ -472,33 +472,33 @@ namespace octomap {
472472
}
473473

474474
template <class NODE,class I>
475-
NODE* OcTreeBaseImpl<NODE,I>::search(const point3d& value, unsigned int depth) const {
475+
NODE* OcTreeBaseImpl<NODE,I>::search(const point3d& value, unsigned int depth, unsigned int* found_depth) const {
476476
OcTreeKey key;
477477
if (!coordToKeyChecked(value, key)){
478478
OCTOMAP_ERROR_STR("Error in search: ["<< value <<"] is out of OcTree bounds!");
479479
return NULL;
480480
}
481481
else {
482-
return this->search(key, depth);
482+
return this->search(key, depth, found_depth);
483483
}
484484

485485
}
486486

487487
template <class NODE,class I>
488-
NODE* OcTreeBaseImpl<NODE,I>::search(double x, double y, double z, unsigned int depth) const {
488+
NODE* OcTreeBaseImpl<NODE,I>::search(double x, double y, double z, unsigned int depth, unsigned int* found_depth) const {
489489
OcTreeKey key;
490490
if (!coordToKeyChecked(x, y, z, key)){
491491
OCTOMAP_ERROR_STR("Error in search: ["<< x <<" "<< y << " " << z << "] is out of OcTree bounds!");
492492
return NULL;
493493
}
494494
else {
495-
return this->search(key, depth);
495+
return this->search(key, depth, found_depth);
496496
}
497497
}
498498

499499

500500
template <class NODE,class I>
501-
NODE* OcTreeBaseImpl<NODE,I>::search (const OcTreeKey& key, unsigned int depth) const {
501+
NODE* OcTreeBaseImpl<NODE,I>::search (const OcTreeKey& key, unsigned int depth, unsigned int* found_depth) const {
502502
assert(depth <= tree_depth);
503503
if (root == NULL)
504504
return NULL;
@@ -517,9 +517,11 @@ namespace octomap {
517517
NODE* curNode (root);
518518

519519
int diff = tree_depth - depth;
520+
unsigned int current_depth = 0;
520521

521522
// follow nodes down to requested level (for diff = 0 it's the last level)
522-
for (int i=(tree_depth-1); i>=diff; --i) {
523+
for (int i=(tree_depth-1); i>=diff; --i, ++current_depth) {
524+
assert(current_depth < depth);
523525
unsigned int pos = computeChildIdx(key_at_depth, i);
524526
if (nodeChildExists(curNode, pos)) {
525527
// cast needed: (nodes need to ensure it's the right pointer)
@@ -528,13 +530,20 @@ namespace octomap {
528530
// we expected a child but did not get it
529531
// is the current node a leaf already?
530532
if (!nodeHasChildren(curNode)) { // TODO similar check to nodeChildExists?
533+
if (found_depth) {
534+
*found_depth = current_depth;
535+
}
531536
return curNode;
532537
} else {
533538
// it is not, search failed
534539
return NULL;
535540
}
536541
}
537542
} // end for
543+
assert(current_depth == depth);
544+
if (found_depth) {
545+
*found_depth = current_depth;
546+
}
538547
return curNode;
539548
}
540549

0 commit comments

Comments
 (0)