Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testRowVector.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Test some vpColVector functionalities.
32 */
33
39
40#include <stdio.h>
41#include <stdlib.h>
42
43#include <visp3/core/vpMath.h>
44#include <visp3/core/vpRowVector.h>
45
46#ifdef ENABLE_VISP_NAMESPACE
47using namespace VISP_NAMESPACE_NAME;
48#endif
49
50bool test(const std::string &s, const vpRowVector &v, const std::vector<double> &bench);
51
52bool test(const std::string &s, const vpRowVector &v, const std::vector<double> &bench)
53{
54 static unsigned int cpt = 0;
55 std::cout << "** Test " << ++cpt << std::endl;
56 std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v << "]" << std::endl;
57 if (bench.size() != v.size()) {
58 std::cout << "Test fails: bad size wrt bench" << std::endl;
59 return false;
60 }
61 for (unsigned int i = 0; i < v.size(); i++) {
62 if (std::fabs(v[i] - bench[i]) > std::fabs(v[i]) * std::numeric_limits<double>::epsilon()) {
63 std::cout << "Test fails: bad content" << std::endl;
64 return false;
65 }
66 }
67
68 return true;
69}
70
71int main()
72{
73 int err = 1;
74
75 {
77
78 v.resize(4);
79 v = 3;
80 std::vector<double> bench1(4, 3);
81 if (test("v", v, bench1) == false)
82 return err;
83 std::vector<double> bench2(4, 3. / 6);
84 v.normalize();
85 if (test("v", v, bench2) == false)
86 return err;
87
88 v.resize(1, 5, true);
89 std::vector<double> bench3(5, 0);
90 if (test("v", v, bench3) == false)
91 return err;
92 }
93
94 {
95 vpRowVector v(4);
96 std::vector<double> bench1(4);
97 for (unsigned int i = 0; i < v.size(); i++) {
98 v[i] = static_cast<double>(i);
99 bench1[i] = static_cast<double>(i);
100 }
101 if (test("v", v, bench1) == false)
102 return err;
103
105 w.init(v, 0, 2);
106 std::vector<double> bench2;
107 bench2.push_back(0);
108 bench2.push_back(1);
109 if (test("w", w, bench2) == false)
110 return err;
111
112 std::vector<double> bench3;
113 bench3.push_back(1);
114 bench3.push_back(2);
115 bench3.push_back(3);
116
117 vpRowVector r1;
118 for (size_t i = 0; i < 4; i++)
119 r1.stack(static_cast<double>(i));
120
121 vpRowVector r2 = r1.extract(1, 3);
122 if (test("r2", r2, bench3) == false)
123 return err;
124 }
125 {
126 vpMatrix M(1, 4);
127 std::vector<double> bench(4);
128 for (unsigned int i = 0; i < M.getCols(); i++) {
129 M[0][i] = i;
130 bench[i] = i;
131 }
132 if (test("M", vpRowVector(M), bench) == false)
133 return err;
135 v = M;
136 if (test("v", v, bench) == false)
137 return err;
138 vpRowVector w(M);
139 if (test("w", w, bench) == false)
140 return err;
141 vpRowVector z1(bench);
142 if (test("z1", z1, bench) == false)
143 return err;
144 vpRowVector z2 = vpRowVector(bench);
145 if (test("z2", z2, bench) == false)
146 return err;
147 }
148 {
149 vpRowVector v(3);
150 v[0] = 1;
151 v[1] = 2;
152 v[2] = 3;
153 std::vector<double> bench1;
154 bench1.push_back(3);
155 bench1.push_back(6);
156 bench1.push_back(9);
157
158 vpRowVector w = v * 3;
159 // v is unchanged
160 // w is now equal to : [3 6 9]
161 if (test("w", w, bench1) == false)
162 return err;
163
164 vpRowVector x(w);
165 if (test("x", x, bench1) == false)
166 return err;
167
168 std::vector<float> bench2;
169 bench2.push_back(3);
170 bench2.push_back(6);
171 bench2.push_back(9);
172 vpRowVector y1(bench2);
173 if (test("y1", y1, bench1) == false)
174 return err;
175 vpRowVector y2 = vpRowVector(bench2);
176 if (test("y2", y2, bench1) == false)
177 return err;
178 }
179 {
180 vpRowVector r1(3, 1);
181 vpRowVector r2 = -r1;
182 std::vector<double> bench(3, -1);
183 // v contains [-1 -1 -1]
184 if (test("r2", r2, bench) == false)
185 return err;
186 r2.stack(-2);
187 bench.push_back(-2);
188 if (test("r2", r2, bench) == false)
189 return err;
190 vpRowVector r3 = vpRowVector::stack(r1, r2);
191 std::vector<double> bench3(7, 1);
192 bench3[3] = bench3[4] = bench3[5] = -1;
193 bench3[6] = -2;
194 if (test("r3", r3, bench3) == false)
195 return err;
196
197 r1.stack(r2);
198 if (test("r1", r1, bench3) == false)
199 return err;
200 }
201 {
202 vpRowVector r1(3, 2);
203 vpRowVector r2(3, 4);
204 vpRowVector r = r1 + r2;
205 std::vector<double> bench(3, 6);
206 if (test("r", r, bench) == false)
207 return err;
208 r1 += r2;
209 if (test("r1", r1, bench) == false)
210 return err;
211 }
212 {
213 vpRowVector r1(3, 2);
214 vpRowVector r2(3, 4);
215 vpRowVector r = r1 - r2;
216 std::vector<double> bench(3, -2);
217 if (test("r", r, bench) == false)
218 return err;
219 r1 -= r2;
220 if (test("r1", r1, bench) == false)
221 return err;
222 }
223 {
224 vpRowVector r(5, 1);
225 r.clear();
226 r.resize(5);
227 r = 5;
228 std::vector<double> bench(5, 5);
229 if (test("r", r, bench) == false)
230 return err;
231 }
232 {
233 // Test mean, median and standard deviation against Matlab with rng(0) and
234 // rand(10,1)*10
235 vpRowVector r(10);
236 r[0] = 8.1472;
237 r[1] = 9.0579;
238 r[2] = 1.2699;
239 r[3] = 9.1338;
240 r[4] = 6.3236;
241 r[5] = 0.9754;
242 r[6] = 2.7850;
243 r[7] = 5.4688;
244 r[8] = 9.5751;
245 r[9] = 9.6489;
246
247 std::cout << "** Test mean" << std::endl;
248 double res = vpRowVector::mean(r);
249 if (!vpMath::equal(res, 6.2386, 0.001)) {
250 std::cout << "Test fails: bad mean " << res << std::endl;
251 return err;
252 }
253
254 std::cout << "** Test stdev" << std::endl;
255 res = vpRowVector::stdev(r);
256 if (!vpMath::equal(res, 3.2810, 0.001)) {
257 std::cout << "Test fails: bad stdev " << res << std::endl;
258 return err;
259 }
260
261 std::cout << "** Test stdev(bessel)" << std::endl;
262 res = vpRowVector::stdev(r, true);
263 if (!vpMath::equal(res, 3.4585, 0.001)) {
264 std::cout << "Test fails: bad stdev(bessel) " << res << std::endl;
265 return err;
266 }
267
268 std::cout << "** Test median" << std::endl;
269 res = vpRowVector::median(r);
270 if (!vpMath::equal(res, 7.2354, 0.001)) {
271 std::cout << "Test fails: bad median " << res << std::endl;
272 return err;
273 }
274
275 // Test median with odd number of elements
276 std::cout << "** Test median (odd)" << std::endl;
277 r.stack(1.5761);
278 res = vpRowVector::median(r);
279 if (!vpMath::equal(res, 6.3236, 0.001)) {
280 std::cout << "Test fails: bad median (odd) " << res << std::endl;
281 return err;
282 }
283 std::cout << "r: [" << r << "]" << std::endl;
284 r.print(std::cout, 8, "r");
285 }
286
287 {
288 std::cout << "** Test conversion to/from std::vector" << std::endl;
289 std::vector<double> std_vector(5);
290 for (size_t i = 0; i < std_vector.size(); i++) {
291 std_vector[i] = static_cast<double>(i);
292 }
293 vpRowVector v(std_vector);
294 if (test("v", v, std_vector) == false)
295 return EXIT_FAILURE;
296
297 std_vector.clear();
298 std_vector = v.toStdVector();
299 if (test("v", v, std_vector) == false)
300 return EXIT_FAILURE;
301 }
302 std::cout << "All tests succeed" << std::endl;
303 return EXIT_SUCCESS;
304}
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Implementation of row vector and the associated operations.
static double mean(const vpRowVector &v)
void stack(double d)
static double median(const vpRowVector &v)
static double stdev(const vpRowVector &v, bool useBesselCorrection=false)
vpRowVector extract(unsigned int c, unsigned int rowsize) const