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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:26