g2o
property.h
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 #ifndef G2O_PROPERTY_H_
28 #define G2O_PROPERTY_H_
29 
30 #include <string>
31 #include <map>
32 #include <sstream>
33 
34 #include "string_tools.h"
35 #include "g2o_stuff_api.h"
36 
37 namespace g2o {
38 
40  public:
41  BaseProperty(const std::string name_);
42  virtual ~BaseProperty();
43  const std::string& name() {return _name;}
44  virtual std::string toString() const = 0;
45  virtual bool fromString(const std::string& s) = 0;
46  protected:
47  std::string _name;
48  };
49 
50  template <typename T>
51  class Property: public BaseProperty {
52  public:
53  typedef T ValueType;
54  Property(const std::string& name_): BaseProperty(name_){}
55  Property(const std::string& name_, const T& v): BaseProperty(name_), _value(v){}
56  void setValue(const T& v) {_value = v; }
57  const T& value() const {return _value;}
58  virtual std::string toString() const
59  {
60  std::stringstream sstr;
61  sstr << _value;
62  return sstr.str();
63  }
64  virtual bool fromString(const std::string& s)
65  {
66  bool status = convertString(s, _value);
67  return status;
68  }
69  protected:
70  T _value;
71  };
72 
76  class G2O_STUFF_API PropertyMap : protected std::map<std::string, BaseProperty*>
77  {
78  public:
79  typedef std::map<std::string, BaseProperty*> BaseClass;
80  typedef BaseClass::iterator PropertyMapIterator;
81  typedef BaseClass::const_iterator PropertyMapConstIterator;
82 
83  ~PropertyMap();
84 
88  bool addProperty(BaseProperty* p);
89 
93  bool eraseProperty(const std::string& name_);
94 
98  template <typename P>
99  P* getProperty(const std::string& name_)
100  {
101  PropertyMapIterator it=find(name_);
102  if (it==end())
103  return 0;
104  return dynamic_cast<P*>(it->second);
105  }
106  template <typename P>
107  const P* getProperty(const std::string& name_) const
108  {
109  PropertyMapConstIterator it=find(name_);
110  if (it==end())
111  return 0;
112  return dynamic_cast<P*>(it->second);
113  }
114 
118  template <typename P>
119  P* makeProperty(const std::string& name_, const typename P::ValueType& v)
120  {
121  PropertyMapIterator it=find(name_);
122  if (it==end()){
123  P* p=new P(name_, v);
124  addProperty(p);
125  return p;
126  } else
127  return dynamic_cast<P*>(it->second);
128  }
129 
134  bool updatePropertyFromString(const std::string& name, const std::string& value);
135 
140  bool updateMapFromString(const std::string& values);
141 
142  void writeToCSV(std::ostream& os) const;
143 
144  using BaseClass::size;
145  using BaseClass::begin;
146  using BaseClass::end;
147  using BaseClass::iterator;
148  using BaseClass::const_iterator;
149 
150  };
151 
157 
158 } // end namespace
159 #endif
BaseClass::iterator PropertyMapIterator
Definition: property.h:80
BaseClass::const_iterator PropertyMapConstIterator
Definition: property.h:81
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the explicitly state the reprensentation poses are represented by poses by VERTEX_XYZRPY In the Quaternions and other representations could be but note that it is up to the SLAM algorithm to choose the internal representation of the angles The keyword is followed by a unique vertex ID and an optional initialization of the values
Definition: protocol.txt:7
virtual std::string toString() const
Definition: property.h:58
Property(const std::string &name_, const T &v)
Definition: property.h:55
Property< double > DoubleProperty
Definition: property.h:155
std::string _name
Definition: property.h:47
Property(const std::string &name_)
Definition: property.h:54
P * makeProperty(const std::string &name_, const typename P::ValueType &v)
Definition: property.h:119
bool convertString(const std::string &s, T &x)
void setValue(const T &v)
Definition: property.h:56
std::map< std::string, BaseProperty * > BaseClass
Definition: property.h:79
Property< std::string > StringProperty
Definition: property.h:156
Property< int > IntProperty
Definition: property.h:152
virtual bool fromString(const std::string &s)
Definition: property.h:64
#define G2O_STUFF_API
Definition: g2o_stuff_api.h:30
const T & value() const
Definition: property.h:57
const P * getProperty(const std::string &name_) const
Definition: property.h:107
Property< float > FloatProperty
Definition: property.h:154
a collection of properties mapping from name to the property itself
Definition: property.h:76
const std::string & name()
Definition: property.h:43
Property< bool > BoolProperty
Definition: property.h:153
P * getProperty(const std::string &name_)
Definition: property.h:99