Code-Eli  0.3.6
bounding_box_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 bounding_box_test_suite_hpp
14 #define bounding_box_test_suite_hpp
15 
16 #include <cmath> // cos(), sin()
17 
18 #include <typeinfo> // typeid
19 
21 
22 template<typename data__>
23 class bounding_box_test_suite : public Test::Suite
24 {
25  private:
26  typedef data__ data_type;
33 
35 
36  protected:
37  void AddTests(const float &)
38  {
39  // add the tests
45  }
46  void AddTests(const double &)
47  {
48  // add the tests
54  }
55  void AddTests(const long double &)
56  {
57  // add the tests
63  }
64 
65  public:
67  {
68  AddTests(data__());
69  }
71  {
72  }
73 
74  private:
76  {
77  // test 1D
78  {
79  bounding_box_type1 bb1, bb2;
80 
81  TEST_ASSERT(bb1.empty_set());
82 
83  bb2=bb1;
84  TEST_ASSERT(bb2==bb1);
85 
86  point_type1 pt1;
87  pt1 << 1;
88 
89  bounding_box_type1 bb3(pt1);
90 
91  TEST_ASSERT(!bb3.empty_set());
92  TEST_ASSERT(bb3.get_min()==pt1);
93  TEST_ASSERT(bb3.get_max()==pt1);
94 
95  bounding_box_type1 bb4(bb1);
96 
97  TEST_ASSERT(bb1==bb4);
98  }
99 
100  // test 2D
101  {
102  bounding_box_type2 bb1, bb2;
103 
104  TEST_ASSERT(bb1.empty_set());
105 
106  bb2=bb1;
107  TEST_ASSERT(bb2==bb1);
108 
109  point_type2 pt1;
110  pt1 << 1, 2;
111 
112  bounding_box_type2 bb3(pt1);
113 
114  TEST_ASSERT(!bb3.empty_set());
115  TEST_ASSERT(bb3.get_min()==pt1);
116  TEST_ASSERT(bb3.get_max()==pt1);
117 
118  bounding_box_type2 bb4(bb1);
119 
120  TEST_ASSERT(bb1==bb4);
121  }
122 
123  // test 3D
124  {
125  bounding_box_type3 bb1, bb2;
126 
127  TEST_ASSERT(bb1.empty_set());
128 
129  bb2=bb1;
130  TEST_ASSERT(bb2==bb1);
131 
132  point_type3 pt1;
133  pt1 << 1, 2, 3;
134 
135  bounding_box_type3 bb3(pt1);
136 
137  TEST_ASSERT(!bb3.empty_set());
138  TEST_ASSERT(bb3.get_min()==pt1);
139  TEST_ASSERT(bb3.get_max()==pt1);
140 
141  bounding_box_type3 bb4(bb1);
142 
143  TEST_ASSERT(bb1==bb4);
144  }
145  }
146 
147  void add_test()
148  {
149  // test 1D
150  {
151  bounding_box_type1 bb;
152  point_type1 pt1, pt2, pt3, pt4, pt5;
153  bool changed;
154 
155  pt1 << 1;
156  pt2 << 3;
157  pt3 << 4;
158  pt4 << -1;
159  pt5 << 0;
160 
161  TEST_ASSERT(bb.empty_set());
162 
163  changed=bb.add(pt1);
164  TEST_ASSERT(changed);
165  TEST_ASSERT(bb.get_min()==pt1);
166  TEST_ASSERT(bb.get_max()==pt1);
167 
168  changed=bb.add(pt2);
169  TEST_ASSERT(changed);
170  TEST_ASSERT(bb.get_min()==pt1);
171  TEST_ASSERT(bb.get_max()==pt2);
172 
173  changed=bb.add(pt3);
174  TEST_ASSERT(changed);
175  TEST_ASSERT(bb.get_min()==pt1);
176  TEST_ASSERT(bb.get_max()==pt3);
177 
178  changed=bb.add(pt4);
179  TEST_ASSERT(changed);
180  TEST_ASSERT(bb.get_min()==pt4);
181  TEST_ASSERT(bb.get_max()==pt3);
182 
183  changed=bb.add(pt5);
184  TEST_ASSERT(!changed);
185  TEST_ASSERT(bb.get_min()==pt4);
186  TEST_ASSERT(bb.get_max()==pt3);
187  }
188 
189  // test 2D
190  {
191  bounding_box_type2 bb;
192  point_type2 pt1, pt2, pt3, pt4, pt5;
193  bool changed;
194 
195  pt1 << 1, 2;
196  pt2 << 3, 4;
197  pt3 << 4, 1;
198  pt4 << -1, 5;
199  pt5 << 0, 3;
200 
201  TEST_ASSERT(bb.empty_set());
202 
203  changed=bb.add(pt1);
204  TEST_ASSERT(changed);
205  TEST_ASSERT(bb.get_min()==pt1);
206  TEST_ASSERT(bb.get_max()==pt1);
207 
208  changed=bb.add(pt2);
209  TEST_ASSERT(changed);
210  TEST_ASSERT(bb.get_min()==pt1);
211  TEST_ASSERT(bb.get_max()==pt2);
212 
213  changed=bb.add(pt3);
214  TEST_ASSERT(changed);
215  TEST_ASSERT(bb.get_min().x()==pt1.x());
216  TEST_ASSERT(bb.get_min().y()==pt3.y());
217  TEST_ASSERT(bb.get_max().x()==pt3.x());
218  TEST_ASSERT(bb.get_max().y()==pt2.y());
219 
220  changed=bb.add(pt4);
221  TEST_ASSERT(changed);
222  TEST_ASSERT(bb.get_min().x()==pt4.x());
223  TEST_ASSERT(bb.get_min().y()==pt3.y());
224  TEST_ASSERT(bb.get_max().x()==pt3.x());
225  TEST_ASSERT(bb.get_max().y()==pt4.y());
226 
227  changed=bb.add(pt5);
228  TEST_ASSERT(!changed);
229  TEST_ASSERT(bb.get_min().x()==pt4.x());
230  TEST_ASSERT(bb.get_min().y()==pt3.y());
231  TEST_ASSERT(bb.get_max().x()==pt3.x());
232  TEST_ASSERT(bb.get_max().y()==pt4.y());
233  }
234 
235  // test 3D
236  {
237  bounding_box_type3 bb;
238  point_type3 pt1, pt2, pt3, pt4, pt5;
239  bool changed;
240 
241  pt1 << 1, 2, 1;
242  pt2 << 3, 4, 2;
243  pt3 << 4, 1, -1;
244  pt4 << -1, 5, 4;
245  pt5 << 0, 3, 0;
246 
247  TEST_ASSERT(bb.empty_set());
248 
249  changed=bb.add(pt1);
250  TEST_ASSERT(changed);
251  TEST_ASSERT(bb.get_min()==pt1);
252  TEST_ASSERT(bb.get_max()==pt1);
253 
254  changed=bb.add(pt2);
255  TEST_ASSERT(changed);
256  TEST_ASSERT(bb.get_min()==pt1);
257  TEST_ASSERT(bb.get_max()==pt2);
258 
259  changed=bb.add(pt3);
260  TEST_ASSERT(changed);
261  TEST_ASSERT(bb.get_min().x()==pt1.x());
262  TEST_ASSERT(bb.get_min().y()==pt3.y());
263  TEST_ASSERT(bb.get_min().z()==pt3.z());
264  TEST_ASSERT(bb.get_max().x()==pt3.x());
265  TEST_ASSERT(bb.get_max().y()==pt2.y());
266  TEST_ASSERT(bb.get_max().z()==pt2.z());
267 
268  changed=bb.add(pt4);
269  TEST_ASSERT(changed);
270  TEST_ASSERT(bb.get_min().x()==pt4.x());
271  TEST_ASSERT(bb.get_min().y()==pt3.y());
272  TEST_ASSERT(bb.get_min().z()==pt3.z());
273  TEST_ASSERT(bb.get_max().x()==pt3.x());
274  TEST_ASSERT(bb.get_max().y()==pt4.y());
275  TEST_ASSERT(bb.get_max().z()==pt4.z());
276 
277  changed=bb.add(pt5);
278  TEST_ASSERT(!changed);
279  TEST_ASSERT(bb.get_min().x()==pt4.x());
280  TEST_ASSERT(bb.get_min().y()==pt3.y());
281  TEST_ASSERT(bb.get_min().z()==pt3.z());
282  TEST_ASSERT(bb.get_max().x()==pt3.x());
283  TEST_ASSERT(bb.get_max().y()==pt4.y());
284  TEST_ASSERT(bb.get_max().z()==pt4.z());
285  }
286  }
287 
288  void add_bb_test()
289  {
290  // test 1D
291  {
292  bounding_box_type1 bb1, bb2, bb3;
293  point_type1 pt1, pt2, pt3, pt4, pt5, pt6;
294  bool changed;
295 
296  pt1 << 1;
297  pt2 << 3;
298  pt3 << 4;
299  pt4 << -1;
300  pt5 << 0;
301  pt6 << 1;
302 
303  bb1.add(pt1);
304  bb1.add(pt2);
305 
306  bb2.add(pt3);
307  bb2.add(pt4);
308 
309  bb3.add(pt5);
310  bb3.add(pt6);
311 
312  // test add that changes bbox
313  changed=bb1.add(bb2);
314  TEST_ASSERT(changed);
315  TEST_ASSERT(bb1.get_min()==pt4);
316  TEST_ASSERT(bb1.get_max()==pt3);
317 
318  // test add that does change bbox
319  changed=bb1.add(bb3);
320  TEST_ASSERT(!changed);
321  TEST_ASSERT(bb1.get_min()==pt4);
322  TEST_ASSERT(bb1.get_max()==pt3);
323  }
324 
325  // test 2D
326  {
327  bounding_box_type2 bb1, bb2;
328  point_type2 pt1, pt2, pt3, pt4, pt5;
329  bool changed;
330 
331  pt1 << 1, 2;
332  pt2 << 3, 4;
333  pt3 << 4, 1;
334  pt4 << -1, 5;
335  pt5 << 0, 3;
336 
337  bb1.add(pt1);
338  bb1.add(pt2);
339 
340  bb2.add(pt3);
341  bb2.add(pt4);
342 
343  changed=bb1.add(bb2);
344  TEST_ASSERT(changed);
345  TEST_ASSERT(bb1.get_min().x()==pt4.x());
346  TEST_ASSERT(bb1.get_min().y()==pt3.y());
347  TEST_ASSERT(bb1.get_max().x()==pt3.x());
348  TEST_ASSERT(bb1.get_max().y()==pt4.y());
349  }
350 
351  // test 3D
352  {
353  bounding_box_type3 bb1, bb2;
354  point_type3 pt1, pt2, pt3, pt4, pt5;
355  bool changed;
356 
357  pt1 << 1, 2, 1;
358  pt2 << 3, 4, 2;
359  pt3 << 4, 1, -1;
360  pt4 << -1, 5, 4;
361  pt5 << 0, 3, 0;
362 
363  bb1.add(pt1);
364  bb1.add(pt2);
365 
366  bb2.add(pt3);
367  bb2.add(pt4);
368 
369  changed=bb1.add(bb2);
370  TEST_ASSERT(changed);
371  TEST_ASSERT(bb1.get_min().x()==pt4.x());
372  TEST_ASSERT(bb1.get_min().y()==pt3.y());
373  TEST_ASSERT(bb1.get_min().z()==pt3.z());
374  TEST_ASSERT(bb1.get_max().x()==pt3.x());
375  TEST_ASSERT(bb1.get_max().y()==pt4.y());
376  TEST_ASSERT(bb1.get_max().z()==pt4.z());
377  }
378  }
379 
380  void inside_test()
381  {
382  // test 1D
383  {
384  bounding_box_type1 bb;
385  point_type1 pt;
386 
387  pt << -1;
388  bb.set_min(pt);
389  TEST_ASSERT(bb.get_min()==pt);
390  TEST_ASSERT(bb.get_max()==pt);
391 
392  pt << 1;
393  bb.set_max(pt);
394  TEST_ASSERT(bb.get_max()==pt);
395 
396  // test case where there is intersection
397  pt << static_cast<data_type>(0.9);
398  TEST_ASSERT(bb.inside(pt));
399 
400  // test case where there is no intersection
401  pt << static_cast<data_type>(1.1);
402  TEST_ASSERT(!bb.inside(pt));
403 
404  // test case where there is contact intersection
405  pt << static_cast<data_type>(1);
406  TEST_ASSERT(bb.inside(pt));
407  }
408 
409  // test 2D
410  {
411  bounding_box_type2 bb;
412  point_type2 pt;
413 
414  pt << -1, -1;
415  bb.set_min(pt);
416  TEST_ASSERT(bb.get_min()==pt);
417  TEST_ASSERT(bb.get_max()==pt);
418 
419  pt << 1, 1;
420  bb.set_max(pt);
421  TEST_ASSERT(bb.get_max()==pt);
422 
423  // test case where there is intersection
424  pt << static_cast<data_type>(0.9), static_cast<data_type>(0.9);
425  TEST_ASSERT(bb.inside(pt));
426 
427  // test case where there is no intersection
428  pt << static_cast<data_type>(1.1), static_cast<data_type>(1.1);
429  TEST_ASSERT(!bb.inside(pt));
430 
431  // test case where there is contact intersection
432  pt << static_cast<data_type>(1), static_cast<data_type>(0);
433  TEST_ASSERT(bb.inside(pt));
434  }
435 
436  // test 3D
437  {
438  bounding_box_type3 bb;
439  point_type3 pt;
440 
441  pt << -1, -1, -1;
442  bb.set_min(pt);
443  TEST_ASSERT(bb.get_min()==pt);
444  TEST_ASSERT(bb.get_max()==pt);
445 
446  pt << 1, 1, 1;
447  bb.set_max(pt);
448  TEST_ASSERT(bb.get_max()==pt);
449 
450  // test case where there is intersection
451  pt << static_cast<data_type>(0.9), static_cast<data_type>(0.9), static_cast<data_type>(0.9);
452  TEST_ASSERT(bb.inside(pt));
453 
454  // test case where there is no intersection
455  pt << static_cast<data_type>(1.1), static_cast<data_type>(1.1), static_cast<data_type>(1.1);
456  TEST_ASSERT(!bb.inside(pt));
457 
458  // test case where there is contact intersection
459  pt << static_cast<data_type>(1), static_cast<data_type>(0), static_cast<data_type>(0);
460  TEST_ASSERT(bb.inside(pt));
461  }
462  }
463 
465  {
466  // test 1D
467  {
468  bounding_box_type1 bb1, bb2;
469  point_type1 pt;
470 
471  pt << -1;
472  bb1.set_min(pt);
473  pt << 1;
474  bb1.set_max(pt);
475 
476  // test case where simple intersection
477  pt << static_cast<data_type>(0);
478  bb2.set_min(pt);
479  pt << static_cast<data_type>(2);
480  bb2.set_max(pt);
481  TEST_ASSERT(bb1.intersect(bb2));
482 
483  // test case where no intersection
484  pt << static_cast<data_type>(-3);
485  bb2.set_min(pt);
486  pt << static_cast<data_type>(-2);
487  bb2.set_max(pt);
488  TEST_ASSERT(!bb1.intersect(bb2));
489 
490  // test case where contact intersection
491  pt << static_cast<data_type>(1);
492  bb2.set_min(pt);
493  pt << static_cast<data_type>(2);
494  bb2.set_max(pt);
495  TEST_ASSERT(bb1.intersect(bb2));
496 
497  // test case where one contains other
498  pt << static_cast<data_type>(0);
499  bb2.set_min(pt);
500  pt << static_cast<data_type>(0.5);
501  bb2.set_max(pt);
502  TEST_ASSERT(bb1.intersect(bb2));
503 
504  // test case where one is contained by other
505  pt << static_cast<data_type>(-2);
506  bb2.set_min(pt);
507  pt << static_cast<data_type>(2);
508  bb2.set_max(pt);
509  TEST_ASSERT(bb1.intersect(bb2));
510  }
511 
512  // test 2D
513  {
514  bounding_box_type2 bb1, bb2;
515  point_type2 pt;
516 
517  pt << -1, -1;
518  bb1.set_min(pt);
519  pt << 1, 1;
520  bb1.set_max(pt);
521 
522  // test case where simple intersection
523  pt << static_cast<data_type>(0), static_cast<data_type>(0);
524  bb2.set_min(pt);
525  pt << static_cast<data_type>(2), static_cast<data_type>(2);
526  bb2.set_max(pt);
527  TEST_ASSERT(bb1.intersect(bb2));
528 
529  // test case where no intersection
530  pt << static_cast<data_type>(-3), static_cast<data_type>(-4);
531  bb2.set_min(pt);
532  pt << static_cast<data_type>(-2), static_cast<data_type>(0);
533  bb2.set_max(pt);
534  TEST_ASSERT(!bb1.intersect(bb2));
535 
536  // test case where contact intersection
537  pt << static_cast<data_type>(1), static_cast<data_type>(0);
538  bb2.set_min(pt);
539  pt << static_cast<data_type>(2), static_cast<data_type>(2);
540  bb2.set_max(pt);
541  TEST_ASSERT(bb1.intersect(bb2));
542 
543  // test case where one contains other
544  pt << static_cast<data_type>(0), static_cast<data_type>(0);
545  bb2.set_min(pt);
546  pt << static_cast<data_type>(0.5), static_cast<data_type>(0.5);
547  bb2.set_max(pt);
548  TEST_ASSERT(bb1.intersect(bb2));
549 
550  // test case where one is contained by other
551  pt << static_cast<data_type>(-2), static_cast<data_type>(-2);
552  bb2.set_min(pt);
553  pt << static_cast<data_type>(2), static_cast<data_type>(2);
554  bb2.set_max(pt);
555  TEST_ASSERT(bb1.intersect(bb2));
556  }
557 
558  // test 3D
559  {
560  bounding_box_type3 bb1, bb2;
561  point_type3 pt;
562 
563  pt << -1, -1, -1;
564  bb1.set_min(pt);
565  pt << 1, 1, 1;
566  bb1.set_max(pt);
567 
568  // test case where simple intersection
569  pt << static_cast<data_type>(0), static_cast<data_type>(0), static_cast<data_type>(0);
570  bb2.set_min(pt);
571  pt << static_cast<data_type>(2), static_cast<data_type>(2), static_cast<data_type>(2);
572  bb2.set_max(pt);
573  TEST_ASSERT(bb1.intersect(bb2));
574 
575  // test case where no intersection
576  pt << static_cast<data_type>(-3), static_cast<data_type>(-4), static_cast<data_type>(-3);
577  bb2.set_min(pt);
578  pt << static_cast<data_type>(-2), static_cast<data_type>(0), static_cast<data_type>(0);
579  bb2.set_max(pt);
580  TEST_ASSERT(!bb1.intersect(bb2));
581 
582  // test case where contact intersection
583  pt << static_cast<data_type>(1), static_cast<data_type>(0), static_cast<data_type>(0);
584  bb2.set_min(pt);
585  pt << static_cast<data_type>(2), static_cast<data_type>(2), static_cast<data_type>(2);
586  bb2.set_max(pt);
587  TEST_ASSERT(bb1.intersect(bb2));
588 
589  // test case where one contains other
590  pt << static_cast<data_type>(0), static_cast<data_type>(0), static_cast<data_type>(0);
591  bb2.set_min(pt);
592  pt << static_cast<data_type>(0.5), static_cast<data_type>(0.5), static_cast<data_type>(0.5);
593  bb2.set_max(pt);
594  TEST_ASSERT(bb1.intersect(bb2));
595 
596  // test case where one is contained by other
597  pt << static_cast<data_type>(-2), static_cast<data_type>(-2), static_cast<data_type>(-2);
598  bb2.set_min(pt);
599  pt << static_cast<data_type>(2), static_cast<data_type>(2), static_cast<data_type>(2);
600  bb2.set_max(pt);
601  TEST_ASSERT(bb1.intersect(bb2));
602  }
603  }
604 };
605 
606 #endif
607 
void inside_test()
Definition: bounding_box_test_suite.hpp:380
void intersect_test()
Definition: bounding_box_test_suite.hpp:464
point_type::Index index_type
Definition: bounding_box.hpp:33
Definition: bounding_box.hpp:27
Eigen::Matrix< data_type, 1, dim__ > point_type
Definition: bounding_box.hpp:32
bounding_box_type3::point_type point_type3
Definition: bounding_box_test_suite.hpp:32
point_type get_max() const
Definition: bounding_box.hpp:104
bool intersect(const bounding_box< data_type, dim__ > &bb) const
Definition: bounding_box.hpp:160
eli::geom::general::bounding_box< data__, 3 > bounding_box_type3
Definition: bounding_box_test_suite.hpp:31
void construction_test()
Definition: bounding_box_test_suite.hpp:75
bounding_box_test_suite()
Definition: bounding_box_test_suite.hpp:66
eli::geom::general::bounding_box< data__, 2 > bounding_box_type2
Definition: bounding_box_test_suite.hpp:29
bounding_box_type2::point_type point_type2
Definition: bounding_box_test_suite.hpp:30
void add_test()
Definition: bounding_box_test_suite.hpp:147
void AddTests(const float &)
Definition: bounding_box_test_suite.hpp:37
bool add(const point_type &p)
Definition: bounding_box.hpp:113
void set_min(const point_type &pm)
Definition: bounding_box.hpp:84
bool inside(const point_type &p) const
Definition: bounding_box.hpp:149
eli::geom::general::bounding_box< data__, 1 > bounding_box_type1
Definition: bounding_box_test_suite.hpp:27
bool empty_set() const
Definition: bounding_box.hpp:82
point_type get_min() const
Definition: bounding_box.hpp:93
Definition: bounding_box_test_suite.hpp:23
bounding_box_type1::point_type point_type1
Definition: bounding_box_test_suite.hpp:28
void AddTests(const long double &)
Definition: bounding_box_test_suite.hpp:55
bounding_box_type1::index_type index_type
Definition: bounding_box_test_suite.hpp:34
void set_max(const point_type &pm)
Definition: bounding_box.hpp:95
void add_bb_test()
Definition: bounding_box_test_suite.hpp:288
~bounding_box_test_suite()
Definition: bounding_box_test_suite.hpp:70
void AddTests(const double &)
Definition: bounding_box_test_suite.hpp:46
data__ data_type
Definition: bounding_box_test_suite.hpp:26