Key.cpp
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 
20 #include <gtsam/inference/Key.h>
22 
23 #include <iostream>
24 
25 using namespace std;
26 
27 namespace gtsam {
28 
31 
32 /* ************************************************************************* */
34  const Symbol asSymbol(key);
35  if (asSymbol.chr() > 0) {
36  return (string) asSymbol;
37  }
38  else {
39  return std::to_string(key);
40  }
41 }
42 
43 /* ************************************************************************* */
44 void PrintKey(Key key, const string& s, const KeyFormatter& keyFormatter) {
45  cout << s << keyFormatter(key);
46 }
47 
48 /* ************************************************************************* */
50  const LabeledSymbol asLabeledSymbol(key);
51  if (asLabeledSymbol.chr() > 0 && asLabeledSymbol.label() > 0)
52  return (string) asLabeledSymbol;
53 
54  const Symbol asSymbol(key);
55  if (asLabeledSymbol.chr() > 0) {
56  return (string) asSymbol;
57  }
58  else {
59  return std::to_string(key);
60  }
61 }
62 
63 /* ************************************************************************* */
64 template<class CONTAINER>
65 void Print(const CONTAINER& keys, const string& s,
66  const KeyFormatter& keyFormatter) {
67  cout << s << " ";
68  if (keys.empty())
69  cout << "(none)" << endl;
70  else {
71  for(const Key& key: keys)
72  cout << keyFormatter(key) << " ";
73  cout << endl;
74  }
75 }
76 
77 /* ************************************************************************* */
78 void PrintKeyList(const KeyList& keys, const string& s,
79  const KeyFormatter& keyFormatter) {
80  Print(keys, s, keyFormatter);
81 }
82 /* ************************************************************************* */
83 void PrintKeyVector(const KeyVector& keys, const string& s,
84  const KeyFormatter& keyFormatter) {
85  Print(keys, s, keyFormatter);
86 }
87 /* ************************************************************************* */
88 void PrintKeySet(const KeySet& keys, const string& s,
89  const KeyFormatter& keyFormatter) {
90  Print(keys, s, keyFormatter);
91 }
92 
93 /* ************************************************************************* */
94 // Access to custom stream property.
95 void *&key_formatter::property(ios_base &s) {
96  static int kUniqueIndex = ios_base::xalloc();
97  return s.pword(kUniqueIndex);
98 }
99 
100 /* ************************************************************************* */
101 // Store pointer to formatter in property.
102 void key_formatter::set_property(ios_base &s, const KeyFormatter &f) {
103  property(s) = (void *)(&f);
104 }
105 
106 /* ************************************************************************* */
107 // Get pointer to formatter from property.
108 KeyFormatter *key_formatter::get_property(ios_base &s) {
109  return (KeyFormatter *)(property(s));
110 }
111 
112 /* ************************************************************************* */
113 // Stream operator that will take a key_formatter and set the stream property.
114 ostream &operator<<(ostream &os, const key_formatter &m) {
115  key_formatter::set_property(os, m.formatter_);
116  return os;
117 }
118 
119 /* ************************************************************************* */
120 // Stream operator that takes a StreamedKey and properly formats it
121 ostream &operator<<(ostream &os, const StreamedKey &streamedKey) {
122  const KeyFormatter *formatter = key_formatter::get_property(os);
123  if (formatter == nullptr) {
125  }
126  os << (*formatter)(streamedKey.key_);
127  return (os);
128 }
129 
130 /* ************************************************************************* */
131 
132 } // \namespace gtsam
gtsam::LabeledSymbol::chr
unsigned char chr() const
Retrieve key character.
Definition: LabeledSymbol.h:84
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::LabeledSymbol
Definition: LabeledSymbol.h:37
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::StreamedKey::key_
const Key & key_
Definition: Key.h:65
gtsam::PrintKeyVector
void PrintKeyVector(const KeyVector &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:83
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::FastSet< Key >
os
ofstream os("timeSchurFactors.csv")
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::Symbol::chr
unsigned char chr() const
Definition: inference/Symbol.h:75
Key.h
gtsam::_multirobotKeyFormatter
string _multirobotKeyFormatter(Key key)
Definition: Key.cpp:49
gtsam::PrintKey
void PrintKey(Key key, const string &s, const KeyFormatter &keyFormatter)
Utility function to print one key with optional prefix.
Definition: Key.cpp:44
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::operator<<
ostream & operator<<(ostream &os, const StreamedKey &streamedKey)
Definition: Key.cpp:121
LabeledSymbol.h
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
gtsam::FastList
Definition: FastList.h:43
gtsam::LabeledSymbol::label
unsigned char label() const
Retrieve label character.
Definition: LabeledSymbol.h:81
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::StreamedKey
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
Definition: Key.h:64
gtsam
traits
Definition: SFMdata.h:40
std
Definition: BFloat16.h:88
gtsam::key_formatter
Definition: Key.h:78
gtsam::_defaultKeyFormatter
string _defaultKeyFormatter(Key key)
Definition: Key.cpp:33
gtsam::Print
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:65
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::PrintKeySet
void PrintKeySet(const KeySet &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:88
gtsam::PrintKeyList
void PrintKeyList(const KeyList &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:78
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:51