g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
core
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
35
namespace
g2o
{
36
using namespace
std;
37
38
Cache::CacheKey::CacheKey
() :
_type
(),
_parameters
() {}
39
40
Cache::CacheKey::CacheKey
(
const
std::string& type_,
41
const
ParameterVector
& parameters_)
42
:
_type
(type_),
_parameters
(parameters_) {}
43
44
Cache::Cache
(
CacheContainer
* container_,
const
ParameterVector
& parameters_)
45
:
_updateNeeded
(true),
_parameters
(parameters_),
_container
(container_) {}
46
47
bool
Cache::CacheKey::operator<
(
const
Cache::CacheKey
& c)
const
{
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
57
OptimizableGraph::Vertex
*
Cache::vertex
() {
58
if
(
container
())
return
container
()->
vertex
();
59
return
nullptr
;
60
}
61
62
OptimizableGraph
*
Cache::graph
() {
63
if
(
container
())
return
container
()->
graph
();
64
return
nullptr
;
65
}
66
67
CacheContainer
*
Cache::container
() {
return
_container
; }
68
69
ParameterVector
&
Cache::parameters
() {
return
_parameters
; }
70
71
Cache::CacheKey
Cache::key
()
const
{
72
Factory
* factory =
Factory::instance
();
73
return
CacheKey
(factory->
tag
(
this
),
_parameters
);
74
};
75
76
void
Cache::update
() {
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
86
Cache
*
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
105
bool
Cache::resolveDependencies
() {
return
true
; }
106
107
CacheContainer::CacheContainer
(
OptimizableGraph::Vertex
* vertex_)
108
:
_updateNeeded
(true) {
109
_vertex
= vertex_;
110
}
111
112
Cache
*
CacheContainer::findCache
(
const
Cache::CacheKey
& key) {
113
iterator it = find(key);
114
if
(it == end())
return
nullptr
;
115
return
it->second;
116
}
117
118
Cache
*
CacheContainer::createCache
(
const
Cache::CacheKey
& key) {
119
Factory
* f =
Factory::instance
();
120
HyperGraph::HyperGraphElement
* e = f->
construct
(key.
type
());
121
if
(!e) {
122
G2O_ERROR
(
"{}"
,
__PRETTY_FUNCTION__
);
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) {
128
G2O_ERROR
(
"{}"
,
__PRETTY_FUNCTION__
);
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
143
OptimizableGraph::Vertex
*
CacheContainer::vertex
() {
return
_vertex
; }
144
145
OptimizableGraph
*
CacheContainer::graph
() {
146
if
(
_vertex
)
return
_vertex
->graph();
147
return
nullptr
;
148
}
149
150
void
CacheContainer::update
() {
151
for
(iterator it = begin(); it != end(); ++it) {
152
(it->second)->
update
();
153
}
154
_updateNeeded
=
false
;
155
}
156
157
void
CacheContainer::setUpdateNeeded
(
bool
needUpdate) {
158
_updateNeeded
= needUpdate;
159
for
(iterator it = begin(); it != end(); ++it) {
160
(it->second)->
_updateNeeded
= needUpdate;
161
}
162
}
163
164
CacheContainer::~CacheContainer
() {
165
for
(iterator it = begin(); it != end(); ++it) {
166
delete
(it->second);
167
}
168
}
169
170
}
// namespace g2o
cache.h
g2o::CacheContainer::createCache
Cache * createCache(const Cache::CacheKey &key)
Definition
cache.cpp:118
g2o::CacheContainer::setUpdateNeeded
void setUpdateNeeded(bool needUpdate=true)
Definition
cache.cpp:157
g2o::CacheContainer::findCache
Cache * findCache(const Cache::CacheKey &key)
Definition
cache.cpp:112
g2o::CacheContainer::~CacheContainer
virtual ~CacheContainer()
Definition
cache.cpp:164
g2o::CacheContainer::graph
OptimizableGraph * graph()
Definition
cache.cpp:145
g2o::CacheContainer::_updateNeeded
bool _updateNeeded
Definition
cache.h:121
g2o::CacheContainer::_vertex
OptimizableGraph::Vertex * _vertex
Definition
cache.h:120
g2o::CacheContainer::update
void update()
Definition
cache.cpp:150
g2o::CacheContainer::vertex
OptimizableGraph::Vertex * vertex()
Definition
cache.cpp:143
g2o::CacheContainer::CacheContainer
CacheContainer(OptimizableGraph::Vertex *vertex_)
Definition
cache.cpp:107
g2o::Cache::CacheKey
Definition
cache.h:42
g2o::Cache::CacheKey::operator<
bool operator<(const CacheKey &c) const
Definition
cache.cpp:47
g2o::Cache::CacheKey::_parameters
ParameterVector _parameters
Definition
cache.h:55
g2o::Cache::CacheKey::CacheKey
CacheKey()
Definition
cache.cpp:38
g2o::Cache::CacheKey::type
const std::string & type() const
Definition
cache.h:50
g2o::Cache::CacheKey::_type
std::string _type
Definition
cache.h:54
g2o::Cache
Definition
cache.h:39
g2o::Cache::_container
CacheContainer * _container
Definition
cache.h:105
g2o::Cache::_parentCaches
std::vector< Cache * > _parentCaches
Definition
cache.h:104
g2o::Cache::parameters
ParameterVector & parameters()
Definition
cache.cpp:69
g2o::Cache::graph
OptimizableGraph * graph()
Definition
cache.cpp:62
g2o::Cache::container
CacheContainer * container()
Definition
cache.cpp:67
g2o::Cache::_updateNeeded
bool _updateNeeded
Definition
cache.h:102
g2o::Cache::key
CacheKey key() const
Definition
cache.cpp:71
g2o::Cache::installDependency
Cache * installDependency(const std::string &type_, const std::vector< int > ¶meterIndices)
Definition
cache.cpp:86
g2o::Cache::CacheContainer
friend class CacheContainer
Definition
cache.h:41
g2o::Cache::resolveDependencies
virtual bool resolveDependencies()
Definition
cache.cpp:105
g2o::Cache::update
void update()
Definition
cache.cpp:76
g2o::Cache::vertex
OptimizableGraph::Vertex * vertex()
Definition
cache.cpp:57
g2o::Cache::_parameters
ParameterVector _parameters
Definition
cache.h:103
g2o::Cache::Cache
Cache(CacheContainer *container_=0, const ParameterVector ¶meters_=ParameterVector())
Definition
cache.cpp:44
g2o::Cache::updateImpl
virtual void updateImpl()=0
redefine this to do the update
g2o::Factory
create vertices and edges based on TAGs in, for example, a file
Definition
factory.h:48
g2o::Factory::instance
static Factory * instance()
return the instance
Definition
factory.cpp:46
g2o::Factory::construct
HyperGraph::HyperGraphElement * construct(const std::string &tag) const
Definition
factory.cpp:129
g2o::Factory::tag
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition
factory.cpp:138
g2o::OptimizableGraph::Vertex
A general case Vertex for optimization.
Definition
optimizable_graph.h:108
factory.h
G2O_ERROR
#define G2O_ERROR(...)
logger.h
__PRETTY_FUNCTION__
#define __PRETTY_FUNCTION__
Definition
macros.h:90
g2o
Definition
dl_wrapper.cpp:55
g2o::ParameterVector
std::vector< Parameter * > ParameterVector
Definition
parameter.h:54
optimizable_graph.h
g2o::HyperGraph::HyperGraphElement
Definition
hyper_graph.h:80
g2o::OptimizableGraph
Definition
optimizable_graph.h:62
Generated on
for g2o by
1.17.0