ocra-recipes
Doxygen documentation for the ocra-recipes repository
uncompress.h
Go to the documentation of this file.
1 
12 #ifndef _OCRA_UTILS_UNCOMPRESS_H_
13 #define _OCRA_UTILS_UNCOMPRESS_H_
14 
15 #ifdef WIN32
16 # pragma once
17 #endif
18 
19 #include <Eigen/Eigen>
20 namespace ocra{using namespace Eigen;}
21 
22 namespace ocra
23 {
24  namespace utils
25  {
26  template <class T, class U>
28  {
29  // to understand a bit more the following return type
30  //typedef const Eigen::CwiseBinaryOp<Eigen::internal::scalar_min_op<typename Eigen::internal::traits<V>::Scalar>, const Eigen::ArrayWrapper<V>, const Eigen::ArrayWrapper<W> > return_t;
31  //typedef Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<typename Eigen::internal::traits<V>::Scalar>, Eigen::ArrayWrapper<W> > mult_t;
32  //typedef const Eigen::CwiseBinaryOp<Eigen::internal::scalar_min_op<typename Eigen::internal::traits<V>::Scalar>, Eigen::ArrayWrapper<V>, mult_t > return2_t;
33 
34  template <class V, class W>
35  static const Eigen::CwiseBinaryOp<Eigen::internal::scalar_min_op<typename Eigen::internal::traits<V>::Scalar>, const Eigen::ArrayWrapper<V>, const Eigen::ArrayWrapper<W> >
36  run(const V& a, const W& b) { return a.array().min(b.array()); }
37 
38  template <class V, class W>
39  static const Eigen::CwiseBinaryOp<Eigen::internal::scalar_min_op<typename Eigen::internal::traits<V>::Scalar>, const Eigen::ArrayWrapper<V>, const Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<typename Eigen::internal::traits<T>::Scalar>, const Eigen::ArrayWrapper<W> > >
40  run(const V& a, double alpha, const W& b) { return a.array().min(alpha*b.array()); }
41  };
42 
43  template <>
44  struct specialized_min<double, double>
45  {
46  static const double& run(const double& a, const double& b) { return std::min(a, b); }
47  static const double& run(const double& a, double alpha, const double& b) { return std::min(a, alpha*b); }
48  };
49 
50 
51  template <class T, class U>
53  {
54  //typedef const Eigen::CwiseBinaryOp<Eigen::internal::scalar_max_op<typename Eigen::internal::traits<T>::Scalar>, Eigen::ArrayWrapper<T>, Eigen::ArrayWrapper<U> > return_t;
55  //typedef Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<typename Eigen::internal::traits<T>::Scalar>, Eigen::ArrayWrapper<U> > mult_t;
56  //typedef const Eigen::CwiseBinaryOp<Eigen::internal::scalar_max_op<typename Eigen::internal::traits<T>::Scalar>, Eigen::ArrayWrapper<T>, mult_t > return2_t;
57 
58  template <class V, class W>
59  static const Eigen::CwiseBinaryOp<Eigen::internal::scalar_max_op<typename Eigen::internal::traits<V>::Scalar>, const Eigen::ArrayWrapper<V>, const Eigen::ArrayWrapper<W> >
60  run(const V& a, const W& b) { return a.array().max(b.array()); }
61  template <class V, class W>
62  static const Eigen::CwiseBinaryOp<Eigen::internal::scalar_max_op<typename Eigen::internal::traits<V>::Scalar>, const Eigen::ArrayWrapper<V>, const Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<typename Eigen::internal::traits<V>::Scalar>, const Eigen::ArrayWrapper<W> > >
63  run(const V& a, double alpha, const W& b) { return a.array().max(alpha*b.array()); }
64  };
65 
66  template <>
67  struct specialized_max<double, double>
68  {
69  static const double& run(const double& a, const double& b) { return std::max(a, b); }
70  static const double& run(const double& a, double alpha, const double& b) { return std::max(a, alpha*b); }
71  };
72 
73 
74  template<class T, class U>
76  {
77  static void run(T& out, const U& in) { out = in; }
78  static void run(T& out, double alpha, const U& in) { out = alpha*in; }
79  };
80 
81  template<class T, class U>
82  struct add_functor
83  {
84  static void run(T& out, const U& in) { out += in; }
85  static void run(T& out, double alpha, const U& in) { out += alpha*in; }
86  };
87 
88  template<class T, class U>
89  struct min_functor
90  {
91  static void run(T& out, const U& in) { out = out.array().min(in.array());/*specialized_min<T,U>::run(out, in);*/ }
92  static void run(T& out, double alpha, const U& in) { out = out.array().min(alpha*in.array());/*specialized_min<T,U>::run(out, alpha, in);*/ }
93  };
94 
95  template<class T, class U>
96  struct max_functor
97  {
98  static void run(T& out, const U& in) { out = out.array().max(in.array()); /*specialized_max<T,U>::run(out, in);*/ }
99  static void run(T& out, double alpha, const U& in) { out = out.array().max(alpha*in.array()); /*specialized_max<T,U>::run(out, alpha, in);*/ }
100  };
101 
102 
103 
104  template<template <class T, class U> class Functor, bool ApplyMappingToOut = true>
105  struct uncompress
106  {
107  template<typename ArgType>
108  static ArgType selector(ArgType i, ArgType j) { return ApplyMappingToOut?i:j; }
109 
110  template<class Derived1, class Derived2>
111  static void uncompressByCol(const MatrixBase<Derived1>& in, MatrixBase<Derived2>& out,
112  const std::vector<int>& mapping, double scale)
113  {
114  typedef typename MatrixBase<Derived1>::ConstColXpr col1_t;
115  typedef typename MatrixBase<Derived2>::ColXpr col2_t;
116 
117  const int N = static_cast<int>( selector(in.cols(), out.cols()) );
118  ocra_assert(in.rows() == out.rows());
119  ocra_assert(N == mapping.size());
120  if (scale == 1.)
121  {
122  for (int i=0; i<N; ++i) {
123  col2_t c2 = out.col(selector(mapping[i],i));
124  Functor<col2_t, col1_t>::run(c2,
125  in.col(selector(i, mapping[i])));
126  }
127  }
128  else
129  {
130  for (int i=0; i<N; ++i) {
131  col2_t c2 = out.col(selector(mapping[i],i));
132  Functor<col2_t, col1_t>::run(c2,
133  scale,
134  in.col(selector(i, mapping[i])));
135  }
136  }
137  }
138 
139  template<class Derived1, class Derived2>
140  static void uncompressByRow(const MatrixBase<Derived1>& in, MatrixBase<Derived2>& out,
141  const std::vector<int>& mapping, double scale)
142  {
143  typedef typename MatrixBase<Derived1>::ConstRowXpr row1_t;
144  typedef typename MatrixBase<Derived2>::RowXpr row2_t;
145 
146  const int N = static_cast<int>( selector(in.rows(), out.rows()) );
147  ocra_assert(in.cols() == out.cols());
148  ocra_assert(N == mapping.size());
149  if (scale == 1.)
150  {
151  for (int i=0; i<N; ++i) {
152  row2_t r2 = out.row(selector(mapping[i],i));
153  Functor<row2_t, row1_t>::run(r2,
154  in.row(selector(i, mapping[i])));
155  }
156  }
157  else
158  {
159  for (int i=0; i<N; ++i) {
160  row2_t r2 = out.row(selector(mapping[i],i));
161  Functor<row2_t, row1_t>::run(r2, scale,
162  in.row(selector(i, mapping[i])));
163  }
164  }
165  }
166 
167  template<class Derived1, class Derived2>
168  static void uncompress2d(const MatrixBase<Derived1>& in, MatrixBase<Derived2>& out,
169  const std::vector<int>& mappingRow, const std::vector<int>& mappingCol, double scale)
170  {
171  typedef typename Eigen::internal::traits<Derived1>::Scalar scalar1_t;
172  typedef typename Eigen::internal::traits<Derived2>::Scalar scalar2_t;
173 
174  const int Nc = static_cast<int>( selector(in.cols(), out.cols()) );
175  const int Nr = static_cast<int>( selector(in.rows(), out.rows()) );
176  ocra_assert(Nc == mappingCol.size());
177  ocra_assert(Nr == mappingRow.size());
178  if (scale==1.)
179  {
180  for (int i=0; i<Nc; ++i)
181  {
182  int n = mappingCol[i];
183  for (int j=0; j<Nr; ++j) {
184  scalar2_t& s2 = out(selector(mappingRow[j], j), selector(n, i));
185  Functor<scalar2_t, scalar1_t>::run(s2,
186  in(selector(j, mappingRow[j]), selector(i, n)));
187  }
188  }
189  }
190  else
191  {
192  for (int i=0; i<Nc; ++i)
193  {
194  int n = mappingCol[i];
195  for (int j=0; j<Nr; ++j) {
196  scalar2_t& s2 = out(selector(mappingRow[j], j), selector(n, i));
197  Functor<scalar2_t, scalar1_t>::run(s2, scale,
198  in(selector(j, mappingRow[j]), selector(i, n)));
199  }
200  }
201  }
202  }
203  };
204 
205  }//utils
206 }//ocra
207 
208 #endif //_OCRA_UTILS_UNCOMPRESS_H_
209 
210 // cmake:sourcegroup=utils
static const Eigen::CwiseBinaryOp< Eigen::internal::scalar_min_op< typename Eigen::internal::traits< V >::Scalar >, const Eigen::ArrayWrapper< V >, const Eigen::CwiseUnaryOp< Eigen::internal::scalar_multiple_op< typename Eigen::internal::traits< T >::Scalar >, const Eigen::ArrayWrapper< W > > > run(const V &a, double alpha, const W &b)
Definition: uncompress.h:40
static const Eigen::CwiseBinaryOp< Eigen::internal::scalar_max_op< typename Eigen::internal::traits< V >::Scalar >, const Eigen::ArrayWrapper< V >, const Eigen::ArrayWrapper< W > > run(const V &a, const W &b)
Definition: uncompress.h:60
static const double & run(const double &a, const double &b)
Definition: uncompress.h:69
static void run(T &out, const U &in)
Definition: uncompress.h:84
static void run(T &out, double alpha, const U &in)
Definition: uncompress.h:99
static void run(T &out, double alpha, const U &in)
Definition: uncompress.h:92
static void run(T &out, const U &in)
Definition: uncompress.h:91
static void uncompress2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > &out, const std::vector< int > &mappingRow, const std::vector< int > &mappingCol, double scale)
Definition: uncompress.h:168
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
static const double & run(const double &a, const double &b)
Definition: uncompress.h:46
static void uncompressByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > &out, const std::vector< int > &mapping, double scale)
Definition: uncompress.h:140
static void run(T &out, double alpha, const U &in)
Definition: uncompress.h:78
static const double & run(const double &a, double alpha, const double &b)
Definition: uncompress.h:70
static void run(T &out, const U &in)
Definition: uncompress.h:77
static ArgType selector(ArgType i, ArgType j)
Definition: uncompress.h:108
static const Eigen::CwiseBinaryOp< Eigen::internal::scalar_min_op< typename Eigen::internal::traits< V >::Scalar >, const Eigen::ArrayWrapper< V >, const Eigen::ArrayWrapper< W > > run(const V &a, const W &b)
Definition: uncompress.h:36
static const Eigen::CwiseBinaryOp< Eigen::internal::scalar_max_op< typename Eigen::internal::traits< V >::Scalar >, const Eigen::ArrayWrapper< V >, const Eigen::CwiseUnaryOp< Eigen::internal::scalar_multiple_op< typename Eigen::internal::traits< V >::Scalar >, const Eigen::ArrayWrapper< W > > > run(const V &a, double alpha, const W &b)
Definition: uncompress.h:63
static const double & run(const double &a, double alpha, const double &b)
Definition: uncompress.h:47
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
static void uncompressByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > &out, const std::vector< int > &mapping, double scale)
Definition: uncompress.h:111
static void run(T &out, const U &in)
Definition: uncompress.h:98
static void run(T &out, double alpha, const U &in)
Definition: uncompress.h:85