Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testMatrixInitialization.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 Matrix initialization.
32 */
33
39
40#include <visp3/core/vpMatrix.h>
41
42#ifdef ENABLE_VISP_NAMESPACE
43using namespace VISP_NAMESPACE_NAME;
44#endif
45
46bool equal(const vpArray2D<double> &a1, const vpArray2D<double> &a2, double epsilon);
47bool equal(const vpRotationVector &a1, const vpRotationVector &a2, double epsilon);
48bool equal(const vpColVector &a1, const vpColVector &a2, double epsilon);
49bool equal(const vpRowVector &a1, const vpRowVector &a2, double epsilon);
50
51bool equal(const vpArray2D<double> &a1, const vpArray2D<double> &a2, double epsilon)
52{
53 if (a1.size() != a2.size()) {
54 std::cout << "Rotation vector size differ" << std::endl;
55 return false;
56 }
57 for (unsigned int i = 0; i < a1.getRows(); i++) {
58 for (unsigned int j = 0; j < a1.getCols(); j++) {
59 if (!vpMath::equal(a1[i][j], a2[i][j], epsilon)) {
60 std::cout << "Array content differ" << std::endl;
61 return false;
62 }
63 }
64 }
65 return true;
66}
67
68bool equal(const vpRotationVector &a1, const vpRotationVector &a2, double epsilon)
69{
70 if (a1.size() != a2.size()) {
71 std::cout << "Rotation vector size differ" << std::endl;
72 return false;
73 }
74 for (unsigned int i = 0; i < a1.size(); i++) {
75 if (!vpMath::equal(a1[i], a2[i], epsilon)) {
76 std::cout << "Rotation vector content differ" << std::endl;
77 return false;
78 }
79 }
80 return true;
81}
82
83bool equal(const vpColVector &a1, const vpColVector &a2, double epsilon)
84{
85 if (a1.size() != a2.size()) {
86 std::cout << "Column vector size differ" << std::endl;
87 return false;
88 }
89 for (unsigned int i = 0; i < a1.size(); i++) {
90 if (!vpMath::equal(a1[i], a2[i], epsilon)) {
91 std::cout << "Column vector content differ" << std::endl;
92 return false;
93 }
94 }
95 return true;
96}
97
98bool equal(const vpRowVector &a1, const vpRowVector &a2, double epsilon)
99{
100 if (a1.size() != a2.size()) {
101 std::cout << "Row vector size differ" << std::endl;
102 return false;
103 }
104 for (unsigned int i = 0; i < a1.size(); i++) {
105 if (!vpMath::equal(a1[i], a2[i], epsilon)) {
106 std::cout << "Row vector content differ" << std::endl;
107 return false;
108 }
109 }
110 return true;
111}
112
113int main()
114{
115 double epsilon = 1e-10;
116
117#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
118 {
119 vpArray2D<float> a { 1.f, 2.f, 3.f };
120 std::cout << "a:\n" << a << std::endl;
121 a = { -1, -2, -3, 4, 5.5, 6.0f };
122 std::cout << "a:\n" << a << std::endl;
123 a.reshape(2, 3);
124 std::cout << "a.reshape(2,3):\n" << a << std::endl;
125 a.reshape(3, 2);
126 std::cout << "a.reshape(3,2):\n" << a << std::endl;
127
129 a2.resize(2, 2);
130 a2 = { 1, 2, 3, 4 };
131 std::cout << "a2:\n" << a2 << std::endl;
132
133 vpArray2D<double> a3(2, 3, { 1, 2, 3, 4, 5, 6 });
134 std::cout << "a3:\n" << a3 << std::endl;
135
136 vpArray2D<int> a4 { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
137 std::cout << "a4:\n" << a4 << std::endl;
138
140 a5 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
141 std::cout << "a5:\n" << a5 << std::endl;
142
143 vpArray2D<int> a6 { a5 };
144 std::cout << "a6:\n" << a6 << std::endl;
145
146 vpMatrix m { 1, 2, 3 };
147 std::cout << "m:\n" << m << std::endl;
148 m = { -1, -2, -3, -4 };
149 std::cout << "m:\n" << m << std::endl;
150 m.reshape(2, 2);
151 std::cout << "m:\n" << m << std::endl;
152
153 vpMatrix m2(3, 2, { 1, 2, 3, 4, 5, 6 });
154 std::cout << "m2:\n" << m2 << std::endl;
155
156 vpMatrix m3 { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
157 std::cout << "m3:\n" << m3 << std::endl;
158
159 vpMatrix m4;
160 m4 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
161 std::cout << "m4:\n" << m4 << std::endl;
162
163 vpMatrix m5 { m4 };
164 std::cout << "m5:\n" << m5 << std::endl;
165
166 // vpMatrix m6;
167 // m6 = {m2}; // Fails on travis
168 // std::cout << "m6:\n" << m6 << std::endl;
169 }
170#endif
171
172 {
173 vpMatrix m1;
174 m1 << 1, 2, 3;
175 std::cout << "m1:\n" << m1 << std::endl;
176
177 m1 << -1, -2, -3, -4;
178 m1.reshape(1, 4);
179 std::cout << "m1:\n" << m1 << std::endl;
180
181 vpMatrix m2(2, 2);
182 m2 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
183 std::cout << "m2:\n" << m2 << std::endl;
184
185 m2.resize(3, 3, false);
186 std::cout << "m2:\n" << m2 << std::endl;
187
188 m2 << 0.0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11;
189 m2.reshape(2, 6);
190 std::cout << "m2:\n" << m2 << std::endl;
191 }
192
193 {
194 std::cout << "** Test vpColVector" << std::endl;
195 vpColVector c_ref(6);
196 for (unsigned int i = 0; i < 6; i++) {
197 c_ref[i] = i;
198 }
199 std::cout << "c_ref: " << c_ref.t() << std::endl;
200#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
201 {
202 vpColVector c { 0, 1, 2, 3, 4, 5 };
203 std::cout << "c: " << c.t() << std::endl;
204 if (!equal(c_ref, c, epsilon)) {
205 return EXIT_FAILURE;
206 }
207 c_ref.resize(3, false);
208 c_ref *= -1;
209 std::cout << "c_ref: " << c_ref.t() << std::endl;
210 c = { 0, -1, -2 };
211 std::cout << "c: " << c.t() << std::endl;
212 if (!equal(c_ref, c, epsilon)) {
213 return EXIT_FAILURE;
214 }
215
216 // Test move constructor
217 vpColVector c1(c_ref);
218 std::cout << "c1: " << c1.t() << std::endl;
219 if (!equal(c_ref, c1, epsilon)) {
220 return EXIT_FAILURE;
221 }
222 vpColVector c2 = std::move(c1); // Move c1 into c2; c1 is now "empty"
223 std::cout << "c1: " << c1.t() << std::endl;
224 if (c1.size()) {
225 return EXIT_FAILURE;
226 }
227 std::cout << "c2: " << c2.t() << std::endl;
228 if (!equal(c_ref, c2, epsilon)) {
229 return EXIT_FAILURE;
230 }
231 }
232#endif
233 {
234 vpColVector c;
235 c << 1, 2, 3, 4;
236 std::cout << "c: " << c << std::endl;
237
238 try {
239 c.reshape(2, 2);
240 std::cout << "after c.reshape(2, 2): " << c.t() << std::endl;
241 c = c.reshape(2, 2);
242 std::cout << "c:" << c << std::endl;
243 }
244 catch (const vpException &e) {
245 std::cerr << "Exception expected: c = c.reshape(2, 2);\n" << e.what() << std::endl;
246 }
247
248 std::cout << "c: " << c.t() << std::endl;
249 vpArray2D<double> *ptr_array = &c;
250 ptr_array->reshape(2, 2);
251 std::cout << "ptr_array->reshape(2,2)" << std::endl;
252 std::cout << "c: (" << c.getRows() << ", " << c.getCols() << "):\n" << c << std::endl;
253 std::cout << "dynamic_cast<vpColVector *>(ptr_array):\n" << *dynamic_cast<vpColVector *>(ptr_array) << std::endl;
254 std::cout << "ptr_array:\n" << *ptr_array << std::endl;
255 }
256 }
257
258 {
259 std::cout << "** Test vpRowVector" << std::endl;
260 vpRowVector r_ref(6);
261 for (unsigned int i = 0; i < 6; i++) {
262 r_ref[i] = i;
263 }
264 std::cout << "r_ref: " << r_ref << std::endl;
265#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
266 {
267 vpRowVector r { 0, 1, 2, 3, 4, 5 };
268 std::cout << "r: " << r << std::endl;
269 if (!equal(r_ref, r, epsilon)) {
270 return EXIT_FAILURE;
271 }
272 r_ref.resize(3, false);
273 r_ref *= -1;
274 std::cout << "r_ref: " << r_ref << std::endl;
275 r = { 0, -1, -2 };
276 std::cout << "r: " << r << std::endl;
277 if (!equal(r_ref, r, epsilon)) {
278 return EXIT_FAILURE;
279 }
280
281 // Test move constructor
282 vpRowVector r1(r_ref);
283 std::cout << "r1: " << r1 << std::endl;
284 if (!equal(r_ref, r1, epsilon)) {
285 return EXIT_FAILURE;
286 }
287 vpRowVector r2 = std::move(r1); // Move r1 into r2; r1 is now "empty"
288 std::cout << "r1: " << r1 << std::endl;
289 if (r1.size()) {
290 return EXIT_FAILURE;
291 }
292 std::cout << "r2: " << r2 << std::endl;
293 if (!equal(r_ref, r2, epsilon)) {
294 return EXIT_FAILURE;
295 }
296 }
297#endif
298 {
300 r << 1, 2, 3;
301 std::cout << "r: " << r << std::endl;
302
303 vpMatrix m = r.reshape(3, 1);
304 std::cout << "m:\n" << m << std::endl;
305
306 try {
307 r.reshape(3, 1);
308 std::cout << "after r.reshape(3, 1): " << r << std::endl;
309 }
310 catch (const vpException &e) {
311 std::cerr << "Exception: r.reshape(3, 1);\n" << e.what() << std::endl;
312 }
313 }
314 }
315
316 {
317 std::cout << "** Test vpThetaUVector" << std::endl;
318 vpThetaUVector tu_ref(0, M_PI_2, M_PI);
319 std::cout << "tu_ref: " << tu_ref.t() << std::endl;
320#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
321 {
322 vpThetaUVector tu = { 0, M_PI_2, M_PI };
323 std::cout << "tu: " << tu.t() << std::endl;
324 if (!equal(tu_ref, tu, epsilon)) {
325 return EXIT_FAILURE;
326 }
327 }
328#endif
329 {
331 tu << 0, M_PI_2, M_PI;
332 std::cout << "tu: " << tu.t() << std::endl;
333 if (!equal(tu_ref, tu, epsilon)) {
334 return EXIT_FAILURE;
335 }
336 // Do it twice
337 tu << 0, M_PI_2, M_PI;
338 std::cout << "tu: " << tu.t() << std::endl;
339 if (!equal(tu_ref, tu, epsilon)) {
340 return EXIT_FAILURE;
341 }
342 }
343 }
344
345 {
346 std::cout << "** Test vpRxyzVector" << std::endl;
347 vpRxyzVector rxyz_ref(0, M_PI_2, M_PI);
348 std::cout << "rxyz_ref: " << rxyz_ref.t() << std::endl;
349#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
350 {
351 vpRxyzVector rxyz = { 0, M_PI_2, M_PI };
352 std::cout << "rxyz: " << rxyz.t() << std::endl;
353 if (!equal(rxyz_ref, rxyz, epsilon)) {
354 return EXIT_FAILURE;
355 }
356 }
357#endif
358 {
359 vpRxyzVector rxyz;
360 rxyz << 0, M_PI_2, M_PI;
361 std::cout << "rxyz: " << rxyz.t() << std::endl;
362 if (!equal(rxyz_ref, rxyz, epsilon)) {
363 return EXIT_FAILURE;
364 }
365 // Do it twice
366 rxyz << 0, M_PI_2, M_PI;
367 std::cout << "rxyz: " << rxyz.t() << std::endl;
368 if (!equal(rxyz_ref, rxyz, epsilon)) {
369 return EXIT_FAILURE;
370 }
371 }
372 }
373
374 {
375 std::cout << "** Test vpRzyxVector" << std::endl;
376 vpRzyxVector rzyx_ref(0, M_PI_2, M_PI);
377 std::cout << "rzyx_ref: " << rzyx_ref.t() << std::endl;
378#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
379 {
380 vpRzyxVector rzyx = { 0, M_PI_2, M_PI };
381 std::cout << "rzyx: " << rzyx.t() << std::endl;
382 if (!equal(rzyx_ref, rzyx, epsilon)) {
383 return EXIT_FAILURE;
384 }
385 }
386#endif
387 {
388 vpRzyxVector rzyx;
389 rzyx << 0, M_PI_2, M_PI;
390 std::cout << "rzyx: " << rzyx.t() << std::endl;
391 if (!equal(rzyx_ref, rzyx, epsilon)) {
392 return EXIT_FAILURE;
393 }
394 // Do it twice
395 rzyx << 0, M_PI_2, M_PI;
396 std::cout << "rzyx: " << rzyx.t() << std::endl;
397 if (!equal(rzyx_ref, rzyx, epsilon)) {
398 return EXIT_FAILURE;
399 }
400 }
401 }
402
403 {
404 std::cout << "** Test vpRzyzVector" << std::endl;
405 vpRzyzVector rzyz_ref(0, M_PI_2, M_PI);
406 std::cout << "rzyz_ref: " << rzyz_ref.t() << std::endl;
407#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
408 {
409 vpRzyzVector rzyz = { 0, M_PI_2, M_PI };
410 std::cout << "rzyz: " << rzyz.t() << std::endl;
411 if (!equal(rzyz_ref, rzyz, epsilon)) {
412 return EXIT_FAILURE;
413 }
414 }
415#endif
416 {
417 vpRzyzVector rzyz;
418 rzyz << 0, M_PI_2, M_PI;
419 std::cout << "rzyz: " << rzyz.t() << std::endl;
420 if (!equal(rzyz_ref, rzyz, epsilon)) {
421 return EXIT_FAILURE;
422 }
423 // Do it twice
424 rzyz << 0, M_PI_2, M_PI;
425 std::cout << "rzyz: " << rzyz.t() << std::endl;
426 if (!equal(rzyz_ref, rzyz, epsilon)) {
427 return EXIT_FAILURE;
428 }
429 }
430 }
431
432 {
433 std::cout << "** Test vpQuaternionVector" << std::endl;
434 vpThetaUVector tu_ref(0, M_PI_2, M_PI);
435 vpQuaternionVector q_ref(tu_ref);
436 std::cout << "q_ref: " << q_ref.t() << std::endl;
437#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
438 {
439 vpQuaternionVector q = { q_ref[0], q_ref[1], q_ref[2], q_ref[3] };
440 std::cout << "q: " << q.t() << std::endl;
441 if (!equal(q_ref, q, epsilon)) {
442 return EXIT_FAILURE;
443 }
444 }
445#endif
446 {
448 q << q_ref[0], q_ref[1], q_ref[2], q_ref[3];
449 std::cout << "q: " << q.t() << std::endl;
450 if (!equal(q_ref, q, epsilon)) {
451 return EXIT_FAILURE;
452 }
453 // Do it twice
454 q << q_ref[0], q_ref[1], q_ref[2], q_ref[3];
455 std::cout << "q: " << q.t() << std::endl;
456 if (!equal(q_ref, q, epsilon)) {
457 return EXIT_FAILURE;
458 }
459 }
460 }
461
462 {
463 std::cout << "** Test vpTranslationVector" << std::endl;
464 vpTranslationVector t_ref(0, 0.1, 0.5);
465 std::cout << "t_ref: " << t_ref.t() << std::endl;
466#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
467 {
468 vpTranslationVector t = { t_ref[0], t_ref[1], t_ref[2] };
469 std::cout << "t: " << t.t() << std::endl;
470 if (!equal(t_ref, t, epsilon)) {
471 return EXIT_FAILURE;
472 }
473 }
474#endif
475 {
477 t << 0, 0.1, 0.5;
478 std::cout << "t: " << t.t() << std::endl;
479 if (!equal(t_ref, t, epsilon)) {
480 return EXIT_FAILURE;
481 }
482 // Do it twice
483 t << 0, 0.1, 0.5;
484 std::cout << "t: " << t.t() << std::endl;
485 if (!equal(t_ref, t, epsilon)) {
486 return EXIT_FAILURE;
487 }
488 }
489 }
490
491 {
492 std::cout << "** Test vpRotationMatrix" << std::endl;
493 vpRotationMatrix R_ref(vpRxyzVector(0, -M_PI_2, M_PI));
494 std::cout << "R_ref:\n" << R_ref << std::endl;
495#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
496 {
497 vpRotationMatrix R({ 0, 0, -1, 0, -1, 0, -1, 0, 0 });
498 std::cout << "R:\n" << R << std::endl;
499 if (!equal(R_ref, R, epsilon)) {
500 return EXIT_FAILURE;
501 }
502 }
503 {
505 R = { 0, 0, -1, 0, -1, 0, -1, 0, 0 };
506 std::cout << "R:\n" << R << std::endl;
507 if (!equal(R_ref, R, epsilon)) {
508 return EXIT_FAILURE;
509 }
510 }
511#endif
512 {
514 R << 0, 0, -1, 0, -1, 0, -1, 0, 0;
515 std::cout << "R:\n" << R << std::endl;
516 if (!equal(R_ref, R, epsilon)) {
517 return EXIT_FAILURE;
518 }
519 // Do it twice
520 R << 0, 0, -1, 0, -1, 0, -1, 0, 0;
521 std::cout << "R:\n" << R << std::endl;
522 if (!equal(R_ref, R, epsilon)) {
523 return EXIT_FAILURE;
524 }
525 }
526
527 { // Test that a matrix view is copied through copy operator
528 double data[4];
529 for (size_t i = 0; i < 4; ++i) {
530 data[i] = static_cast<double>(i);
531 }
532 vpMatrix m = vpMatrix::view(data, 2, 2);
533 std::cout << "M = " << m << std::endl;
534 vpMatrix m1;
535 m1 = m;
536
537 if (!equal(m1, m, epsilon) || m.data == m1.data) {
538 return EXIT_FAILURE;
539 }
540
541 vpMatrix m2 = m;
542
543 if (!equal(m2, m, epsilon) || m.data == m2.data) {
544 return EXIT_FAILURE;
545 }
546 }
547 }
548
549 std::cout << "Test succeed" << std::endl;
550 return EXIT_SUCCESS;
551}
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition vpArray2D.h:146
unsigned int getCols() const
Definition vpArray2D.h:423
Type * data
Address of the first element of the data array.
Definition vpArray2D.h:149
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition vpArray2D.h:448
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
unsigned int getRows() const
Definition vpArray2D.h:433
void reshape(unsigned int nrows, unsigned int ncols)
Definition vpArray2D.h:545
Implementation of column vector and the associated operations.
void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition vpException.h:60
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
static vpMatrix view(double *data, unsigned int rows, unsigned int cols)
Create a matrix view of a raw data array. The view can modify the contents of the raw data array,...
Definition vpMatrix.cpp:307
Implementation of a rotation vector as quaternion angle minimal representation.
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a generic rotation vector.
vpRowVector t() const
Implementation of row vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
Implementation of a rotation vector as Euler angle minimal representation.
Implementation of a rotation vector as Euler angle minimal representation.
Implementation of a rotation vector as Euler angle minimal representation.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.