GlobalMap.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2023, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, 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 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON 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
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <rtabmap/core/GlobalMap.h>
30 #include <rtabmap/utilite/UStl.h>
31 
32 namespace rtabmap {
33 
34 GlobalMap::GlobalMap(const LocalGridCache * cache, const ParametersMap & parameters) :
35  cellSize_(Parameters::defaultGridCellSize()),
36  updateError_(Parameters::defaultGridGlobalUpdateError()),
37  occupancyThr_(Parameters::defaultGridGlobalOccupancyThr()),
38  logOddsHit_(logodds(Parameters::defaultGridGlobalProbHit())),
39  logOddsMiss_(logodds(Parameters::defaultGridGlobalProbMiss())),
40  logOddsClampingMin_(logodds(Parameters::defaultGridGlobalProbClampingMin())),
41  logOddsClampingMax_(logodds(Parameters::defaultGridGlobalProbClampingMax())),
42  cache_(cache)
43 {
44  UASSERT(cache_);
45 
46  minValues_[0] = minValues_[1] = minValues_[2] = 0.0;
47  maxValues_[0] = maxValues_[1] = maxValues_[2] = 0.0;
48 
49  Parameters::parse(parameters, Parameters::kGridCellSize(), cellSize_);
50  UASSERT(cellSize_>0.0f);
51 
52  Parameters::parse(parameters, Parameters::kGridGlobalUpdateError(), updateError_);
53 
54  UDEBUG("cellSize_ =%f", cellSize_);
55  UDEBUG("updateError_ =%f", updateError_);
56 
57  // Probabilistic parameters
58  Parameters::parse(parameters, Parameters::kGridGlobalOccupancyThr(), occupancyThr_);
59  if(Parameters::parse(parameters, Parameters::kGridGlobalProbHit(), logOddsHit_))
60  {
62  UASSERT_MSG(logOddsHit_ >= 0.0f, uFormat("probHit_=%f",logOddsHit_).c_str());
63  }
64  if(Parameters::parse(parameters, Parameters::kGridGlobalProbMiss(), logOddsMiss_))
65  {
67  UASSERT_MSG(logOddsMiss_ <= 0.0f, uFormat("probMiss_=%f",logOddsMiss_).c_str());
68  }
69  if(Parameters::parse(parameters, Parameters::kGridGlobalProbClampingMin(), logOddsClampingMin_))
70  {
72  }
73  if(Parameters::parse(parameters, Parameters::kGridGlobalProbClampingMax(), logOddsClampingMax_))
74  {
76  }
78 }
79 
81 {
82  clear();
83 }
84 
86 {
87  UDEBUG("Clearing");
88  addedNodes_.clear();
89  minValues_[0] = minValues_[1] = minValues_[2] = 0.0;
90  maxValues_[0] = maxValues_[1] = maxValues_[2] = 0.0;
91 }
92 
93 unsigned long GlobalMap::getMemoryUsed() const
94 {
95  unsigned long memoryUsage = 0;
96 
97  memoryUsage += addedNodes_.size()*(sizeof(int) + sizeof(Transform)+ sizeof(float)*12 + sizeof(std::map<int, Transform>::iterator)) + sizeof(std::map<int, Transform>);
98 
99  return memoryUsage;
100 }
101 
102 bool GlobalMap::update(const std::map<int, Transform> & poses)
103 {
104  UDEBUG("Update (poses=%d addedNodes_=%d)", (int)poses.size(), (int)addedNodes_.size());
105 
106  // First, check of the graph has changed. If so, re-create the octree by moving all occupied nodes.
107  bool graphOptimized = false; // If a loop closure happened (e.g., poses are modified)
108  bool graphChanged = addedNodes_.size()>0; // If the new map doesn't have any node from the previous map
109  float updateErrorSqrd = updateError_*updateError_;
110  for(std::map<int, Transform>::iterator iter=addedNodes_.begin(); iter!=addedNodes_.end(); ++iter)
111  {
112  std::map<int, Transform>::const_iterator jter = poses.find(iter->first);
113  if(jter != poses.end())
114  {
115  graphChanged = false;
116  UASSERT(!iter->second.isNull() && !jter->second.isNull());
117  if(iter->second.getDistanceSquared(jter->second) > updateErrorSqrd)
118  {
119  graphOptimized = true;
120  }
121  }
122  else
123  {
124  UDEBUG("Updated pose for node %d is not found, some points may not be copied. Use negative ids to just update cell values without adding new ones.", iter->first);
125  }
126  }
127 
128  if(graphOptimized || graphChanged)
129  {
130  // clear all but keep cache
131  clear();
132  }
133 
134  std::list<std::pair<int, Transform> > orderedPoses;
135 
136  // add old poses that were not in the current map (they were just retrieved from LTM)
137  for(std::map<int, Transform>::const_iterator iter=poses.lower_bound(1); iter!=poses.end(); ++iter)
138  {
139  if(!isNodeAssembled(iter->first))
140  {
141  UDEBUG("Pose %d not found in current added poses, it will be added to map", iter->first);
142  orderedPoses.push_back(*iter);
143  }
144  }
145 
146  // insert zero after
147  if(poses.find(0) != poses.end())
148  {
149  orderedPoses.push_back(std::make_pair(-1, poses.at(0)));
150  }
151 
152  if(!orderedPoses.empty())
153  {
154  assemble(orderedPoses);
155  }
156 
157  return !orderedPoses.empty();
158 }
159 
160 void GlobalMap::addAssembledNode(int id, const Transform & pose)
161 {
162  if(id > 0)
163  {
164  uInsert(addedNodes_, std::make_pair(id, pose));
165  }
166 }
167 
168 
169 } // namespace rtabmap
int
int
rtabmap::GlobalMap::addedNodes_
std::map< int, Transform > addedNodes_
Definition: GlobalMap.h:97
rtabmap::GlobalMap::logOddsHit_
float logOddsHit_
Definition: GlobalMap.h:87
rtabmap::GlobalMap::logodds
static float logodds(double probability)
Definition: GlobalMap.h:43
rtabmap::GlobalMap::clear
virtual void clear()
Definition: GlobalMap.cpp:85
rtabmap::GlobalMap::logOddsMiss_
float logOddsMiss_
Definition: GlobalMap.h:88
rtabmap::GlobalMap::getMemoryUsed
virtual unsigned long getMemoryUsed() const
Definition: GlobalMap.cpp:93
rtabmap::LocalGridCache
Definition: LocalGrid.h:56
rtabmap::ParametersMap
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
rtabmap::GlobalMap::cache_
const LocalGridCache * cache_
Definition: GlobalMap.h:96
rtabmap::Parameters::parse
static bool parse(const ParametersMap &parameters, const std::string &key, bool &value)
Definition: Parameters.cpp:500
GlobalMap.h
rtabmap::GlobalMap::isNodeAssembled
bool isNodeAssembled(int id)
Definition: GlobalMap.h:79
uInsert
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:441
rtabmap::GlobalMap::GlobalMap
GlobalMap(const LocalGridCache *cache, const ParametersMap &parameters=ParametersMap())
Definition: GlobalMap.cpp:34
rtabmap::GlobalMap::logOddsClampingMin_
float logOddsClampingMin_
Definition: GlobalMap.h:89
rtabmap::GlobalMap::update
bool update(const std::map< int, Transform > &poses)
Definition: GlobalMap.cpp:102
rtabmap::GlobalMap::addAssembledNode
void addAssembledNode(int id, const Transform &pose)
Definition: GlobalMap.cpp:160
rtabmap::GlobalMap::minValues_
double minValues_[3]
Definition: GlobalMap.h:92
rtabmap::GlobalMap::~GlobalMap
virtual ~GlobalMap()
Definition: GlobalMap.cpp:80
rtabmap::GlobalMap::occupancyThr_
float occupancyThr_
Definition: GlobalMap.h:86
rtabmap::GlobalMap::assemble
virtual void assemble(const std::list< std::pair< int, Transform > > &newPoses)=0
UASSERT
#define UASSERT(condition)
rtabmap::Parameters
Definition: Parameters.h:170
UASSERT_MSG
#define UASSERT_MSG(condition, msg_str)
Definition: ULogger.h:67
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
uFormat
std::string UTILITE_EXPORT uFormat(const char *fmt,...)
Definition: UConversion.cpp:365
ULogger.h
ULogger class and convenient macros.
rtabmap::Transform
Definition: Transform.h:41
rtabmap::GlobalMap::updateError_
float updateError_
Definition: GlobalMap.h:84
iter
iterator iter(handle obj)
c_str
const char * c_str(Args &&...args)
UStl.h
Wrappers of STL for convenient functions.
rtabmap::GlobalMap::maxValues_
double maxValues_[3]
Definition: GlobalMap.h:93
UDEBUG
#define UDEBUG(...)
rtabmap::GlobalMap::logOddsClampingMax_
float logOddsClampingMax_
Definition: GlobalMap.h:90
float
float
rtabmap::GlobalMap::cellSize_
float cellSize_
Definition: GlobalMap.h:83
rtabmap
Definition: CameraARCore.cpp:35


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:10