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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:29