linear_variable.h
Go to the documentation of this file.
1 
9 #ifndef _CLASS_LINEAR_VARIABLE
10 #define _CLASS_LINEAR_VARIABLE
11 
12 #include "curve_abc.h"
13 #include "bezier_curve.h"
14 #include "serialization/archive.hpp"
15 #include "serialization/eigen-matrix.hpp"
16 
17 #include "MathDefs.h"
18 
19 #include <math.h>
20 #include <vector>
21 #include <Eigen/Core>
22 #include <stdexcept>
23 
24 namespace ndcurves {
25 template <typename Numeric = double, bool Safe = true>
26 struct linear_variable : public serialization::Serializable {
27  typedef Eigen::Matrix<Numeric, Eigen::Dynamic, 1> vector_x_t;
28  typedef Eigen::Matrix<Numeric, Eigen::Dynamic, Eigen::Dynamic> matrix_x_t;
29  typedef Eigen::Matrix<Numeric, 3, 1> vector_3_t;
30  typedef Eigen::Matrix<Numeric, 3, 3> matrix_3_t;
32 
33  linear_variable() : B_(matrix_x_t::Identity(0, 0)), c_(vector_x_t::Zero(0)), zero(true) {} // variable
34  linear_variable(const vector_x_t& c) : B_(matrix_x_t::Zero(c.size(), c.size())), c_(c), zero(false) {} // constant
35  linear_variable(const matrix_x_t& B, const vector_x_t& c) : B_(B), c_(c), zero(false) {} // mixed
36  linear_variable(const linear_variable_t& other)
37  : B_(other.B()), c_(other.c()), zero(other.isZero()) {} // copy constructor
38 
40 
45  vector_x_t operator()(const Eigen::Ref<const vector_x_t>& val) const {
46  if (isZero()) return c();
47  if (Safe && B().cols() != val.rows())
48  throw std::length_error("Cannot evaluate linear variable, variable value does not have the correct dimension");
49  return B() * val + c();
50  }
51 
56  linear_variable_t& operator+=(const linear_variable_t& w1) {
57  if (w1.isZero()) return *this;
58  if (isZero()) {
59  this->B_ = w1.B_;
60  this->c_ = w1.c_;
61  zero = w1.isZero();
62  } else {
63  if (Safe && B().rows() != w1.B().rows())
64  throw std::length_error("Cannot add linear variables, variables do not have the same dimension");
65  else if (B().cols() > w1.B().cols()) { // new variables added left for primitive
66  B_.block(0, B().cols() - w1.B().cols(), B().rows(), w1.B().cols()) += w1.B();
67  c_.tail(w1.c().rows()) += w1.c();
68  } else if (B().cols() < w1.B().cols()) { // new variables added left for primitive
69  linear_variable_t opp = w1 + (*this);
70  this->B_ = opp.B_;
71  this->c_ = opp.c_;
72  } else {
73  this->B_ += w1.B_;
74  this->c_ += w1.c_;
75  }
76  }
77  return *this;
78  }
79 
84  linear_variable_t& operator-=(const linear_variable_t& w1) {
85  if (w1.isZero()) return *this;
86  if (isZero()) {
87  this->B_ = -w1.B_;
88  this->c_ = -w1.c_;
89  zero = w1.isZero();
90  } else {
91  if (Safe && B().rows() != w1.B().rows())
92  throw std::length_error("Cannot add linear variables, variables do not have the same dimension");
93  else if (B().cols() > w1.B().cols()) { // new variables added left for primitive
94  B_.block(0, B().cols() - w1.B().cols(), B().rows(), w1.B().cols()) -= w1.B();
95  c_.tail(w1.c().rows()) -= w1.c();
96  } else if (B().cols() < w1.B().cols()) { // new variables added left for primitive
97  linear_variable_t opp = -w1 + (*this);
98  this->B_ = opp.B_;
99  this->c_ = opp.c_;
100  } else {
101  this->B_ -= w1.B_;
102  this->c_ -= w1.c_;
103  }
104  }
105  return *this;
106  }
107 
112  linear_variable_t& operator/=(const double d) {
113  B_ /= d;
114  c_ /= d;
115  return *this;
116  }
117 
122  linear_variable_t& operator*=(const double d) {
123  B_ *= d;
124  c_ *= d;
125  return *this;
126  }
127 
134  linear_variable_t cross(const linear_variable_t& other) const {
135  if (B().rows() != 3)
136  throw std::invalid_argument("Can't perform cross product on linear variables with dimensions != 3 ");
137  if (B().cols() != 3)
138  throw std::invalid_argument("Can't perform cross product on linear variables more than one unknown ");
139  if (isZero() || other.isZero()) return linear_variable_t::Zero(3);
140  if ((B().squaredNorm() - B().diagonal().squaredNorm() > MARGIN) ||
141  (other.B().squaredNorm() - other.B().diagonal().squaredNorm() > MARGIN))
142  throw std::invalid_argument("Can't perform cross product on linear variables if B is not diagonal ");
143  // (B1 x + c1) X (B2 x + c2) = (-c2X B1) x + (bX B2) x + b1Xb2
144  typename linear_variable_t::matrix_3_t newB =
145  skew<typename linear_variable_t::matrix_3_t, typename linear_variable_t::vector_3_t>(-other.c()) * B() +
146  skew<typename linear_variable_t::matrix_3_t, typename linear_variable_t::vector_3_t>(c()) * other.B();
147  typename linear_variable_t::vector_3_t newC = ndcurves::cross(c(), other.c());
148  return linear_variable_t(newB, newC);
149  }
150 
155  static linear_variable_t Zero(size_t dim = 0) {
156  return linear_variable_t(matrix_x_t::Zero(dim, dim), vector_x_t::Zero(dim));
157  }
158 
163  static linear_variable_t X(size_t dim = 0) {
164  return linear_variable_t(matrix_x_t::Identity(dim, dim), vector_x_t::Zero(dim));
165  }
166 
170  std::size_t size() const { return zero ? 0 : std::max(B_.rows(), c_.size()); }
171 
174  Numeric norm() const { return isZero() ? 0 : (B_.norm() + c_.norm()); }
175 
180  bool isApprox(const linear_variable_t& other,
181  const double prec = Eigen::NumTraits<Numeric>::dummy_precision()) const {
182  return (*this - other).norm() < prec;
183  }
184 
185  const matrix_x_t& B() const { return B_; }
186  const vector_x_t& c() const { return c_; }
187  bool isZero() const { return zero; }
188 
189  // Serialization of the class
191 
192  template <class Archive>
193  void serialize(Archive& ar, const unsigned int version) {
194  if (version) {
195  // Do something depending on version ?
196  }
197  ar& boost::serialization::make_nvp("B_", B_);
198  ar& boost::serialization::make_nvp("c_", c_);
199  ar& boost::serialization::make_nvp("zero", zero);
200  }
201 
202  private:
203  matrix_x_t B_;
204  vector_x_t c_;
205  bool zero;
206 };
207 
208 template <typename N, bool S>
210  linear_variable<N, S> res(w1.B(), w1.c());
211  return res += w2;
212 }
213 
214 template <typename N, bool S>
216  linear_variable<N, S> res(w1.B(), w1.c());
217  return res -= w2;
218 }
219 
220 template <typename N, bool S>
222  return linear_variable<N, S>(-w1.B(), -w1.c());
223 }
224 
225 template <typename N, bool S>
227  linear_variable<N, S> res(w.B(), w.c());
228  return res *= k;
229 }
230 
231 template <typename N, bool S>
233  linear_variable<N, S> res(w.B(), w.c());
234  return res *= k;
235 }
236 
237 template <typename N, bool S>
239  linear_variable<N, S> res(w.B(), w.c());
240  return res /= k;
241 }
242 
243 template <typename BezierFixed, typename BezierLinear, typename X>
244 BezierFixed evaluateLinear(const BezierLinear& bIn, const X x) {
245  typename BezierFixed::t_point_t fixed_wps;
246  for (typename BezierLinear::cit_point_t cit = bIn.waypoints().begin(); cit != bIn.waypoints().end(); ++cit)
247  fixed_wps.push_back(cit->operator()(x));
248  return BezierFixed(fixed_wps.begin(), fixed_wps.end(), bIn.T_min_, bIn.T_max_);
249 }
250 
251 template <typename N, bool S>
252 std::ostream& operator<<(std::ostream& os, const linear_variable<N, S>& l) {
253  return os << "linear_variable: \n \t B:\n" << l.B() << "\t c: \n" << l.c().transpose();
254 }
255 
256 } // namespace ndcurves
257 
258 DEFINE_CLASS_TEMPLATE_VERSION(SINGLE_ARG(typename Numeric, bool Safe),
260 #endif //_CLASS_LINEAR_VARIABLE
std::size_t size() const
Get dimension of linear variable.
Definition: linear_variable.h:170
Eigen::Matrix< Numeric, 3, 1 > vector_3_t
Definition: linear_variable.h:29
Definition: bernstein.h:20
linear_variable(const vector_x_t &c)
Definition: linear_variable.h:34
linear_variable(const matrix_x_t &B, const vector_x_t &c)
Definition: linear_variable.h:35
Eigen::Matrix< Numeric, 3, 3 > matrix_3_t
Definition: linear_variable.h:30
const matrix_x_t & B() const
Definition: linear_variable.h:185
Eigen::Vector3d cross(const Eigen::VectorXd &a, const Eigen::VectorXd &b)
Definition: cross_implementation.h:14
static linear_variable_t X(size_t dim=0)
Get a linear variable equal to the variable.
Definition: linear_variable.h:163
bool isZero() const
Definition: linear_variable.h:187
BezierFixed evaluateLinear(const BezierLinear &bIn, const X x)
Definition: linear_variable.h:244
interface for a Curve of arbitrary dimension.
linear_variable_t & operator+=(const linear_variable_t &w1)
Add another linear variable.
Definition: linear_variable.h:56
bezier_curve< T, N, S, P > operator/(const bezier_curve< T, N, S, P > &p1, const double k)
Definition: bezier_curve.h:711
bool isApprox(const linear_variable_t &other, const double prec=Eigen::NumTraits< Numeric >::dummy_precision()) const
Check if actual linear variable and other are approximately equal given a precision treshold...
Definition: linear_variable.h:180
bezier_curve< T, N, S, P > operator+(const bezier_curve< T, N, S, P > &p1, const bezier_curve< T, N, S, P > &p2)
Definition: bezier_curve.h:660
Numeric norm() const
Get norm of linear variable (Norm of B plus norm of C).
Definition: linear_variable.h:174
static linear_variable_t Zero(size_t dim=0)
Get a linear variable equal to zero.
Definition: linear_variable.h:155
class allowing to create a Bezier curve of dimension 1 <= n <= 3.
const vector_x_t & c() const
Definition: linear_variable.h:186
bezier_curve< T, N, S, P > operator*(const bezier_curve< T, N, S, P > &p1, const double k)
Definition: bezier_curve.h:717
linear_variable_t & operator*=(const double d)
Multiply by a constant : p_i / d = B_i*x*d + c_i*d.
Definition: linear_variable.h:122
void serialize(Archive &ar, const unsigned int version)
Definition: linear_variable.h:193
vector_x_t operator()(const Eigen::Ref< const vector_x_t > &val) const
Linear evaluation for vector x.
Definition: linear_variable.h:45
friend class boost::serialization::access
Definition: linear_variable.h:190
linear_variable_t & operator-=(const linear_variable_t &w1)
Substract another linear variable.
Definition: linear_variable.h:84
Definition: fwd.h:55
linear_variable_t & operator/=(const double d)
Divide by a constant : p_i / d = B_i*x/d + c_i/d.
Definition: linear_variable.h:112
linear_variable(const linear_variable_t &other)
Definition: linear_variable.h:36
linear_variable()
Definition: linear_variable.h:33
double Numeric
Definition: effector_spline.h:26
linear_variable_t cross(const linear_variable_t &other) const
Compute the cross product of the current linear_variable and the other. This method of course only ma...
Definition: linear_variable.h:134
Eigen::Matrix< Numeric, Eigen::Dynamic, Eigen::Dynamic > matrix_x_t
Definition: linear_variable.h:28
linear_variable< Numeric > linear_variable_t
Definition: linear_variable.h:31
~linear_variable()
Definition: linear_variable.h:39
bezier_curve< T, N, S, P > operator-(const bezier_curve< T, N, S, P > &p1)
Definition: bezier_curve.h:666
Eigen::Matrix< Numeric, Eigen::Dynamic, 1 > vector_x_t
Definition: linear_variable.h:27