A LabeledSymbol
is a specialized version of gtsam.Symbol
designed primarily for multi-robot applications or scenarios where an additional label is needed besides the type character and index. It encodes a type character (unsigned char
), a label character (unsigned char
), and an index (uint64_t
) into a single 64-bit gtsam.Key
.
import gtsam
from gtsam import LabeledSymbol
Initialization¶
A LabeledSymbol
can be created by providing a type character, a label character, and an index. It can also be created by decoding an existing gtsam.Key
(integer).
# Create LabeledSymbol 'x' from robot 'A' with index 7
# The underlying C++ expects chars, so we have to convert single-character strings with ord()
lsym1 = LabeledSymbol(ord('x'), ord('A'), 7)
print(f"LabeledSymbol from char/label/index: {lsym1}")
# Get the underlying integer key
key1 = lsym1.key()
# Reconstruct LabeledSymbol from the key
# Note: Decoding a key assumes it was encoded as a LabeledSymbol.
# If you decode a standard Symbol key, the label might be garbage.
x0_key = gtsam.Symbol('x', 0).key()
lsym2 = LabeledSymbol(x0_key)
print(f"LabeledSymbol from key {x0_key}: {lsym2}")
LabeledSymbol from char/label/index: : xA7
LabeledSymbol from key 8646911284551352320: : x
Properties and Usage¶
You can access the type character, label character, index, and underlying integer key.
robotB_landmark = LabeledSymbol(ord('l'), ord('B'), 3)
print(f"LabeledSymbol: {robotB_landmark}")
print(f" Char (Type): {robotB_landmark.chr()}")
print(f" Label (Robot): {robotB_landmark.label()}")
print(f" Index: {robotB_landmark.index()}")
print(f" Key: {robotB_landmark.key()}")
LabeledSymbol: : lB3
Char (Type): 108
Label (Robot): 66
Index: 3
Key: 7800797504559120387
Shorthand Function¶
GTSAM provides a convenient shorthand function gtsam.mrsymbol(c, label, j)
.
pc2_key = gtsam.mrsymbol(ord('p'), ord('C'), 2)
print(f"LabeledSymbol(ord('p'), ord('C'), 2).key() == gtsam.mrsymbol(ord('p'), ord('C'), 2): {LabeledSymbol(ord('p'), ord('C'), 2).key() == pc2_key}")
LabeledSymbol(ord('p'), ord('C'), 2).key() == gtsam.mrsymbol(ord('p'), ord('C'), 2): True