Cheetah Software  1.0
test_spline.cpp
Go to the documentation of this file.
1 #include "gmock/gmock.h"
2 #include "gtest/gtest.h"
3 
7 #include "Utilities/save_file.h"
8 #include "Utilities/utilities.h"
9 #include <Configuration.h>
10 #include "../user/WBC_Controller/WBC_States/Bounding/CtrlSet/ImpulseCurve.hpp"
11 #include "Utilities/Timer.h"
12 
13 std::string folder_name = "/common/test/test_data/";
14 
15 TEST(Spline, BezierCurve_test) {
16  constexpr int dim = 3;
17  constexpr int num_ctrl_pt = 4;
18 
20  double** ctrl_pt = new double*[num_ctrl_pt];
21 
22  for (int i(0); i < 4; ++i) {
23  ctrl_pt[i] = new double[dim];
24  }
25  double gap(0.1);
26  // Initial
27  ctrl_pt[0][0] = 0.0;
28  ctrl_pt[0][1] = 0.0;
29  ctrl_pt[0][2] = 0.4;
30 
31  // mid 1
32  ctrl_pt[1][0] = 0.4;
33  ctrl_pt[1][1] = -0.7;
34  ctrl_pt[1][2] = ctrl_pt[0][2] + gap * 0.3;
35 
36  // mid 2
37  ctrl_pt[2][0] = 2.5;
38  ctrl_pt[2][1] = 0.5;
39  ctrl_pt[2][2] = ctrl_pt[1][2] + gap * 0.6;
40 
41  // Final
42  ctrl_pt[3][0] = 4.2;
43  ctrl_pt[3][1] = -1.0;
44  ctrl_pt[3][2] = ctrl_pt[2][2] + gap * 1.0;
45 
46  double end_time(3.);
47  bc.SetParam(ctrl_pt, end_time);
48 
49  double curve_pt[dim];
50  double curve_vel[dim];
51 
52  // 2nd curve
53  double mid_time(2.);
54  bc.getCurvePoint(mid_time, curve_pt);
55  for (size_t i(0); i < dim; ++i) ctrl_pt[0][i] = curve_pt[i];
56 
57  ctrl_pt[1][2] = ctrl_pt[0][2] + gap;
58  ctrl_pt[2][2] = ctrl_pt[1][2] + gap * 1.3;
59  ctrl_pt[3][2] = ctrl_pt[2][2] + gap * 1.7;
60 
62  bc2.SetParam(ctrl_pt, end_time);
63 
64  // std::string folder_name = "/common/test/test_data/";
65  //create_folder(folder_name);
66  double t;
67  double dt = 0.005;
68  for (int i(0); i < 1001; ++i) {
69  t = (double)i * dt;
70  if (t < mid_time) {
71  bc.getCurvePoint(t, curve_pt);
72  bc.getCurveVelocity(t, curve_vel);
73  } else {
74  bc2.getCurvePoint(t - mid_time, curve_pt);
75  bc2.getCurveVelocity(t - mid_time, curve_vel);
76  }
77 // saveVector(curve_pt, folder_name, "bezier_pos", dim);
78 // saveVector(curve_vel, folder_name, "bezier_vel", dim);
79 // saveValue(t, folder_name, "bz_time");
80  }
81 
82  bc.getCurvePoint(0., curve_pt);
83  EXPECT_TRUE(fpEqual(curve_pt[0], 0., .0001));
84  EXPECT_TRUE(fpEqual(curve_pt[1], 0.0, .0001));
85  EXPECT_TRUE(fpEqual(curve_pt[2], 0.4, .0001));
86 
87  bc.getCurvePoint(end_time, curve_pt);
88  EXPECT_TRUE(fpEqual(curve_pt[0], 4.2, .0001));
89  EXPECT_TRUE(fpEqual(curve_pt[1], -1.0, .0001));
90  // EXPECT_TRUE(fpEqual(curve_pt[2], 0.9, .0001));
91 
92  for (int i(0); i < num_ctrl_pt; ++i) {
93  delete[] ctrl_pt[i];
94  }
95  delete[] ctrl_pt;
96 }
97 
98 TEST(Spline, BSpline_test) {
99  constexpr int dim_bs = 3;
100  constexpr int degree = 3;
101  constexpr int num_middle_pt = 3;
102  constexpr int ini_cstr_level = 2;
103  constexpr int fin_cstr_level = 2;
104 
105  BS_Basic<double, dim_bs, degree, num_middle_pt, ini_cstr_level,
106  fin_cstr_level>
107  bs;
108  double ini_pt[dim_bs * (degree)];
109  double fin_pt[dim_bs * (degree)];
110 
111  for (int i(0); i < dim_bs * degree; ++i) {
112  ini_pt[i] = 0.;
113  fin_pt[i] = 0.;
114  }
115  double height_gap = 0.3;
116  ini_pt[0] = 0.0; // Initial
117  ini_pt[1] = 0.0;
118  ini_pt[2] = 0.4;
119 
120  double** middle_pt = NULL;
121  if (num_middle_pt > 1) {
122  middle_pt = new double*[num_middle_pt];
123  for (int i(0); i < num_middle_pt; ++i) {
124  middle_pt[i] = new double[dim_bs];
125  }
126  // Middle 1
127  middle_pt[0][0] = 0.4;
128  middle_pt[0][1] = -0.7;
129  middle_pt[0][2] = ini_pt[2] + height_gap * 0.25;
130 
131  // Middle 2
132  middle_pt[1][0] = 0.4;
133  middle_pt[1][1] = -0.7;
134  middle_pt[1][2] = ini_pt[2] + height_gap * 0.5;
135 
136  // Middle 3
137  middle_pt[2][0] = 2.5;
138  middle_pt[2][1] = 0.5;
139  middle_pt[2][2] = ini_pt[2] + height_gap * 0.75;
140  }
141  // Final
142  fin_pt[0] = 4.2;
143  fin_pt[1] = -1.0;
144  fin_pt[2] = ini_pt[2] + height_gap;
145 
146  fin_pt[3] = 0.0; // Final vel
147  fin_pt[4] = 0.0;
148  fin_pt[5] = 0.0;
149 
150  fin_pt[6] = 0.0; // Final acc
151  fin_pt[7] = 0.0;
152  fin_pt[8] = 0.0;
153 
154  double end_time(4.);
155  bs.SetParam(ini_pt, fin_pt, middle_pt, end_time);
156 
157  double curve_pt[dim_bs];
158  double curve_vel[dim_bs];
159  double curve_acc[dim_bs];
160  double curve_d3[dim_bs];
161 
162  // Spline 2
163  double mid_time(1.);
164  bs.getCurvePoint(mid_time, curve_pt);
165  bs.getCurveDerPoint(mid_time, 1, curve_vel);
166  bs.getCurveDerPoint(mid_time, 2, curve_acc);
167  if (degree > 3) {
168  bs.getCurveDerPoint(mid_time, 3, curve_d3);
169  }
170  BS_Basic<double, dim_bs, degree, num_middle_pt, ini_cstr_level,
171  fin_cstr_level>
172  bs_2;
173  double ini_pt2[dim_bs * degree];
174  double fin_pt2[dim_bs * degree];
175  for (int i(0); i < dim_bs * degree; ++i) {
176  ini_pt2[i] = 0.;
177  fin_pt2[i] = 0.;
178  }
179  for (int i(0); i < 3; ++i) {
180  ini_pt2[i] = curve_pt[i]; // Initial
181  ini_pt2[i + 3] = curve_vel[i];
182  ini_pt2[i + 6] = curve_acc[i];
183 
184  if (degree > 3) ini_pt2[i + 9] = curve_d3[i];
185  }
186 
187  double** middle_pt2 = NULL;
188  if (num_middle_pt > 0) {
189  middle_pt2 = new double*[num_middle_pt];
190  for (int i(0); i < num_middle_pt; ++i) {
191  middle_pt2[i] = new double[dim_bs];
192  }
193  // Middle 1
194  middle_pt2[0][0] = 0.4;
195  middle_pt2[0][1] = -0.7;
196  middle_pt2[0][2] = ini_pt2[2] + height_gap * 0.25;
197 
198  // Middle 2
199  middle_pt2[1][0] = 0.4;
200  middle_pt2[1][1] = -0.7;
201  middle_pt2[1][2] = ini_pt2[2] + height_gap * 0.5;
202 
203  // Middle 3
204  middle_pt2[2][0] = 2.5;
205  middle_pt2[2][1] = 0.5;
206  middle_pt2[2][2] = ini_pt2[2] + height_gap * 0.75;
207  }
208 
209  // Final
210  fin_pt2[0] = 4.2;
211  fin_pt2[1] = -1.0;
212  fin_pt2[2] = ini_pt2[2] + height_gap;
213 
214  bs_2.SetParam(ini_pt2, fin_pt2, middle_pt2, end_time);
215 
216  double t;
217  double dt(0.005);
218  for (int i(0); i < 1001; ++i) {
219  t = (double)i * dt;
220 
221  if (t < mid_time) {
222  bs.getCurvePoint(t, curve_pt);
223  bs.getCurveDerPoint(t, 1, curve_vel);
224  bs.getCurveDerPoint(t, 2, curve_acc);
225  } else {
226  bs_2.getCurvePoint(t - mid_time, curve_pt);
227  bs_2.getCurveDerPoint(t - mid_time, 1, curve_vel);
228  bs_2.getCurveDerPoint(t - mid_time, 2, curve_acc);
229  }
230 
231  // saveVector(curve_pt, folder_name, "bspline_pos", dim_bs);
232  // saveVector(curve_vel, folder_name, "bspline_vel", dim_bs);
233  // saveVector(curve_acc, folder_name, "bspline_acc", dim_bs);
234  // saveValue(t, folder_name, "bs_time");
235  }
236 
237  bs.getCurvePoint(0., curve_pt);
238  EXPECT_TRUE(fpEqual(curve_pt[0], 0., .0001));
239  EXPECT_TRUE(fpEqual(curve_pt[1], 0.0, .0001));
240  EXPECT_TRUE(fpEqual(curve_pt[2], 0.4, .0001));
241 
242  bs.getCurvePoint(end_time, curve_pt);
243  EXPECT_TRUE(fpEqual(curve_pt[0], 4.2, .0001));
244  EXPECT_TRUE(fpEqual(curve_pt[1], -1.0, .0001));
245  // EXPECT_TRUE(fpEqual(curve_pt[2], ini_pt[2]+ height_gap, .0001));
246 
247  bs.getCurveDerPoint(end_time, 2, curve_acc);
248  EXPECT_TRUE(fpEqual(curve_acc[0], 0., .0001));
249  EXPECT_TRUE(fpEqual(curve_acc[1], 0.0, .0001));
250  EXPECT_TRUE(fpEqual(curve_acc[2], 0.0, .0001));
251 
252  for (int i(0); i < num_middle_pt; ++i) {
253  delete[] middle_pt[i];
254  }
255  delete[] middle_pt;
256 }
257 
258 TEST(Spline, BSpline_1D_test) {
259  constexpr int dim_bs = 1;
260  constexpr int degree = 3;
261  constexpr int num_middle_pt = 3;
262  constexpr int ini_cstr_level = 2;
263  constexpr int fin_cstr_level = 2;
264 
265  BS_Basic<double, dim_bs, degree, num_middle_pt, ini_cstr_level,
266  fin_cstr_level>
267  bs;
268  double ini_pt[dim_bs * (degree)];
269  double fin_pt[dim_bs * (degree)];
270 
271  for (int i(0); i < dim_bs * degree; ++i) {
272  ini_pt[i] = 0.;
273  fin_pt[i] = 0.;
274  }
275  double height_gap = 0.3;
276  ini_pt[0] = 0.2; // Initial
277  ini_pt[1] = 0.0;
278  ini_pt[2] = 0.0;
279 
280  double** middle_pt = NULL;
281  if (num_middle_pt > 1) {
282  middle_pt = new double*[num_middle_pt];
283  for (int i(0); i < num_middle_pt; ++i) {
284  middle_pt[i] = new double[dim_bs];
285  }
286  // Middle 1
287  middle_pt[0][0] = ini_pt[0] + height_gap * 0.25;
288 
289  // Middle 2
290  middle_pt[1][0] = ini_pt[0] + height_gap * 0.5;
291 
292  // Middle 3
293  middle_pt[2][0] = ini_pt[0] + height_gap * 0.75;
294  }
295  // Final
296  fin_pt[0] = ini_pt[0] + height_gap;
297 
298  double end_time(5.);
299  bs.SetParam(ini_pt, fin_pt, middle_pt, end_time);
300 
301  double curve_pt[dim_bs];
302  curve_pt[0] = 0.;
303  double curve_vel[dim_bs];
304  curve_vel[0] = 0.;
305  double curve_acc[dim_bs];
306  curve_acc[0] = 0.;
307 
308  double t;
309  double dt(0.005);
310  Vec3<double> bs_pva;
311  for (int i(0); i < 1001; ++i) {
312  t = (double)i * dt;
313 
314  bs.getCurvePoint(t, curve_pt);
315  bs.getCurveDerPoint(t, 1, curve_vel);
316  bs.getCurveDerPoint(t, 2, curve_acc);
317 
318  bs_pva[0] = curve_pt[0];
319  bs_pva[1] = curve_vel[0];
320  bs_pva[2] = curve_acc[0];
321 // saveVector(bs_pva, folder_name, "bspline_1d");
322 // saveValue(t, folder_name, "bs_time_1d");
323  }
324 
325  for (int i(0); i < num_middle_pt; ++i) {
326  delete[] middle_pt[i];
327  }
328  delete[] middle_pt;
329 }
330 
331 TEST(Spline, ImpulseCurve) {
332  ImpulseCurve<double> curve;
333 
334  double apex_value(2.9);
335  double time(0.1);
336  curve.setCurve(apex_value, time);
337 
338  double curve_pt;
339  double t;
340  double dt(0.001);
341  double sum(0.);
342  for (int i(-10); i < 501; ++i) {
343  t = (double)i * dt;
344 
345  curve_pt = curve.getValue(t);
346  sum += (curve_pt * dt);
347 // saveValue(curve_pt, folder_name, "curve_pos");
348 // saveValue(t, folder_name, "curve_time");
349  }
350  double integrated_value = 0.7 * apex_value * time;
351 
352  // printf("sum, integration: (%f, %f)\n", sum, integrated_value);
353 
354  EXPECT_TRUE(fpEqual(curve.getValue(time / 2.), apex_value, .0001));
355  EXPECT_TRUE(fpEqual(integrated_value, sum, .0001));
356 }
bool fpEqual(T a, T b, T tol)
Definition: utilities.h:15
bool SetParam(T **ctrl_pt, T fin_time)
Definition: BezierCurve.h:24
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
TEST(Spline, BezierCurve_test)
Definition: test_spline.cpp:15
bool getCurvePoint(T u, T *ret)
Definition: BezierCurve.h:36
Timer for measuring how long things take.
std::string folder_name
Definition: test_spline.cpp:13
bool SetParam(T *init, T *fin, T **middle_pt, T fin_time)
Definition: BSplineBasic.h:63
bool getCurveVelocity(T u, T *ret)
Definition: BezierCurve.h:60