hpp-pinocchio 6.0.0
Wrapping of the kinematic/dynamic chain Pinocchio for HPP.
Loading...
Searching...
No Matches
util.hh
Go to the documentation of this file.
1//
2// Copyright (c) 2017-2018 CNRS
3// Author: Joseph Mirabel
4//
5//
6
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// 2. Redistributions in binary form must reproduce the above copyright
15// notice, this list of conditions and the following disclaimer in the
16// documentation and/or other materials provided with the distribution.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29// DAMAGE.
30
31#ifndef HPP_PINOCCHIO_UTIL_HH
32#define HPP_PINOCCHIO_UTIL_HH
33
35#include <hpp/pinocchio/fwd.hh>
36#include <hpp/util/indent.hh>
37
38namespace hpp {
75
77template <typename T, int Option>
79 static std::ostream& run(std::ostream& os, const T& pp);
80};
81
83enum {
85
88 PrettyOutput = 2
89};
90
92HPP_PINOCCHIO_DLLAPI long& getpythonformat(std::ostream& o);
93
94template <bool OneLine, bool PythonStyle, bool Vector>
95struct HPP_PINOCCHIO_DLLAPI eigen_format {
96 static const Eigen::IOFormat run();
97};
98
99// Default implementation
100template <bool OneLine, bool PythonStyle, bool Vector>
101const Eigen::IOFormat eigen_format<OneLine, PythonStyle, Vector>::run() {
102 static const Eigen::IOFormat fmt(
103 (PythonStyle ? Eigen::FullPrecision : Eigen::StreamPrecision), 0,
104 ", ", // Coeff separator
105 (PythonStyle // Row separator
106 ? (OneLine ? ", " : ",\n")
107 : (OneLine ? "; " : "\n")),
108 (PythonStyle ? "(" : ""), // row prefix
109 (PythonStyle ? ",)" : ""), // row suffix
110 (PythonStyle && !Vector ? "( " : ""), // mat prefix
111 (PythonStyle && !Vector ? ", )" : "") // mat suffix
112 );
113 return fmt;
114}
115
116template <typename T, int Option>
117struct PrettyPrint {
118 const T& value;
119 inline explicit PrettyPrint(const T& t) : value(t) {}
120};
121
122template <typename T, int Option>
123std::ostream& operator<<(std::ostream& os, const PrettyPrint<T, Option> pp) {
124 return prettyPrint<T, Option>::run(os, pp.value);
125}
126
128template <typename Derived, int Option>
129struct HPP_PINOCCHIO_DLLAPI prettyPrintEigen{
130 static inline std::ostream &
131 run(std::ostream & os, const Derived& M){
132 enum {Condensed = ((Option & OutputFormatBits) == OneLineOutput) ||
133 ((Option & OutputFormatBits) == CondensedOutput)};
134static const Eigen::IOFormat mfmt_py =
135 eigen_format<Condensed, true, false>::run();
136static const Eigen::IOFormat vfmt_py =
137 eigen_format<Condensed, true, true>::run();
138static const Eigen::IOFormat mfmt_raw =
139 eigen_format<Condensed, false, false>::run();
140static const Eigen::IOFormat vfmt_raw =
141 eigen_format<Condensed, false, true>::run();
142bool use_py_fmt = (getpythonformat(os) != 0);
143const Eigen::IOFormat& fmt =
144 (Derived::IsVectorAtCompileTime ? (use_py_fmt ? vfmt_py : vfmt_raw)
145 : (use_py_fmt ? mfmt_py : mfmt_raw));
146bool transpose = (Derived::ColsAtCompileTime == 1);
147
148if (transpose)
149 return os << M.transpose().format(fmt);
150else
151 return os << M.format(fmt);
152} // namespace hpp
153}
154;
156
158template <typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows,
159 int _MaxCols, int Option>
160struct HPP_PINOCCHIO_DLLAPI prettyPrint<
161 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Option>
162 : prettyPrintEigen<
163 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>,
164 Option> {};
165
167template <typename OtherDerived, int Size, int Option>
169 prettyPrint<Eigen::VectorBlock<OtherDerived, Size>, Option>
170 : prettyPrintEigen<Eigen::VectorBlock<OtherDerived, Size>, Option> {};
171
173template <typename XprType, int BlockRows, int BlockCols, bool InnerPanel,
174 int Option>
176 prettyPrint<Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>, Option>
177 : prettyPrintEigen<Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>,
178 Option> {};
179
181template <typename _PlainObjectType, int _Options, typename _StrideType,
182 int Option>
184 prettyPrint<Eigen::Ref<_PlainObjectType, _Options, _StrideType>, Option>
185 : prettyPrintEigen<Eigen::Ref<_PlainObjectType, _Options, _StrideType>,
186 Option> {};
187
189template <typename _PlainObjectType, int Option>
191 prettyPrint<Eigen::Transpose<_PlainObjectType>, Option>
192 : prettyPrintEigen<Eigen::Transpose<_PlainObjectType>, Option> {};
193
195template <typename _Scalar, int _Options, int Option>
197 prettyPrint<Eigen::Quaternion<_Scalar, _Options>, Option> {
198 typedef Eigen::Quaternion<_Scalar, _Options> Derived;
199 typedef typename Eigen::internal::traits<Derived>::Coefficients Coefficients;
200 static inline std::ostream& run(std::ostream& os, const Derived& M) {
201 return prettyPrint<Coefficients, Option>::run(os, M.coeffs());
202 }
203};
205
206// Set python formatting of vector and matrices
207HPP_PINOCCHIO_DLLAPI std::ostream& setpyformat(std::ostream& o);
208
209// Unset python formatting of vector and matrices
210HPP_PINOCCHIO_DLLAPI std::ostream& unsetpyformat(std::ostream& o);
211
213template <typename T>
214inline PrettyPrint<T, PrettyOutput> pretty_print(const T& t) {
215 return PrettyPrint<T, PrettyOutput>(t);
216}
218template <typename T>
219inline PrettyPrint<T, CondensedOutput> condensed(const T& t) {
220 return PrettyPrint<T, CondensedOutput>(t);
221}
223template <typename T>
224inline PrettyPrint<T, OneLineOutput> one_line(const T& t) {
225 return PrettyPrint<T, OneLineOutput>(t);
226}
227
229} // namespace hpp
230#endif // HPP_PINOCCHIO_UTIL_HH
#define HPP_PINOCCHIO_DLLAPI
Definition config.hh:88
PrettyPrint< T, OneLineOutput > one_line(const T &t)
Print on one line.
Definition util.hh:224
HPP_PINOCCHIO_DLLAPI std::ostream & setpyformat(std::ostream &o)
PrettyPrint< T, CondensedOutput > condensed(const T &t)
Condensed printing.
Definition util.hh:219
PrettyPrint< T, PrettyOutput > pretty_print(const T &t)
Pretty printing.
Definition util.hh:214
HPP_PINOCCHIO_DLLAPI std::ostream & unsetpyformat(std::ostream &o)
@ PrettyOutput
Definition util.hh:88
@ CondensedOutput
Definition util.hh:87
@ OutputFormatBits
Definition util.hh:84
@ OneLineOutput
Definition util.hh:86
std::ostream & operator<<(std::ostream &os, const hpp::pinocchio::Device &device)
Definition device.hh:366
Utility functions.
Definition body.hh:39
This function must be specialized for the type you want to print.
Definition util.hh:78
static std::ostream & run(std::ostream &os, const T &pp)