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