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) : B_(other.B()), c_(other.c()), zero(other.isZero()) {} // copy constructor
37 
42  vector_x_t operator()(const Eigen::Ref<const vector_x_t>& val) const {
43  if (isZero()) return c();
44  if (Safe && B().cols() != val.rows())
45  throw std::length_error("Cannot evaluate linear variable, variable value does not have the correct dimension");
46  return B() * val + c();
47  }
48 
53  linear_variable_t& operator+=(const linear_variable_t& w1) {
54  if (w1.isZero()) return *this;
55  if (isZero()) {
56  this->B_ = w1.B_;
57  zero = w1.isZero();
58  } else {
59  this->B_ += w1.B_;
60  }
61  this->c_ += w1.c_;
62  return *this;
63  }
64 
69  linear_variable_t& operator-=(const linear_variable_t& w1) {
70  if (w1.isZero()) return *this;
71  if (isZero()) {
72  this->B_ = -w1.B_;
73  zero = w1.isZero();
74  } else {
75  this->B_ -= w1.B_;
76  }
77  this->c_ -= w1.c_;
78  return *this;
79  }
80 
85  linear_variable_t& operator/=(const double d) {
86  B_ /= d;
87  c_ /= d;
88  return *this;
89  }
90 
95  linear_variable_t& operator*=(const double d) {
96  B_ *= d;
97  c_ *= d;
98  return *this;
99  }
100 
107  linear_variable_t cross(const linear_variable_t& other) const {
108  if (B().rows() !=3)
109  throw std::invalid_argument("Can't perform cross product on linear variables with dimensions != 3 ");
110  if (B().cols() !=3)
111  throw std::invalid_argument("Can't perform cross product on linear variables more than one unknown ");
112  if (isZero() || other.isZero())
113  return linear_variable_t::Zero(3);
114  if ((B().squaredNorm() - B().diagonal().squaredNorm() > MARGIN ) || (other.B().squaredNorm() - other.B().diagonal().squaredNorm() > MARGIN ) )
115  throw std::invalid_argument("Can't perform cross product on linear variables if B is not diagonal ");
116  // (B1 x + c1) X (B2 x + c2) = (-c2X B1) x + (bX B2) x + b1Xb2
117  typename linear_variable_t::matrix_3_t newB = skew<typename linear_variable_t::matrix_3_t, typename linear_variable_t::vector_3_t>(-other.c()) * B() +
118  skew<typename linear_variable_t::matrix_3_t, typename linear_variable_t::vector_3_t>(c()) * other.B();
119  typename linear_variable_t::vector_3_t newC = ndcurves::cross(c(),other.c());
120  return linear_variable_t(newB,newC);
121  }
122 
127  static linear_variable_t Zero(size_t dim = 0) {
128  return linear_variable_t(matrix_x_t::Zero(dim, dim), vector_x_t::Zero(dim));
129  }
130 
135  static linear_variable_t X(size_t dim = 0) {
136  return linear_variable_t(matrix_x_t::Identity(dim, dim), vector_x_t::Zero(dim));
137  }
138 
139 
143  std::size_t size() const { return zero ? 0 : std::max(B_.cols(), c_.size()); }
144 
147  Numeric norm() const { return isZero() ? 0 : (B_.norm() + c_.norm()); }
148 
153  bool isApprox(const linear_variable_t& other,
154  const double prec = Eigen::NumTraits<Numeric>::dummy_precision()) const {
155  return (*this - other).norm() < prec;
156  }
157 
158  const matrix_x_t& B() const { return B_; }
159  const vector_x_t& c() const { return c_; }
160  bool isZero() const { return zero; }
161 
162  // Serialization of the class
164 
165  template <class Archive>
166  void serialize(Archive& ar, const unsigned int version) {
167  if (version) {
168  // Do something depending on version ?
169  }
170  ar& boost::serialization::make_nvp("B_", B_);
171  ar& boost::serialization::make_nvp("c_", c_);
172  ar& boost::serialization::make_nvp("zero", zero);
173  }
174 
175  private:
176  matrix_x_t B_;
177  vector_x_t c_;
178  bool zero;
179 };
180 
181 template <typename N, bool S>
183  linear_variable<N, S> res(w1.B(), w1.c());
184  return res += w2;
185 }
186 
187 template <typename N, bool S>
189  linear_variable<N, S> res(w1.B(), w1.c());
190  return res -= w2;
191 }
192 
193 template <typename N, bool S>
195  return linear_variable<N, S> (-w1.B(), -w1.c());
196 }
197 
198 template <typename N, bool S>
200  linear_variable<N, S> res(w.B(), w.c());
201  return res *= k;
202 }
203 
204 template <typename N, bool S>
206  linear_variable<N, S> res(w.B(), w.c());
207  return res *= k;
208 }
209 
210 template <typename N, bool S>
212  linear_variable<N, S> res(w.B(), w.c());
213  return res /= k;
214 }
215 
216 template <typename BezierFixed, typename BezierLinear, typename X>
217 BezierFixed evaluateLinear(const BezierLinear& bIn, const X x) {
218  typename BezierFixed::t_point_t fixed_wps;
219  for (typename BezierLinear::cit_point_t cit = bIn.waypoints().begin(); cit != bIn.waypoints().end(); ++cit)
220  fixed_wps.push_back(cit->operator()(x));
221  return BezierFixed(fixed_wps.begin(), fixed_wps.end(), bIn.T_min_, bIn.T_max_);
222 }
223 
224 template <typename N, bool S>
225 std::ostream &operator<<(std::ostream &os, const linear_variable<N, S>& l) {
226  return os << "linear_variable: \n \t B:\n"<< l.B() << "\t c: \n" << l.c().transpose();
227 }
228 
229 } // namespace ndcurves
230 
231 DEFINE_CLASS_TEMPLATE_VERSION(SINGLE_ARG(typename Numeric, bool Safe),
233 #endif //_CLASS_LINEAR_VARIABLE
std::size_t size() const
Get dimension of linear variable.
Definition: linear_variable.h:143
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
const matrix_x_t & B() const
Definition: linear_variable.h:158
Eigen::Vector3d cross(const Eigen::VectorXd &a, const Eigen::VectorXd &b)
Definition: cross_implementation.h:15
static linear_variable_t X(size_t dim=0)
Get a linear variable equal to the variable.
Definition: linear_variable.h:135
bool isZero() const
Definition: linear_variable.h:160
Eigen::Matrix< Numeric, 3, 1 > vector_3_t
Definition: linear_variable.h:29
BezierFixed evaluateLinear(const BezierLinear &bIn, const X x)
Definition: linear_variable.h:217
interface for a Curve of arbitrary dimension.
linear_variable_t & operator+=(const linear_variable_t &w1)
Add another linear variable.
Definition: linear_variable.h:53
bezier_curve< T, N, S, P > operator-(const bezier_curve< T, N, S, P > &p1)
Definition: bezier_curve.h:672
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:153
Numeric norm() const
Get norm of linear variable (Norm of B plus norm of C).
Definition: linear_variable.h:147
static linear_variable_t Zero(size_t dim=0)
Get a linear variable equal to zero.
Definition: linear_variable.h:127
class allowing to create a Bezier curve of dimension 1 <= n <= 3.
const vector_x_t & c() const
Definition: linear_variable.h:159
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:95
void serialize(Archive &ar, const unsigned int version)
Definition: linear_variable.h:166
vector_x_t operator()(const Eigen::Ref< const vector_x_t > &val) const
Linear evaluation for vector x.
Definition: linear_variable.h:42
friend class boost::serialization::access
Definition: linear_variable.h:163
Eigen::Matrix< Numeric, 3, 3 > matrix_3_t
Definition: linear_variable.h:30
linear_variable_t & operator-=(const linear_variable_t &w1)
Substract another linear variable.
Definition: linear_variable.h:69
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:85
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:107
Eigen::Matrix< Numeric, Eigen::Dynamic, Eigen::Dynamic > matrix_x_t
Definition: linear_variable.h:28
bezier_curve< T, N, S, P > operator/(const bezier_curve< T, N, S, P > &p1, const double k)
Definition: bezier_curve.h:714
bezier_curve< T, N, S, P > operator*(const bezier_curve< T, N, S, P > &p1, const double k)
Definition: bezier_curve.h:720
linear_variable< Numeric > linear_variable_t
Definition: linear_variable.h:31
Eigen::Matrix< Numeric, Eigen::Dynamic, 1 > vector_x_t
Definition: linear_variable.h:27
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:666