1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 //       notice, this list of conditions and the following disclaimer.
9 //
10 //     * Redistributions in binary form must reproduce the above copyright
11 //       notice, this list of conditions and the following disclaimer in the
12 //       documentation and/or other materials provided with the distribution.
13 //
14 //     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 //       its contributors may be used to endorse or promote products derived
16 //       from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #ifdef __CUDACC__
33 #define BOOST_PP_VARIADICS 0
34 #endif  // __CUDACC__
35 
36 #define TEST_NAME "mvs/gpu_mat_test"
37 #include "util/testing.h"
38 
39 #include "mvs/gpu_mat.h"
40 #include "mvs/gpu_mat_prng.h"
41 #include "util/math.h"
42 
43 using namespace colmap;
44 using namespace colmap::mvs;
45 
BOOST_AUTO_TEST_CASE(TestFillWithVector)46 BOOST_AUTO_TEST_CASE(TestFillWithVector) {
47   GpuMat<float> array(100, 100, 2);
48   const std::vector<float> vector = {1.0f, 2.0f};
49   array.FillWithVector(vector.data());
50 
51   std::vector<float> array_host(100 * 100 * 2, 0.0f);
52   array.CopyToHost(array_host.data(), 100 * sizeof(float));
53 
54   for (size_t r = 0; r < 100; ++r) {
55     for (size_t c = 0; c < 100; ++c) {
56       BOOST_CHECK_EQUAL(array_host[0 * 100 * 100 + r * 100 + c], 1.0f);
57       BOOST_CHECK_EQUAL(array_host[1 * 100 * 100 + r * 100 + c], 2.0f);
58     }
59   }
60 }
61 
62 template <typename T>
TestTransposeImage(const size_t width,const size_t height,const size_t depth)63 void TestTransposeImage(const size_t width, const size_t height,
64                         const size_t depth) {
65   GpuMat<T> array(width, height, depth);
66 
67   GpuMatPRNG prng_array(width, height);
68   array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
69 
70   GpuMat<T> array_transposed(height, width, depth);
71   array.Transpose(&array_transposed);
72 
73   std::vector<T> array_host(width * height * depth, T(0.0));
74   array.CopyToHost(array_host.data(), width * sizeof(T));
75 
76   std::vector<T> array_transposed_host(width * height * depth, 0);
77   array_transposed.CopyToHost(array_transposed_host.data(), height * sizeof(T));
78 
79   for (size_t r = 0; r < height; ++r) {
80     for (size_t c = 0; c < width; ++c) {
81       for (size_t d = 0; d < depth; ++d) {
82         BOOST_CHECK_EQUAL(
83             array_host[d * width * height + r * width + c],
84             array_transposed_host[d * width * height + c * height + r]);
85       }
86     }
87   }
88 }
89 
BOOST_AUTO_TEST_CASE(TestTranspose)90 BOOST_AUTO_TEST_CASE(TestTranspose) {
91   for (size_t w = 1; w <= 5; ++w) {
92     for (size_t h = 1; h <= 5; ++h) {
93       for (size_t d = 1; d <= 3; ++d) {
94         const size_t width = 20 * w;
95         const size_t height = 20 * h;
96         TestTransposeImage<int8_t>(width, height, d);
97         TestTransposeImage<int16_t>(width, height, d);
98         TestTransposeImage<int32_t>(width, height, d);
99         TestTransposeImage<int64_t>(width, height, d);
100         TestTransposeImage<float>(width, height, d);
101         TestTransposeImage<double>(width, height, d);
102       }
103     }
104   }
105 }
106 
107 template <typename T>
TestFlipHorizontalImage(const size_t width,const size_t height,const size_t depth)108 void TestFlipHorizontalImage(const size_t width, const size_t height,
109                              const size_t depth) {
110   GpuMat<T> array(width, height, depth);
111 
112   GpuMatPRNG prng_array(width, height);
113   array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
114 
115   GpuMat<T> array_flipped(width, height, depth);
116   array.FlipHorizontal(&array_flipped);
117 
118   std::vector<T> array_host(width * height * depth, T(0.0));
119   array.CopyToHost(array_host.data(), width * sizeof(T));
120 
121   std::vector<T> array_flipped_host(width * height * depth, 0);
122   array_flipped.CopyToHost(array_flipped_host.data(), width * sizeof(T));
123 
124   for (size_t r = 0; r < height; ++r) {
125     for (size_t c = 0; c < width; ++c) {
126       for (size_t d = 0; d < depth; ++d) {
127         BOOST_CHECK_EQUAL(
128             array_host[d * width * height + r * width + c],
129             array_flipped_host[d * width * height + r * width + width - 1 - c]);
130       }
131     }
132   }
133 }
134 
BOOST_AUTO_TEST_CASE(TestFlipHorizontal)135 BOOST_AUTO_TEST_CASE(TestFlipHorizontal) {
136   for (size_t w = 1; w <= 5; ++w) {
137     for (size_t h = 1; h <= 5; ++h) {
138       for (size_t d = 1; d <= 3; ++d) {
139         const size_t width = 20 * w;
140         const size_t height = 20 * h;
141         TestFlipHorizontalImage<int8_t>(width, height, d);
142         TestFlipHorizontalImage<int16_t>(width, height, d);
143         TestFlipHorizontalImage<int32_t>(width, height, d);
144         TestFlipHorizontalImage<int64_t>(width, height, d);
145         TestFlipHorizontalImage<float>(width, height, d);
146         TestFlipHorizontalImage<double>(width, height, d);
147       }
148     }
149   }
150 }
151 
152 template <typename T>
TestRotateImage(const size_t width,const size_t height,const size_t depth)153 void TestRotateImage(const size_t width, const size_t height,
154                      const size_t depth) {
155   GpuMat<T> array(width, height, depth);
156 
157   GpuMatPRNG prng_array(width, height);
158   array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
159 
160   GpuMat<T> array_rotated(height, width, depth);
161   array.Rotate(&array_rotated);
162 
163   std::vector<T> array_host(width * height * depth, T(0.0));
164   array.CopyToHost(array_host.data(), width * sizeof(T));
165 
166   std::vector<T> array_rotated_host(width * height * depth, 0);
167   array_rotated.CopyToHost(array_rotated_host.data(), height * sizeof(T));
168 
169   const double arrayCenterH = width / 2.0 - 0.5;
170   const double arrayCenterV = height / 2.0 - 0.5;
171   const double angle = -M_PI / 2;
172   for (size_t r = 0; r < height; ++r) {
173     for (size_t c = 0; c < width; ++c) {
174       for (size_t d = 0; d < depth; ++d) {
175         const size_t rotc =
176             std::round(std::cos(angle) * (c - arrayCenterH) -
177                        std::sin(angle) * (r - arrayCenterV) + arrayCenterV);
178         const size_t rotr =
179             std::round(std::sin(angle) * (c - arrayCenterH) +
180                        std::cos(angle) * (r - arrayCenterV) + arrayCenterH);
181         BOOST_CHECK_EQUAL(
182             array_host[d * width * height + r * width + c],
183             array_rotated_host[d * width * height + rotr * height + rotc]);
184       }
185     }
186   }
187 }
188 
BOOST_AUTO_TEST_CASE(TestRotate)189 BOOST_AUTO_TEST_CASE(TestRotate) {
190   for (size_t w = 1; w <= 5; ++w) {
191     for (size_t h = 1; h <= 5; ++h) {
192       for (size_t d = 1; d <= 3; ++d) {
193         const size_t width = 20 * w;
194         const size_t height = 20 * h;
195         TestRotateImage<int8_t>(width, height, d);
196         TestRotateImage<int16_t>(width, height, d);
197         TestRotateImage<int32_t>(width, height, d);
198         TestRotateImage<int64_t>(width, height, d);
199         TestRotateImage<float>(width, height, d);
200         TestRotateImage<double>(width, height, d);
201       }
202     }
203   }
204 }
205