octomap 1.5.0
|
00001 #ifndef OCTOMAP_OCTREE_BASE_IMPL_H 00002 #define OCTOMAP_OCTREE_BASE_IMPL_H 00003 00004 // $Id: OcTreeBaseImpl.h 420 2012-08-27 14:10:25Z ahornung $ 00005 00014 /* 00015 * Copyright (c) 2009-2011, K. M. Wurm, A. Hornung, University of Freiburg 00016 * All rights reserved. 00017 * 00018 * Redistribution and use in source and binary forms, with or without 00019 * modification, are permitted provided that the following conditions are met: 00020 * 00021 * * Redistributions of source code must retain the above copyright 00022 * notice, this list of conditions and the following disclaimer. 00023 * * Redistributions in binary form must reproduce the above copyright 00024 * notice, this list of conditions and the following disclaimer in the 00025 * documentation and/or other materials provided with the distribution. 00026 * * Neither the name of the University of Freiburg nor the names of its 00027 * contributors may be used to endorse or promote products derived from 00028 * this software without specific prior written permission. 00029 * 00030 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00031 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00032 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00033 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00034 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00035 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00036 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00037 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00038 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00039 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00040 * POSSIBILITY OF SUCH DAMAGE. 00041 */ 00042 00043 #include <list> 00044 #include <limits> 00045 #include <iterator> 00046 #include <stack> 00047 00048 00049 #include "octomap_types.h" 00050 #include "OcTreeKey.h" 00051 #include "ScanGraph.h" 00052 00053 00054 namespace octomap { 00055 00056 00076 template <class NODE,class INTERFACE> 00077 class OcTreeBaseImpl : public INTERFACE { 00078 00079 public: 00081 typedef NODE NodeType; 00082 00083 OcTreeBaseImpl(double resolution); 00084 virtual ~OcTreeBaseImpl(); 00085 00086 OcTreeBaseImpl(const OcTreeBaseImpl<NODE,INTERFACE>& rhs); 00087 00088 bool operator== (const OcTreeBaseImpl<NODE,INTERFACE>& rhs) const; 00089 00090 std::string getTreeType() const {return "OcTreeBaseImpl";} 00091 00094 void setResolution(double r); 00095 inline double getResolution() const { return resolution; } 00096 00097 inline unsigned int getTreeDepth () const { return tree_depth; } 00098 00099 inline double getNodeSize(unsigned depth) const {assert(depth <= tree_depth); return sizeLookupTable[depth];} 00100 00106 inline NODE* getRoot() const { return root; } 00107 00112 NODE* search(float x, float y, float z, unsigned int depth = 0) const; 00113 00118 NODE* search(const point3d& value, unsigned int depth = 0) const; 00119 00124 NODE* search(const OcTreeKey& key, unsigned int depth = 0) const; 00125 00131 bool deleteNode(float x, float y, float z, unsigned int depth = 0); 00132 00138 bool deleteNode(const point3d& value, unsigned int depth = 0); 00139 00145 bool deleteNode(const OcTreeKey& key, unsigned int depth = 0); 00146 00148 void clear(); 00149 00150 OcTreeBaseImpl deepCopy() const; 00151 00152 00155 virtual void prune(); 00156 00159 virtual void expand(); 00160 00161 // -- statistics ---------------------- 00162 00164 virtual inline size_t size() const { return tree_size; } 00165 00167 virtual size_t memoryUsage() const; 00168 00170 virtual inline size_t memoryUsageNode() const {return sizeof(NODE); }; 00171 00173 size_t memoryFullGrid(); 00174 00175 double volume(); 00176 00178 virtual void getMetricSize(double& x, double& y, double& z); 00180 virtual void getMetricMin(double& x, double& y, double& z); 00182 void getMetricMin(double& x, double& y, double& z) const; 00184 virtual void getMetricMax(double& x, double& y, double& z); 00186 void getMetricMax(double& x, double& y, double& z) const; 00187 00189 size_t calcNumNodes() const; 00190 00192 size_t getNumLeafNodes() const; 00193 00194 00195 // -- access tree nodes ------------------ 00196 00198 void getUnknownLeafCenters(point3d_list& node_centers, point3d pmin, point3d pmax) const; 00199 00200 00201 // -- raytracing ----------------------- 00202 00213 bool computeRayKeys(const point3d& origin, const point3d& end, KeyRay& ray) const; 00214 00215 00227 bool computeRay(const point3d& origin, const point3d& end, std::vector<point3d>& ray); 00228 00229 00230 // file IO 00231 00238 std::istream& readData(std::istream &s); 00239 00242 std::ostream& writeData(std::ostream &s) const; 00243 00244 class leaf_iterator; 00245 class tree_iterator; 00246 class leaf_bbx_iterator; 00247 typedef leaf_iterator iterator; 00248 00250 iterator begin(unsigned char maxDepth=0) const {return iterator(this, maxDepth);}; 00252 const iterator end() const {return leaf_iterator_end;}; // TODO: RVE? 00253 00255 leaf_iterator begin_leafs(unsigned char maxDepth=0) const {return leaf_iterator(this, maxDepth);}; 00257 const leaf_iterator end_leafs() const {return leaf_iterator_end;} 00258 00260 leaf_bbx_iterator begin_leafs_bbx(const OcTreeKey& min, const OcTreeKey& max, unsigned char maxDepth=0) const { 00261 return leaf_bbx_iterator(this, min, max, maxDepth); 00262 } 00264 leaf_bbx_iterator begin_leafs_bbx(const point3d& min, const point3d& max, unsigned char maxDepth=0) const { 00265 return leaf_bbx_iterator(this, min, max, maxDepth); 00266 } 00268 const leaf_bbx_iterator end_leafs_bbx() const {return leaf_iterator_bbx_end;} 00269 00271 tree_iterator begin_tree(unsigned char maxDepth=0) const {return tree_iterator(this, maxDepth);} 00273 const tree_iterator end_tree() const {return tree_iterator_end;} 00274 00275 // 00276 // Key / coordinate conversion functions 00277 // 00278 00280 inline unsigned short int coordToKey(double coordinate) const{ 00281 return ((int) floor(resolution_factor * coordinate)) + tree_max_val; 00282 } 00283 00285 unsigned short int coordToKey(double coordinate, unsigned depth) const; 00286 00287 00289 inline OcTreeKey coordToKey(const point3d& coord) const{ 00290 return OcTreeKey(coordToKey(coord(0)), coordToKey(coord(1)), coordToKey(coord(2))); 00291 } 00292 00294 inline OcTreeKey coordToKey(const point3d& coord, unsigned depth) const{ 00295 if (depth == tree_depth) 00296 return coordToKey(coord); 00297 else 00298 return OcTreeKey(coordToKey(coord(0), depth), coordToKey(coord(1), depth), coordToKey(coord(2), depth)); 00299 } 00300 00309 inline OcTreeKey adjustKeyAtDepth(const OcTreeKey& key, unsigned int depth) const{ 00310 if (depth == tree_depth) 00311 return key; 00312 00313 assert(depth <= tree_depth); 00314 return OcTreeKey(adjustKeyAtDepth(key[0], depth), adjustKeyAtDepth(key[1], depth), adjustKeyAtDepth(key[2], depth)); 00315 } 00316 00325 unsigned short int adjustKeyAtDepth(unsigned short int key, unsigned int depth) const; 00326 00334 bool coordToKeyChecked(const point3d& coord, OcTreeKey& key) const; 00335 00344 bool coordToKeyChecked(const point3d& coord, unsigned depth, OcTreeKey& key) const; 00345 00353 bool coordToKeyChecked(double coordinate, unsigned short int& key) const; 00354 00363 bool coordToKeyChecked(double coordinate, unsigned depth, unsigned short int& key) const; 00364 00367 double keyToCoord(unsigned short int key, unsigned depth) const; 00368 00371 inline double keyToCoord(unsigned short int key) const{ 00372 return (double( (int) key - (int) this->tree_max_val ) +0.5) * this->resolution; 00373 } 00374 00377 inline point3d keyToCoord(const OcTreeKey& key) const{ 00378 return point3d(float(keyToCoord(key[0])), float(keyToCoord(key[1])), float(keyToCoord(key[2]))); 00379 } 00380 00383 inline point3d keyToCoord(const OcTreeKey& key, unsigned depth) const{ 00384 return point3d(float(keyToCoord(key[0], depth)), float(keyToCoord(key[1], depth)), float(keyToCoord(key[2], depth))); 00385 } 00386 00388 DEPRECATED( bool genKeyValue(double coordinate, unsigned short int& keyval) const) { 00389 return coordToKeyChecked(coordinate, keyval); 00390 } 00391 00393 DEPRECATED( bool genKey(const point3d& point, OcTreeKey& key) const ) { 00394 return coordToKeyChecked(point, key); 00395 } 00396 00398 DEPRECATED( bool genKeyValueAtDepth(const unsigned short int keyval, unsigned int depth, unsigned short int &out_keyval) const ); 00399 00401 DEPRECATED( bool genKeyAtDepth(const OcTreeKey& key, unsigned int depth, OcTreeKey& out_key) const ); 00402 00405 DEPRECATED( bool genCoordFromKey(const unsigned short int& key, unsigned depth, float& coord) const ){ 00406 coord = float(keyToCoord(key, depth)); 00407 return true; 00408 } 00409 00412 DEPRECATED( inline bool genCoordFromKey(const unsigned short int& key, float& coord, unsigned depth) const) { 00413 coord = float(keyToCoord(key, depth)); 00414 return true; 00415 } 00416 00419 DEPRECATED( inline bool genCoordFromKey(const unsigned short int& key, float& coord) const) { 00420 coord = float(keyToCoord(key)); 00421 return true; 00422 } 00423 00425 DEPRECATED( double genCoordFromKey(const unsigned short int& key, unsigned depth) const) { 00426 return keyToCoord(key, depth); 00427 } 00428 00430 DEPRECATED( inline double genCoordFromKey(const unsigned short int& key) const) { 00431 return keyToCoord(key); 00432 } 00433 00436 DEPRECATED( inline bool genCoords(const OcTreeKey& key, unsigned int depth, point3d& point) const){ 00437 point = keyToCoord(key, depth); 00438 return true; 00439 } 00440 00443 DEPRECATED( inline void genPos(const OcTreeKey& key, int depth, unsigned int& pos) const) { 00444 pos = computeChildIdx(key, depth); 00445 } 00446 00447 protected: 00450 OcTreeBaseImpl(double resolution, unsigned int tree_depth, unsigned int tree_max_val); 00451 00453 void calcMinMax(); 00454 00455 void calcNumNodesRecurs(NODE* node, size_t& num_nodes) const; 00456 00458 bool deleteNodeRecurs(NODE* node, unsigned int depth, unsigned int max_depth, const OcTreeKey& key); 00459 00461 void pruneRecurs(NODE* node, unsigned int depth, unsigned int max_depth, unsigned int& num_pruned); 00462 00464 void expandRecurs(NODE* node, unsigned int depth, unsigned int max_depth); 00465 00466 size_t getNumLeafNodesRecurs(const NODE* parent) const; 00467 00468 private: 00471 OcTreeBaseImpl<NODE,INTERFACE>& operator=(const OcTreeBaseImpl<NODE,INTERFACE>&); 00472 00473 protected: 00474 00475 NODE* root; 00476 00477 // constants of the tree 00478 const unsigned int tree_depth; 00479 const unsigned int tree_max_val; 00480 double resolution; 00481 double resolution_factor; 00482 00483 size_t tree_size; 00484 00485 bool size_changed; 00486 00487 point3d tree_center; // coordinate offset of tree 00488 00489 double max_value[3]; 00490 double min_value[3]; 00491 00492 std::vector<double> sizeLookupTable; 00493 00494 KeyRay keyray; // data structure for ray casting 00495 00496 const leaf_iterator leaf_iterator_end; 00497 const leaf_bbx_iterator leaf_iterator_bbx_end; 00498 const tree_iterator tree_iterator_end; 00499 00500 public: 00501 // the actual iterator implementation within this class: 00502 #include <octomap/OcTreeIterator.hxx> 00503 }; 00504 00505 } 00506 00507 #include <octomap/OcTreeBaseImpl.hxx> 00508 00509 #endif