RTAB-Map 0.24.5
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
OctoMap.h
1/*
2Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Universite de Sherbrooke nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef SRC_OCTOMAP_H_
29#define SRC_OCTOMAP_H_
30
31#include "rtabmap/core/rtabmap_core_export.h" // DLL export/import defines
32
33// octomap includes <ciso646>, which GCC deprecates (#warning) from C++20.
34#if defined(__GNUC__) && !defined(__clang__)
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wcpp"
37#endif
38#include <octomap/ColorOcTree.h>
39#include <octomap/OcTreeKey.h>
40#if defined(__GNUC__) && !defined(__clang__)
41#pragma GCC diagnostic pop
42#endif
43
44#include <pcl/pcl_base.h>
45#include <pcl/point_types.h>
46
47#include <rtabmap/core/Transform.h>
48#include <rtabmap/core/Parameters.h>
49#include <rtabmap/core/GlobalMap.h>
50
51#include <map>
52#include <unordered_set>
53#include <string>
54#include <queue>
55
56namespace rtabmap {
57
58// forward declaraton for "friend"
59class RtabmapColorOcTree;
60
61class RtabmapColorOcTreeNode : public octomap::ColorOcTreeNode
62{
63public:
64 enum OccupancyType {kTypeUnknown=-1, kTypeEmpty=0, kTypeGround=1, kTypeObstacle=100};
65
66public:
67 friend class RtabmapColorOcTree; // needs access to node children (inherited)
68
69 RtabmapColorOcTreeNode() : ColorOcTreeNode(), nodeRefId_(0), type_(kTypeUnknown) {}
70 RtabmapColorOcTreeNode(const RtabmapColorOcTreeNode& rhs) : ColorOcTreeNode(rhs), nodeRefId_(rhs.nodeRefId_), type_(rhs.type_) {}
71
72 void setNodeRefId(int nodeRefId) {nodeRefId_ = nodeRefId;}
73 void setOccupancyType(char type) {type_=type;}
74 void setPointRef(const octomap::point3d & point) {pointRef_ = point;}
75 int getNodeRefId() const {return nodeRefId_;}
76 int getOccupancyType() const {return type_;}
77 const octomap::point3d & getPointRef() const {return pointRef_;}
78
79 // following methods defined for octomap < 1.8 compatibility
80 RtabmapColorOcTreeNode* getChild(unsigned int i);
81 const RtabmapColorOcTreeNode* getChild(unsigned int i) const;
82 bool pruneNode();
83 void expandNode();
84 bool createChild(unsigned int i);
85
86 void updateOccupancyTypeChildren();
87
88private:
89 int nodeRefId_;
90 int type_; // -1=undefined, 0=empty, 100=obstacle, 1=ground
91 octomap::point3d pointRef_;
92};
93
94// Same as official ColorOctree but using RtabmapColorOcTreeNode, which is inheriting ColorOcTreeNode
95class RtabmapColorOcTree : public octomap::OccupancyOcTreeBase <RtabmapColorOcTreeNode> {
96
97 public:
99 RtabmapColorOcTree(double resolution);
100 virtual ~RtabmapColorOcTree() {}
101
104 RtabmapColorOcTree* create() const {return new RtabmapColorOcTree(resolution); }
105
106 std::string getTreeType() const {return "ColorOcTree";} // same type as ColorOcTree to be compatible with ROS OctoMap msg
107
115
116 virtual bool isNodeCollapsible(const RtabmapColorOcTreeNode* node) const;
117
118 // set node color at given key or coordinate. Replaces previous color.
119 RtabmapColorOcTreeNode* setNodeColor(const octomap::OcTreeKey& key, uint8_t r,
120 uint8_t g, uint8_t b);
121
122 RtabmapColorOcTreeNode* setNodeColor(float x, float y,
123 float z, uint8_t r,
124 uint8_t g, uint8_t b) {
125 octomap::OcTreeKey key;
126 if (!this->coordToKeyChecked(octomap::point3d(x,y,z), key)) return NULL;
127 return setNodeColor(key,r,g,b);
128 }
129
130 // integrate color measurement at given key or coordinate. Average with previous color
131 RtabmapColorOcTreeNode* averageNodeColor(const octomap::OcTreeKey& key, uint8_t r,
132 uint8_t g, uint8_t b);
133
134 RtabmapColorOcTreeNode* averageNodeColor(float x, float y,
135 float z, uint8_t r,
136 uint8_t g, uint8_t b) {
137 octomap:: OcTreeKey key;
138 if (!this->coordToKeyChecked(octomap::point3d(x,y,z), key)) return NULL;
139 return averageNodeColor(key,r,g,b);
140 }
141
142 // integrate color measurement at given key or coordinate. Average with previous color
143 RtabmapColorOcTreeNode* integrateNodeColor(const octomap::OcTreeKey& key, uint8_t r,
144 uint8_t g, uint8_t b);
145
146 RtabmapColorOcTreeNode* integrateNodeColor(float x, float y,
147 float z, uint8_t r,
148 uint8_t g, uint8_t b) {
149 octomap::OcTreeKey key;
150 if (!this->coordToKeyChecked(octomap::point3d(x,y,z), key)) return NULL;
151 return integrateNodeColor(key,r,g,b);
152 }
153
154 // update inner nodes, sets color to average child color
155 void updateInnerOccupancy();
156
157 protected:
158 void updateInnerOccupancyRecurs(RtabmapColorOcTreeNode* node, unsigned int depth);
159
168 public:
170
176 void ensureLinking() {};
177 };
180
181 };
182
183class RTABMAP_CORE_EXPORT OctoMap : public GlobalMap {
184public:
185 OctoMap(const LocalGridCache * cache, const ParametersMap & parameters = ParametersMap());
186
187 const RtabmapColorOcTree * octree() const {return octree_;}
188
189 pcl::PointCloud<pcl::PointXYZRGB>::Ptr createCloud(
190 unsigned int treeDepth = 0,
191 std::vector<int> * obstacleIndices = 0,
192 std::vector<int> * emptyIndices = 0,
193 std::vector<int> * groundIndices = 0,
194 bool originalRefPoints = true,
195 std::vector<int> * frontierIndices = 0,
196 std::vector<double> * cloudProb = 0) const;
197
198 cv::Mat createProjectionMap(
199 float & xMin,
200 float & yMin,
201 float & gridCellSize,
202 float minGridSize = 0.0f,
203 unsigned int treeDepth = 0);
204
205 bool writeBinary(const std::string & path);
206
207 virtual ~OctoMap();
208 virtual void clear();
209 virtual unsigned long getMemoryUsed() const;
210
211 bool hasColor() const {return hasColor_;}
212
213 static std::unordered_set<octomap::OcTreeKey, octomap::OcTreeKey::KeyHash> findEmptyNode(RtabmapColorOcTree* octree_, unsigned int treeDepth, octomap::point3d startPosition);
214 static void floodFill(RtabmapColorOcTree* octree_, unsigned int treeDepth,octomap::point3d startPosition, std::unordered_set<octomap::OcTreeKey, octomap::OcTreeKey::KeyHash> & EmptyNodes,std::queue<octomap::point3d>& positionToExplore);
215 static bool isNodeVisited(std::unordered_set<octomap::OcTreeKey,octomap::OcTreeKey::KeyHash> const & EmptyNodes,octomap::OcTreeKey const key);
216 static octomap::point3d findCloseEmpty(RtabmapColorOcTree* octree_, unsigned int treeDepth,octomap::point3d startPosition);
217 static bool isValidEmpty(RtabmapColorOcTree* octree_, unsigned int treeDepth,octomap::point3d startPosition);
218
219protected:
220 virtual void assemble(const std::list<std::pair<int, Transform> > & newPoses);
221
222private:
223 void updateMinMax(const octomap::point3d & point);
224
225private:
226 RtabmapColorOcTree * octree_;
227 bool hasColor_;
228 float rangeMax_;
229 bool rayTracing_;
230 unsigned int emptyFloodFillDepth_;
231};
232
233} /* namespace rtabmap */
234
235#endif /* SRC_OCTOMAP_H_ */
Abstract base for assembling per-node LocalGrid data into a global map.
Definition GlobalMap.h:56
Cache of LocalGrid entries keyed by map node id.
Definition LocalGrid.h:98
virtual void clear()
Clears assembled nodes and grid bounds; does not clear LocalGridCache.
virtual unsigned long getMemoryUsed() const
Approximate memory used by assembled-node bookkeeping (bytes).
virtual void assemble(const std::list< std::pair< int, Transform > > &newPoses)
Subclass hook: merge newPoses into the global map; call addAssembledNode().
RtabmapColorOcTree(double resolution)
Default constructor, sets resolution of leafs.
static StaticMemberInitializer RtabmapColorOcTreeMemberInit
static member to ensure static initialization (only once)
Definition OctoMap.h:179
RtabmapColorOcTree * create() const
Definition OctoMap.h:104
virtual bool pruneNode(RtabmapColorOcTreeNode *node)
std::map< std::string, std::string > ParametersMap
Parameter keys mapped to their values, as used by every configurable class (see Parameters).
Definition Parameters.h:44