Cheetah Software  1.0
BezierCurve.h
Go to the documentation of this file.
1 #ifndef BEZIER_CURVE
2 #define BEZIER_CURVE
3 
4 #include <math.h>
5 
6 // N = NUM_CTR_PT -1
7 template <typename T, int DIM, int NUM_CTR_PT>
8 class BezierCurve {
9  public:
10  T _CtrlPt[NUM_CTR_PT][DIM];
11  T _coeff[NUM_CTR_PT];
14  for (int j(0); j < NUM_CTR_PT; ++j) {
15  for (int i(0); i < DIM; ++i) {
16  _CtrlPt[j][i] = 0.;
17  }
18  _coeff[j] = 0.;
19  }
20  }
21 
22  // ctrl pt 0: initial
23  // ctrl pt n-1(end): final
24  bool SetParam(T** ctrl_pt, T fin_time) {
25  _end_time = fin_time;
26  T n_fact = _factorial(NUM_CTR_PT - 1);
27  for (int j(0); j < NUM_CTR_PT; ++j) {
28  for (int i(0); i < DIM; ++i) {
29  _CtrlPt[j][i] = ctrl_pt[j][i];
30  }
31  _coeff[j] = n_fact / (_factorial(j) * _factorial((NUM_CTR_PT - 1) - j));
32  }
33  return true;
34  }
35 
36  bool getCurvePoint(T u, T* ret) {
37  if (u > _end_time) {
38  for (int i(0); i < DIM; ++i) {
39  ret[i] = _CtrlPt[NUM_CTR_PT - 1][i];
40  }
41  return true;
42  } else if (u < 0.) {
43  for (int i(0); i < DIM; ++i) {
44  ret[i] = _CtrlPt[0][i];
45  }
46  return true;
47  } else {
48  u /= _end_time;
49  for (int i(0); i < DIM; ++i) {
50  ret[i] = 0.;
51  for (int j(0); j < NUM_CTR_PT; ++j) {
52  ret[i] += _coeff[j] * pow(u, j) * pow(1 - u, (NUM_CTR_PT - 1) - j) *
53  _CtrlPt[j][i];
54  }
55  }
56  }
57  return true;
58  }
59 
60  bool getCurveVelocity(T u, T* ret) {
61  if (u > _end_time) {
62  for (int i(0); i < DIM; ++i) {
63  ret[i] = 0.;
64  }
65  return true;
66  } else if (u < 0.) {
67  for (int i(0); i < DIM; ++i) {
68  ret[i] = 0.;
69  }
70  return true;
71  } else {
72  u /= _end_time;
73  for (int i(0); i < DIM; ++i) {
74  ret[i] = 0.;
75  ret[i] += _coeff[0] *
76  (-(NUM_CTR_PT - 1) * pow(1 - u, (NUM_CTR_PT - 1) - 1)) *
77  _CtrlPt[0][i];
78  for (int j(1); j < NUM_CTR_PT - 1; ++j) {
79  ret[i] += _coeff[j] *
80  (j * pow(u, j - 1) * pow(1 - u, (NUM_CTR_PT - 1) - j) -
81  (NUM_CTR_PT - 1 - j) * pow(u, j) *
82  pow(1 - u, (NUM_CTR_PT - 1) - j - 1)) *
83  _CtrlPt[j][i];
84  }
85  ret[i] += _coeff[NUM_CTR_PT - 1] * (NUM_CTR_PT - 1) *
86  pow(u, NUM_CTR_PT - 2) * _CtrlPt[NUM_CTR_PT - 1][i];
87 
88  ret[i] /= _end_time;
89  }
90  }
91  return true;
92  }
93 
94  private:
95  int _factorial(const int& n) {
96  int ret = 1;
97  for (int i(1); i < n + 1; ++i) ret *= (i);
98 
99  return ret;
100  }
101 };
102 #endif
bool SetParam(T **ctrl_pt, T fin_time)
Definition: BezierCurve.h:24
bool getCurvePoint(T u, T *ret)
Definition: BezierCurve.h:36
T _CtrlPt[NUM_CTR_PT][DIM]
Definition: BezierCurve.h:10
T _coeff[NUM_CTR_PT]
Definition: BezierCurve.h:11
bool getCurveVelocity(T u, T *ret)
Definition: BezierCurve.h:60
int _factorial(const int &n)
Definition: BezierCurve.h:95