g2o
Loading...
Searching...
No Matches
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 "g2o_core_api.h"
33#include "optimizable_graph.h"
34
35namespace g2o {
36
37class CacheContainer;
38
40 public:
41 friend class CacheContainer;
43 public:
44 friend class CacheContainer;
45 CacheKey();
46 CacheKey(const std::string& type_, const ParameterVector& parameters_);
47
48 bool operator<(const CacheKey& c) const;
49
50 const std::string& type() const { return _type; }
51 const ParameterVector& parameters() const { return _parameters; }
52
53 protected:
54 std::string _type;
56 };
57
58 Cache(CacheContainer* container_ = 0,
59 const ParameterVector& parameters_ = ParameterVector());
60
61 CacheKey key() const;
62
64 OptimizableGraph* graph();
65 CacheContainer* container();
66 ParameterVector& parameters();
67
68 void update();
69
70 virtual HyperGraph::HyperGraphElementType elementType() const {
71 return HyperGraph::HGET_CACHE;
72 }
73
74 protected:
76 virtual void updateImpl() = 0;
77
90 Cache* installDependency(const std::string& type_,
91 const std::vector<int>& parameterIndices);
92
100 virtual bool resolveDependencies();
101
104 std::vector<Cache*> _parentCaches;
106};
107
108class G2O_CORE_API CacheContainer : public std::map<Cache::CacheKey, Cache*> {
109 public:
111 virtual ~CacheContainer();
112 OptimizableGraph::Vertex* vertex();
113 OptimizableGraph* graph();
114 Cache* findCache(const Cache::CacheKey& key);
115 Cache* createCache(const Cache::CacheKey& key);
116 void setUpdateNeeded(bool needUpdate = true);
117 void update();
118
119 protected:
122};
123
124template <typename CacheType>
127 const std::string& type_,
128 const ParameterVector& parameters_) {
129 cache = 0;
130 CacheContainer* container = v->cacheContainer();
131 Cache::CacheKey key(type_, parameters_);
132 Cache* c = container->findCache(key);
133 if (!c) {
134 c = container->createCache(key);
135 }
136 if (c) {
137 cache = dynamic_cast<CacheType*>(c);
138 }
139}
140
141} // namespace g2o
142
143#endif
Cache * createCache(const Cache::CacheKey &key)
Definition cache.cpp:118
Cache * findCache(const Cache::CacheKey &key)
Definition cache.cpp:112
OptimizableGraph::Vertex * _vertex
Definition cache.h:120
ParameterVector _parameters
Definition cache.h:55
const std::string & type() const
Definition cache.h:50
std::string _type
Definition cache.h:54
const ParameterVector & parameters() const
Definition cache.h:51
CacheContainer * _container
Definition cache.h:105
std::vector< Cache * > _parentCaches
Definition cache.h:104
bool _updateNeeded
Definition cache.h:102
virtual HyperGraph::HyperGraphElementType elementType() const
Definition cache.h:70
ParameterVector _parameters
Definition cache.h:103
virtual void updateImpl()=0
redefine this to do the update
void resolveCache(CacheType *&cache, OptimizableGraph::Vertex *, const std::string &_type, const ParameterVector &parameters)
Definition cache.h:125
A general case Vertex for optimization.
#define G2O_CORE_API
bool operator<(const HyperDijkstra::AdjacencyMapEntry &a, const HyperDijkstra::AdjacencyMapEntry &b)
std::vector< Parameter * > ParameterVector
Definition parameter.h:54