octomap 1.9.8
OcTreeBaseImpl.h
Go to the documentation of this file.
1/*
2 * OctoMap - An Efficient Probabilistic 3D Mapping Framework Based on Octrees
3 * https://octomap.github.io/
4 *
5 * Copyright (c) 2009-2013, K.M. Wurm and A. Hornung, University of Freiburg
6 * All rights reserved.
7 * License: New BSD
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of the University of Freiburg nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef OCTOMAP_OCTREE_BASE_IMPL_H
35#define OCTOMAP_OCTREE_BASE_IMPL_H
36
37
38#include <list>
39#include <limits>
40#include <iterator>
41#include <stack>
42#include <bitset>
43
44#include "octomap_types.h"
45#include "OcTreeKey.h"
46#include "ScanGraph.h"
47
48
49namespace octomap {
50
51 // forward declaration for NODE children array
52 class AbstractOcTreeNode;
53
54
74 template <class NODE,class INTERFACE>
75 class OcTreeBaseImpl : public INTERFACE {
76
77 public:
79 typedef NODE NodeType;
80
81 // the actual iterator implementation is included here
82 // as a member from this file
84
86 virtual ~OcTreeBaseImpl();
87
90
91
99
102 bool operator== (const OcTreeBaseImpl<NODE,INTERFACE>& rhs) const;
103
104 std::string getTreeType() const {return "OcTreeBaseImpl";}
105
108 void setResolution(double r);
109 inline double getResolution() const { return resolution; }
110
111 inline unsigned int getTreeDepth () const { return tree_depth; }
112
113 inline double getNodeSize(unsigned depth) const {assert(depth <= tree_depth); return sizeLookupTable[depth];}
114
121 keyrays.clear();
122 }
123
124 // -- Tree structure operations formerly contained in the nodes ---
125
127 NODE* createNodeChild(NODE* node, unsigned int childIdx);
128
130 void deleteNodeChild(NODE* node, unsigned int childIdx);
131
133 NODE* getNodeChild(NODE* node, unsigned int childIdx) const;
134
136 const NODE* getNodeChild(const NODE* node, unsigned int childIdx) const;
137
140 virtual bool isNodeCollapsible(const NODE* node) const;
141
147 bool nodeChildExists(const NODE* node, unsigned int childIdx) const;
148
153 bool nodeHasChildren(const NODE* node) const;
154
163 virtual void expandNode(NODE* node);
164
169 virtual bool pruneNode(NODE* node);
170
171
172 // --------
173
179 inline NODE* getRoot() const { return root; }
180
186 NODE* search(double x, double y, double z, unsigned int depth = 0) const;
187
193 NODE* search(const point3d& value, unsigned int depth = 0) const;
194
200 NODE* search(const OcTreeKey& key, unsigned int depth = 0) const;
201
207 bool deleteNode(double x, double y, double z, unsigned int depth = 0);
208
214 bool deleteNode(const point3d& value, unsigned int depth = 0);
215
221 bool deleteNode(const OcTreeKey& key, unsigned int depth = 0);
222
224 void clear();
225
232 virtual void prune();
233
236 virtual void expand();
237
238 // -- statistics ----------------------
239
241 virtual inline size_t size() const { return tree_size; }
242
244 virtual size_t memoryUsage() const;
245
247 virtual inline size_t memoryUsageNode() const {return sizeof(NODE); };
248
251 unsigned long long memoryFullGrid() const;
252
253 double volume();
254
256 virtual void getMetricSize(double& x, double& y, double& z);
258 virtual void getMetricSize(double& x, double& y, double& z) const;
260 virtual void getMetricMin(double& x, double& y, double& z);
262 void getMetricMin(double& x, double& y, double& z) const;
264 virtual void getMetricMax(double& x, double& y, double& z);
266 void getMetricMax(double& x, double& y, double& z) const;
267
269 size_t calcNumNodes() const;
270
272 size_t getNumLeafNodes() const;
273
274
275 // -- access tree nodes ------------------
276
278 void getUnknownLeafCenters(point3d_list& node_centers, point3d pmin, point3d pmax, unsigned int depth = 0) const;
279
280
281 // -- raytracing -----------------------
282
293 bool computeRayKeys(const point3d& origin, const point3d& end, KeyRay& ray) const;
294
295
307 bool computeRay(const point3d& origin, const point3d& end, std::vector<point3d>& ray);
308
309
310 // file IO
311
318 std::istream& readData(std::istream &s);
319
322 std::ostream& writeData(std::ostream &s) const;
323
325
327 iterator begin(unsigned char maxDepth=0) const {return iterator(this, maxDepth);};
329 const iterator end() const {return leaf_iterator_end;}; // TODO: RVE?
330
332 leaf_iterator begin_leafs(unsigned char maxDepth=0) const {return leaf_iterator(this, maxDepth);};
335
337 leaf_bbx_iterator begin_leafs_bbx(const OcTreeKey& min, const OcTreeKey& max, unsigned char maxDepth=0) const {
338 return leaf_bbx_iterator(this, min, max, maxDepth);
339 }
341 leaf_bbx_iterator begin_leafs_bbx(const point3d& min, const point3d& max, unsigned char maxDepth=0) const {
342 return leaf_bbx_iterator(this, min, max, maxDepth);
343 }
346
348 tree_iterator begin_tree(unsigned char maxDepth=0) const {return tree_iterator(this, maxDepth);}
351
352 //
353 // Key / coordinate conversion functions
354 //
355
357 inline key_type coordToKey(double coordinate) const{
358 return ((int) floor(resolution_factor * coordinate)) + tree_max_val;
359 }
360
362 key_type coordToKey(double coordinate, unsigned depth) const;
363
364
366 inline OcTreeKey coordToKey(const point3d& coord) const{
367 return OcTreeKey(coordToKey(coord(0)), coordToKey(coord(1)), coordToKey(coord(2)));
368 }
369
371 inline OcTreeKey coordToKey(double x, double y, double z) const{
372 return OcTreeKey(coordToKey(x), coordToKey(y), coordToKey(z));
373 }
374
376 inline OcTreeKey coordToKey(const point3d& coord, unsigned depth) const{
377 if (depth == tree_depth)
378 return coordToKey(coord);
379 else
380 return OcTreeKey(coordToKey(coord(0), depth), coordToKey(coord(1), depth), coordToKey(coord(2), depth));
381 }
382
384 inline OcTreeKey coordToKey(double x, double y, double z, unsigned depth) const{
385 if (depth == tree_depth)
386 return coordToKey(x,y,z);
387 else
388 return OcTreeKey(coordToKey(x, depth), coordToKey(y, depth), coordToKey(z, depth));
389 }
390
399 inline OcTreeKey adjustKeyAtDepth(const OcTreeKey& key, unsigned int depth) const{
400 if (depth == tree_depth)
401 return key;
402
403 assert(depth <= tree_depth);
404 return OcTreeKey(adjustKeyAtDepth(key[0], depth), adjustKeyAtDepth(key[1], depth), adjustKeyAtDepth(key[2], depth));
405 }
406
415 key_type adjustKeyAtDepth(key_type key, unsigned int depth) const;
416
424 bool coordToKeyChecked(const point3d& coord, OcTreeKey& key) const;
425
434 bool coordToKeyChecked(const point3d& coord, unsigned depth, OcTreeKey& key) const;
435
445 bool coordToKeyChecked(double x, double y, double z, OcTreeKey& key) const;
446
457 bool coordToKeyChecked(double x, double y, double z, unsigned depth, OcTreeKey& key) const;
458
466 bool coordToKeyChecked(double coordinate, key_type& key) const;
467
476 bool coordToKeyChecked(double coordinate, unsigned depth, key_type& key) const;
477
480 double keyToCoord(key_type key, unsigned depth) const;
481
484 inline double keyToCoord(key_type key) const{
485 return (double( (int) key - (int) this->tree_max_val ) +0.5) * this->resolution;
486 }
487
490 inline point3d keyToCoord(const OcTreeKey& key) const{
491 return point3d(float(keyToCoord(key[0])), float(keyToCoord(key[1])), float(keyToCoord(key[2])));
492 }
493
496 inline point3d keyToCoord(const OcTreeKey& key, unsigned depth) const{
497 return point3d(float(keyToCoord(key[0], depth)), float(keyToCoord(key[1], depth)), float(keyToCoord(key[2], depth)));
498 }
499
500 protected:
503 OcTreeBaseImpl(double resolution, unsigned int tree_depth, unsigned int tree_max_val);
504
506 void init();
507
509 void calcMinMax();
510
511 void calcNumNodesRecurs(NODE* node, size_t& num_nodes) const;
512
514 std::istream& readNodesRecurs(NODE*, std::istream &s);
515
517 std::ostream& writeNodesRecurs(const NODE*, std::ostream &s) const;
518
521 void deleteNodeRecurs(NODE* node);
522
524 bool deleteNodeRecurs(NODE* node, unsigned int depth, unsigned int max_depth, const OcTreeKey& key);
525
527 void pruneRecurs(NODE* node, unsigned int depth, unsigned int max_depth, unsigned int& num_pruned);
528
530 void expandRecurs(NODE* node, unsigned int depth, unsigned int max_depth);
531
532 size_t getNumLeafNodesRecurs(const NODE* parent) const;
533
534 private:
538
539 protected:
540 void allocNodeChildren(NODE* node);
541
542 NODE* root;
543
544 // constants of the tree
545 const unsigned int tree_depth;
546 const unsigned int tree_max_val;
547 double resolution;
549
550 size_t tree_size;
553
554 point3d tree_center; // coordinate offset of tree
555
556 double max_value[3];
557 double min_value[3];
559 std::vector<double> sizeLookupTable;
560
562 std::vector<KeyRay> keyrays;
563
567
568
569 };
570
571}
572
574
575#endif
Bounding-box leaf iterator.
Definition: OcTreeIterator.hxx:335
Iterator to iterate over all leafs of the tree.
Definition: OcTreeIterator.hxx:263
Definition: OcTreeKey.h:139
OcTree base class, to be used with with any kind of OcTreeDataNode.
Definition: OcTreeBaseImpl.h:75
size_t getNumLeafNodesRecurs(const NODE *parent) const
Definition: OcTreeBaseImpl.hxx:1095
NODE * getNodeChild(NODE *node, unsigned int childIdx) const
Definition: OcTreeBaseImpl.hxx:200
std::istream & readData(std::istream &s)
Read all nodes from the input stream (without file header), for this the tree needs to be already cre...
Definition: OcTreeBaseImpl.hxx:801
const unsigned int tree_max_val
Definition: OcTreeBaseImpl.h:546
double max_value[3]
max in x, y, z
Definition: OcTreeBaseImpl.h:556
const leaf_bbx_iterator leaf_iterator_bbx_end
Definition: OcTreeBaseImpl.h:565
key_type coordToKey(double coordinate) const
Converts from a single coordinate into a discrete key.
Definition: OcTreeBaseImpl.h:357
bool coordToKeyChecked(const point3d &coord, OcTreeKey &key) const
Converts a 3D coordinate into a 3D OcTreeKey, with boundary checking.
Definition: OcTreeBaseImpl.hxx:340
virtual void getMetricMax(double &x, double &y, double &z)
maximum value of the bounding box of all known space in x, y, z
Definition: OcTreeBaseImpl.hxx:950
point3d keyToCoord(const OcTreeKey &key) const
converts from an addressing key at the lowest tree level into a coordinate corresponding to the key's...
Definition: OcTreeBaseImpl.h:490
unsigned int getTreeDepth() const
Definition: OcTreeBaseImpl.h:111
virtual void getMetricMin(double &x, double &y, double &z)
minimum value of the bounding box of all known space in x, y, z
Definition: OcTreeBaseImpl.hxx:942
OcTreeKey coordToKey(const point3d &coord, unsigned depth) const
Converts from a 3D coordinate into a 3D addressing key at a given depth.
Definition: OcTreeBaseImpl.h:376
virtual size_t memoryUsage() const
Definition: OcTreeBaseImpl.hxx:1039
void deleteNodeRecurs(NODE *node)
Recursively delete a node and all children. Deallocates memory but does NOT set the node ptr to NULL ...
Definition: OcTreeBaseImpl.hxx:662
bool nodeChildExists(const NODE *node, unsigned int childIdx) const
Safe test if node has a child at index childIdx.
Definition: OcTreeBaseImpl.hxx:235
virtual void prune()
Lossless compression of the octree: A node will replace all of its eight children if they have identi...
Definition: OcTreeBaseImpl.hxx:523
bool deleteNode(double x, double y, double z, unsigned int depth=0)
Delete a node (if exists) given a 3d point.
Definition: OcTreeBaseImpl.hxx:488
void clearKeyRays()
Clear KeyRay vector to minimize unneeded memory.
Definition: OcTreeBaseImpl.h:120
double keyToCoord(key_type key) const
converts from a discrete key at the lowest tree level into a coordinate corresponding to the key's ce...
Definition: OcTreeBaseImpl.h:484
const iterator end() const
Definition: OcTreeBaseImpl.h:329
double keyToCoord(key_type key, unsigned depth) const
converts from a discrete key at a given depth into a coordinate corresponding to the key's center
Definition: OcTreeBaseImpl.hxx:394
virtual void expandNode(NODE *node)
Expands a node (reverse of pruning): All children are created and their occupancy probability is set ...
Definition: OcTreeBaseImpl.hxx:257
void setResolution(double r)
Change the resolution of the octree, scaling all voxels. This will not preserve the (metric) scale!
Definition: OcTreeBaseImpl.hxx:156
const tree_iterator end_tree() const
Definition: OcTreeBaseImpl.h:350
OcTreeBaseImpl(const OcTreeBaseImpl< NODE, INTERFACE > &rhs)
Deep copy constructor.
NODE * getRoot() const
Definition: OcTreeBaseImpl.h:179
const unsigned int tree_depth
Maximum tree depth is fixed to 16 currently.
Definition: OcTreeBaseImpl.h:545
NODE NodeType
Make the templated NODE type available from the outside.
Definition: OcTreeBaseImpl.h:79
void expandRecurs(NODE *node, unsigned int depth, unsigned int max_depth)
recursive call of expand()
Definition: OcTreeBaseImpl.hxx:742
virtual void expand()
Expands all pruned nodes (reverse of prune())
Definition: OcTreeBaseImpl.hxx:536
double resolution
in meters
Definition: OcTreeBaseImpl.h:547
virtual size_t memoryUsageNode() const
Definition: OcTreeBaseImpl.h:247
double getResolution() const
Definition: OcTreeBaseImpl.h:109
size_t calcNumNodes() const
Traverses the tree to calculate the total number of nodes.
Definition: OcTreeBaseImpl.hxx:1016
std::vector< KeyRay > keyrays
data structure for ray casting, array for multithreading
Definition: OcTreeBaseImpl.h:562
const tree_iterator tree_iterator_end
Definition: OcTreeBaseImpl.h:566
const leaf_bbx_iterator end_leafs_bbx() const
Definition: OcTreeBaseImpl.h:345
leaf_bbx_iterator begin_leafs_bbx(const OcTreeKey &min, const OcTreeKey &max, unsigned char maxDepth=0) const
Definition: OcTreeBaseImpl.h:337
std::string getTreeType() const
Definition: OcTreeBaseImpl.h:104
std::ostream & writeData(std::ostream &s) const
Write complete state of tree to stream (without file header) unmodified. Pruning the tree first produ...
Definition: OcTreeBaseImpl.hxx:763
virtual ~OcTreeBaseImpl()
Definition: OcTreeBaseImpl.hxx:68
bool operator==(const OcTreeBaseImpl< NODE, INTERFACE > &rhs) const
Comparison between two octrees, all meta data, all nodes, and the structure must be identical.
Definition: OcTreeBaseImpl.hxx:125
void swapContent(OcTreeBaseImpl< NODE, INTERFACE > &rhs)
Swap contents of two octrees, i.e., only the underlying pointer / tree structure.
Definition: OcTreeBaseImpl.hxx:114
leaf_iterator iterator
Definition: OcTreeBaseImpl.h:324
void deleteNodeChild(NODE *node, unsigned int childIdx)
Deletes the i-th child of the node.
Definition: OcTreeBaseImpl.hxx:189
OcTreeKey coordToKey(double x, double y, double z, unsigned depth) const
Converts from a 3D coordinate into a 3D addressing key at a given depth.
Definition: OcTreeBaseImpl.h:384
iterator begin(unsigned char maxDepth=0) const
Definition: OcTreeBaseImpl.h:327
void pruneRecurs(NODE *node, unsigned int depth, unsigned int max_depth, unsigned int &num_pruned)
recursive call of prune()
Definition: OcTreeBaseImpl.hxx:719
unsigned long long memoryFullGrid() const
Definition: OcTreeBaseImpl.hxx:850
point3d keyToCoord(const OcTreeKey &key, unsigned depth) const
converts from an addressing key at a given depth into a coordinate corresponding to the key's center
Definition: OcTreeBaseImpl.h:496
void clear()
Deletes the complete tree structure.
Definition: OcTreeBaseImpl.hxx:512
NODE * createNodeChild(NODE *node, unsigned int childIdx)
Creates (allocates) the i-th child of the node.
Definition: OcTreeBaseImpl.hxx:173
double volume()
Definition: OcTreeBaseImpl.hxx:1112
tree_iterator begin_tree(unsigned char maxDepth=0) const
Definition: OcTreeBaseImpl.h:348
void calcNumNodesRecurs(NODE *node, size_t &num_nodes) const
Definition: OcTreeBaseImpl.hxx:1026
const leaf_iterator end_leafs() const
Definition: OcTreeBaseImpl.h:334
void getUnknownLeafCenters(point3d_list &node_centers, point3d pmin, point3d pmax, unsigned int depth=0) const
return centers of leafs that do NOT exist (but could) in a given bounding box
Definition: OcTreeBaseImpl.hxx:1046
void init()
initialize non-trivial members, helper for constructors
Definition: OcTreeBaseImpl.hxx:87
point3d tree_center
Definition: OcTreeBaseImpl.h:554
virtual bool pruneNode(NODE *node)
Prunes a node when it is collapsible.
Definition: OcTreeBaseImpl.hxx:267
double resolution_factor
= 1. / resolution
Definition: OcTreeBaseImpl.h:548
bool computeRay(const point3d &origin, const point3d &end, std::vector< point3d > &ray)
Traces a ray from origin to end (excluding), returning the coordinates of all nodes traversed by the ...
Definition: OcTreeBaseImpl.hxx:651
bool nodeHasChildren(const NODE *node) const
Safe test if node has any children.
Definition: OcTreeBaseImpl.hxx:244
virtual size_t size() const
Definition: OcTreeBaseImpl.h:241
virtual bool isNodeCollapsible(const NODE *node) const
A node is collapsible if all children exist, don't have children of their own and have the same occup...
Definition: OcTreeBaseImpl.hxx:214
OcTreeKey coordToKey(double x, double y, double z) const
Converts from a 3D coordinate into a 3D addressing key.
Definition: OcTreeBaseImpl.h:371
bool size_changed
Definition: OcTreeBaseImpl.h:552
OcTreeKey coordToKey(const point3d &coord) const
Converts from a 3D coordinate into a 3D addressing key.
Definition: OcTreeBaseImpl.h:366
std::ostream & writeNodesRecurs(const NODE *, std::ostream &s) const
recursive call of writeData()
Definition: OcTreeBaseImpl.hxx:771
NODE * search(double x, double y, double z, unsigned int depth=0) const
Search node at specified depth given a 3d point (depth=0: search full tree depth).
Definition: OcTreeBaseImpl.hxx:421
size_t tree_size
number of nodes in tree flag to denote whether the octree extent changed (for lazy min/max eval)
Definition: OcTreeBaseImpl.h:550
leaf_iterator begin_leafs(unsigned char maxDepth=0) const
Definition: OcTreeBaseImpl.h:332
size_t getNumLeafNodes() const
Traverses the tree to calculate the total number of leaf nodes.
Definition: OcTreeBaseImpl.hxx:1086
std::istream & readNodesRecurs(NODE *, std::istream &s)
recursive call of readData()
Definition: OcTreeBaseImpl.hxx:824
bool computeRayKeys(const point3d &origin, const point3d &end, KeyRay &ray) const
Traces a ray from origin to end (excluding), returning an OcTreeKey of all nodes traversed by the bea...
Definition: OcTreeBaseImpl.hxx:542
leaf_bbx_iterator begin_leafs_bbx(const point3d &min, const point3d &max, unsigned char maxDepth=0) const
Definition: OcTreeBaseImpl.h:341
double getNodeSize(unsigned depth) const
Definition: OcTreeBaseImpl.h:113
virtual void getMetricSize(double &x, double &y, double &z)
Size of OcTree (all known space) in meters for x, y and z dimension.
Definition: OcTreeBaseImpl.hxx:872
std::vector< double > sizeLookupTable
Definition: OcTreeBaseImpl.h:559
NODE * root
Pointer to the root NODE, NULL for empty tree.
Definition: OcTreeBaseImpl.h:542
double min_value[3]
min in x, y, z contains the size of a voxel at level i (0: root node). tree_depth+1 levels (incl....
Definition: OcTreeBaseImpl.h:557
void allocNodeChildren(NODE *node)
Definition: OcTreeBaseImpl.hxx:286
OcTreeKey adjustKeyAtDepth(const OcTreeKey &key, unsigned int depth) const
Adjusts a 3D key from the lowest level to correspond to a higher depth (by shifting the key values)
Definition: OcTreeBaseImpl.h:399
void calcMinMax()
recalculates min and max in x, y, z. Does nothing when tree size didn't change.
Definition: OcTreeBaseImpl.hxx:900
const leaf_iterator leaf_iterator_end
Definition: OcTreeBaseImpl.h:564
OcTreeBaseImpl(double resolution)
Definition: OcTreeBaseImpl.hxx:46
OcTreeKey is a container class for internal key addressing.
Definition: OcTreeKey.h:70
This class represents a three-dimensional vector.
Definition: Vector3.h:50
Iterator over the complete tree (inner nodes and leafs).
Definition: OcTreeIterator.hxx:207
Namespace the OctoMap library and visualization tools.
uint16_t key_type
Definition: OcTreeKey.h:63
std::list< octomath::Vector3 > point3d_list
Definition: octomap_types.h:54
octomath::Vector3 point3d
Use Vector3 (float precision) as a point3d in octomap.
Definition: octomap_types.h:49