gtsam  4.0.0
gtsam
Symbol.h
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 
115 private:
116 
118  friend class boost::serialization::access;
119  template<class ARCHIVE>
120  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
121  ar & BOOST_SERIALIZATION_NVP(c_);
122  ar & BOOST_SERIALIZATION_NVP(j_);
123  }
124 };
125 
127 inline Key symbol(unsigned char c, std::uint64_t j) { return (Key)Symbol(c,j); }
128 
130 inline unsigned char symbolChr(Key key) { return Symbol(key).chr(); }
131 
133 inline std::uint64_t symbolIndex(Key key) { return Symbol(key).index(); }
134 
135 namespace symbol_shorthand {
136 inline Key A(std::uint64_t j) { return Symbol('a', j); }
137 inline Key B(std::uint64_t j) { return Symbol('b', j); }
138 inline Key C(std::uint64_t j) { return Symbol('c', j); }
139 inline Key D(std::uint64_t j) { return Symbol('d', j); }
140 inline Key E(std::uint64_t j) { return Symbol('e', j); }
141 inline Key F(std::uint64_t j) { return Symbol('f', j); }
142 inline Key G(std::uint64_t j) { return Symbol('g', j); }
143 inline Key H(std::uint64_t j) { return Symbol('h', j); }
144 inline Key I(std::uint64_t j) { return Symbol('i', j); }
145 inline Key J(std::uint64_t j) { return Symbol('j', j); }
146 inline Key K(std::uint64_t j) { return Symbol('k', j); }
147 inline Key L(std::uint64_t j) { return Symbol('l', j); }
148 inline Key M(std::uint64_t j) { return Symbol('m', j); }
149 inline Key N(std::uint64_t j) { return Symbol('n', j); }
150 inline Key O(std::uint64_t j) { return Symbol('o', j); }
151 inline Key P(std::uint64_t j) { return Symbol('p', j); }
152 inline Key Q(std::uint64_t j) { return Symbol('q', j); }
153 inline Key R(std::uint64_t j) { return Symbol('r', j); }
154 inline Key S(std::uint64_t j) { return Symbol('s', j); }
155 inline Key T(std::uint64_t j) { return Symbol('t', j); }
156 inline Key U(std::uint64_t j) { return Symbol('u', j); }
157 inline Key V(std::uint64_t j) { return Symbol('v', j); }
158 inline Key W(std::uint64_t j) { return Symbol('w', j); }
159 inline Key X(std::uint64_t j) { return Symbol('x', j); }
160 inline Key Y(std::uint64_t j) { return Symbol('y', j); }
161 inline Key Z(std::uint64_t j) { return Symbol('z', j); }
162 }
163 
165 template<> struct traits<Symbol> : public Testable<Symbol> {};
166 
167 } // \ namespace gtsam
168 
Symbol()
Default constructor.
Definition: Symbol.h:41
Symbol(unsigned char c, std::uint64_t j)
Constructor.
Definition: Symbol.h:51
std::uint64_t index() const
Retrieve key index.
Definition: Symbol.h:76
Character and index key used to refer to variables.
Definition: Symbol.h:33
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Template to create a binary predicate.
Definition: Testable.h:110
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
unsigned char symbolChr(Key key)
Return the character portion of a symbol key.
Definition: Symbol.h:130
bool operator==(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:89
std::uint64_t symbolIndex(Key key)
Return the index portion of a symbol key.
Definition: Symbol.h:133
unsigned char chr() const
Retrieve key character.
Definition: Symbol.h:71
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
bool operator!=(Key comp) const
Comparison for use in maps.
Definition: Symbol.h:104
Symbol(const Symbol &key)
Copy constructor.
Definition: Symbol.h:46
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Concept check for values that can be used in unit tests.
bool operator!=(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:99
Key symbol(unsigned char c, std::uint64_t j)
Create a symbol key from a character and index, i.e.
Definition: Symbol.h:127
bool operator==(Key comp) const
Comparison for use in maps.
Definition: Symbol.h:94
bool operator<(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:84