gtsam
Loading...
Searching...
No Matches
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
18
19#pragma once
20
21#include <gtsam/base/Testable.h>
22#include <gtsam/inference/Key.h>
23
24#if GTSAM_ENABLE_BOOST_SERIALIZATION
25#include <boost/serialization/nvp.hpp>
26#endif
27#include <cstdint>
28#include <functional>
29
30namespace gtsam {
31
37class GTSAM_EXPORT Symbol {
38protected:
39 unsigned char c_;
40 std::uint64_t j_;
41
42public:
43
46 c_(0), j_(0) {
47 }
48
50 Symbol(const Symbol& key) :
51 c_(key.c_), j_(key.j_) {
52 }
53
55 Symbol(unsigned char c, std::uint64_t j) :
56 c_(c), j_(j) {
57 }
58
60 Symbol(Key key);
61
63 Key key() const;
64
66 operator Key() const { return key(); }
67
69 void print(const std::string& s = "") const;
70
72 bool equals(const Symbol& expected, double tol = 0.0) const;
73
75 unsigned char chr() const {
76 return c_;
77 }
78
80 std::uint64_t index() const {
81 return j_;
82 }
83
85 operator std::string() const;
86
88 std::string string() const { return std::string(*this); }
89
91 bool operator<(const Symbol& comp) const {
92 return c_ < comp.c_ || (comp.c_ == c_ && j_ < comp.j_);
93 }
94
96 bool operator==(const Symbol& comp) const {
97 return comp.c_ == c_ && comp.j_ == j_;
98 }
99
101 bool operator==(Key comp) const {
102 return comp == (Key)(*this);
103 }
104
106 bool operator!=(const Symbol& comp) const {
107 return comp.c_ != c_ || comp.j_ != j_;
108 }
109
111 bool operator!=(Key comp) const {
112 return comp != (Key)(*this);
113 }
114
120 static std::function<bool(Key)> ChrTest(unsigned char c);
121
123 GTSAM_EXPORT friend std::ostream &operator<<(std::ostream &, const Symbol &);
124
125private:
126
127#if GTSAM_ENABLE_BOOST_SERIALIZATION
129 friend class boost::serialization::access;
130 template<class ARCHIVE>
131 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
132 ar & BOOST_SERIALIZATION_NVP(c_);
133 ar & BOOST_SERIALIZATION_NVP(j_);
134 }
135#endif
136};
137
139inline Key symbol(unsigned char c, std::uint64_t j) { return (Key)Symbol(c,j); }
140
142inline unsigned char symbolChr(Key key) { return Symbol(key).chr(); }
143
145inline std::uint64_t symbolIndex(Key key) { return Symbol(key).index(); }
146
147namespace symbol_shorthand {
148inline Key A(std::uint64_t j) { return Symbol('a', j); }
149inline Key B(std::uint64_t j) { return Symbol('b', j); }
150inline Key C(std::uint64_t j) { return Symbol('c', j); }
151inline Key D(std::uint64_t j) { return Symbol('d', j); }
152inline Key E(std::uint64_t j) { return Symbol('e', j); }
153inline Key F(std::uint64_t j) { return Symbol('f', j); }
154inline Key G(std::uint64_t j) { return Symbol('g', j); }
155inline Key H(std::uint64_t j) { return Symbol('h', j); }
156inline Key I(std::uint64_t j) { return Symbol('i', j); }
157inline Key J(std::uint64_t j) { return Symbol('j', j); }
158inline Key K(std::uint64_t j) { return Symbol('k', j); }
159inline Key L(std::uint64_t j) { return Symbol('l', j); }
160inline Key M(std::uint64_t j) { return Symbol('m', j); }
161inline Key N(std::uint64_t j) { return Symbol('n', j); }
162inline Key O(std::uint64_t j) { return Symbol('o', j); }
163inline Key P(std::uint64_t j) { return Symbol('p', j); }
164inline Key Q(std::uint64_t j) { return Symbol('q', j); }
165inline Key R(std::uint64_t j) { return Symbol('r', j); }
166inline Key S(std::uint64_t j) { return Symbol('s', j); }
167inline Key T(std::uint64_t j) { return Symbol('t', j); }
168inline Key U(std::uint64_t j) { return Symbol('u', j); }
169inline Key V(std::uint64_t j) { return Symbol('v', j); }
170inline Key W(std::uint64_t j) { return Symbol('w', j); }
171inline Key X(std::uint64_t j) { return Symbol('x', j); }
172inline Key Y(std::uint64_t j) { return Symbol('y', j); }
173inline Key Z(std::uint64_t j) { return Symbol('z', j); }
174}
175
178class SymbolGenerator {
179 const unsigned char c_;
180public:
181 constexpr SymbolGenerator(const unsigned char c) : c_(c) {}
182 Symbol operator()(const std::uint64_t j) const { return Symbol(c_, j); }
183 constexpr unsigned char chr() const { return c_; }
184};
185
187template<> struct traits<Symbol> : public Testable<Symbol> {};
188
189} // \ 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:145
Key symbol(unsigned char c, std::uint64_t j)
Create a symbol key from a character and index, i.e.
Definition Symbol.h:139
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
unsigned char symbolChr(Key key)
Return the character portion of a symbol key.
Definition Symbol.h:142
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
Character and index key used to refer to variables.
Definition Symbol.h:37
unsigned char chr() const
Retrieve key character.
Definition Symbol.h:75
bool operator==(Key comp) const
Comparison for use in maps.
Definition Symbol.h:101
bool operator!=(const Symbol &comp) const
Comparison for use in maps.
Definition Symbol.h:106
bool operator==(const Symbol &comp) const
Comparison for use in maps.
Definition Symbol.h:96
Key key() const
return Key (integer) representation
Definition Symbol.cpp:40
Symbol(const Symbol &key)
Copy constructor.
Definition Symbol.h:50
Symbol()
Default constructor.
Definition Symbol.h:45
Symbol(unsigned char c, std::uint64_t j)
Constructor.
Definition Symbol.h:55
bool operator<(const Symbol &comp) const
Comparison for use in maps.
Definition Symbol.h:91
std::uint64_t index() const
Retrieve key index.
Definition Symbol.h:80
std::string string() const
Return string representation of the key.
Definition Symbol.h:88
bool operator!=(Key comp) const
Comparison for use in maps.
Definition Symbol.h:111