ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
gpu_mat_test.cu
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #ifdef __CUDACC__
9 #define BOOST_PP_VARIADICS 0
10 #endif // __CUDACC__
11 
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"
17 
18 using namespace colmap;
19 using namespace colmap::mvs;
20 
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());
25 
26  std::vector<float> array_host(100 * 100 * 2, 0.0f);
27  array.CopyToHost(array_host.data(), 100 * sizeof(float));
28 
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);
33  }
34  }
35 }
36 
37 template <typename T>
38 void TestTransposeImage(const size_t width,
39  const size_t height,
40  const size_t depth) {
41  GpuMat<T> array(width, height, depth);
42 
43  GpuMatPRNG prng_array(width, height);
44  array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
45 
46  GpuMat<T> array_transposed(height, width, depth);
47  array.Transpose(&array_transposed);
48 
49  std::vector<T> array_host(width * height * depth, T(0.0));
50  array.CopyToHost(array_host.data(), width * sizeof(T));
51 
52  std::vector<T> array_transposed_host(width * height * depth, 0);
53  array_transposed.CopyToHost(array_transposed_host.data(),
54  height * sizeof(T));
55 
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) {
59  BOOST_CHECK_EQUAL(
60  array_host[d * width * height + r * width + c],
61  array_transposed_host[d * width * height + c * height +
62  r]);
63  }
64  }
65  }
66 }
67 
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);
80  }
81  }
82  }
83 }
84 
85 template <typename T>
86 void TestFlipHorizontalImage(const size_t width,
87  const size_t height,
88  const size_t depth) {
89  GpuMat<T> array(width, height, depth);
90 
91  GpuMatPRNG prng_array(width, height);
92  array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
93 
94  GpuMat<T> array_flipped(width, height, depth);
95  array.FlipHorizontal(&array_flipped);
96 
97  std::vector<T> array_host(width * height * depth, T(0.0));
98  array.CopyToHost(array_host.data(), width * sizeof(T));
99 
100  std::vector<T> array_flipped_host(width * height * depth, 0);
101  array_flipped.CopyToHost(array_flipped_host.data(), width * sizeof(T));
102 
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) {
106  BOOST_CHECK_EQUAL(
107  array_host[d * width * height + r * width + c],
108  array_flipped_host[d * width * height + r * width +
109  width - 1 - c]);
110  }
111  }
112  }
113 }
114 
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);
127  }
128  }
129  }
130 }
131 
132 template <typename T>
133 void TestRotateImage(const size_t width,
134  const size_t height,
135  const size_t depth) {
136  GpuMat<T> array(width, height, depth);
137 
138  GpuMatPRNG prng_array(width, height);
139  array.FillWithRandomNumbers(T(0.0), T(100.0), prng_array);
140 
141  GpuMat<T> array_rotated(height, width, depth);
142  array.Rotate(&array_rotated);
143 
144  std::vector<T> array_host(width * height * depth, T(0.0));
145  array.CopyToHost(array_host.data(), width * sizeof(T));
146 
147  std::vector<T> array_rotated_host(width * height * depth, 0);
148  array_rotated.CopyToHost(array_rotated_host.data(), height * sizeof(T));
149 
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);
162  BOOST_CHECK_EQUAL(
163  array_host[d * width * height + r * width + c],
164  array_rotated_host[d * width * height + rotr * height +
165  rotc]);
166  }
167  }
168  }
169 }
170 
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);
183  }
184  }
185  }
186 }