g2o
Loading...
Searching...
No Matches
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"
34#include "g2o/stuff/logger.h"
35#include "g2o/stuff/macros.h"
37#include "parameter.h"
38
39namespace g2o {
40
41using namespace std;
42
44 : _isMainStorage(isMainStorage_) {}
45
47 if (!_isMainStorage) return;
48 for (iterator it = begin(); it != end(); ++it) {
49 delete it->second;
50 }
51 BaseClass::clear();
52}
53
55
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
65 iterator it = find(id);
66 if (it == end()) return nullptr;
67 return it->second;
68}
69
71 const_iterator it = find(id);
72 if (it == end()) return nullptr;
73 return it->second;
74}
75
77 iterator it = find(id);
78 if (it == end()) return nullptr;
79 Parameter* p = it->second;
80 erase(it);
81 return p;
82}
83
84bool 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
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();
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
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 {}",
133 delete p;
134 } else {
135 if (!addParameter(p)) {
136 G2O_ERROR("{}: Parameter of type: {} id: {} already defined",
138 }
139 }
140 } // while read line
141
142 return true;
143}
144
145} // namespace g2o
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
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition hyper_graph.h:70
virtual bool write(std::ostream &os) const
write the data to a stream
ParameterContainer(bool isMainStorage_=true)
Parameter * detachParameter(int id)
remove a parameter from the container, i.e., the user now owns the pointer
bool addParameter(Parameter *p)
add parameter to the container
Parameter * getParameter(int id)
return a parameter based on its ID
virtual bool read(std::istream &is, const std::map< std::string, std::string > *renamedMap=0)
read parameters from a stream
int id() const
Definition parameter.h:44
void setId(int id_)
Definition parameter.cpp:33
virtual bool read(std::istream &is)=0
read the data from a stream
SlamParser::Parser::token token
#define G2O_ERROR(...)
Definition logger.h:89
#define __PRETTY_FUNCTION__
Definition macros.h:90
int readLine(std::istream &is, std::stringstream &currentLine)
Definition jet.h:876
virtual HyperGraphElementType elementType() const =0