gtsam  4.1.0
gtsam
Symbol.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #pragma once
20 
21 #include <gtsam/inference/Key.h>
22 #include <gtsam/base/Testable.h>
23 #include <boost/serialization/nvp.hpp>
24 #include <cstdint>
25 
26 namespace gtsam {
27 
33 class GTSAM_EXPORT Symbol {
34 protected:
35  unsigned char c_;
36  std::uint64_t j_;
37 
38 public:
39 
41  Symbol() :
42  c_(0), j_(0) {
43  }
44 
46  Symbol(const Symbol& key) :
47  c_(key.c_), j_(key.j_) {
48  }
49 
51  Symbol(unsigned char c, std::uint64_t j) :
52  c_(c), j_(j) {
53  }
54 
56  Symbol(Key key);
57 
59  Key key() const;
60 
62  operator Key() const { return key(); }
63 
65  void print(const std::string& s = "") const;
66 
68  bool equals(const Symbol& expected, double tol = 0.0) const;
69 
71  unsigned char chr() const {
72  return c_;
73  }
74 
76  std::uint64_t index() const {
77  return j_;
78  }
79 
81  operator std::string() const;
82 
84  bool operator<(const Symbol& comp) const {
85  return c_ < comp.c_ || (comp.c_ == c_ && j_ < comp.j_);
86  }
87 
89  bool operator==(const Symbol& comp) const {
90  return comp.c_ == c_ && comp.j_ == j_;
91  }
92 
94  bool operator==(Key comp) const {
95  return comp == (Key)(*this);
96  }
97 
99  bool operator!=(const Symbol& comp) const {
100  return comp.c_ != c_ || comp.j_ != j_;
101  }
102 
104  bool operator!=(Key comp) const {
105  return comp != (Key)(*this);
106  }
107 
113  static boost::function<bool(Key)> ChrTest(unsigned char c);
114 
116  GTSAM_EXPORT friend std::ostream &operator<<(std::ostream &, const Symbol &);
117 
118 private:
119 
121  friend class boost::serialization::access;
122  template<class ARCHIVE>
123  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
124  ar & BOOST_SERIALIZATION_NVP(c_);
125  ar & BOOST_SERIALIZATION_NVP(j_);
126  }
127 };
128 
130 inline Key symbol(unsigned char c, std::uint64_t j) { return (Key)Symbol(c,j); }
131 
133 inline unsigned char symbolChr(Key key) { return Symbol(key).chr(); }
134 
136 inline std::uint64_t symbolIndex(Key key) { return Symbol(key).index(); }
137 
138 namespace symbol_shorthand {
139 inline Key A(std::uint64_t j) { return Symbol('a', j); }
140 inline Key B(std::uint64_t j) { return Symbol('b', j); }
141 inline Key C(std::uint64_t j) { return Symbol('c', j); }
142 inline Key D(std::uint64_t j) { return Symbol('d', j); }
143 inline Key E(std::uint64_t j) { return Symbol('e', j); }
144 inline Key F(std::uint64_t j) { return Symbol('f', j); }
145 inline Key G(std::uint64_t j) { return Symbol('g', j); }
146 inline Key H(std::uint64_t j) { return Symbol('h', j); }
147 inline Key I(std::uint64_t j) { return Symbol('i', j); }
148 inline Key J(std::uint64_t j) { return Symbol('j', j); }
149 inline Key K(std::uint64_t j) { return Symbol('k', j); }
150 inline Key L(std::uint64_t j) { return Symbol('l', j); }
151 inline Key M(std::uint64_t j) { return Symbol('m', j); }
152 inline Key N(std::uint64_t j) { return Symbol('n', j); }
153 inline Key O(std::uint64_t j) { return Symbol('o', j); }
154 inline Key P(std::uint64_t j) { return Symbol('p', j); }
155 inline Key Q(std::uint64_t j) { return Symbol('q', j); }
156 inline Key R(std::uint64_t j) { return Symbol('r', j); }
157 inline Key S(std::uint64_t j) { return Symbol('s', j); }
158 inline Key T(std::uint64_t j) { return Symbol('t', j); }
159 inline Key U(std::uint64_t j) { return Symbol('u', j); }
160 inline Key V(std::uint64_t j) { return Symbol('v', j); }
161 inline Key W(std::uint64_t j) { return Symbol('w', j); }
162 inline Key X(std::uint64_t j) { return Symbol('x', j); }
163 inline Key Y(std::uint64_t j) { return Symbol('y', j); }
164 inline Key Z(std::uint64_t j) { return Symbol('z', j); }
165 }
166 
170  const char c_;
171 public:
172  SymbolGenerator(const char c) : c_(c) {}
173  Symbol operator()(const std::uint64_t j) const { return Symbol(c_, j); }
174 };
175 
177 template<> struct traits<Symbol> : public Testable<Symbol> {};
178 
179 } // \ namespace gtsam
Testable.h
Concept check for values that can be used in unit tests.
gtsam::equals
Template to create a binary predicate.
Definition: Testable.h:110
gtsam::symbolChr
unsigned char symbolChr(Key key)
Return the character portion of a symbol key.
Definition: Symbol.h:133
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
gtsam::Symbol::Symbol
Symbol()
Default constructor.
Definition: Symbol.h:41
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
gtsam::Symbol::operator==
bool operator==(Key comp) const
Comparison for use in maps.
Definition: Symbol.h:94
gtsam::serialize
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:100
gtsam::Symbol::Symbol
Symbol(unsigned char c, std::uint64_t j)
Constructor.
Definition: Symbol.h:51
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
gtsam::symbol
Key symbol(unsigned char c, std::uint64_t j)
Create a symbol key from a character and index, i.e.
Definition: Symbol.h:130
gtsam::Symbol::operator!=
bool operator!=(Key comp) const
Comparison for use in maps.
Definition: Symbol.h:104
Key.h
gtsam::Symbol::Symbol
Symbol(const Symbol &key)
Copy constructor.
Definition: Symbol.h:46
gtsam::SymbolGenerator
Generates symbol shorthands with alternative names different than the one-letter predefined ones.
Definition: Symbol.h:169
gtsam::Symbol::operator!=
bool operator!=(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:99
gtsam::Symbol::index
std::uint64_t index() const
Retrieve key index.
Definition: Symbol.h:76
gtsam::Symbol::operator==
bool operator==(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:89
gtsam::Symbol::operator<
bool operator<(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:84
gtsam::symbolIndex
std::uint64_t symbolIndex(Key key)
Return the index portion of a symbol key.
Definition: Symbol.h:136
gtsam::Symbol::chr
unsigned char chr() const
Retrieve key character.
Definition: Symbol.h:71
gtsam::Symbol
Character and index key used to refer to variables.
Definition: Symbol.h:33