gtsam 4.1.1
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/base/Testable.h>
22#include <gtsam/inference/Key.h>
23
24#include <boost/serialization/nvp.hpp>
25#include <cstdint>
26#include <functional>
27
28namespace gtsam {
29
35class GTSAM_EXPORT Symbol {
36protected:
37 unsigned char c_;
38 std::uint64_t j_;
39
40public:
41
44 c_(0), j_(0) {
45 }
46
48 Symbol(const Symbol& key) :
49 c_(key.c_), j_(key.j_) {
50 }
51
53 Symbol(unsigned char c, std::uint64_t j) :
54 c_(c), j_(j) {
55 }
56
58 Symbol(Key key);
59
61 Key key() const;
62
64 operator Key() const { return key(); }
65
67 void print(const std::string& s = "") const;
68
70 bool equals(const Symbol& expected, double tol = 0.0) const;
71
73 unsigned char chr() const {
74 return c_;
75 }
76
78 std::uint64_t index() const {
79 return j_;
80 }
81
83 operator std::string() const;
84
86 std::string string() const { return std::string(*this); };
87
89 bool operator<(const Symbol& comp) const {
90 return c_ < comp.c_ || (comp.c_ == c_ && j_ < comp.j_);
91 }
92
94 bool operator==(const Symbol& comp) const {
95 return comp.c_ == c_ && comp.j_ == j_;
96 }
97
99 bool operator==(Key comp) const {
100 return comp == (Key)(*this);
101 }
102
104 bool operator!=(const Symbol& comp) const {
105 return comp.c_ != c_ || comp.j_ != j_;
106 }
107
109 bool operator!=(Key comp) const {
110 return comp != (Key)(*this);
111 }
112
118 static std::function<bool(Key)> ChrTest(unsigned char c);
119
121 GTSAM_EXPORT friend std::ostream &operator<<(std::ostream &, const Symbol &);
122
123private:
124
126 friend class boost::serialization::access;
127 template<class ARCHIVE>
128 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
129 ar & BOOST_SERIALIZATION_NVP(c_);
130 ar & BOOST_SERIALIZATION_NVP(j_);
131 }
132};
133
135inline Key symbol(unsigned char c, std::uint64_t j) { return (Key)Symbol(c,j); }
136
138inline unsigned char symbolChr(Key key) { return Symbol(key).chr(); }
139
141inline std::uint64_t symbolIndex(Key key) { return Symbol(key).index(); }
142
143namespace symbol_shorthand {
144inline Key A(std::uint64_t j) { return Symbol('a', j); }
145inline Key B(std::uint64_t j) { return Symbol('b', j); }
146inline Key C(std::uint64_t j) { return Symbol('c', j); }
147inline Key D(std::uint64_t j) { return Symbol('d', j); }
148inline Key E(std::uint64_t j) { return Symbol('e', j); }
149inline Key F(std::uint64_t j) { return Symbol('f', j); }
150inline Key G(std::uint64_t j) { return Symbol('g', j); }
151inline Key H(std::uint64_t j) { return Symbol('h', j); }
152inline Key I(std::uint64_t j) { return Symbol('i', j); }
153inline Key J(std::uint64_t j) { return Symbol('j', j); }
154inline Key K(std::uint64_t j) { return Symbol('k', j); }
155inline Key L(std::uint64_t j) { return Symbol('l', j); }
156inline Key M(std::uint64_t j) { return Symbol('m', j); }
157inline Key N(std::uint64_t j) { return Symbol('n', j); }
158inline Key O(std::uint64_t j) { return Symbol('o', j); }
159inline Key P(std::uint64_t j) { return Symbol('p', j); }
160inline Key Q(std::uint64_t j) { return Symbol('q', j); }
161inline Key R(std::uint64_t j) { return Symbol('r', j); }
162inline Key S(std::uint64_t j) { return Symbol('s', j); }
163inline Key T(std::uint64_t j) { return Symbol('t', j); }
164inline Key U(std::uint64_t j) { return Symbol('u', j); }
165inline Key V(std::uint64_t j) { return Symbol('v', j); }
166inline Key W(std::uint64_t j) { return Symbol('w', j); }
167inline Key X(std::uint64_t j) { return Symbol('x', j); }
168inline Key Y(std::uint64_t j) { return Symbol('y', j); }
169inline Key Z(std::uint64_t j) { return Symbol('z', j); }
170}
171
175 const unsigned char c_;
176public:
177 constexpr SymbolGenerator(const unsigned char c) : c_(c) {}
178 Symbol operator()(const std::uint64_t j) const { return Symbol(c_, j); }
179 constexpr unsigned char chr() const { return c_; }
180};
181
183template<> struct traits<Symbol> : public Testable<Symbol> {};
184
185} // \ namespace gtsam
Concept check for values that can be used in unit tests.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::uint64_t symbolIndex(Key key)
Return the index portion of a symbol key.
Definition: Symbol.h:141
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:112
Key symbol(unsigned char c, std::uint64_t j)
Create a symbol key from a character and index, i.e.
Definition: Symbol.h:135
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
unsigned char symbolChr(Key key)
Return the character portion of a symbol key.
Definition: Symbol.h:138
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Template to create a binary predicate.
Definition: Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
Character and index key used to refer to variables.
Definition: Symbol.h:35
unsigned char chr() const
Retrieve key character.
Definition: Symbol.h:73
bool operator==(Key comp) const
Comparison for use in maps.
Definition: Symbol.h:99
bool operator!=(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:104
bool operator==(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:94
Symbol(const Symbol &key)
Copy constructor.
Definition: Symbol.h:48
Symbol()
Default constructor.
Definition: Symbol.h:43
Symbol(unsigned char c, std::uint64_t j)
Constructor.
Definition: Symbol.h:53
bool operator<(const Symbol &comp) const
Comparison for use in maps.
Definition: Symbol.h:89
std::uint64_t index() const
Retrieve key index.
Definition: Symbol.h:78
std::string string() const
Return string representation of the key.
Definition: Symbol.h:86
bool operator!=(Key comp) const
Comparison for use in maps.
Definition: Symbol.h:109
Generates symbol shorthands with alternative names different than the one-letter predefined ones.
Definition: Symbol.h:174