Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testMatrixConvolution.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test matrix convolution.
33 *
34*****************************************************************************/
35
41#include <visp3/core/vpMatrix.h>
42#include <visp3/core/vpTime.h>
43
44namespace
45{
46bool compareMatrix(const vpMatrix &m, const double *const array)
47{
48 for (unsigned int i = 0; i < m.getRows(); i++) {
49 for (unsigned int j = 0; j < m.getCols(); j++) {
50 if (!vpMath::equal(m[i][j], array[i * m.getCols() + j], std::numeric_limits<double>::epsilon()))
51 return false;
52 }
53 }
54
55 return true;
56}
57} // namespace
58
59int main()
60{
61 try {
62 {
63 vpMatrix A(4, 4);
64 A[0][0] = 16;
65 A[0][1] = 2;
66 A[0][2] = 3;
67 A[0][3] = 13;
68 A[1][0] = 5;
69 A[1][1] = 11;
70 A[1][2] = 10;
71 A[1][3] = 8;
72 A[2][0] = 9;
73 A[2][1] = 7;
74 A[2][2] = 6;
75 A[2][3] = 12;
76 A[3][0] = 4;
77 A[3][1] = 14;
78 A[3][2] = 15;
79 A[3][3] = 1;
80
81 vpMatrix B(2, 2);
82 B[0][0] = 1;
83 B[0][1] = 3;
84 B[1][0] = 4;
85 B[1][1] = 2;
86
87 {
88 vpMatrix res = vpMatrix::conv2(A, B, "full");
89 double ground_truth[5 * 5] = {16, 50, 9, 22, 39, 69, 66, 59, 96, 50, 29, 88, 89,
90 82, 52, 40, 72, 95, 106, 27, 16, 64, 88, 34, 2};
91
92 std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, full):\n" << res << std::endl;
93
94 if (res.getRows() != 5 || res.getCols() != 5 || !compareMatrix(res, ground_truth)) {
95 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
96 }
97 }
98 {
99 vpMatrix res = vpMatrix::conv2(A, B, "same");
100 double ground_truth[4 * 4] = {66, 59, 96, 50, 88, 89, 82, 52, 72, 95, 106, 27, 64, 88, 34, 2};
101
102 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, same):\n" << res << std::endl;
103
104 if (res.getRows() != 4 || res.getCols() != 4 || !compareMatrix(res, ground_truth)) {
105 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
106 }
107 }
108 {
109 vpMatrix res = vpMatrix::conv2(A, B, "valid");
110 double ground_truth[3 * 3] = {66, 59, 96, 88, 89, 82, 72, 95, 106};
111
112 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, valid):\n" << res << std::endl;
113
114 if (res.getRows() != 3 || res.getCols() != 3 || !compareMatrix(res, ground_truth)) {
115 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
116 }
117 }
118 }
119
120 {
121 vpMatrix A(2, 6);
122 for (unsigned int i = 0; i < A.getRows(); i++)
123 for (unsigned int j = 0; j < A.getCols(); j++)
124 A[i][j] = i * A.getCols() + j;
125
126 vpMatrix B(4, 2);
127 for (unsigned int i = 0; i < B.getRows(); i++)
128 for (unsigned int j = 0; j < B.getCols(); j++)
129 B[i][j] = i * B.getCols() + j;
130
131 {
132 vpMatrix res = vpMatrix::conv2(A, B, "full");
133 double ground_truth[5 * 7] = {0, 0, 1, 2, 3, 4, 5, 0, 8, 14, 20, 26, 32, 26, 12, 36, 50, 64,
134 78, 92, 58, 24, 64, 86, 108, 130, 152, 90, 36, 84, 97, 110, 123, 136, 77};
135
136 std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, full):\n" << res << std::endl;
137
138 if (res.getRows() != 5 || res.getCols() != 7 || !compareMatrix(res, ground_truth)) {
139 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
140 }
141 }
142 {
143 vpMatrix res = vpMatrix::conv2(A, B, "same");
144 double ground_truth[2 * 6] = {36, 50, 64, 78, 92, 58, 64, 86, 108, 130, 152, 90};
145
146 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, same):\n" << res << std::endl;
147
148 if (res.getRows() != 2 || res.getCols() != 6 || !compareMatrix(res, ground_truth)) {
149 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
150 }
151 }
152 {
153 vpMatrix res = vpMatrix::conv2(A, B, "valid");
154
155 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, valid):\n" << res << std::endl;
156
157 if (res.getRows() != 0 || res.getCols() != 0) {
158 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
159 }
160 }
161
162 {
163 vpMatrix res = vpMatrix::conv2(B, A, "full");
164 double ground_truth[5 * 7] = {0, 0, 1, 2, 3, 4, 5, 0, 8, 14, 20, 26, 32, 26, 12, 36, 50, 64,
165 78, 92, 58, 24, 64, 86, 108, 130, 152, 90, 36, 84, 97, 110, 123, 136, 77};
166
167 std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(B, A, full):\n" << res << std::endl;
168
169 if (res.getRows() != 5 || res.getCols() != 7 || !compareMatrix(res, ground_truth)) {
170 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
171 }
172 }
173 {
174 vpMatrix res = vpMatrix::conv2(B, A, "same");
175 double ground_truth[4 * 2] = {20, 26, 64, 78, 108, 130, 110, 123};
176
177 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(B, A, same):\n" << res << std::endl;
178
179 if (res.getRows() != 4 || res.getCols() != 2 || !compareMatrix(res, ground_truth)) {
180 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
181 }
182 }
183 {
184 vpMatrix res = vpMatrix::conv2(B, A, "valid");
185
186 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(B, A, valid):\n" << res << std::endl;
187
188 if (res.getRows() != 0 || res.getCols() != 0) {
189 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
190 }
191 }
192 }
193
194 {
195 vpMatrix A(4, 4);
196 A[0][0] = 16;
197 A[0][1] = 2;
198 A[0][2] = 3;
199 A[0][3] = 13;
200 A[1][0] = 5;
201 A[1][1] = 11;
202 A[1][2] = 10;
203 A[1][3] = 8;
204 A[2][0] = 9;
205 A[2][1] = 7;
206 A[2][2] = 6;
207 A[2][3] = 12;
208 A[3][0] = 4;
209 A[3][1] = 14;
210 A[3][2] = 15;
211 A[3][3] = 1;
212
213 vpMatrix B(3, 3);
214 B[0][0] = 8;
215 B[0][1] = 1;
216 B[0][2] = 6;
217 B[1][0] = 3;
218 B[1][1] = 5;
219 B[1][2] = 7;
220 B[2][0] = 4;
221 B[2][1] = 9;
222 B[2][2] = 2;
223
224 {
225 vpMatrix res = vpMatrix::conv2(A, B, "full");
226 double ground_truth[6 * 6] = {128, 32, 122, 119, 31, 78, 88, 179, 252, 208, 154, 139,
227 151, 275, 291, 378, 281, 154, 79, 271, 423, 366, 285, 106,
228 48, 171, 248, 292, 230, 31, 16, 92, 194, 167, 39, 2};
229
230 std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, full):\n" << res << std::endl;
231
232 if (res.getRows() != 6 || res.getCols() != 6 || !compareMatrix(res, ground_truth)) {
233 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
234 }
235 }
236 {
237 vpMatrix res = vpMatrix::conv2(A, B, "same");
238 double ground_truth[4 * 4] = {179, 252, 208, 154, 275, 291, 378, 281, 271, 423, 366, 285, 171, 248, 292, 230};
239
240 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, same):\n" << res << std::endl;
241
242 if (res.getRows() != 4 || res.getCols() != 4 || !compareMatrix(res, ground_truth)) {
243 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
244 }
245 }
246 {
247 vpMatrix res = vpMatrix::conv2(A, B, "valid");
248 double ground_truth[2 * 2] = {291, 378, 423, 366};
249
250 std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, valid):\n" << res << std::endl;
251
252 if (res.getRows() != 2 || res.getCols() != 2 || !compareMatrix(res, ground_truth)) {
253 throw vpException(vpException::badValue, "Issue with vpMatrix::conv2()");
254 }
255 }
256 }
257 } catch (const vpException &e) {
258 std::cout << "Catch an exception: " << e.what() << std::endl;
259 return EXIT_FAILURE;
260 }
261}
unsigned int getCols() const
Definition vpArray2D.h:280
static vpArray2D< double > conv2(const vpArray2D< double > &M, const vpArray2D< double > &kernel, const std::string &mode)
Definition vpArray2D.h:1070
unsigned int getRows() const
Definition vpArray2D.h:290
error that can be emitted by ViSP classes.
Definition vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:85
const char * what() const
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:369
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:152