1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
9 #define BOOST_PP_VARIADICS 0
12 #define TEST_NAME "mvs/gpu_mat_test"
13 #include "mvs/gpu_mat.h"
14 #include "mvs/gpu_mat_prng.h"
15 #include "util/math.h"
16 #include "util/testing.h"
18 using namespace colmap;
19 using namespace colmap::mvs;
21 BOOST_AUTO_TEST_CASE(TestFillWithVector) {
22 GpuMat<float> array(100, 100, 2);
23 const std::vector<float> vector = {1.0f, 2.0f};
24 array.FillWithVector(vector.data());
26 std::vector<float> array_host(100 * 100 * 2, 0.0f);
27 array.CopyToHost(array_host.data(), 100 * sizeof(float));
29 for (size_t r = 0; r < 100; ++r) {
30 for (size_t c = 0; c < 100; ++c) {
31 BOOST_CHECK_EQUAL(array_host[0 * 100 * 100 + r * 100 + c], 1.0f);
32 BOOST_CHECK_EQUAL(array_host[1 * 100 * 100 + r * 100 + c], 2.0f);
38 void TestTransposeImage(const size_t width,
41 GpuMat<T> array(width, height, depth);
43 GpuMatPRNG prng_array(width, height);
44 array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
46 GpuMat<T> array_transposed(height, width, depth);
47 array.Transpose(&array_transposed);
49 std::vector<T> array_host(width * height * depth, T(0.0));
50 array.CopyToHost(array_host.data(), width * sizeof(T));
52 std::vector<T> array_transposed_host(width * height * depth, 0);
53 array_transposed.CopyToHost(array_transposed_host.data(),
56 for (size_t r = 0; r < height; ++r) {
57 for (size_t c = 0; c < width; ++c) {
58 for (size_t d = 0; d < depth; ++d) {
60 array_host[d * width * height + r * width + c],
61 array_transposed_host[d * width * height + c * height +
68 BOOST_AUTO_TEST_CASE(TestTranspose) {
69 for (size_t w = 1; w <= 5; ++w) {
70 for (size_t h = 1; h <= 5; ++h) {
71 for (size_t d = 1; d <= 3; ++d) {
72 const size_t width = 20 * w;
73 const size_t height = 20 * h;
74 TestTransposeImage<int8_t>(width, height, d);
75 TestTransposeImage<int16_t>(width, height, d);
76 TestTransposeImage<int32_t>(width, height, d);
77 TestTransposeImage<int64_t>(width, height, d);
78 TestTransposeImage<float>(width, height, d);
79 TestTransposeImage<double>(width, height, d);
86 void TestFlipHorizontalImage(const size_t width,
89 GpuMat<T> array(width, height, depth);
91 GpuMatPRNG prng_array(width, height);
92 array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
94 GpuMat<T> array_flipped(width, height, depth);
95 array.FlipHorizontal(&array_flipped);
97 std::vector<T> array_host(width * height * depth, T(0.0));
98 array.CopyToHost(array_host.data(), width * sizeof(T));
100 std::vector<T> array_flipped_host(width * height * depth, 0);
101 array_flipped.CopyToHost(array_flipped_host.data(), width * sizeof(T));
103 for (size_t r = 0; r < height; ++r) {
104 for (size_t c = 0; c < width; ++c) {
105 for (size_t d = 0; d < depth; ++d) {
107 array_host[d * width * height + r * width + c],
108 array_flipped_host[d * width * height + r * width +
115 BOOST_AUTO_TEST_CASE(TestFlipHorizontal) {
116 for (size_t w = 1; w <= 5; ++w) {
117 for (size_t h = 1; h <= 5; ++h) {
118 for (size_t d = 1; d <= 3; ++d) {
119 const size_t width = 20 * w;
120 const size_t height = 20 * h;
121 TestFlipHorizontalImage<int8_t>(width, height, d);
122 TestFlipHorizontalImage<int16_t>(width, height, d);
123 TestFlipHorizontalImage<int32_t>(width, height, d);
124 TestFlipHorizontalImage<int64_t>(width, height, d);
125 TestFlipHorizontalImage<float>(width, height, d);
126 TestFlipHorizontalImage<double>(width, height, d);
132 template <typename T>
133 void TestRotateImage(const size_t width,
135 const size_t depth) {
136 GpuMat<T> array(width, height, depth);
138 GpuMatPRNG prng_array(width, height);
139 array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
141 GpuMat<T> array_rotated(height, width, depth);
142 array.Rotate(&array_rotated);
144 std::vector<T> array_host(width * height * depth, T(0.0));
145 array.CopyToHost(array_host.data(), width * sizeof(T));
147 std::vector<T> array_rotated_host(width * height * depth, 0);
148 array_rotated.CopyToHost(array_rotated_host.data(), height * sizeof(T));
150 const double arrayCenterH = width / 2.0 - 0.5;
151 const double arrayCenterV = height / 2.0 - 0.5;
152 const double angle = -M_PI / 2;
153 for (size_t r = 0; r < height; ++r) {
154 for (size_t c = 0; c < width; ++c) {
155 for (size_t d = 0; d < depth; ++d) {
156 const size_t rotc = std::round(
157 std::cos(angle) * (c - arrayCenterH) -
158 std::sin(angle) * (r - arrayCenterV) + arrayCenterV);
159 const size_t rotr = std::round(
160 std::sin(angle) * (c - arrayCenterH) +
161 std::cos(angle) * (r - arrayCenterV) + arrayCenterH);
163 array_host[d * width * height + r * width + c],
164 array_rotated_host[d * width * height + rotr * height +
171 BOOST_AUTO_TEST_CASE(TestRotate) {
172 for (size_t w = 1; w <= 5; ++w) {
173 for (size_t h = 1; h <= 5; ++h) {
174 for (size_t d = 1; d <= 3; ++d) {
175 const size_t width = 20 * w;
176 const size_t height = 20 * h;
177 TestRotateImage<int8_t>(width, height, d);
178 TestRotateImage<int16_t>(width, height, d);
179 TestRotateImage<int32_t>(width, height, d);
180 TestRotateImage<int64_t>(width, height, d);
181 TestRotateImage<float>(width, height, d);
182 TestRotateImage<double>(width, height, d);