curve_abc.h
Go to the documentation of this file.
1 
11 #ifndef _STRUCT_CURVE_ABC
12 #define _STRUCT_CURVE_ABC
13 
14 #include <boost/serialization/shared_ptr.hpp>
15 #include <boost/smart_ptr/shared_ptr.hpp>
16 #include <functional>
17 
18 #include "MathDefs.h"
19 #include "serialization/archive.hpp"
20 #include "serialization/eigen-matrix.hpp"
21 #include "serialization/registeration.hpp"
22 
23 namespace ndcurves {
24 
25 template <typename T>
26 bool isApprox(const T a, const T b, const T eps = 1e-6) {
27  return fabs(a - b) < eps;
28 }
29 
34 template <typename Time = double, typename Numeric = Time, bool Safe = false,
35  typename Point = Eigen::Matrix<Numeric, Eigen::Dynamic, 1>,
36  typename Point_derivate = Point>
37 struct curve_abc : std::unary_function<Time, Point>,
38  public serialization::Serializable {
39  typedef Point point_t;
40  typedef Point_derivate point_derivate_t;
41  typedef Time time_t;
42  typedef Numeric num_t;
44  curve_t; // parent class
46  curve_derivate_t; // parent class
47  typedef boost::shared_ptr<curve_t> curve_ptr_t;
48 
49  /* Constructors - destructors */
50  public:
52  curve_abc() {}
53 
55  virtual ~curve_abc() {}
56  /* Constructors - destructors */
57 
58  /*Operations*/
62  virtual point_t operator()(const time_t t) const = 0;
63 
69  const std::size_t order) const = 0;
70 
76  virtual point_derivate_t derivate(const time_t t,
77  const std::size_t order) const = 0;
78 
91  const curve_t* other,
92  const Numeric prec = Eigen::NumTraits<Numeric>::dummy_precision(),
93  const size_t order = 5) const {
94  bool equal = ndcurves::isApprox<num_t>(min(), other->min()) &&
95  ndcurves::isApprox<num_t>(max(), other->max()) &&
96  (dim() == other->dim());
97  if (!equal) {
98  return false;
99  }
100  time_t inc =
101  (max() - min()) / 10.; // FIXME : define this step somewhere ??
102  // check the value along the two curves
103  time_t t = min();
104  while (t <= max()) {
105  if (!(*this)(t).isApprox(other->operator()(t), prec)) {
106  return false;
107  }
108  t += inc;
109  }
110  // check if the derivatives are equal
111  for (size_t n = 1; n <= order; ++n) {
112  t = min();
113  while (t <= max()) {
114  if (!derivate(t, n).isApprox(other->derivate(t, n), prec)) {
115  return false;
116  }
117  t += inc;
118  }
119  }
120  return true;
121  }
122 
132  virtual bool isApprox(
133  const curve_t* other,
134  const Numeric prec =
135  Eigen::NumTraits<Numeric>::dummy_precision()) const = 0;
136 
137  /*Operations*/
138 
139  /*Helpers*/
142  virtual std::size_t dim() const = 0;
145  virtual time_t min() const = 0;
148  virtual time_t max() const = 0;
151  virtual std::size_t degree() const = 0;
152 
153  std::pair<time_t, time_t> timeRange() { return std::make_pair(min(), max()); }
154  /*Helpers*/
155 
156  // Serialization of the class
157  friend class boost::serialization::access;
158  template <class Archive>
159  void serialize(Archive& ar, const unsigned int version) {
160  serialization::register_types<Archive>(ar, version);
161  if (version) {
162  // Do something depending on version ?
163  }
164  }
165 };
166 BOOST_SERIALIZATION_ASSUME_ABSTRACT(curve_abc)
167 } // namespace ndcurves
168 
169 DEFINE_CLASS_TEMPLATE_VERSION(
170  SINGLE_ARG(typename Time, typename Numeric, bool Safe, typename Point,
171  typename Point_derivate),
173 #endif //_STRUCT_CURVE_ABC
Definition: bernstein.h:20
Point point_t
Definition: curve_abc.h:39
virtual point_t operator()(const time_t t) const =0
Evaluation of the cubic spline at time t.
curve_abc< Time, Numeric, Safe, point_derivate_t > curve_derivate_t
Definition: curve_abc.h:46
virtual time_t max() const =0
Get the maximum time for which the curve is defined.
Numeric num_t
Definition: curve_abc.h:42
std::pair< time_t, time_t > timeRange()
Definition: curve_abc.h:153
virtual point_derivate_t derivate(const time_t t, const std::size_t order) const =0
Evaluate the derivative of order N of curve at time t.
boost::shared_ptr< curve_t > curve_ptr_t
Definition: curve_abc.h:47
virtual curve_derivate_t * compute_derivate_ptr(const std::size_t order) const =0
Compute the derived curve at order N.
void serialize(Archive &ar, const unsigned int version)
Definition: curve_abc.h:159
virtual std::size_t degree() const =0
Get the degree of the curve.
double Time
Definition: effector_spline.h:27
virtual bool isApprox(const curve_t *other, const Numeric prec=Eigen::NumTraits< Numeric >::dummy_precision()) const =0
isApprox check if other and *this are approximately equal given a precision treshold Only two curves ...
virtual time_t min() const =0
Get the minimum time for which the curve is defined.
curve_abc< Time, Numeric, Safe, point_t, point_derivate_t > curve_t
Definition: curve_abc.h:44
bool isApprox(const T a, const T b, const T eps=1e-6)
Definition: curve_abc.h:26
virtual std::size_t dim() const =0
Get dimension of curve.
Eigen::Matrix< Numeric, Eigen::Dynamic, 1 > Point
Definition: effector_spline.h:28
Point_derivate point_derivate_t
Definition: curve_abc.h:40
virtual ~curve_abc()
Destructor.
Definition: curve_abc.h:55
double Numeric
Definition: effector_spline.h:26
bool isEquivalent(const curve_t *other, const Numeric prec=Eigen::NumTraits< Numeric >::dummy_precision(), const size_t order=5) const
isEquivalent check if other and *this are approximately equal by values, given a precision treshold...
Definition: curve_abc.h:90
Time time_t
Definition: curve_abc.h:41
curve_abc()
Constructor.
Definition: curve_abc.h:52
Represents a curve of dimension Dim. If value of parameter Safe is false, no verification is made on ...
Definition: curve_abc.h:37