GSLAM  3.0.0
HashMap.h
1 // GSLAM - A general SLAM framework and benchmark
2 // Copyright 2018 PILAB Inc. All rights reserved.
3 // https://github.com/zdzhaoyong/GSLAM
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 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: zd5945@126.com (Yong Zhao)
30 //
31 // HashMap is a simple implementation of Map
32 
33 #ifndef GSLAM_CORE_HASHMAP_H_
34 #define GSLAM_CORE_HASHMAP_H_
35 #include <string>
36 #include <unordered_map>
37 #include <utility>
38 #include "GSLAM.h"
39 
40 namespace GSLAM {
41 
42 typedef std::unordered_map<PointID, PointPtr> PointMap;
43 typedef std::unordered_map<FrameID, FramePtr> FrameMap;
44 typedef PointMap::iterator PointIt;
45 typedef FrameMap::iterator FrameIt;
46 typedef PointMap::const_iterator ConstPointIt;
47 typedef FrameMap::const_iterator ConstFrameIt;
48 
49 class HashMap : public Map {
50  public:
51  HashMap() {}
52 
53  virtual ~HashMap() {}
54 
55  virtual std::string type() const { return "HashMap"; }
56 
57  /// MapFrame & MapPoint interface
58  virtual bool insertMapPoint(const PointPtr& point);
59  virtual bool insertMapFrame(const FramePtr& frame);
60  virtual bool eraseMapPoint(const PointID& pointId);
61  virtual bool eraseMapFrame(const FrameID& frameId);
62  virtual void clear();
63 
64  virtual std::size_t frameNum() const;
65  virtual std::size_t pointNum() const;
66 
67  virtual FramePtr getFrame(const FrameID& id) const;
68  virtual PointPtr getPoint(const PointID& id) const;
69  virtual bool getFrames(FrameArray& frames) const;
70  virtual bool getPoints(PointArray& points) const;
71 
72  /// Save or load the map from/to the file
73  virtual bool save(std::string path) const;
74  virtual bool load(std::string path);
75 
76  private:
77  PointMap _points;
78  FrameMap _frames;
79  mutable MutexRW _mutexPoints, _mutexFrames;
80 };
81 
82 inline bool HashMap::insertMapPoint(const PointPtr& point) {
83  WriteMutex lock(_mutexPoints);
84  PointIt it = _points.find(point->id());
85  if (it != _points.end()) return false;
86  _points.insert(make_pair(point->id(), point));
87  return true;
88 }
89 
90 inline bool HashMap::insertMapFrame(const FramePtr& frame) {
91  WriteMutex lock(_mutexFrames);
92  FrameIt it = _frames.find(frame->id());
93  if (it != _frames.end()) return false;
94  _frames.insert(make_pair(frame->id(), frame));
95  return true;
96 }
97 
98 inline bool HashMap::eraseMapPoint(const PointID& pointId) {
99  WriteMutex lock(_mutexPoints);
100  return _points.erase(pointId) != 0;
101 }
102 
103 inline bool HashMap::eraseMapFrame(const FrameID& frameId) {
104  WriteMutex lock(_mutexFrames);
105  return _frames.erase(frameId) != 0;
106 }
107 
108 inline void HashMap::clear() {
109  WriteMutex lock1(_mutexFrames);
110  WriteMutex lock2(_mutexPoints);
111  _points.clear();
112  _frames.clear();
113 }
114 
115 inline std::size_t HashMap::frameNum() const {
116  ReadMutex lock(_mutexFrames);
117  return _frames.size();
118 }
119 
120 inline std::size_t HashMap::pointNum() const {
121  ReadMutex lock(_mutexPoints);
122  return _points.size();
123 }
124 
125 inline FramePtr HashMap::getFrame(const FrameID& id) const {
126  ReadMutex lock(_mutexFrames);
127  ConstFrameIt it = _frames.find(id);
128  if (it != _frames.end())
129  return it->second;
130  else
131  return FramePtr();
132 }
133 
134 inline PointPtr HashMap::getPoint(const PointID& id) const {
135  ReadMutex lock(_mutexPoints);
136  ConstPointIt it = _points.find(id);
137  if (it != _points.end())
138  return it->second;
139  else
140  return PointPtr();
141 }
142 
143 inline bool HashMap::getFrames(FrameArray& frames) const {
144  ReadMutex lock(_mutexFrames);
145  frames.clear();
146  frames.reserve(_frames.size());
147  for (const std::pair<FrameID, FramePtr>& fr : _frames)
148  frames.push_back(fr.second);
149  return true;
150 }
151 
152 inline bool HashMap::getPoints(PointArray& points) const {
153  ReadMutex lock(_mutexPoints);
154  points.clear();
155  points.reserve(_points.size());
156  for (const std::pair<PointID, PointPtr>& pt : _points)
157  points.push_back(pt.second);
158  return true;
159 }
160 
161 /// Save or load the map from/to the file
162 inline bool HashMap::save(std::string path) const { return false; }
163 
164 inline bool HashMap::load(std::string path) { return false; }
165 } // namespace GSLAM
166 #endif // GSLAM_CORE_HASHMAP_H_
Definition: Map.h:397
virtual bool save(std::string path) const
Save or load the map from/to the file.
Definition: HashMap.h:162
Definition: Camera.h:45
Definition: HashMap.h:49
virtual bool insertMapPoint(const PointPtr &point)
MapFrame & MapPoint interface.
Definition: HashMap.h:82