File: testMatrixInitialization.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (520 lines) | stat: -rw-r--r-- 14,776 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Test Matrix initialization.
 *
*****************************************************************************/

/*!
  \example testMatrixInitialization.cpp

  Test Matrix initialization
*/

#include <visp3/core/vpMatrix.h>

bool equal(const vpArray2D<double> &a1, const vpArray2D<double> &a2, double epsilon)
{
  if (a1.size() != a2.size()) {
    std::cout << "Rotation vector size differ" << std::endl;
    return false;
  }
  for (unsigned int i = 0; i < a1.getRows(); i++) {
    for (unsigned int j = 0; j < a1.getCols(); j++) {
      if (!vpMath::equal(a1[i][j], a2[i][j], epsilon)) {
        std::cout << "Array content differ" << std::endl;
        return false;
      }
    }
  }
  return true;
}

bool equal(const vpRotationVector &a1, const vpRotationVector &a2, double epsilon)
{
  if (a1.size() != a2.size()) {
    std::cout << "Rotation vector size differ" << std::endl;
    return false;
  }
  for (unsigned int i = 0; i < a1.size(); i++) {
    if (!vpMath::equal(a1[i], a2[i], epsilon)) {
      std::cout << "Rotation vector content differ" << std::endl;
      return false;
    }
  }
  return true;
}

bool equal(const vpColVector &a1, const vpColVector &a2, double epsilon)
{
  if (a1.size() != a2.size()) {
    std::cout << "Column vector size differ" << std::endl;
    return false;
  }
  for (unsigned int i = 0; i < a1.size(); i++) {
    if (!vpMath::equal(a1[i], a2[i], epsilon)) {
      std::cout << "Column vector content differ" << std::endl;
      return false;
    }
  }
  return true;
}

bool equal(const vpRowVector &a1, const vpRowVector &a2, double epsilon)
{
  if (a1.size() != a2.size()) {
    std::cout << "Row vector size differ" << std::endl;
    return false;
  }
  for (unsigned int i = 0; i < a1.size(); i++) {
    if (!vpMath::equal(a1[i], a2[i], epsilon)) {
      std::cout << "Row vector content differ" << std::endl;
      return false;
    }
  }
  return true;
}

