Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
testArray2D.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 <cmath>
43 #include <limits>
44 #include <vector>
45 
46 #include <visp3/core/vpTranslationVector.h>
47 
48 template <typename Type> bool test(const std::string &s, const vpArray2D<Type> &A, const std::vector<Type> &bench)
49 {
50  static unsigned int cpt = 0;
51  std::cout << "** Test " << ++cpt << std::endl;
52  std::cout << s << "(" << A.getRows() << "," << A.getCols() << ") = \n" << A << std::endl;
53  if (bench.size() != A.size()) {
54  std::cout << "Test fails: bad size wrt bench" << std::endl;
55  return false;
56  }
57  for (unsigned int i = 0; i < A.size(); i++) {
58  if (std::fabs(A.data[i] - bench[i]) > std::fabs(A.data[i]) * std::numeric_limits<double>::epsilon()) {
59  std::cout << "Test fails: bad content" << std::endl;
60  return false;
61  }
62  }
63 
64  return true;
65 }
66 
67 int main()
68 {
69  {
70  // test default constructor
72  std::vector<double> bench;
73  if (test("A", A, bench) == false)
74  return EXIT_FAILURE;
75  }
76  {
77  // test copy constructor
78  vpArray2D<double> A(3, 4);
79 
80  std::vector<double> bench(12);
81  for (unsigned int i = 0; i < 3; i++) {
82  for (unsigned int j = 0; j < 4; j++) {
83  A[i][j] = (double)(i + j);
84  bench[i * 4 + j] = (double)(i + j);
85  }
86  }
87  if (test("A", A, bench) == false)
88  return EXIT_FAILURE;
89 
90  vpArray2D<double> B(A);
91  if (test("B", B, bench) == false)
92  return EXIT_FAILURE;
93  std::cout << "Min/Max: " << B.getMinValue() << " " << B.getMaxValue() << std::endl;
94  }
95  {
96  // test constructor with initial value
97  vpArray2D<double> A(3, 4, 2.);
98  std::vector<double> bench1(12, 2);
99  if (test("A", A, bench1) == false)
100  return EXIT_FAILURE;
101 
102  A.resize(5, 6);
103  std::vector<double> bench2(30, 0);
104  if (test("A", A, bench2) == false)
105  return EXIT_FAILURE;
106 
107  A = -2.;
108  std::vector<double> bench3(30, -2);
109  if (test("A", A, bench3) == false)
110  return EXIT_FAILURE;
111  }
112 
113  // Test with float
114  {
115  // test default constructor
117  std::vector<float> bench;
118  if (test("A", A, bench) == false)
119  return EXIT_FAILURE;
120  }
121  {
122  // test copy constructor
123  vpArray2D<float> A(3, 4);
124 
125  std::vector<float> bench(12);
126  for (unsigned int i = 0; i < 3; i++) {
127  for (unsigned int j = 0; j < 4; j++) {
128  A[i][j] = (float)(i + j);
129  bench[i * 4 + j] = (float)(i + j);
130  }
131  }
132  if (test("A", A, bench) == false)
133  return EXIT_FAILURE;
134 
135  vpArray2D<float> B(A);
136  if (test("B", B, bench) == false)
137  return EXIT_FAILURE;
138  std::cout << "Min/Max: " << B.getMinValue() << " " << B.getMaxValue() << std::endl;
139  }
140  {
141  // test constructor with initial value
142  vpArray2D<float> A(3, 4, 2.);
143  std::vector<float> bench1(12, 2);
144  if (test("A", A, bench1) == false)
145  return EXIT_FAILURE;
146 
147  A.resize(5, 6);
148  std::vector<float> bench2(30, 0);
149  if (test("A", A, bench2) == false)
150  return EXIT_FAILURE;
151 
152  A = -2.;
153  std::vector<float> bench3(30, -2);
154  if (test("A", A, bench3) == false)
155  return EXIT_FAILURE;
156  }
157  {
158  // Test Hadamard product
159  std::cout << "\nTest Hadamard product" << std::endl;
160  vpArray2D<int> A1(3, 5), A2(3, 5);
161  vpRowVector R1(15), R2(15);
162  vpColVector C1(15), C2(15);
163 
164  for (unsigned int i = 0; i < A1.size(); i++) {
165  A1.data[i] = i;
166  A2.data[i] = i + 2;
167  R1.data[i] = i;
168  R2.data[i] = i + 2;
169  C1.data[i] = i;
170  C2.data[i] = i + 2;
171  }
172 
173  std::cout << "A1:\n" << A1 << std::endl;
174  std::cout << "\nA2:\n" << A2 << std::endl;
175  A2 = A1.hadamard(A2);
176  std::cout << "\nRes:\n" << A2 << std::endl;
177 
178  std::cout << "\nR1:\n" << R1 << std::endl;
179  std::cout << "\nR2:\n" << R2 << std::endl;
180  R2 = R1.hadamard(R2);
181  std::cout << "\nRes:\n" << R2 << std::endl;
182 
183  std::cout << "\nC1:\n" << C1 << std::endl;
184  std::cout << "\nC2:\n" << C2 << std::endl;
185  C2 = C1.hadamard(C2);
186  std::cout << "\nRes:\n" << C2 << std::endl;
187  }
188  std::cout << "All tests succeed" << std::endl;
189  return EXIT_SUCCESS;
190 }
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:125
unsigned int getCols() const
Definition: vpArray2D.h:274
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:138
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:299
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:286
unsigned int getRows() const
Definition: vpArray2D.h:284
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:107