Visual Servoing Platform version 3.5.0
testRowVector.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Authors:
35 * Eric Marchand
36 *
37 *****************************************************************************/
38
45#include <stdio.h>
46#include <stdlib.h>
47
48#include <visp3/core/vpMath.h>
49#include <visp3/core/vpRowVector.h>
50
51bool test(const std::string &s, const vpRowVector &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 << "]" << 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
70int main()
71{
72 int err = 1;
73
74 {
76
77 v.resize(4);
78 v = 3;
79 std::vector<double> bench1(4, 3);
80 if (test("v", v, bench1) == false)
81 return err;
82 std::vector<double> bench2(4, 3. / 6);
83 v.normalize();
84 if (test("v", v, bench2) == false)
85 return err;
86
87 v.resize(1, 5, true);
88 std::vector<double> bench3(5, 0);
89 if (test("v", v, bench3) == false)
90 return err;
91 }
92
93 {
94 vpRowVector v(4);
95 std::vector<double> bench1(4);
96 for (unsigned int i = 0; i < v.size(); i++) {
97 v[i] = (double)i;
98 bench1[i] = (double)i;
99 }
100 if (test("v", v, bench1) == false)
101 return err;
102
103 vpRowVector w;
104 w.init(v, 0, 2);
105 std::vector<double> bench2;
106 bench2.push_back(0);
107 bench2.push_back(1);
108 if (test("w", w, bench2) == false)
109 return err;
110
111 std::vector<double> bench3;
112 bench3.push_back(1);
113 bench3.push_back(2);
114 bench3.push_back(3);
115
116 vpRowVector r1;
117 for (size_t i = 0; i < 4; i++)
118 r1.stack((double)i);
119
120 vpRowVector r2 = r1.extract(1, 3);
121 if (test("r2", r2, bench3) == false)
122 return err;
123 }
124 {
125 vpMatrix M(1, 4);
126 std::vector<double> bench(4);
127 for (unsigned int i = 0; i < M.getCols(); i++) {
128 M[0][i] = i;
129 bench[i] = i;
130 }
131 if (test("M", M, bench) == false)
132 return err;
133 vpRowVector v;
134 v = M;
135 if (test("v", v, bench) == false)
136 return err;
137 vpRowVector w(M);
138 if (test("w", w, bench) == false)
139 return err;
140 vpRowVector z1(bench);
141 if (test("z1", z1, bench) == false)
142 return err;
143 vpRowVector z2 = bench;
144 if (test("z2", z2, bench) == false)
145 return err;
146 }
147 {
148 vpRowVector v(3);
149 v[0] = 1;
150 v[1] = 2;
151 v[2] = 3;
152 std::vector<double> bench1;
153 bench1.push_back(3);
154 bench1.push_back(6);
155 bench1.push_back(9);
156
157 vpRowVector w = v * 3;
158 // v is unchanged
159 // w is now equal to : [3 6 9]
160 if (test("w", w, bench1) == false)
161 return err;
162
163 vpRowVector x(w);
164 if (test("x", x, bench1) == false)
165 return err;
166
167 std::vector<float> bench2;
168 bench2.push_back(3);
169 bench2.push_back(6);
170 bench2.push_back(9);
171 vpRowVector y1(bench2);
172 if (test("y1", y1, bench1) == false)
173 return err;
174 vpRowVector y2 = bench2;
175 if (test("y2", y2, bench1) == false)
176 return err;
177 }
178 {
179 vpRowVector r1(3, 1);
180 vpRowVector r2 = -r1;
181 std::vector<double> bench(3, -1);
182 // v contains [-1 -1 -1]
183 if (test("r2", r2, bench) == false)
184 return err;
185 r2.stack(-2);
186 bench.push_back(-2);
187 if (test("r2", r2, bench) == false)
188 return err;
189 vpRowVector r3 = vpRowVector::stack(r1, r2);
190 std::vector<double> bench3(7, 1);
191 bench3[3] = bench3[4] = bench3[5] = -1;
192 bench3[6] = -2;
193 if (test("r3", r3, bench3) == false)
194 return err;
195
196 r1.stack(r2);
197 if (test("r1", r1, bench3) == false)
198 return err;
199 }
200 {
201 vpRowVector r1(3, 2);
202 vpRowVector r2(3, 4);
203 vpRowVector r = r1 + r2;
204 std::vector<double> bench(3, 6);
205 if (test("r", r, bench) == false)
206 return err;
207 r1 += r2;
208 if (test("r1", r1, bench) == false)
209 return err;
210 }
211 {
212 vpRowVector r1(3, 2);
213 vpRowVector r2(3, 4);
214 vpRowVector r = r1 - r2;
215 std::vector<double> bench(3, -2);
216 if (test("r", r, bench) == false)
217 return err;
218 r1 -= r2;
219 if (test("r1", r1, bench) == false)
220 return err;
221 }
222 {
223 vpRowVector r(5, 1);
224 r.clear();
225 r.resize(5);
226 r = 5;
227 std::vector<double> bench(5, 5);
228 if (test("r", r, bench) == false)
229 return err;
230 }
231 {
232 // Test mean, median and standard deviation against Matlab with rng(0) and
233 // rand(10,1)*10
234 vpRowVector r(10);
235 r[0] = 8.1472;
236 r[1] = 9.0579;
237 r[2] = 1.2699;
238 r[3] = 9.1338;
239 r[4] = 6.3236;
240 r[5] = 0.9754;
241 r[6] = 2.7850;
242 r[7] = 5.4688;
243 r[8] = 9.5751;
244 r[9] = 9.6489;
245
246 std::cout << "** Test mean" << std::endl;
247 double res = vpRowVector::mean(r);
248 if (!vpMath::equal(res, 6.2386, 0.001)) {
249 std::cout << "Test fails: bad mean " << res << std::endl;
250 return err;
251 }
252
253 std::cout << "** Test stdev" << std::endl;
254 res = vpRowVector::stdev(r);
255 if (!vpMath::equal(res, 3.2810, 0.001)) {
256 std::cout << "Test fails: bad stdev " << res << std::endl;
257 return err;
258 }
259
260 std::cout << "** Test stdev(bessel)" << std::endl;
261 res = vpRowVector::stdev(r, true);
262 if (!vpMath::equal(res, 3.4585, 0.001)) {
263 std::cout << "Test fails: bad stdev(bessel) " << res << std::endl;
264 return err;
265 }
266
267 std::cout << "** Test median" << std::endl;
268 res = vpRowVector::median(r);
269 if (!vpMath::equal(res, 7.2354, 0.001)) {
270 std::cout << "Test fails: bad median " << res << std::endl;
271 return err;
272 }
273
274 // Test median with odd number of elements
275 std::cout << "** Test median (odd)" << std::endl;
276 r.stack(1.5761);
277 res = vpRowVector::median(r);
278 if (!vpMath::equal(res, 6.3236, 0.001)) {
279 std::cout << "Test fails: bad median (odd) " << res << std::endl;
280 return err;
281 }
282 std::cout << "r: [" << r << "]" << std::endl;
283 r.print(std::cout, 8, "r");
284 }
285
286 {
287 std::cout << "** Test conversion to/from std::vector" << std::endl;
288 std::vector<double> std_vector(5);
289 for (size_t i = 0; i < std_vector.size(); i++) {
290 std_vector[i] = (double) i;
291 }
292 vpRowVector v(std_vector);
293 if (test("v", v, std_vector) == false)
294 return EXIT_FAILURE;
295
296 std_vector.clear();
297 std_vector = v.toStdVector();
298 if (test("v", v, std_vector) == false)
299 return EXIT_FAILURE;
300 }
301 std::cout << "All tests succeed" << std::endl;
302 return 0;
303}
unsigned int getCols() const
Definition: vpArray2D.h:279
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:291
unsigned int getRows() const
Definition: vpArray2D.h:289
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:295
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:116
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:271
static double mean(const vpRowVector &v)
void stack(double d)
void init(const vpRowVector &v, unsigned int c, unsigned int ncols)
void clear()
Definition: vpRowVector.h:146
vpRowVector & normalize()
static double median(const vpRowVector &v)
std::vector< double > toStdVector() const
static double stdev(const vpRowVector &v, bool useBesselCorrection=false)
vpRowVector extract(unsigned int c, unsigned int rowsize) const
Definition: vpRowVector.h:191
int print(std::ostream &s, unsigned int length, char const *intro=0) const