Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testColVector.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 some vpColVector functionalities.
33 *
34*****************************************************************************/
35
42#include <stdio.h>
43#include <stdlib.h>
44
45#include <visp3/core/vpColVector.h>
46#include <visp3/core/vpGaussRand.h>
47#include <visp3/core/vpMath.h>
48
49namespace
50{
51bool test(const std::string &s, const vpColVector &v, const std::vector<double> &bench)
52{
53 static unsigned int cpt = 0;
54 std::cout << "** Test " << ++cpt << std::endl;
55 std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v.t() << "]^T" << std::endl;
56 if (bench.size() != v.size()) {
57 std::cout << "Test fails: bad size wrt bench" << std::endl;
58 return false;
59 }
60 for (unsigned int i = 0; i < v.size(); i++) {
61 if (std::fabs(v[i] - bench[i]) > std::fabs(v[i]) * std::numeric_limits<double>::epsilon()) {
62 std::cout << "Test fails: bad content" << std::endl;
63 return false;
64 }
65 }
66
67 return true;
68}
69
70double getRandomValues(double min, double max) { return (max - min) * ((double)rand() / (double)RAND_MAX) + min; }
71} // namespace
72
73int main()
74{
75 {
76 vpColVector v1(7, 0.1), v2;
77 if (v1 == v2) {
78 std::cerr << "Issue with vpColVector comparison operator." << std::endl;
79 return EXIT_FAILURE;
80 }
81 v2 = v1;
82 if (v1 != v2) {
83 std::cerr << "Issue with vpColVector comparison operator." << std::endl;
84 return EXIT_FAILURE;
85 }
86 v2[3] = 0.2;
87 if (v1 == v2) {
88 std::cerr << "Issue with vpColVector comparison operator." << std::endl;
89 return EXIT_FAILURE;
90 }
91 }
92 {
94
95 v.resize(4);
96 v = 3;
97 std::vector<double> bench1(4, 3);
98 if (test("v", v, bench1) == false)
99 return EXIT_FAILURE;
100 std::vector<double> bench2(4, 3. / 6);
101 v.normalize();
102 if (test("v", v, bench2) == false)
103 return EXIT_FAILURE;
104
105 v.resize(5, 1, true);
106 std::vector<double> bench3(5, 0);
107 if (test("v", v, bench3) == false)
108 return EXIT_FAILURE;
109 }
110
111 {
112 vpColVector v(4);
113 std::vector<double> bench1(4);
114 for (unsigned int i = 0; i < v.size(); i++) {
115 v[i] = (double)i;
116 bench1[i] = (double)i;
117 }
118 if (test("v", v, bench1) == false)
119 return EXIT_FAILURE;
120
121 vpColVector w;
122 w.init(v, 0, 2);
123 std::vector<double> bench2;
124 bench2.push_back(0);
125 bench2.push_back(1);
126 if (test("w", w, bench2) == false)
127 return EXIT_FAILURE;
128
129 std::vector<double> bench3;
130 bench3.push_back(1);
131 bench3.push_back(2);
132 bench3.push_back(3);
133
134 vpColVector r1;
135 for (size_t i = 0; i < 4; i++)
136 r1.stack((double)i);
137
138 vpColVector r2 = r1.extract(1, 3);
139 if (test("r2", r2, bench3) == false)
140 return EXIT_FAILURE;
141 }
142
143 {
144 vpMatrix M(4, 1);
145 std::vector<double> bench(4);
146 for (unsigned int i = 0; i < M.getRows(); i++) {
147 M[i][0] = i;
148 bench[i] = i;
149 }
150 if (test("M", M, bench) == false)
151 return EXIT_FAILURE;
152 vpColVector v;
153 v = M;
154 if (test("v", v, bench) == false)
155 return EXIT_FAILURE;
156 vpColVector w(M);
157 if (test("w", w, bench) == false)
158 return EXIT_FAILURE;
159 vpColVector z1(bench);
160 if (test("z1", z1, bench) == false)
161 return EXIT_FAILURE;
162 vpColVector z2 = bench;
163 if (test("z2", z2, bench) == false)
164 return EXIT_FAILURE;
165 }
166
167 {
168 vpColVector v(3);
169 v[0] = 1;
170 v[1] = 2;
171 v[2] = 3;
172 std::vector<double> bench1;
173 bench1.push_back(3);
174 bench1.push_back(6);
175 bench1.push_back(9);
176
177 vpColVector w = v * 3;
178 // v is unchanged
179 // w is now equal to : [3 6 9]
180 if (test("w", w, bench1) == false)
181 return EXIT_FAILURE;
182
183 vpColVector x(w);
184 if (test("x", x, bench1) == false)
185 return EXIT_FAILURE;
186
187 std::vector<float> bench2;
188 bench2.push_back(3);
189 bench2.push_back(6);
190 bench2.push_back(9);
191 vpColVector y1(bench2);
192 if (test("y1", y1, bench1) == false)
193 return EXIT_FAILURE;
194 vpColVector y2 = bench2;
195 if (test("y2", y2, bench1) == false)
196 return EXIT_FAILURE;
197 }
198
199 {
200 vpColVector r1(3, 1);
201 vpColVector r2 = -r1;
202 std::vector<double> bench(3, -1);
203 // v contains [-1 -1 -1]
204 if (test("r2", r2, bench) == false)
205 return EXIT_FAILURE;
206 r2.stack(-2);
207 bench.push_back(-2);
208 if (test("r2", r2, bench) == false)
209 return EXIT_FAILURE;
210 vpColVector r3 = vpColVector::stack(r1, r2);
211 std::vector<double> bench3(7, 1);
212 bench3[3] = bench3[4] = bench3[5] = -1;
213 bench3[6] = -2;
214 if (test("r3", r3, bench3) == false)
215 return EXIT_FAILURE;
216
217 r1.stack(r2);
218 if (test("r1", r1, bench3) == false)
219 return EXIT_FAILURE;
220 }
221
222 {
223 vpColVector r1(3, 2);
224 vpColVector r2(3, 4);
225 std::cout << "test r1: " << r1 << std::endl;
226 std::cout << "test r2: " << r2 << std::endl;
227 vpColVector r = r1 + r2;
228 std::cout << "test r1+r2: " << r1 + r2 << std::endl;
229 std::cout << "test r: " << r << std::endl;
230 std::vector<double> bench(3, 6);
231 if (test("r", r, bench) == false)
232 return EXIT_FAILURE;
233 r1 += r2;
234 if (test("r1", r1, bench) == false)
235 return EXIT_FAILURE;
236 }
237
238 {
239 vpColVector r1(3, 2);
240 vpColVector r2(3, 4);
241 vpColVector r = r1 - r2;
242 std::vector<double> bench(3, -2);
243 if (test("r", r, bench) == false)
244 return EXIT_FAILURE;
245 r1 -= r2;
246 if (test("r1", r1, bench) == false)
247 return EXIT_FAILURE;
248 }
249
250 {
251 vpColVector r(5, 1);
252 r.clear();
253 r.resize(5);
254 r = 5;
255 std::vector<double> bench(5, 5);
256 if (test("r", r, bench) == false)
257 return EXIT_FAILURE;
258 }
259
260 {
261 // Test mean, median and standard deviation against Matlab with rng(0) and
262 // rand(10,1)*10
263 vpColVector r(10);
264 r[0] = 8.1472;
265 r[1] = 9.0579;
266 r[2] = 1.2699;
267 r[3] = 9.1338;
268 r[4] = 6.3236;
269 r[5] = 0.9754;
270 r[6] = 2.7850;
271 r[7] = 5.4688;
272 r[8] = 9.5751;
273 r[9] = 9.6489;
274
275 std::cout << "** Test mean" << std::endl;
276 double res = vpColVector::mean(r);
277 if (!vpMath::equal(res, 6.2386, 0.001)) {
278 std::cout << "Test fails: bad mean " << res << std::endl;
279 return EXIT_FAILURE;
280 }
281
282 std::cout << "** Test stdev" << std::endl;
283 res = vpColVector::stdev(r);
284 if (!vpMath::equal(res, 3.2810, 0.001)) {
285 std::cout << "Test fails: bad stdev " << res << std::endl;
286 return EXIT_FAILURE;
287 }
288
289 std::cout << "** Test stdev(bessel)" << std::endl;
290 res = vpColVector::stdev(r, true);
291 if (!vpMath::equal(res, 3.4585, 0.001)) {
292 std::cout << "Test fails: bad stdev(bessel) " << res << std::endl;
293 return EXIT_FAILURE;
294 }
295
296 std::cout << "** Test median" << std::endl;
297 res = vpColVector::median(r);
298 if (!vpMath::equal(res, 7.2354, 0.001)) {
299 std::cout << "Test fails: bad median " << res << std::endl;
300 return EXIT_FAILURE;
301 }
302
303 // Test median with odd number of elements
304 std::cout << "** Test median (odd)" << std::endl;
305 r.stack(1.5761);
306 res = vpColVector::median(r);
307 if (!vpMath::equal(res, 6.3236, 0.001)) {
308 std::cout << "Test fails: bad median (odd) " << res << std::endl;
309 return EXIT_FAILURE;
310 }
311 std::cout << "r: [" << r << "]^T" << std::endl;
312 r.print(std::cout, 8, "r");
313 }
314
315 {
316 // Test insert with big vector
317 unsigned int nb = 1000;
318 const unsigned int size = 10000;
319 std::vector<vpColVector> vec(nb);
320
321 for (size_t i = 0; i < nb; i++) {
322 vpColVector v(size);
323 for (unsigned int j = 0; j < size; j++) {
324 v[j] = getRandomValues(-100.0, 100.0);
325 }
326 vec[i] = v;
327 }
328
329 vpColVector v_big(nb * size);
330 double t = vpTime::measureTimeMs();
331 for (unsigned int i = 0; i < nb; i++) {
332 v_big.insert(i * size, vec[(size_t)i]);
333 }
334 t = vpTime::measureTimeMs() - t;
335 std::cout << "\nBig insert: " << t << " ms" << std::endl;
336
337 for (unsigned int i = 0; i < nb; i++) {
338 for (unsigned int j = 0; j < size; j++) {
339 if (!vpMath::equal(v_big[i * size + j], vec[(size_t)i][j], std::numeric_limits<double>::epsilon())) {
340 std::cerr << "Problem in vpColVector insert()!" << std::endl;
341 return EXIT_FAILURE;
342 }
343 }
344 }
345
346 // Try to insert empty vpColVector
347 vpColVector v1(2), v2, v3;
348 v1.insert(0, v2);
349 v3.insert(0, v2);
350
351 std::cout << "Insert empty vectors:" << std::endl;
352 std::cout << "v1: " << v1.t() << std::endl;
353 std::cout << "v2: " << v2.t() << std::endl;
354 std::cout << "v3: " << v3.t() << std::endl;
355 }
356
357 {
358 std::cout << "** Test conversion to/from std::vector" << std::endl;
359 std::vector<double> std_vector(5);
360 for (size_t i = 0; i < std_vector.size(); i++) {
361 std_vector[i] = (double)i;
362 }
363 vpColVector v(std_vector);
364 if (test("v", v, std_vector) == false)
365 return EXIT_FAILURE;
366
367 std_vector.clear();
368 std_vector = v.toStdVector();
369 if (test("v", v, std_vector) == false)
370 return EXIT_FAILURE;
371 }
372
373 {
374 std::cout << "** Test operator == and operator !=" << std::endl;
375 vpColVector v(3, 1.);
376 double val = 1.;
377 std::cout << "v: " << v.t() << " != " << val << std::endl;
378 if (v != val)
379 return EXIT_FAILURE;
380 val = 0.;
381 std::cout << "v: " << v.t() << " == " << val << std::endl;
382 if (v == val)
383 return EXIT_FAILURE;
384 v[1] = val;
385 std::cout << "v: " << v.t() << " == " << val << std::endl;
386 if (v == val)
387 return EXIT_FAILURE;
388 }
389 std::cout << "\nAll tests succeed" << std::endl;
390 return EXIT_SUCCESS;
391}
unsigned int getCols() const
Definition vpArray2D.h:280
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:292
unsigned int getRows() const
Definition vpArray2D.h:290
Implementation of column vector and the associated operations.
vpColVector extract(unsigned int r, unsigned int colsize) const
vpColVector & normalize()
static double median(const vpColVector &v)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
void init(const vpColVector &v, unsigned int r, unsigned int nrows)
void stack(double d)
std::vector< double > toStdVector() const
static double mean(const vpColVector &v)
vpRowVector t() const
static double stdev(const vpColVector &v, bool useBesselCorrection=false)
void insert(unsigned int i, const vpColVector &v)
void resize(unsigned int i, bool flagNullify=true)
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
VISP_EXPORT double measureTimeMs()