Code-Eli  0.3.6
piecewise_circle_creator_test_suite.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef piecewise_circle_creator_test_suite_hpp
14 #define piecewise_circle_creator_test_suite_hpp
15 
16 #include <cmath> // std::pow, std::exp
17 
18 #include <typeinfo> // typeid
19 #include <string> // std::string
20 #include <sstream> // std::stringstream
21 #include <iomanip> // std::setw
22 #include <limits> // std::numeric_limits
23 
24 #include "eli/constants/math.hpp"
27 
28 template<typename data__>
29 class piecewise_circle_creator_test_suite : public Test::Suite
30 {
31  private:
40 
41  tolerance_type tol;
42 
43  protected:
44  void AddTests(const float &)
45  {
46  // add the tests
52  }
53  void AddTests(const double &)
54  {
55  // add the tests
61  }
62  void AddTests(const long double &)
63  {
64  // add the tests
70  }
71 
72  public:
74  {
75  AddTests(data__());
76  }
78  {
79  }
80 
81  private:
82  void octave_print(int figno, const piecewise_curve_type &pc) const
83  {
84  index_type i, pp, ns;
85  data_type tmin, tmax;
86 
87  ns=pc.number_segments();
88  pc.get_parameter_min(tmin);
89  pc.get_parameter_max(tmax);
90 
91  std::cout << "figure(" << figno << ");" << std::endl;
92 
93  // get control points and print
94  std::cout << "cp_x=[";
95  for (pp=0; pp<ns; ++pp)
96  {
97  curve_type bez;
98  pc.get(bez, pp);
99  for (i=0; i<=bez.degree(); ++i)
100  {
101  std::cout << bez.get_control_point(i).x();
102  if (i<bez.degree())
103  std::cout << ", ";
104  else if (pp<ns-1)
105  std::cout << "; ";
106  }
107  std::cout << std::endl;
108  }
109  std::cout << "];" << std::endl;
110 
111  std::cout << "cp_y=[";
112  for (pp=0; pp<ns; ++pp)
113  {
114  curve_type bez;
115  pc.get(bez, pp);
116  for (i=0; i<=bez.degree(); ++i)
117  {
118  std::cout << bez.get_control_point(i).y();
119  if (i<bez.degree())
120  std::cout << ", ";
121  else if (pp<ns-1)
122  std::cout << "; ";
123  }
124  std::cout << std::endl;
125  }
126  std::cout << "];" << std::endl;
127 
128  std::cout << "cp_z=[";
129  for (pp=0; pp<ns; ++pp)
130  {
131  curve_type bez;
132  pc.get(bez, pp);
133  for (i=0; i<=bez.degree(); ++i)
134  {
135  std::cout << bez.get_control_point(i).z();
136  if (i<bez.degree())
137  std::cout << ", ";
138  else if (pp<ns-1)
139  std::cout << "; ";
140  }
141  std::cout << std::endl;
142  }
143  std::cout << "];" << std::endl;
144 
145  // initialize the t parameters
146  std::vector<data__> t(129);
147  for (i=0; i<static_cast<index_type>(t.size()); ++i)
148  {
149  t[i]=tmin+(tmax-tmin)*static_cast<data__>(i)/(t.size()-1);
150  }
151 
152  // set the surface points
153  std::cout << "surf_x=[";
154  for (i=0; i<static_cast<index_type>(t.size()); ++i)
155  {
156  std::cout << pc.f(t[i]).x();
157  if (i<static_cast<index_type>(t.size()-1))
158  std::cout << ", ";
159  }
160  std::cout << "];" << std::endl;
161 
162  std::cout << "surf_y=[";
163  for (i=0; i<static_cast<index_type>(t.size()); ++i)
164  {
165  std::cout << pc.f(t[i]).y();
166  if (i<static_cast<index_type>(t.size()-1))
167  std::cout << ", ";
168  }
169  std::cout << "];" << std::endl;
170 
171  std::cout << "surf_z=[";
172  for (i=0; i<static_cast<index_type>(t.size()); ++i)
173  {
174  std::cout << pc.f(t[i]).z();
175  if (i<static_cast<index_type>(t.size()-1))
176  std::cout << ", ";
177  }
178  std::cout << "];" << std::endl;
179 
180  std::cout << "setenv('GNUTERM', 'x11');" << std::endl;
181  std::cout << "plot3(surf_x, surf_y, surf_z, '-k');" << std::endl;
182  std::cout << "hold on;" << std::endl;
183  std::cout << "plot3(cp_x', cp_y', cp_z', '-ok', 'MarkerFaceColor', [0 0 0]);" << std::endl;
184  std::cout << "hold off;" << std::endl;
185  }
186 
188  {
189  {
190  circle_creator_type circle_creator;
191  piecewise_curve_type pc;
192  point_type origin, x, y;
193  data_type radius;
194 
195  // set the parameters for circle
196  origin << 1, 1, 1;
197  x << 1, -1, 2;
198  y << 1, 1, 0;
199  radius=3;
200 
201  circle_creator.set(origin, x, y, radius);
202 
203  // create the circle
204  TEST_ASSERT(circle_creator.create(pc));
205  }
206  }
207 
209  {
210  // create circle in x-z plane
211  {
212  circle_creator_type circle_creator;
213  piecewise_curve_type pc;
214  point_type start, origin, normal;
215 
216  // set the parameters for circle
217  start << 1, 0, 0;
218  origin << 0, 0, 0;
219 
220  circle_creator.set(start, origin);
221 
222  // create the circle
223  TEST_ASSERT(circle_creator.create(pc));
224  }
225 
226  // create circle with zero radius
227  {
228  circle_creator_type circle_creator;
229  piecewise_curve_type pc;
230  point_type start, origin, normal;
231 
232  // set the parameters for circle
233  start << 1, 0, 0;
234  origin << 1, 0, 0;
235 
236  circle_creator.set(start, origin);
237 
238  // create the circle
239  TEST_ASSERT(circle_creator.create(pc));
240 
241  TEST_ASSERT(pc.f(0.5)==pc.f(1.75));
242  }
243  }
244 
246  {
247  // create circle in x-z plane
248  {
249  circle_creator_type circle_creator;
250  piecewise_curve_type pc;
251  point_type start, origin, normal;
252 
253  // set the parameters for circle
254  start << 1, 0, 0;
255  origin << 0, 0, 0;
256  normal << 0, 0, 1;
257 
258  circle_creator.set(start, origin, normal);
259 
260  // create the circle
261  TEST_ASSERT(circle_creator.create(pc));
262  }
263 
264  // create circle in 3d space
265  {
266  circle_creator_type circle_creator;
267  piecewise_curve_type pc;
268  point_type start, origin, normal;
269 
270  // set the parameters for circle
271  start << 2, 2, 1;
272  origin << 1, 1, 1;
273  normal << 1,-1, 2;
274 
275  circle_creator.set(start, origin, normal);
276 
277  // create the circle
278  TEST_ASSERT(circle_creator.create(pc));
279  }
280 
281  // create circle with zero radius
282  {
283  circle_creator_type circle_creator;
284  piecewise_curve_type pc;
285  point_type start, origin, normal;
286 
287  // set the parameters for circle
288  start << 1, 0, 0;
289  origin << 1, 0, 0;
290  normal << 0, 0, 1;
291 
292  circle_creator.set(start, origin, normal);
293 
294  // create the circle
295  TEST_ASSERT(circle_creator.create(pc));
296 
297  TEST_ASSERT(pc.f(0.5)==pc.f(1.75));
298  }
299  }
300 
302  {
303  // test 2D
304  {
306  typedef typename piecewise_curve2_type::point_type point2_type;
308 
309  point2_type origin(2, 4), start, middle, end, x, y, xref, yref;
310  data_type radius(2), alpha1(1), alpha2(3), alpha3(5);
311  piecewise_curve2_type pc;
312  circle_creator2_type circle_creator;
313 
314  // set the three points
315  start << radius*std::cos(alpha1), radius*std::sin(alpha1);
316  middle << radius*std::cos(alpha2), radius*std::sin(alpha2);
317  end << radius*std::cos(alpha3), radius*std::sin(alpha3);
318  start+=origin;
319  middle+=origin;
320  end+=origin;
321 
322  circle_creator.set_3pt(start, middle, end);
323 
324  // test the circle creator parameters
325  TEST_ASSERT(tol.approximately_equal(radius, circle_creator.get_radius()));
326  TEST_ASSERT(tol.approximately_equal(origin, circle_creator.get_origin()));
327  circle_creator.get_xy_directions(xref, yref);
328  x=start-origin;
329  x.normalize();
330  y << -x.y(), x.x();
331  TEST_ASSERT(tol.approximately_equal(x, xref));
332  TEST_ASSERT(tol.approximately_equal(y, yref));
333 
334  // create the circle
335  TEST_ASSERT(circle_creator.create(pc));
336  }
337 
338  // test 3D
339  {
340  point_type origin(2, 4, 1), start, middle, end, x, y, xref, yref;
341  data_type radius(2), alpha1(1), alpha2(3), alpha3(5);
342  piecewise_curve_type pc;
343  circle_creator_type circle_creator;
344  Eigen::Matrix<data_type, 3, 3> rotx, roty;
345 
346  // set the three points
347  start << radius*std::cos(alpha1), radius*std::sin(alpha1), 0;
348  middle << radius*std::cos(alpha2), radius*std::sin(alpha2), 0;
349  end << radius*std::cos(alpha3), radius*std::sin(alpha3), 0;
350  rotx << 1, 0, 0,
351  0, std::cos(static_cast<data_type>(0.5)), -std::sin(static_cast<data_type>(0.5)),
352  0, std::sin(static_cast<data_type>(0.5)), std::cos(static_cast<data_type>(0.5));
353  roty << std::cos(static_cast<data_type>(0.25)), 0, std::sin(static_cast<data_type>(0.25)),
354  0, 1, 0,
355  -std::sin(static_cast<data_type>(0.25)), 0, std::cos(static_cast<data_type>(0.25));
356  start=start*rotx*roty+origin;
357  middle=middle*rotx*roty+origin;
358  end=end*rotx*roty+origin;
359 
360  circle_creator.set_3pt(start, middle, end);
361 
362  // test the circle creator parameters
363  TEST_ASSERT(tol.approximately_equal(radius, circle_creator.get_radius()));
364  TEST_ASSERT(tol.approximately_equal(origin, circle_creator.get_origin()));
365  circle_creator.get_xy_directions(xref, yref);
366  x=start-origin;
367  x.normalize();
368  y << -radius*std::sin(alpha1), radius*std::cos(alpha1), 0;
369  y=y*rotx*roty;
370  y.normalize();
371  TEST_ASSERT(tol.approximately_equal(x, xref));
372  TEST_ASSERT(tol.approximately_equal(y, yref));
373 
374  // create the circle
375  TEST_ASSERT(circle_creator.create(pc));
376  }
377  }
378 
380  {
381  // create ellipse in 2D
382  {
383  ellipse_creator_type ellipse_creator;
384  piecewise_curve_type pc;
385  point_type origin, x, y;
386  data_type xr, yr;
387 
388  // set the parameters for ellipse
389  origin << 1, 1, 0;
390  x << 1, -1, 0;
391  y << 1, 1, 0;
392  xr=3;
393  yr=6;
394 
395  ellipse_creator.set(origin, x, y, xr, yr);
396 
397  // create the circle
398  TEST_ASSERT(ellipse_creator.create(pc));
399  }
400 
401  // create ellipse in 3D
402  {
403  ellipse_creator_type ellipse_creator;
404  piecewise_curve_type pc;
405  point_type origin, x, y;
406  data_type xr, yr;
407 
408  // set the parameters for ellipse
409  origin << 1, 1, 1;
410  x << 1, -1, 2;
411  y << 1, 1, 0;
412  xr=3;
413  yr=6;
414 
415  ellipse_creator.set(origin, x, y, xr, yr);
416 
417  // create the circle
418  TEST_ASSERT(ellipse_creator.create(pc));
419  }
420 
421  // create ellipse with one radius zero
422  {
423  ellipse_creator_type ellipse_creator;
424  piecewise_curve_type pc;
425  point_type origin, x, y;
426  data_type xr, yr;
427 
428  // set the parameters for ellipse
429  origin << 1, 1, 1;
430  x << 1, -1, 2;
431  y << 1, 1, 0;
432  xr=0;
433  yr=6;
434 
435  ellipse_creator.set(origin, x, y, xr, yr);
436 
437  // create the circle
438  TEST_ASSERT(ellipse_creator.create(pc));
439  }
440 
441  // create ellipse with both radii zero
442  {
443  ellipse_creator_type ellipse_creator;
444  piecewise_curve_type pc;
445  point_type origin, x, y;
446  data_type xr, yr;
447 
448  // set the parameters for ellipse
449  origin << 1, 1, 1;
450  x << 1, -1, 2;
451  y << 1, 1, 0;
452  xr=0;
453  yr=0;
454 
455  ellipse_creator.set(origin, x, y, xr, yr);
456 
457  // create the circle
458  TEST_ASSERT(ellipse_creator.create(pc));
459  }
460  }
461 };
462 
463 #endif
464 
piecewise_circle_creator_test_suite()
Definition: piecewise_circle_creator_test_suite.hpp:73
tolerance_type tol
Definition: piecewise_circle_creator_test_suite.hpp:41
void create_ellipse_primative_test()
Definition: piecewise_circle_creator_test_suite.hpp:379
eli::geom::curve::piecewise< eli::geom::curve::bezier, data__, 3 > piecewise_curve_type
Definition: piecewise_circle_creator_test_suite.hpp:32
void create_circle_start_origin_normal_test()
Definition: piecewise_circle_creator_test_suite.hpp:245
tol__ tolerance_type
Definition: piecewise.hpp:278
data__ data_type
Definition: piecewise.hpp:276
void AddTests(const float &)
Definition: piecewise_circle_creator_test_suite.hpp:44
eli::geom::curve::piecewise_ellipse_creator< data__, 3, tolerance_type > ellipse_creator_type
Definition: piecewise_circle_creator_test_suite.hpp:39
Definition: piecewise_circle_creator.hpp:435
data_type get_parameter_min() const
Definition: piecewise.hpp:366
curve_type::index_type index_type
Definition: piecewise.hpp:271
piecewise_curve_type::point_type point_type
Definition: piecewise_circle_creator_test_suite.hpp:34
index_type number_segments() const
Definition: piecewise.hpp:419
point_type get_origin() const
Definition: piecewise_circle_creator.hpp:86
void create_circle_primative_test()
Definition: piecewise_circle_creator_test_suite.hpp:187
Definition: piecewise.hpp:244
piecewise_curve_type::data_type data_type
Definition: piecewise_circle_creator_test_suite.hpp:35
curve__< data__, dim__, tol__ > curve_type
Definition: piecewise.hpp:270
Definition: piecewise_circle_creator.hpp:244
void create_circle_3_point_test()
Definition: piecewise_circle_creator_test_suite.hpp:301
piecewise_curve_type::curve_type curve_type
Definition: piecewise_circle_creator_test_suite.hpp:33
void set_3pt(const point_type &start, const point_type &middle, const point_type &end)
Definition: piecewise_circle_creator.hpp:367
eli::geom::curve::piecewise_circle_creator< data__, 3, tolerance_type > circle_creator_type
Definition: piecewise_circle_creator_test_suite.hpp:38
~piecewise_circle_creator_test_suite()
Definition: piecewise_circle_creator_test_suite.hpp:77
virtual bool create(piecewise< bezier, data_type, dim__, tolerance_type > &pc) const
Definition: piecewise_circle_creator.hpp:110
data_type get_parameter_max() const
Definition: piecewise.hpp:374
piecewise_curve_type::index_type index_type
Definition: piecewise_circle_creator_test_suite.hpp:36
void create_circle_start_origin_test()
Definition: piecewise_circle_creator_test_suite.hpp:208
void get_xy_directions(point_type &xdir, point_type &ydir) const
Definition: piecewise_circle_creator.hpp:104
point_type f(const data_type &t) const
Definition: piecewise.hpp:1732
piecewise_curve_type::tolerance_type tolerance_type
Definition: piecewise_circle_creator_test_suite.hpp:37
error_code get(curve_type &curve, const index_type &index) const
Definition: piecewise.hpp:729
void set(const point_type &orig, const point_type &x, const point_type &y, const data_type &xr, const data_type &yr)
Definition: piecewise_circle_creator.hpp:493
Definition: piecewise_circle_creator_test_suite.hpp:29
void set(const point_type &orig, const point_type &x, const point_type &y, const data_type &r)
Definition: piecewise_circle_creator.hpp:283
curve_type::point_type point_type
Definition: piecewise.hpp:272
void AddTests(const double &)
Definition: piecewise_circle_creator_test_suite.hpp:53
void AddTests(const long double &)
Definition: piecewise_circle_creator_test_suite.hpp:62
data_type get_radius() const
Definition: piecewise_circle_creator.hpp:277
void octave_print(int figno, const piecewise_curve_type &pc) const
Definition: piecewise_circle_creator_test_suite.hpp:82