1 // This is mul/vimt3d/tests/test_image_bounds_and_centre_3d.cxx
2
3 #include <iostream>
4 #include "testlib/testlib_test.h"
5 #ifdef _MSC_VER
6 # include "vcl_msvc_warnings.h"
7 #endif
8 #include <vimt3d/vimt3d_image_3d.h>
9 #include <vimt3d/vimt3d_image_3d_of.h>
10 #include "vgl/vgl_point_3d.h"
11 #include "vgl/vgl_box_3d.h"
12
test_world_bounding_box()13 static void test_world_bounding_box()
14 {
15 std::cout << "---------------------------\n"
16 << "test_world_bounding_box():\n"
17 << "---------------------------\n";
18
19 // Image with identity transform
20 {
21 constexpr unsigned ni = 8; // image width (pixels)
22 constexpr unsigned nj = 9; // image height (pixels)
23 constexpr unsigned nk = 10; // image depth (pixels)
24 constexpr unsigned np = 1;
25 const vgl_point_3d<double> pix(1.0, 1.0, 1.0); // pixel dimensions (mm)
26 vimt3d_transform_3d w2i;
27 w2i.set_zoom_only(1.0/pix.x(), 1.0/pix.y(), 1.0/pix.z(), 0, 0, 0);
28 const vimt3d_image_3d_of<int> image(ni, nj, nk, np, w2i);
29
30 const vgl_box_3d<double> bbox = world_bounding_box(image);
31 const vgl_point_3d<double> min_pt(0.0, 0.0, 0.0);
32 const vgl_point_3d<double> max_pt((ni-1)*pix.x(), (nj-1)*pix.y(), (nk-1)*pix.z());
33 TEST("With identity transform",
34 (bbox.min_point()-min_pt).length()<1e-6 &&
35 (bbox.max_point()-max_pt).length()<1e-6,
36 true);
37 }
38
39 // Image with zoom transform (i.e. non-unity pixel size)
40 {
41 constexpr unsigned ni = 8; // image width (pixels)
42 constexpr unsigned nj = 9; // image height (pixels)
43 constexpr unsigned nk = 10; // image depth (pixels)
44 constexpr unsigned np = 1;
45 const vgl_point_3d<double> pix(0.7, 1.2, 1.3); // pixel dimensions (mm)
46 vimt3d_transform_3d w2i;
47 w2i.set_zoom_only(1.0/pix.x(), 1.0/pix.y(), 1.0/pix.z(), 0, 0, 0);
48 const vimt3d_image_3d_of<int> image(ni, nj, nk, np, w2i);
49
50 const vgl_box_3d<double> bbox = world_bounding_box(image);
51 const vgl_point_3d<double> min_pt(0.0, 0.0, 0.0);
52 const vgl_point_3d<double> max_pt((ni-1)*pix.x(), (nj-1)*pix.y(), (nk-1)*pix.z());
53 TEST("With zoom transform",
54 (bbox.min_point()-min_pt).length()<1e-6 &&
55 (bbox.max_point()-max_pt).length()<1e-6,
56 true);
57 }
58 }
59
60
test_centre_image_at_origin()61 static void test_centre_image_at_origin()
62 {
63 std::cout << "-------------------------------\n"
64 << "test_centre_image_at_origin():\n"
65 << "-------------------------------\n";
66
67 // Image with identity transform
68 {
69 constexpr unsigned ni = 8; // image width (pixels)
70 constexpr unsigned nj = 9; // image height (pixels)
71 constexpr unsigned nk = 10; // image depth (pixels)
72 constexpr unsigned np = 1;
73 const vgl_point_3d<double> pix(1.0, 1.0, 1.0); // pixel dimensions (mm)
74 vimt3d_transform_3d w2i;
75 w2i.set_zoom_only(1.0/pix.x(), 1.0/pix.y(), 1.0/pix.z(), 0, 0, 0);
76 vimt3d_image_3d_of<int> image(ni, nj, nk, np, w2i);
77
78 vgl_point_3d<double> orig1 = image.world2im().origin();
79 vgl_box_3d<double> bbox1 = world_bounding_box(image);
80 vimt3d_centre_image_at_origin(image);
81 vgl_point_3d<double> orig2 = image.world2im().origin();
82 vgl_box_3d<double> bbox2 = world_bounding_box(image);
83
84 const vgl_point_3d<double> orig_true((ni-1)/2.0, (nj-1)/2.0, (nk-1)/2.0);
85 bool orig_ok = ((orig1-orig2).length()>1e-6 && // check that origin has changed
86 (orig2-orig_true).length()<1e-6); // check that new origin is correct
87 TEST("With identity transform, orig OK?", orig_ok, true);
88
89 const double hwx = (ni-1)*pix.x()/2.0; // half-width of centred image in world coords
90 const double hwy = (nj-1)*pix.y()/2.0; // half-width of centred image in world coords
91 const double hwz = (nk-1)*pix.z()/2.0; // half-width of centred image in world coords
92 const vgl_point_3d<double> lo(-hwx, -hwy, -hwz);
93 const vgl_point_3d<double> hi(+hwx, +hwy, +hwz);
94 const vgl_box_3d<double> bbox(lo, hi); // bbox of centred image in world coords
95 bool bbox_ok = !(bbox1==bbox2) && // check that bbox has changed
96 (bbox.min_point()-bbox2.min_point()).length()<1e-6 && // check that bbox is correct
97 (bbox.max_point()-bbox2.max_point()).length()<1e-6;
98 TEST("With identity transform, bbox correct?", bbox_ok, true);
99 }
100
101 // Image with zoom transform (i.e. non-unity pixel size)
102 {
103 constexpr unsigned ni = 8; // image width (pixels)
104 constexpr unsigned nj = 9; // image height (pixels)
105 constexpr unsigned nk = 10; // image depth (pixels)
106 constexpr unsigned np = 1;
107 const vgl_point_3d<double> pix(0.7, 1.2, 1.3); // pixel dimensions (mm)
108 vimt3d_transform_3d w2i;
109 w2i.set_zoom_only(1.0/pix.x(), 1.0/pix.y(), 1.0/pix.z(), 0, 0, 0);
110 vimt3d_image_3d_of<int> image(ni, nj, nk, np, w2i);
111
112 vgl_point_3d<double> orig1 = image.world2im().origin();
113 vgl_box_3d<double> bbox1 = world_bounding_box(image);
114 vimt3d_centre_image_at_origin(image);
115 vgl_point_3d<double> orig2 = image.world2im().origin();
116 vgl_box_3d<double> bbox2 = world_bounding_box(image);
117
118 const vgl_point_3d<double> orig_true((ni-1)/2.0, (nj-1)/2.0, (nk-1)/2.0);
119 bool orig_ok = ((orig1-orig2).length()>1e-6 && // check that origin has changed
120 (orig2-orig_true).length()<1e-6); // check that new origin is correct
121 TEST("With zoom transform, origin correct?", orig_ok, true);
122
123 const double hwx = (ni-1)*pix.x()/2.0; // half-width of centred image in world coords
124 const double hwy = (nj-1)*pix.y()/2.0; // half-width of centred image in world coords
125 const double hwz = (nk-1)*pix.z()/2.0; // half-width of centred image in world coords
126 const vgl_point_3d<double> lo(-hwx, -hwy, -hwz);
127 const vgl_point_3d<double> hi(+hwx, +hwy, +hwz);
128 const vgl_box_3d<double> bbox(lo, hi); // bbox of centred image in world coords
129 bool bbox_ok = !(bbox1==bbox2) && // check that bbox has changed
130 (bbox.min_point()-bbox2.min_point()).length()<1e-6 && // check that bbox is correct
131 (bbox.max_point()-bbox2.max_point()).length()<1e-6;
132 TEST("With zoom transform, bbox correct?", bbox_ok, true);
133 }
134 }
135
136
test_image_bounds_and_centre_3d()137 static void test_image_bounds_and_centre_3d()
138 {
139 test_world_bounding_box();
140 test_centre_image_at_origin();
141 }
142
143
144 TESTMAIN(test_image_bounds_and_centre_3d);
145