hpp-pinocchio  4.9.1
Wrapping of the kinematic/dynamic chain Pinocchio for HPP.
util.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017-2018 CNRS
3 // Author: Joseph Mirabel
4 //
5 //
6 // This file is part of hpp-pinocchio
7 // hpp-pinocchio is free software: you can redistribute it
8 // and/or modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation, either version
10 // 3 of the License, or (at your option) any later version.
11 //
12 // hpp-pinocchio is distributed in the hope that it will be
13 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Lesser Public License for more details. You should have
16 // received a copy of the GNU Lesser General Public License along with
17 // hpp-pinocchio If not, see
18 // <http://www.gnu.org/licenses/>.
19 
20 #ifndef HPP_PINOCCHIO_UTIL_HH
21 # define HPP_PINOCCHIO_UTIL_HH
22 
23 # include <hpp/util/indent.hh>
24 
25 # include <hpp/pinocchio/config.hh>
26 # include <hpp/pinocchio/fwd.hh>
27 
28 namespace hpp {
65 
67  template <typename T, int Option> struct HPP_PINOCCHIO_DLLAPI prettyPrint { static std::ostream& run (std::ostream& os, const T& pp); };
68 
70  enum {
72 
76  };
77 
79  HPP_PINOCCHIO_DLLAPI long& getpythonformat (std::ostream& o);
80 
81  template <bool OneLine, bool PythonStyle, bool Vector>
82  struct HPP_PINOCCHIO_DLLAPI eigen_format { static const Eigen::IOFormat run(); };
83 
84  // Default implementation
85  template <bool OneLine, bool PythonStyle, bool Vector> const Eigen::IOFormat
86  eigen_format<OneLine, PythonStyle, Vector>::run() {
87  static const Eigen::IOFormat fmt(
88  (PythonStyle ? Eigen::FullPrecision : Eigen::StreamPrecision),
89  0,
90  ", ", // Coeff separator
91  (PythonStyle // Row separator
92  ? (OneLine ? ", ": ",\n" )
93  : (OneLine ? "; ": "\n" )),
94  (PythonStyle ? "(" : ""), // row prefix
95  (PythonStyle ? ",)" : ""), // row suffix
96  (PythonStyle && !Vector ? "( " : ""), // mat prefix
97  (PythonStyle && !Vector ? ", )" : "") // mat suffix
98  );
99  return fmt;
100  }
101 
102  template <typename T, int Option> struct PrettyPrint {
103  const T& value;
104  inline explicit PrettyPrint (const T& t) : value (t) {}
105  };
106 
107  template <typename T, int Option>
108  std::ostream& operator<< (std::ostream& os, const PrettyPrint<T, Option> pp)
109  {
110  return prettyPrint<T, Option>::run (os, pp.value);
111  }
112 
114  template <typename Derived, int Option>
115  struct HPP_PINOCCHIO_DLLAPI prettyPrintEigen {
116  static inline std::ostream& run (std::ostream& os, const Derived& M)
117  {
118  enum { Condensed = ((Option & OutputFormatBits) == OneLineOutput) || ((Option & OutputFormatBits) == CondensedOutput) };
119  static const Eigen::IOFormat mfmt_py = eigen_format< Condensed, true , false>::run();
120  static const Eigen::IOFormat vfmt_py = eigen_format< Condensed, true , true >::run();
121  static const Eigen::IOFormat mfmt_raw = eigen_format< Condensed, false, false>::run();
122  static const Eigen::IOFormat vfmt_raw = eigen_format< Condensed, false, true >::run();
123  bool use_py_fmt = (getpythonformat(os) != 0);
124  const Eigen::IOFormat& fmt =
125  (Derived::IsVectorAtCompileTime
126  ? (use_py_fmt ? vfmt_py : vfmt_raw)
127  : (use_py_fmt ? mfmt_py : mfmt_raw));
128  bool transpose = (Derived::ColsAtCompileTime == 1);
129 
130  if (transpose) return os << M.transpose().format(fmt);
131  else return os << M.format(fmt);
132  }
133  };
135 
137  template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols, int Option>
138  struct HPP_PINOCCHIO_DLLAPI prettyPrint <Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols >, Option>
139  : prettyPrintEigen <Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols >, Option> {};
140 
142  template<typename OtherDerived, int Size, int Option>
143  struct HPP_PINOCCHIO_DLLAPI prettyPrint <Eigen::VectorBlock< OtherDerived, Size >, Option>
144  : prettyPrintEigen <Eigen::VectorBlock< OtherDerived, Size >, Option > {};
145 
147  template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, int Option>
148  struct HPP_PINOCCHIO_DLLAPI prettyPrint <Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>, Option>
149  : prettyPrintEigen <Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>, Option > {};
150 
152  template<typename _PlainObjectType, int _Options, typename _StrideType, int Option>
153  struct HPP_PINOCCHIO_DLLAPI prettyPrint <Eigen::Ref< _PlainObjectType, _Options, _StrideType>, Option>
154  : prettyPrintEigen <Eigen::Ref< _PlainObjectType, _Options, _StrideType>, Option> {};
155 
157  template<typename _Scalar, int _Options, int Option>
158  struct HPP_PINOCCHIO_DLLAPI prettyPrint <Eigen::Quaternion< _Scalar, _Options>, Option>
159  {
160  typedef Eigen::Quaternion< _Scalar, _Options> Derived;
161  typedef typename Eigen::internal::traits<Derived>::Coefficients Coefficients;
162  static inline std::ostream& run (std::ostream& os, const Derived& M)
163  {
164  return prettyPrint<Coefficients, Option>::run (os, M.coeffs());
165  }
166  };
168 
169  // Set python formatting of vector and matrices
170  HPP_PINOCCHIO_DLLAPI std::ostream& setpyformat (std::ostream& o);
171 
172  // Unset python formatting of vector and matrices
173  HPP_PINOCCHIO_DLLAPI std::ostream& unsetpyformat (std::ostream& o);
174 
176  template <typename T> inline PrettyPrint<T, PrettyOutput > pretty_print (const T& t) { return PrettyPrint<T, PrettyOutput >(t); }
178  template <typename T> inline PrettyPrint<T, CondensedOutput > condensed (const T& t) { return PrettyPrint<T, CondensedOutput >(t); }
180  template <typename T> inline PrettyPrint<T, OneLineOutput > one_line (const T& t) { return PrettyPrint<T, OneLineOutput >(t); }
181 
183 } // namespace hpp
184 #endif // HPP_PINOCCHIO_UTIL_HH
Definition: util.hh:73
static std::ostream & run(std::ostream &os, const T &pp)
Definition: util.hh:71
Utility functions.
HPP_PINOCCHIO_DLLAPI std::ostream & setpyformat(std::ostream &o)
This function must be specialized for the type you want to print.
Definition: util.hh:67
Definition: util.hh:74
PrettyPrint< T, PrettyOutput > pretty_print(const T &t)
Pretty printing.
Definition: util.hh:176
HPP_PINOCCHIO_DLLAPI std::ostream & unsetpyformat(std::ostream &o)
PrettyPrint< T, CondensedOutput > condensed(const T &t)
Condensed printing.
Definition: util.hh:178
PrettyPrint< T, OneLineOutput > one_line(const T &t)
Print on one line.
Definition: util.hh:180
Transform3f t
Definition: util.hh:75
Vec3f o