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