int main()
{
  double epsilon = 1e-10;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
  {
    vpArray2D<float> a{1.f, 2.f, 3.f};
    std::cout << "a:\n" << a << std::endl;
    a = {-1, -2, -3, 4, 5.5, 6.0f};
    std::cout << "a:\n" << a << std::endl;
    a.reshape(2, 3);
    std::cout << "a.reshape(2,3):\n" << a << std::endl;
    a.reshape(3, 2);
    std::cout << "a.reshape(3,2):\n" << a << std::endl;

    vpArray2D<float> a2;
    a2.resize(2, 2);
    a2 = {1, 2, 3, 4};
    std::cout << "a2:\n" << a2 << std::endl;

    vpArray2D<double> a3(2, 3, {1, 2, 3, 4, 5, 6});
    std::cout << "a3:\n" << a3 << std::endl;

    vpArray2D<int> a4{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::cout << "a4:\n" << a4 << std::endl;

    vpArray2D<int> a5;
    a5 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::cout << "a5:\n" << a5 << std::endl;

    vpArray2D<int> a6{a5};
    std::cout << "a6:\n" << a6 << std::endl;

    vpMatrix m{1, 2, 3};
    std::cout << "m:\n" << m << std::endl;
    m = {-1, -2, -3, -4};
    std::cout << "m:\n" << m << std::endl;
    m.reshape(2, 2);
    std::cout << "m:\n" << m << std::endl;

    vpMatrix m2(3, 2, {1, 2, 3, 4, 5, 6});
    std::cout << "m2:\n" << m2 << std::endl;

    vpMatrix m3{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::cout << "m3:\n" << m3 << std::endl;

    vpMatrix m4;
    m4 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::cout << "m4:\n" << m4 << std::endl;

    vpMatrix m5{m4};
    std::cout << "m5:\n" << m5 << std::endl;

    //    vpMatrix m6;
    //    m6 = {m2};  // Fails on travis
    //    std::cout << "m6:\n" << m6 << std::endl;
  }
#endif

  {
    vpMatrix m1;
    m1 << 1, 2, 3;
    std::cout << "m1:\n" << m1 << std::endl;

    m1 << -1, -2, -3, -4;
    m1.reshape(1, 4);
    std::cout << "m1:\n" << m1 << std::endl;

    vpMatrix m2(2, 2);
    m2 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
    std::cout << "m2:\n" << m2 << std::endl;

    m2.resize(3, 3, false);
    std::cout << "m2:\n" << m2 << std::endl;

    m2 << 0.0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11;
    m2.reshape(2, 6);
    std::cout << "m2:\n" << m2 << std::endl;
  }

  {
    std::cout << "** Test vpColVector" << std::endl;
    vpColVector c_ref(6);
    for (unsigned int i = 0; i < 6; i++) {
      c_ref[i] = i;
    }
    std::cout << "c_ref: " << c_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpColVector c{0, 1, 2, 3, 4, 5};
      std::cout << "c: " << c.t() << std::endl;
      if (!equal(c_ref, c, epsilon)) {
        return EXIT_FAILURE;
      }
      c_ref.resize(3, false);
      c_ref *= -1;
      std::cout << "c_ref: " << c_ref.t() << std::endl;
      c = {0, -1, -2};
      std::cout << "c: " << c.t() << std::endl;
      if (!equal(c_ref, c, epsilon)) {
        return EXIT_FAILURE;
      }

      // Test move constructor
      vpColVector c1(c_ref);
      std::cout << "c1: " << c1.t() << std::endl;
      if (!equal(c_ref, c1, epsilon)) {
        return EXIT_FAILURE;
      }
      vpColVector c2 = std::move(c1); // Move c1 into c2; c1 is now "empty"
      std::cout << "c1: " << c1.t() << std::endl;
      if (c1.size()) {
        return EXIT_FAILURE;
      }
      std::cout << "c2: " << c2.t() << std::endl;
      if (!equal(c_ref, c2, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpColVector c;
      c << 1, 2, 3, 4;
      std::cout << "c: " << c << std::endl;

      try {
        c.reshape(2, 2);
        std::cout << "after c.reshape(2, 2): " << c.t() << std::endl;
        c = c.reshape(2, 2);
        std::cout << "c:" << c << std::endl;
      } catch (const vpException &e) {
        std::cerr << "Exception expected: c = c.reshape(2, 2);\n" << e.what() << std::endl;
      }

      std::cout << "c: " << c.t() << std::endl;
      vpArray2D<double> *ptr_array = &c;
      ptr_array->reshape(2, 2);
      std::cout << "ptr_array->reshape(2,2)" << std::endl;
      std::cout << "c: (" << c.getRows() << ", " << c.getCols() << "):\n" << c << std::endl;
      std::cout << "dynamic_cast<vpColVector *>(ptr_array):\n" << *dynamic_cast<vpColVector *>(ptr_array) << std::endl;
      std::cout << "ptr_array:\n" << *ptr_array << std::endl;
    }
  }

  {
    std::cout << "** Test vpRowVector" << std::endl;
    vpRowVector r_ref(6);
    for (unsigned int i = 0; i < 6; i++) {
      r_ref[i] = i;
    }
    std::cout << "r_ref: " << r_ref << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpRowVector r{0, 1, 2, 3, 4, 5};
      std::cout << "r: " << r << std::endl;
      if (!equal(r_ref, r, epsilon)) {
        return EXIT_FAILURE;
      }
      r_ref.resize(3, false);
      r_ref *= -1;
      std::cout << "r_ref: " << r_ref << std::endl;
      r = {0, -1, -2};
      std::cout << "r: " << r << std::endl;
      if (!equal(r_ref, r, epsilon)) {
        return EXIT_FAILURE;
      }

      // Test move constructor
      vpRowVector r1(r_ref);
      std::cout << "r1: " << r1 << std::endl;
      if (!equal(r_ref, r1, epsilon)) {
        return EXIT_FAILURE;
      }
      vpRowVector r2 = std::move(r1); // Move r1 into r2; r1 is now "empty"
      std::cout << "r1: " << r1 << std::endl;
      if (r1.size()) {
        return EXIT_FAILURE;
      }
      std::cout << "r2: " << r2 << std::endl;
      if (!equal(r_ref, r2, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpRowVector r;
      r << 1, 2, 3;
      std::cout << "r: " << r << std::endl;

      vpMatrix m = r.reshape(3, 1);
      std::cout << "m:\n" << m << std::endl;

      try {
        r.reshape(3, 1);
        std::cout << "after r.reshape(3, 1): " << r << std::endl;
      } catch (const vpException &e) {
        std::cerr << "Exception: r.reshape(3, 1);\n" << e.what() << std::endl;
      }
    }
  }

  {
    std::cout << "** Test vpThetaUVector" << std::endl;
    vpThetaUVector tu_ref(0, M_PI_2, M_PI);
    std::cout << "tu_ref: " << tu_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpThetaUVector tu = {0, M_PI_2, M_PI};
      std::cout << "tu: " << tu.t() << std::endl;
      if (!equal(tu_ref, tu, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpThetaUVector tu;
      tu << 0, M_PI_2, M_PI;
      std::cout << "tu: " << tu.t() << std::endl;
      if (!equal(tu_ref, tu, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      tu << 0, M_PI_2, M_PI;
      std::cout << "tu: " << tu.t() << std::endl;
      if (!equal(tu_ref, tu, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  {
    std::cout << "** Test vpRxyzVector" << std::endl;
    vpRxyzVector rxyz_ref(0, M_PI_2, M_PI);
    std::cout << "rxyz_ref: " << rxyz_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpRxyzVector rxyz = {0, M_PI_2, M_PI};
      std::cout << "rxyz: " << rxyz.t() << std::endl;
      if (!equal(rxyz_ref, rxyz, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpRxyzVector rxyz;
      rxyz << 0, M_PI_2, M_PI;
      std::cout << "rxyz: " << rxyz.t() << std::endl;
      if (!equal(rxyz_ref, rxyz, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      rxyz << 0, M_PI_2, M_PI;
      std::cout << "rxyz: " << rxyz.t() << std::endl;
      if (!equal(rxyz_ref, rxyz, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  {
    std::cout << "** Test vpRzyxVector" << std::endl;
    vpRzyxVector rzyx_ref(0, M_PI_2, M_PI);
    std::cout << "rzyx_ref: " << rzyx_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpRzyxVector rzyx = {0, M_PI_2, M_PI};
      std::cout << "rzyx: " << rzyx.t() << std::endl;
      if (!equal(rzyx_ref, rzyx, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpRzyxVector rzyx;
      rzyx << 0, M_PI_2, M_PI;
      std::cout << "rzyx: " << rzyx.t() << std::endl;
      if (!equal(rzyx_ref, rzyx, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      rzyx << 0, M_PI_2, M_PI;
      std::cout << "rzyx: " << rzyx.t() << std::endl;
      if (!equal(rzyx_ref, rzyx, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  {
    std::cout << "** Test vpRzyzVector" << std::endl;
    vpRzyzVector rzyz_ref(0, M_PI_2, M_PI);
    std::cout << "rzyz_ref: " << rzyz_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpRzyzVector rzyz = {0, M_PI_2, M_PI};
      std::cout << "rzyz: " << rzyz.t() << std::endl;
      if (!equal(rzyz_ref, rzyz, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpRzyzVector rzyz;
      rzyz << 0, M_PI_2, M_PI;
      std::cout << "rzyz: " << rzyz.t() << std::endl;
      if (!equal(rzyz_ref, rzyz, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      rzyz << 0, M_PI_2, M_PI;
      std::cout << "rzyz: " << rzyz.t() << std::endl;
      if (!equal(rzyz_ref, rzyz, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  {
    std::cout << "** Test vpQuaternionVector" << std::endl;
    vpThetaUVector tu_ref(0, M_PI_2, M_PI);
    vpQuaternionVector q_ref(tu_ref);
    std::cout << "q_ref: " << q_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpQuaternionVector q = {q_ref[0], q_ref[1], q_ref[2], q_ref[3]};
      std::cout << "q: " << q.t() << std::endl;
      if (!equal(q_ref, q, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpQuaternionVector q;
      q << q_ref[0], q_ref[1], q_ref[2], q_ref[3];
      std::cout << "q: " << q.t() << std::endl;
      if (!equal(q_ref, q, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      q << q_ref[0], q_ref[1], q_ref[2], q_ref[3];
      std::cout << "q: " << q.t() << std::endl;
      if (!equal(q_ref, q, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  {
    std::cout << "** Test vpTranslationVector" << std::endl;
    vpTranslationVector t_ref(0, 0.1, 0.5);
    std::cout << "t_ref: " << t_ref.t() << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpTranslationVector t = {t_ref[0], t_ref[1], t_ref[2]};
      std::cout << "t: " << t.t() << std::endl;
      if (!equal(t_ref, t, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpTranslationVector t;
      t << 0, 0.1, 0.5;
      std::cout << "t: " << t.t() << std::endl;
      if (!equal(t_ref, t, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      t << 0, 0.1, 0.5;
      std::cout << "t: " << t.t() << std::endl;
      if (!equal(t_ref, t, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  {
    std::cout << "** Test vpRotationMatrix" << std::endl;
    vpRotationMatrix R_ref(vpRxyzVector(0, -M_PI_2, M_PI));
    std::cout << "R_ref:\n" << R_ref << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    {
      vpRotationMatrix R({0, 0, -1, 0, -1, 0, -1, 0, 0});
      std::cout << "R:\n" << R << std::endl;
      if (!equal(R_ref, R, epsilon)) {
        return EXIT_FAILURE;
      }
    }
    {
      vpRotationMatrix R;
      R = {0, 0, -1, 0, -1, 0, -1, 0, 0};
      std::cout << "R:\n" << R << std::endl;
      if (!equal(R_ref, R, epsilon)) {
        return EXIT_FAILURE;
      }
    }
#endif
    {
      vpRotationMatrix R;
      R << 0, 0, -1, 0, -1, 0, -1, 0, 0;
      std::cout << "R:\n" << R << std::endl;
      if (!equal(R_ref, R, epsilon)) {
        return EXIT_FAILURE;
      }
      // Do it twice
      R << 0, 0, -1, 0, -1, 0, -1, 0, 0;
      std::cout << "R:\n" << R << std::endl;
      if (!equal(R_ref, R, epsilon)) {
        return EXIT_FAILURE;
      }
    }
  }

  std::cout << "Test succeed" << std::endl;
  return EXIT_SUCCESS;
}