LocalGrid.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 LocalGrid::LocalGrid(const cv::Mat & groundIn,
35  const cv::Mat & obstaclesIn,
36  const cv::Mat & emptyIn,
37  float cellSizeIn,
38  const cv::Point3f & viewPointIn) :
39  groundCells(groundIn),
40  obstacleCells(obstaclesIn),
41  emptyCells(emptyIn),
42  cellSize(cellSizeIn),
43  viewPoint(viewPointIn)
44 {
45  UASSERT(cellSize > 0.0f);
46 }
47 
48 bool LocalGrid::is3D() const
49 {
50  return (groundCells.empty() || groundCells.type() == CV_32FC3 || groundCells.type() == CV_32FC(4) || groundCells.type() == CV_32FC(6)) &&
51  (obstacleCells.empty() || obstacleCells.type() == CV_32FC3 || obstacleCells.type() == CV_32FC(4) || obstacleCells.type() == CV_32FC(6)) &&
52  (emptyCells.empty() || emptyCells.type() == CV_32FC3 || emptyCells.type() == CV_32FC(4) || emptyCells.type() == CV_32FC(6));
53 }
54 
55 void LocalGridCache::add(int nodeId,
56  const cv::Mat & ground,
57  const cv::Mat & obstacles,
58  const cv::Mat & empty,
59  float cellSize,
60  const cv::Point3f & viewPoint)
61 {
62  add(nodeId, LocalGrid(ground, obstacles, empty, cellSize, viewPoint));
63 }
64 
65 void LocalGridCache::add(int nodeId, const LocalGrid & localGrid)
66 {
67  UDEBUG("nodeId=%d (ground=%d/%d obstacles=%d/%d empty=%d/%d)",
68  nodeId, localGrid.groundCells.cols, localGrid.groundCells.channels(), localGrid.obstacleCells.cols, localGrid.obstacleCells.channels(), localGrid.emptyCells.cols, localGrid.emptyCells.channels());
69  if(nodeId < 0)
70  {
71  UWARN("Cannot add nodes with negative id (nodeId=%d)", nodeId);
72  return;
73  }
74  uInsert(localGrids_, std::make_pair(nodeId==0?-1:nodeId, localGrid));
75 }
76 
77 bool LocalGridCache::shareTo(int nodeId, LocalGridCache & anotherCache) const
78 {
79  if(uContains(localGrids_, nodeId) && !uContains(anotherCache.localGrids(), nodeId))
80  {
81  const LocalGrid & localGrid = localGrids_.at(nodeId);
82  anotherCache.add(nodeId, localGrid.groundCells, localGrid.obstacleCells, localGrid.emptyCells, localGrid.cellSize, localGrid.viewPoint);
83  return true;
84  }
85  return false;
86 }
87 
88 unsigned long LocalGridCache::getMemoryUsed() const
89 {
90  unsigned long memoryUsage = 0;
91  memoryUsage += localGrids_.size()*(sizeof(int) + sizeof(LocalGrid) + sizeof(std::map<int, LocalGrid>::iterator)) + sizeof(std::map<int, LocalGrid>);
92  for(std::map<int, LocalGrid>::const_iterator iter=localGrids_.begin(); iter!=localGrids_.end(); ++iter)
93  {
94  memoryUsage += iter->second.groundCells.total() * iter->second.groundCells.elemSize();
95  memoryUsage += iter->second.obstacleCells.total() * iter->second.obstacleCells.elemSize();
96  memoryUsage += iter->second.emptyCells.total() * iter->second.emptyCells.elemSize();
97  memoryUsage += sizeof(int);
98  memoryUsage += sizeof(cv::Point3f);
99  }
100  return memoryUsage;
101 }
102 
103 void LocalGridCache::clear(bool temporaryOnly)
104 {
105  if(temporaryOnly)
106  {
107  //clear only negative ids
108  for(std::map<int, LocalGrid>::iterator iter=localGrids_.begin(); iter!=localGrids_.end();)
109  {
110  if(iter->first < 0)
111  {
112  localGrids_.erase(iter++);
113  }
114  else
115  {
116  break;
117  }
118  }
119  }
120  else
121  {
122  localGrids_.clear();
123  }
124 }
125 
126 } // namespace rtabmap
int
int
rtabmap::LocalGridCache::getMemoryUsed
unsigned long getMemoryUsed() const
Definition: LocalGrid.cpp:88
rtabmap::LocalGrid::obstacleCells
cv::Mat obstacleCells
Definition: LocalGrid.h:50
rtabmap::LocalGridCache::shareTo
bool shareTo(int nodeId, LocalGridCache &anotherCache) const
Definition: LocalGrid.cpp:77
rtabmap::LocalGridCache
Definition: LocalGrid.h:56
rtabmap::LocalGridCache::localGrids
const std::map< int, LocalGrid > & localGrids() const
Definition: LocalGrid.h:78
rtabmap::LocalGrid::groundCells
cv::Mat groundCells
Definition: LocalGrid.h:49
GlobalMap.h
rtabmap::LocalGrid
Definition: LocalGrid.h:38
uInsert
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:441
uContains
bool uContains(const std::list< V > &list, const V &value)
Definition: UStl.h:407
rtabmap::LocalGrid::emptyCells
cv::Mat emptyCells
Definition: LocalGrid.h:51
rtabmap::LocalGridCache::clear
void clear(bool temporaryOnly=false)
Definition: LocalGrid.cpp:103
rtabmap::LocalGrid::is3D
bool is3D() const
Definition: LocalGrid.cpp:48
UASSERT
#define UASSERT(condition)
rtabmap::LocalGridCache::add
void add(int nodeId, const cv::Mat &ground, const cv::Mat &obstacles, const cv::Mat &empty, float cellSize, const cv::Point3f &viewPoint=cv::Point3f(0, 0, 0))
Definition: LocalGrid.cpp:55
UWARN
#define UWARN(...)
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
rtabmap::LocalGridCache::localGrids_
std::map< int, LocalGrid > localGrids_
Definition: LocalGrid.h:85
ULogger.h
ULogger class and convenient macros.
empty
iter
iterator iter(handle obj)
rtabmap::LocalGrid::LocalGrid
LocalGrid(const cv::Mat &ground, const cv::Mat &obstacles, const cv::Mat &empty, float cellSize, const cv::Point3f &viewPoint=cv::Point3f(0, 0, 0))
Definition: LocalGrid.cpp:34
UStl.h
Wrappers of STL for convenient functions.
UDEBUG
#define UDEBUG(...)
rtabmap::LocalGrid::viewPoint
cv::Point3f viewPoint
Definition: LocalGrid.h:53
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::LocalGrid::cellSize
float cellSize
Definition: LocalGrid.h:52


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