g2o
Loading...
Searching...
No Matches
cache.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 G. Grisetti, R. Kuemmerle, 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#include "cache.h"
28
29#include <iostream>
30
31#include "factory.h"
32#include "g2o/stuff/logger.h"
33#include "optimizable_graph.h"
34
35namespace g2o {
36using namespace std;
37
38Cache::CacheKey::CacheKey() : _type(), _parameters() {}
39
40Cache::CacheKey::CacheKey(const std::string& type_,
41 const ParameterVector& parameters_)
42 : _type(type_), _parameters(parameters_) {}
43
44Cache::Cache(CacheContainer* container_, const ParameterVector& parameters_)
45 : _updateNeeded(true), _parameters(parameters_), _container(container_) {}
46
48 if (_type < c._type)
49 return true;
50 else if (c._type < _type)
51 return false;
52 return std::lexicographical_compare(_parameters.begin(), _parameters.end(),
53 c._parameters.begin(),
54 c._parameters.end());
55}
56
58 if (container()) return container()->vertex();
59 return nullptr;
60}
61
63 if (container()) return container()->graph();
64 return nullptr;
65}
66
68
70
72 Factory* factory = Factory::instance();
73 return CacheKey(factory->tag(this), _parameters);
74};
75
77 if (!_updateNeeded) return;
78 for (std::vector<Cache*>::iterator it = _parentCaches.begin();
79 it != _parentCaches.end(); ++it) {
80 (*it)->update();
81 }
82 updateImpl();
83 _updateNeeded = false;
84}
85
86Cache* Cache::installDependency(const std::string& type_,
87 const std::vector<int>& parameterIndices) {
88 ParameterVector pv(parameterIndices.size());
89 for (size_t i = 0; i < parameterIndices.size(); i++) {
90 if (parameterIndices[i] < 0 ||
91 parameterIndices[i] >= (int)_parameters.size())
92 return nullptr;
93 pv[i] = _parameters[parameterIndices[i]];
94 }
95 CacheKey k(type_, pv);
96 if (!container()) return nullptr;
97 Cache* c = container()->findCache(k);
98 if (!c) {
99 c = container()->createCache(k);
100 }
101 if (c) _parentCaches.push_back(c);
102 return c;
103}
104
105bool Cache::resolveDependencies() { return true; }
106
111
113 iterator it = find(key);
114 if (it == end()) return nullptr;
115 return it->second;
116}
117
121 if (!e) {
123 G2O_ERROR("fatal error in creating cache of type {}", key.type());
124 return nullptr;
125 }
126 Cache* c = dynamic_cast<Cache*>(e);
127 if (!c) {
129 G2O_ERROR("fatal error in creating cache of type {}, wrong type",
130 key.type());
131 return nullptr;
132 }
133 c->_container = this;
134 c->_parameters = key._parameters;
135 if (c->resolveDependencies()) {
136 insert(make_pair(key, c));
137 c->update();
138 return c;
139 }
140 return nullptr;
141}
142
144
146 if (_vertex) return _vertex->graph();
147 return nullptr;
148}
149
151 for (iterator it = begin(); it != end(); ++it) {
152 (it->second)->update();
153 }
154 _updateNeeded = false;
155}
156
157void CacheContainer::setUpdateNeeded(bool needUpdate) {
158 _updateNeeded = needUpdate;
159 for (iterator it = begin(); it != end(); ++it) {
160 (it->second)->_updateNeeded = needUpdate;
161 }
162}
163
165 for (iterator it = begin(); it != end(); ++it) {
166 delete (it->second);
167 }
168}
169
170} // namespace g2o
Cache * createCache(const Cache::CacheKey &key)
Definition cache.cpp:118
void setUpdateNeeded(bool needUpdate=true)
Definition cache.cpp:157
Cache * findCache(const Cache::CacheKey &key)
Definition cache.cpp:112
virtual ~CacheContainer()
Definition cache.cpp:164
OptimizableGraph * graph()
Definition cache.cpp:145
OptimizableGraph::Vertex * _vertex
Definition cache.h:120
OptimizableGraph::Vertex * vertex()
Definition cache.cpp:143
CacheContainer(OptimizableGraph::Vertex *vertex_)
Definition cache.cpp:107
bool operator<(const CacheKey &c) const
Definition cache.cpp:47
ParameterVector _parameters
Definition cache.h:55
const std::string & type() const
Definition cache.h:50
std::string _type
Definition cache.h:54
CacheContainer * _container
Definition cache.h:105
std::vector< Cache * > _parentCaches
Definition cache.h:104
ParameterVector & parameters()
Definition cache.cpp:69
OptimizableGraph * graph()
Definition cache.cpp:62
CacheContainer * container()
Definition cache.cpp:67
bool _updateNeeded
Definition cache.h:102
CacheKey key() const
Definition cache.cpp:71
Cache * installDependency(const std::string &type_, const std::vector< int > &parameterIndices)
Definition cache.cpp:86
virtual bool resolveDependencies()
Definition cache.cpp:105
void update()
Definition cache.cpp:76
OptimizableGraph::Vertex * vertex()
Definition cache.cpp:57
ParameterVector _parameters
Definition cache.h:103
Cache(CacheContainer *container_=0, const ParameterVector &parameters_=ParameterVector())
Definition cache.cpp:44
virtual void updateImpl()=0
redefine this to do the update
create vertices and edges based on TAGs in, for example, a file
Definition factory.h:48
static Factory * instance()
return the instance
Definition factory.cpp:46
HyperGraph::HyperGraphElement * construct(const std::string &tag) const
Definition factory.cpp:129
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition factory.cpp:138
A general case Vertex for optimization.
const OptimizableGraph * graph() const
#define G2O_ERROR(...)
Definition logger.h:89
#define __PRETTY_FUNCTION__
Definition macros.h:90
std::vector< Parameter * > ParameterVector
Definition parameter.h:54
Definition jet.h:876