g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
core
parameter_container.cpp
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
#include "
parameter_container.h
"
28
29
#include <cassert>
30
#include <iostream>
31
32
#include "
factory.h
"
33
#include "
g2o/stuff/color_macros.h
"
34
#include "
g2o/stuff/logger.h
"
35
#include "
g2o/stuff/macros.h
"
36
#include "
g2o/stuff/string_tools.h
"
37
#include "
parameter.h
"
38
39
namespace
g2o
{
40
41
using namespace
std;
42
43
ParameterContainer::ParameterContainer
(
bool
isMainStorage_)
44
:
_isMainStorage
(isMainStorage_) {}
45
46
void
ParameterContainer::clear
() {
47
if
(!
_isMainStorage
)
return
;
48
for
(iterator it = begin(); it != end(); ++it) {
49
delete
it->second;
50
}
51
BaseClass::clear();
52
}
53
54
ParameterContainer::~ParameterContainer
() {
clear
(); }
55
56
bool
ParameterContainer::addParameter
(
Parameter
* p) {
57
if
(p->
id
() < 0)
return
false
;
58
iterator it = find(p->
id
());
59
if
(it != end())
return
false
;
60
insert(make_pair(p->
id
(), p));
61
return
true
;
62
}
63
64
Parameter
*
ParameterContainer::getParameter
(
int
id
) {
65
iterator it = find(
id
);
66
if
(it == end())
return
nullptr
;
67
return
it->second;
68
}
69
70
const
Parameter
*
ParameterContainer::getParameter
(
int
id
)
const
{
71
const_iterator it = find(
id
);
72
if
(it == end())
return
nullptr
;
73
return
it->second;
74
}
75
76
Parameter
*
ParameterContainer::detachParameter
(
int
id
) {
77
iterator it = find(
id
);
78
if
(it == end())
return
nullptr
;
79
Parameter
* p = it->second;
80
erase(it);
81
return
p;
82
}
83
84
bool
ParameterContainer::write
(std::ostream& os)
const
{
85
Factory
* factory =
Factory::instance
();
86
for
(const_iterator it = begin(); it != end(); ++it) {
87
os << factory->
tag
(it->second) <<
" "
;
88
os << it->second->id() <<
" "
;
89
it->second->write(os);
90
os << endl;
91
}
92
return
true
;
93
}
94
95
bool
ParameterContainer::read
(
96
std::istream& is,
97
const
std::map<std::string, std::string>* _renamedTypesLookup) {
98
stringstream currentLine;
99
string
token
;
100
101
Factory
* factory =
Factory::instance
();
102
HyperGraph::GraphElemBitset
elemBitset;
103
elemBitset[HyperGraph::HGET_PARAMETER] = 1;
104
105
while
(1) {
106
int
bytesRead =
readLine
(is, currentLine);
107
if
(bytesRead == -1)
break
;
108
currentLine >>
token
;
109
if
(bytesRead == 0 ||
token
.size() == 0 ||
token
[0] ==
'#'
)
continue
;
110
if
(_renamedTypesLookup && _renamedTypesLookup->size() > 0) {
111
map<string, string>::const_iterator foundIt =
112
_renamedTypesLookup->find(
token
);
113
if
(foundIt != _renamedTypesLookup->end()) {
114
token
= foundIt->second;
115
}
116
}
117
118
HyperGraph::HyperGraphElement
* element =
119
factory->
construct
(
token
, elemBitset);
120
if
(!element)
// not a parameter or otherwise unknown tag
121
continue
;
122
assert(element->
elementType
() == HyperGraph::HGET_PARAMETER &&
123
"Should be a param"
);
124
125
Parameter
* p =
static_cast<
Parameter
*
>
(element);
126
int
pid;
127
currentLine >> pid;
128
p->
setId
(pid);
129
bool
r = p->
read
(currentLine);
130
if
(!r) {
131
G2O_ERROR
(
"{}: Error reading data {} for parameter {}"
,
132
__PRETTY_FUNCTION__
,
token
, pid);
133
delete
p;
134
}
else
{
135
if
(!
addParameter
(p)) {
136
G2O_ERROR
(
"{}: Parameter of type: {} id: {} already defined"
,
137
__PRETTY_FUNCTION__
,
token
, pid);
138
}
139
}
140
}
// while read line
141
142
return
true
;
143
}
144
145
}
// namespace g2o
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::HyperGraph::GraphElemBitset
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition
hyper_graph.h:70
g2o::ParameterContainer::write
virtual bool write(std::ostream &os) const
write the data to a stream
Definition
parameter_container.cpp:84
g2o::ParameterContainer::~ParameterContainer
virtual ~ParameterContainer()
Definition
parameter_container.cpp:54
g2o::ParameterContainer::_isMainStorage
bool _isMainStorage
Definition
parameter_container.h:72
g2o::ParameterContainer::ParameterContainer
ParameterContainer(bool isMainStorage_=true)
Definition
parameter_container.cpp:43
g2o::ParameterContainer::detachParameter
Parameter * detachParameter(int id)
remove a parameter from the container, i.e., the user now owns the pointer
Definition
parameter_container.cpp:76
g2o::ParameterContainer::addParameter
bool addParameter(Parameter *p)
add parameter to the container
Definition
parameter_container.cpp:56
g2o::ParameterContainer::getParameter
Parameter * getParameter(int id)
return a parameter based on its ID
Definition
parameter_container.cpp:64
g2o::ParameterContainer::read
virtual bool read(std::istream &is, const std::map< std::string, std::string > *renamedMap=0)
read parameters from a stream
Definition
parameter_container.cpp:95
g2o::ParameterContainer::clear
void clear()
Definition
parameter_container.cpp:46
g2o::Parameter
Definition
parameter.h:36
g2o::Parameter::id
int id() const
Definition
parameter.h:44
g2o::Parameter::setId
void setId(int id_)
Definition
parameter.cpp:33
g2o::Parameter::read
virtual bool read(std::istream &is)=0
read the data from a stream
color_macros.h
factory.h
token
SlamParser::Parser::token token
Definition
flex_scanner.cpp:428
G2O_ERROR
#define G2O_ERROR(...)
logger.h
macros.h
__PRETTY_FUNCTION__
#define __PRETTY_FUNCTION__
Definition
macros.h:90
g2o
Definition
dl_wrapper.cpp:55
g2o::readLine
int readLine(std::istream &is, std::stringstream ¤tLine)
Definition
string_tools.cpp:161
parameter.h
parameter_container.h
string_tools.h
g2o::HyperGraph::HyperGraphElement
Definition
hyper_graph.h:80
g2o::HyperGraph::HyperGraphElement::elementType
virtual HyperGraphElementType elementType() const =0
Generated on
for g2o by
1.17.0