g2o
cache.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
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
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // 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 #ifndef G2O_CACHE_HH_
28 #define G2O_CACHE_HH_
29 
30 #include <map>
31 
32 #include "optimizable_graph.h"
33 #include "g2o_core_api.h"
34 
35 namespace g2o {
36 
37  class CacheContainer;
38 
40  {
41  public:
42  friend class CacheContainer;
44  {
45  public:
46  friend class CacheContainer;
47  CacheKey();
48  CacheKey(const std::string& type_, const ParameterVector& parameters_);
49 
50  bool operator<(const CacheKey& c) const;
51 
52  const std::string& type() const { return _type;}
53  const ParameterVector& parameters() const { return _parameters;}
54 
55  protected:
56  std::string _type;
58  };
59 
60  Cache(CacheContainer* container_ = 0, const ParameterVector& parameters_ = ParameterVector());
61 
62  CacheKey key() const;
63 
64  OptimizableGraph::Vertex* vertex();
66  CacheContainer* container();
67  ParameterVector& parameters();
68 
69  void update();
70 
71  virtual HyperGraph::HyperGraphElementType elementType() const { return HyperGraph::HGET_CACHE;}
72 
73  protected:
75  virtual void updateImpl() = 0;
76 
88  Cache* installDependency(const std::string& type_, const std::vector<int>& parameterIndices);
89 
96  virtual bool resolveDependancies();
97 
100  std::vector<Cache*> _parentCaches;
102  };
103 
104  class G2O_CORE_API CacheContainer: public std::map<Cache::CacheKey, Cache*>
105  {
106  public:
108  virtual ~CacheContainer();
109  OptimizableGraph::Vertex* vertex();
111  Cache* findCache(const Cache::CacheKey& key);
112  Cache* createCache(const Cache::CacheKey& key);
113  void setUpdateNeeded(bool needUpdate=true);
114  void update();
115  protected:
118  };
119 
120 
121  template <typename CacheType>
122  void OptimizableGraph::Edge::resolveCache(CacheType*& cache,
124  const std::string& type_,
125  const ParameterVector& parameters_)
126  {
127  cache = 0;
128  CacheContainer* container= v->cacheContainer();
129  Cache::CacheKey key(type_, parameters_);
130  Cache* c = container->findCache(key);
131  if (!c) {
132  c = container->createCache(key);
133  }
134  if (c) {
135  cache = dynamic_cast<CacheType*>(c);
136  }
137  }
138 
139 } // end namespace
140 
141 #endif
bool _updateNeeded
Definition: cache.h:98
ParameterVector _parameters
Definition: cache.h:99
OptimizableGraph::Vertex * _vertex
Definition: cache.h:116
CacheContainer * _container
Definition: cache.h:101
Cache * findCache(const Cache::CacheKey &key)
Definition: cache.cpp:124
bool _updateNeeded
Definition: cache.h:117
ParameterVector _parameters
Definition: cache.h:57
void resolveCache(CacheType *&cache, OptimizableGraph::Vertex *, const std::string &_type, const ParameterVector &parameters)
Definition: cache.h:122
std::vector< Parameter * > ParameterVector
Definition: parameter.h:52
virtual HyperGraph::HyperGraphElementType elementType() const
Definition: cache.h:71
A general case Vertex for optimization.
const std::string & type() const
Definition: cache.h:52
bool operator<(const HyperDijkstra::AdjacencyMapEntry &a, const HyperDijkstra::AdjacencyMapEntry &b)
Cache * createCache(const Cache::CacheKey &key)
Definition: cache.cpp:131
std::vector< Cache * > _parentCaches
Definition: cache.h:100
const ParameterVector & parameters() const
Definition: cache.h:53
HGET_CACHE
Definition: hyper_graph.h:63
std::string _type
Definition: cache.h:56
#define G2O_CORE_API
Definition: g2o_core_api.h:29
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the graph
Definition: protocol.txt:7