RTAB-Map 0.23.10
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#include <octomap/ColorOcTree.h>
34#include <octomap/OcTreeKey.h>
35
36#include <pcl/pcl_base.h>
37#include <pcl/point_types.h>
38
39#include <rtabmap/core/Transform.h>
40#include <rtabmap/core/Parameters.h>
41#include <rtabmap/core/GlobalMap.h>
42
43#include <map>
44#include <unordered_set>
45#include <string>
46#include <queue>
47
48namespace rtabmap {
49
50// forward declaraton for "friend"
51class RtabmapColorOcTree;
52
53class RtabmapColorOcTreeNode : public octomap::ColorOcTreeNode
54{
55public:
56 enum OccupancyType {kTypeUnknown=-1, kTypeEmpty=0, kTypeGround=1, kTypeObstacle=100};
57
58public:
59 friend class RtabmapColorOcTree; // needs access to node children (inherited)
60
61 RtabmapColorOcTreeNode() : ColorOcTreeNode(), nodeRefId_(0), type_(kTypeUnknown) {}
62 RtabmapColorOcTreeNode(const RtabmapColorOcTreeNode& rhs) : ColorOcTreeNode(rhs), nodeRefId_(rhs.nodeRefId_), type_(rhs.type_) {}
63
64 void setNodeRefId(int nodeRefId) {nodeRefId_ = nodeRefId;}
65 void setOccupancyType(char type) {type_=type;}
66 void setPointRef(const octomap::point3d & point) {pointRef_ = point;}
67 int getNodeRefId() const {return nodeRefId_;}
68 int getOccupancyType() const {return type_;}
69 const octomap::point3d & getPointRef() const {return pointRef_;}
70
71 // following methods defined for octomap < 1.8 compatibility
72 RtabmapColorOcTreeNode* getChild(unsigned int i);
73 const RtabmapColorOcTreeNode* getChild(unsigned int i) const;
74 bool pruneNode();
75 void expandNode();
76 bool createChild(unsigned int i);
77
78 void updateOccupancyTypeChildren();
79
80private:
81 int nodeRefId_;
82 int type_; // -1=undefined, 0=empty, 100=obstacle, 1=ground
83 octomap::point3d pointRef_;
84};
85
86// Same as official ColorOctree but using RtabmapColorOcTreeNode, which is inheriting ColorOcTreeNode
87class RtabmapColorOcTree : public octomap::OccupancyOcTreeBase <RtabmapColorOcTreeNode> {
88
89 public:
91 RtabmapColorOcTree(double resolution);
92 virtual ~RtabmapColorOcTree() {}
93
96 RtabmapColorOcTree* create() const {return new RtabmapColorOcTree(resolution); }
97
98 std::string getTreeType() const {return "ColorOcTree";} // same type as ColorOcTree to be compatible with ROS OctoMap msg
99
107
108 virtual bool isNodeCollapsible(const RtabmapColorOcTreeNode* node) const;
109
110 // set node color at given key or coordinate. Replaces previous color.
111 RtabmapColorOcTreeNode* setNodeColor(const octomap::OcTreeKey& key, uint8_t r,
112 uint8_t g, uint8_t b);
113
114 RtabmapColorOcTreeNode* setNodeColor(float x, float y,
115 float z, uint8_t r,
116 uint8_t g, uint8_t b) {
117 octomap::OcTreeKey key;
118 if (!this->coordToKeyChecked(octomap::point3d(x,y,z), key)) return NULL;
119 return setNodeColor(key,r,g,b);
120 }
121
122 // integrate color measurement at given key or coordinate. Average with previous color
123 RtabmapColorOcTreeNode* averageNodeColor(const octomap::OcTreeKey& key, uint8_t r,
124 uint8_t g, uint8_t b);
125
126 RtabmapColorOcTreeNode* averageNodeColor(float x, float y,
127 float z, uint8_t r,
128 uint8_t g, uint8_t b) {
129 octomap:: OcTreeKey key;
130 if (!this->coordToKeyChecked(octomap::point3d(x,y,z), key)) return NULL;
131 return averageNodeColor(key,r,g,b);
132 }
133
134 // integrate color measurement at given key or coordinate. Average with previous color
135 RtabmapColorOcTreeNode* integrateNodeColor(const octomap::OcTreeKey& key, uint8_t r,
136 uint8_t g, uint8_t b);
137
138 RtabmapColorOcTreeNode* integrateNodeColor(float x, float y,
139 float z, uint8_t r,
140 uint8_t g, uint8_t b) {
141 octomap::OcTreeKey key;
142 if (!this->coordToKeyChecked(octomap::point3d(x,y,z), key)) return NULL;
143 return integrateNodeColor(key,r,g,b);
144 }
145
146 // update inner nodes, sets color to average child color
147 void updateInnerOccupancy();
148
149 protected:
150 void updateInnerOccupancyRecurs(RtabmapColorOcTreeNode* node, unsigned int depth);
151
160 public:
162
168 void ensureLinking() {};
169 };
172
173 };
174
175class RTABMAP_CORE_EXPORT OctoMap : public GlobalMap {
176public:
177 OctoMap(const LocalGridCache * cache, const ParametersMap & parameters = ParametersMap());
178
179 const RtabmapColorOcTree * octree() const {return octree_;}
180
181 pcl::PointCloud<pcl::PointXYZRGB>::Ptr createCloud(
182 unsigned int treeDepth = 0,
183 std::vector<int> * obstacleIndices = 0,
184 std::vector<int> * emptyIndices = 0,
185 std::vector<int> * groundIndices = 0,
186 bool originalRefPoints = true,
187 std::vector<int> * frontierIndices = 0,
188 std::vector<double> * cloudProb = 0) const;
189
190 cv::Mat createProjectionMap(
191 float & xMin,
192 float & yMin,
193 float & gridCellSize,
194 float minGridSize = 0.0f,
195 unsigned int treeDepth = 0);
196
197 bool writeBinary(const std::string & path);
198
199 virtual ~OctoMap();
200 virtual void clear();
201 virtual unsigned long getMemoryUsed() const;
202
203 bool hasColor() const {return hasColor_;}
204
205 static std::unordered_set<octomap::OcTreeKey, octomap::OcTreeKey::KeyHash> findEmptyNode(RtabmapColorOcTree* octree_, unsigned int treeDepth, octomap::point3d startPosition);
206 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);
207 static bool isNodeVisited(std::unordered_set<octomap::OcTreeKey,octomap::OcTreeKey::KeyHash> const & EmptyNodes,octomap::OcTreeKey const key);
208 static octomap::point3d findCloseEmpty(RtabmapColorOcTree* octree_, unsigned int treeDepth,octomap::point3d startPosition);
209 static bool isValidEmpty(RtabmapColorOcTree* octree_, unsigned int treeDepth,octomap::point3d startPosition);
210
211protected:
212 virtual void assemble(const std::list<std::pair<int, Transform> > & newPoses);
213
214private:
215 void updateMinMax(const octomap::point3d & point);
216
217private:
218 RtabmapColorOcTree * octree_;
219 bool hasColor_;
220 float rangeMax_;
221 bool rayTracing_;
222 unsigned int emptyFloodFillDepth_;
223};
224
225} /* namespace rtabmap */
226
227#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:171
RtabmapColorOcTree * create() const
Definition OctoMap.h:96
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