Choreonoid  1.8
UniformCubicBSpline.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_UTIL_UNIFORM_CUBIC_BSPLINE_H_INCLUDED
6 #define CNOID_UTIL_UNIFORM_CUBIC_BSPLINE_H_INCLUDED
7 
8 #include <Eigen/Core>
9 
10 namespace cnoid {
11 
12 template <class ElementType>
14 {
15  int numSamples;
16  double dtinv;
17 
18 protected:
19  Eigen::Matrix<ElementType, 4, 4> M;
20 
21 public:
22  UniformCubicBSplineBase(int numSamples, double dtinv)
23  : numSamples(numSamples),
24  dtinv(dtinv) {
25  M <<
26  -1.0, 3.0, -3.0, 1.0,
27  3.0, -6.0, 3.0, 0.0,
28  -3.0, 0.0, 3.0, 0.0,
29  1.0, 4.0, 1.0, 0.0;
30  }
31 
32  void findKnotPosition(ElementType t, int* out_knot, Eigen::Matrix<ElementType, 1, 4>& out_u) {
33 
34  out_knot[1] = t * dtinv;
35  out_knot[0] = out_knot[1] - 1;
36  out_knot[2] = out_knot[1] + 1;
37  out_knot[3] = out_knot[1] + 2;
38 
39  if(out_knot[0] < 0){
40  out_knot[0] = 0;
41  if(out_knot[1] < 0){
42  out_knot[1] = 0;
43  t = 0.0;
44  }
45 
46  } else if(out_knot[3] >= numSamples){
47  out_knot[3] = numSamples - 1;
48  if(out_knot[2] >= numSamples){
49  out_knot[2] = numSamples - 1;
50  if(out_knot[1] >= numSamples){
51  out_knot[1] = numSamples - 1;
52  t = 1.0;
53  }
54  }
55  }
56  t = fmod(t * dtinv, 1.0);
57 
58  ElementType t2 = t * t;
59  out_u << (t2 * t), t2, t, 1.0;
60  }
61 };
62 
63 
64 template <class ElementType>
65 class UniformCubicBSpline : public UniformCubicBSplineBase<ElementType>
66 {
68  ElementType* x;
69 
70 public:
71  UniformCubicBSpline(ElementType* x, int numSamples, double dtinv)
72  : UniformCubicBSplineBase<ElementType>(numSamples, dtinv) {
73  }
74 
75  double calc(double t) {
76 
77  int knot[4];
78  Eigen::Matrix<ElementType, 1, 4> u;
79  SuperClass::findKnotPosition(t, knot, u);
80 
81  Eigen::Matrix<ElementType, 4, 1> p;
82  p << x[knot[0]], x[knot[1]], x[knot[2]], x[knot[3]];
83 
84  return (u.dot(UniformCubicBSplineBase<ElementType>::M * p)) / 6.0;
85  }
86 };
87 
88 
89 template <class ContainerType, class VectorType, class ElementType>
91 {
93  const ContainerType& container;
94  int vectorSize;
95 
96 public:
97 
98  typedef Eigen::Matrix<ElementType, Eigen::Dynamic, 1> SolutionType;
99 
100  UniformCubicBSplineVector(const ContainerType& container, int numSamples, int vectorSize, double dtinv)
101  : UniformCubicBSplineBase<ElementType>(numSamples, dtinv),
102  container(container),
103  vectorSize(vectorSize) {
104  }
105 
106  void calc(double t, SolutionType& out_solution) {
107 
108  int knot[4];
109  Eigen::Matrix<ElementType, 1, 4> u;
110  SuperClass::findKnotPosition(t, knot, u);
111 
112  typedef Eigen::Matrix<ElementType, 4, Eigen::Dynamic> ValueMatrix;
113  ValueMatrix P(4, vectorSize);
114 
115  for(int i=0; i < 4; ++i){
116  VectorType value = container[knot[i]];
117  typename ValueMatrix::RowXpr p = P.row(i);
118  for(int j=0; j < vectorSize; ++j){
119  p(j) = value[j];
120  }
121  }
122 
123  out_solution = u * (UniformCubicBSplineBase<ElementType>::M * P) / 6.0;
124  }
125 };
126 }
127 
128 #endif
cnoid::UniformCubicBSpline
Definition: UniformCubicBSpline.h:65
cnoid::UniformCubicBSplineVector
Definition: UniformCubicBSpline.h:90
cnoid::UniformCubicBSpline::UniformCubicBSpline
UniformCubicBSpline(ElementType *x, int numSamples, double dtinv)
Definition: UniformCubicBSpline.h:71
cnoid::UniformCubicBSplineBase::UniformCubicBSplineBase
UniformCubicBSplineBase(int numSamples, double dtinv)
Definition: UniformCubicBSpline.h:22
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::UniformCubicBSplineVector::SolutionType
Eigen::Matrix< ElementType, Eigen::Dynamic, 1 > SolutionType
Definition: UniformCubicBSpline.h:98
cnoid::UniformCubicBSplineVector::UniformCubicBSplineVector
UniformCubicBSplineVector(const ContainerType &container, int numSamples, int vectorSize, double dtinv)
Definition: UniformCubicBSpline.h:100
cnoid::UniformCubicBSplineBase
Definition: UniformCubicBSpline.h:13
cnoid::UniformCubicBSplineBase::findKnotPosition
void findKnotPosition(ElementType t, int *out_knot, Eigen::Matrix< ElementType, 1, 4 > &out_u)
Definition: UniformCubicBSpline.h:32
cnoid::UniformCubicBSplineVector::calc
void calc(double t, SolutionType &out_solution)
Definition: UniformCubicBSpline.h:106
cnoid::UniformCubicBSpline::calc
double calc(double t)
Definition: UniformCubicBSpline.h:75
cnoid::UniformCubicBSplineBase::M
Eigen::Matrix< ElementType, 4, 4 > M
Definition: UniformCubicBSpline.h:19