1 // Some tests for vgl_sphere_3d
2 // Ian Scott, Aug 2005.
3 #include <iostream>
4 #include "testlib/testlib_test.h"
5 #include "vgl/vgl_sphere_3d.h"
6 #include "vgl/vgl_line_3d_2_points.h"
7 #ifdef _MSC_VER
8 # include "vcl_msvc_warnings.h"
9 #endif
10
11 static void
test_sphere()12 test_sphere()
13 {
14 std::cout << "Simple sphere\n";
15
16 // Default sphere
17 vgl_sphere_3d<double> s;
18 // Unit sphere, centered at the origin
19 vgl_sphere_3d<double> u(0, 0, 0, 1.0);
20
21 TEST("default sphere is empty", s.is_empty(), true);
22 TEST("unit sphere is not empty", u.is_empty(), false);
23
24 TEST("origin is not inside empty sphere", s.contains(vgl_point_3d<double>(0, 0, 0)), false);
25 TEST("origin is inside unit sphere", u.contains(vgl_point_3d<double>(0, 0, 0)), true);
26 TEST("(1,0,0) is inside unit sphere", u.contains(vgl_point_3d<double>(1, 0, 0)), true);
27 TEST("(1,1,0) is outside unit sphere", u.contains(vgl_point_3d<double>(1, 1, 0)), false);
28
29 // l1 is the X-axis
30 vgl_line_3d_2_points<double> l1(vgl_point_3d<double>(-2, 0, 0), vgl_point_3d<double>(2, 0, 0));
31 vgl_point_3d<double> p1, p2;
32 TEST("clip x-axis to empty sphere", s.clip(l1, p1, p2), false);
33 TEST("clip x-axis to unit sphere", u.clip(l1, p1, p2), true);
34 TEST("Intersection point 1", p1, vgl_point_3d<double>(-1, 0, 0));
35 TEST("Intersection point 2", p2, vgl_point_3d<double>(1, 0, 0));
36
37 // l2 is the line (y=1,z=0) parallel to the X axis, touching the unit sphere in (0,1,0)
38 vgl_line_3d_2_points<double> l2(vgl_point_3d<double>(-2, 1, 0), vgl_point_3d<double>(2, 1, 0));
39 TEST("clip (y=1,z=0) to unit sphere", u.clip(l2, p1, p2), true);
40 TEST("Intersection point 1", p1, vgl_point_3d<double>(0, 1, 0));
41 TEST("Intersection point 2", p2, vgl_point_3d<double>(0, 1, 0));
42
43 // Test basic i/o
44 std::cout << u << std::endl;
45 std::ostringstream oss;
46 oss << u.centre().x() << " " << u.centre().y() << " " << u.centre().z() << " " << u.radius();
47 std::istringstream iss(oss.str());
48 vgl_sphere_3d<double> v;
49 iss >> v;
50 TEST("Basic i/o", u == v, true);
51 }
52
53 TESTMAIN(test_sphere);
54