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


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47