Cheetah Software  1.0
Utilities_print.h
Go to the documentation of this file.
1 #ifndef PRINT_OUT_H
2 #define PRINT_OUT_H
3 
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <iostream>
7 #include <string>
8 #include "cppTypes.h"
9 
10 /*
11  * print out
12  * */
13 template <typename T>
14 std::string pretty_string(T vv) {
15  static int const buflen(32);
16  static char buf[buflen];
17  memset(buf, 0, sizeof(buf));
18  snprintf(buf, buflen - 1, "% 6.6f ", vv);
19  std::string str(buf);
20  return str;
21 }
22 
23 template <typename T>
24 void pretty_print(DMat<T> const &mm, std::ostream &os, std::string const &title,
25  std::string const &prefix = "", bool vecmode = false,
26  bool nonl = false) {
27  char const *nlornot("\n");
28  if (nonl) {
29  nlornot = "";
30  }
31  if (!title.empty()) {
32  os << title << nlornot;
33  }
34  if ((mm.rows() <= 0) || (mm.cols() <= 0)) {
35  os << prefix << " (empty)" << nlornot;
36  } else {
37  if (vecmode) {
38  if (!prefix.empty()) os << prefix;
39  for (int ir(0); ir < mm.rows(); ++ir) {
40  os << pretty_string(mm.coeff(ir, 0));
41  }
42  os << nlornot;
43 
44  } else {
45  for (int ir(0); ir < mm.rows(); ++ir) {
46  if (!prefix.empty()) os << prefix;
47  for (int ic(0); ic < mm.cols(); ++ic) {
48  os << pretty_string(mm.coeff(ir, ic));
49  }
50  os << nlornot;
51  }
52  }
53  }
54 }
55 
56 template <typename T>
57 void pretty_print(Quat<T> const &vv, std::ostream &os, std::string const &title,
58  std::string const &prefix = "", bool nonl = false) {
59  pretty_print((DMat<T> const &)vv, os, title, prefix, true, nonl);
60 }
61 
62 template <typename T>
63 void pretty_print(DVec<T> const &vv, std::ostream &os, std::string const &title,
64  std::string const &prefix = "", bool nonl = false) {
65  pretty_print((DMat<T> const &)vv, os, title, prefix, true, nonl);
66 }
67 
68 template <typename T>
69 void pretty_print(D3Mat<T> const &vv, std::ostream &os,
70  std::string const &title, std::string const &prefix = "",
71  bool nonl = false) {
72  pretty_print((DMat<T> const &)vv, os, title, prefix, false, nonl);
73 }
74 
75 template <typename T>
76 void pretty_print(Mat3<T> const &vv, std::ostream &os, std::string const &title,
77  std::string const &prefix = "", bool nonl = false) {
78  pretty_print((DMat<T> const &)vv, os, title, prefix, false, nonl);
79 }
80 
81 template <typename T>
82 void pretty_print(Mat6<T> const &vv, std::ostream &os, std::string const &title,
83  std::string const &prefix = "", bool nonl = false) {
84  pretty_print((DMat<T> const &)vv, os, title, prefix, false, nonl);
85 }
86 
87 template <typename T>
88 void pretty_print(SVec<T> const &vv, std::ostream &os, std::string const &title,
89  std::string const &prefix = "", bool nonl = false) {
90  pretty_print((DMat<T> const &)vv, os, title, prefix, true, nonl);
91 }
92 
93 template <typename T>
94 void pretty_print(Vec3<T> const &vv, std::ostream &os, std::string const &title,
95  std::string const &prefix = "", bool nonl = false) {
96  pretty_print((DMat<T> const &)vv, os, title, prefix, true, nonl);
97 }
98 
99 template <typename T>
100 void pretty_print(Vec2<T> const &vv, std::ostream &os, std::string const &title,
101  std::string const &prefix = "", bool nonl = false) {
102  pretty_print((DMat<T> const &)vv, os, title, prefix, true, nonl);
103 }
104 
105 template <typename T>
106 void pretty_print(const std::vector<T> &_vec, const char *title) {
107  printf("%s: ", title);
108  for (size_t i(0); i < _vec.size(); ++i) {
109  printf("% 6.4f, \t", _vec[i]);
110  }
111  printf("\n");
112 }
113 
114 template <typename T>
115 void pretty_print(const T *_vec, const char *title, size_t size) {
116  printf("%s: ", title);
117  for (size_t i(0); i < size; ++i) {
118  printf("% 6.4f, \t", _vec[i]);
119  }
120  printf("\n");
121 }
122 
124 
125 void printf_color(PrintColor color, const char *fmt, ...);
126 void fprintf_color(PrintColor color, FILE* stream, const char *fmt, ...);
127 
128 #endif
typename Eigen::Matrix< T, 6, 6 > Mat6
Definition: cppTypes.h:70
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
void fprintf_color(PrintColor color, FILE *stream, const char *fmt,...)
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
typename Eigen::Matrix< T, 3, Eigen::Dynamic > D3Mat
Definition: cppTypes.h:114
typename Eigen::Matrix< T, 6, 1 > SVec
Definition: cppTypes.h:62
void pretty_print(DMat< T > const &mm, std::ostream &os, std::string const &title, std::string const &prefix="", bool vecmode=false, bool nonl=false)
typename Eigen::Matrix< T, 2, 1 > Vec2
Definition: cppTypes.h:22
std::string pretty_string(T vv)
void printf_color(PrintColor color, const char *fmt,...)
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102
PrintColor