Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testMatrix.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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 some vpMatrix functionalities.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpHomogeneousMatrix.h>
48 #include <visp3/core/vpVelocityTwistMatrix.h>
49 #include <visp3/core/vpGEMM.h>
50 
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 bool test(const std::string &s, const vpMatrix &M, const std::vector<double> &bench)
55 {
56  static unsigned int cpt = 0;
57  std::cout << "** Test " << ++cpt << std::endl;
58  std::cout << s << "(" << M.getRows() << "," << M.getCols() << ") = \n" << M << std::endl;
59  if(bench.size() != M.size()) {
60  std::cout << "Test fails: bad size wrt bench" << std::endl;
61  return false;
62  }
63  for (unsigned int i=0; i<M.size(); i++) {
64  if (std::fabs(M.data[i]-bench[i]) > std::fabs(M.data[i])*std::numeric_limits<double>::epsilon()) {
65  std::cout << "Test fails: bad content" << std::endl;
66  return false;
67  }
68  }
69 
70  return true;
71 }
72 
73 int
74 main()
75 {
76  try {
77  int err = 1;
78  {
79  vpColVector c(6, 1);
80  vpRowVector r(6, 1);
81  std::vector<double> bench(6, 1);
82  vpMatrix M1(c);
83  if (test("M1", M1, bench) == false)
84  return err;
85  vpMatrix M2(r);
86  if (test("M2", M2, bench) == false)
87  return err;
88  }
89  {
90  vpMatrix M(4,5);
91  int val = 0;
92  for(unsigned int i=0; i<M.getRows(); i++) {
93  for(unsigned int j=0; j<M.getCols(); j++) {
94  M[i][j] = val++;
95  }
96  }
97  std::cout <<"M ";
98  M.print (std::cout, 4);
99 
100  vpMatrix N;
101  N.init(M, 0, 1, 2, 3);
102  std::cout <<"N ";
103  N.print (std::cout, 4);
104  std::string header("My 4-by-5 matrix\nwith a second line");
105 
106  // Save matrix in text format
107  if (vpMatrix::saveMatrix("matrix.mat", M, false, header.c_str()))
108  std::cout << "Matrix saved in matrix.mat file" << std::endl;
109  else
110  return err;
111 
112  // Load matrix in text format
113  vpMatrix M1;
114  char header_[100];
115  if (vpMatrix::loadMatrix("matrix.mat", M1, false, header_))
116  std::cout << "Matrix loaded from matrix.mat file with header \"" << header_ << "\": \n" << M1 << std::endl;
117  else
118  return err;
119  if (header != std::string(header_)) {
120  std::cout << "Bad header in matrix.mat" << std::endl;
121  return err;
122  }
123 
124  // Save matrix in binary format
125  if (vpMatrix::saveMatrix("matrix.bin", M, true, header.c_str()))
126  std::cout << "Matrix saved in matrix.bin file" << std::endl;
127  else
128  return err;
129 
130  // Load matrix in binary format
131  if (vpMatrix::loadMatrix("matrix.bin", M1, true, header_))
132  std::cout << "Matrix loaded from matrix.bin file with header \"" << header_ << "\": \n" << M1 << std::endl;
133  else
134  return err;
135  if (header != std::string(header_)) {
136  std::cout << "Bad header in matrix.bin" << std::endl;
137  return err;
138  }
139 
140  // Save matrix in YAML format
141  if (vpMatrix::saveMatrixYAML("matrix.yml", M, header.c_str()))
142  std::cout << "Matrix saved in matrix.yml file" << std::endl;
143  else
144  return err;
145 
146  // Read matrix in YAML format
147  vpMatrix M2;
148  if (vpMatrix::loadMatrixYAML("matrix.yml", M2, header_))
149  std::cout << "Matrix loaded from matrix.yml file with header \"" << header_ << "\": \n" << M2 << std::endl;
150  else
151  return err;
152  if (header != std::string(header_)) {
153  std::cout << "Bad header in matrix.mat" << std::endl;
154  return err;
155  }
156  }
157 
158  {
160  std::cout << "R: \n" << R << std::endl;
161  vpMatrix M1(R);
162  std::cout << "M1: \n" << M1 << std::endl;
163  vpMatrix M2(M1);
164  std::cout << "M2: \n" << M2 << std::endl;
165  vpMatrix M3 = R;
166  std::cout << "M3: \n" << M3 << std::endl;
167  vpMatrix M4 = M1;
168  std::cout << "M4: \n" << M4 << std::endl;
169  }
170  {
171 
172  std::cout << "------------------------" << std::endl;
173  std::cout << "--- TEST PRETTY PRINT---" << std::endl;
174  std::cout << "------------------------" << std::endl;
175  vpMatrix M ;
176  M.eye(4);
177 
178  std::cout << "call std::cout << M;" << std::endl;
179  std::cout << M << std::endl;
180 
181  std::cout << "call M.print (std::cout, 4);" << std::endl;
182  M.print (std::cout, 4);
183 
184  std::cout << "------------------------" << std::endl;
185  M.resize(3,3) ;
186  M.eye(3);
187  M[1][0]=1.235;
188  M[1][1]=12.345;
189  M[1][2]=.12345;
190  std::cout << "call std::cout << M;" << std::endl;
191  std::cout << M;
192  std::cout << "call M.print (std::cout, 6);" << std::endl;
193  M.print (std::cout, 6);
194  std::cout << std::endl;
195 
196  std::cout << "------------------------" << std::endl;
197  M[0][0]=-1.235;
198  M[1][0]=-12.235;
199 
200  std::cout << "call std::cout << M;" << std::endl;
201  std::cout << M << std::endl;
202 
203  std::cout << "call M.print (std::cout, 10);" << std::endl;
204  M.print (std::cout, 10);
205  std::cout << std::endl;
206 
207  std::cout << "call M.print (std::cout, 2);" << std::endl;
208  M.print (std::cout, 2);
209  std::cout << std::endl;
210 
211  std::cout << "------------------------" << std::endl;
212  M.resize(3,3) ;
213  M.eye(3);
214  M[0][2]=-0.0000000876;
215  std::cout << "call std::cout << M;" << std::endl;
216  std::cout << M << std::endl;
217 
218  std::cout << "call M.print (std::cout, 4);" << std::endl;
219  M.print (std::cout, 4);
220  std::cout << std::endl;
221  std::cout << "call M.print (std::cout, 10, \"M\");" << std::endl;
222  M.print (std::cout, 10, "M");
223  std::cout << std::endl;
224  std::cout << "call M.print (std::cout, 20, \"M\");" << std::endl;
225  M.print (std::cout, 20, "M");
226  std::cout << std::endl;
227 
228 
229  std::cout << "------------------------" << std::endl;
230  std::cout << "--- TEST RESIZE --------" << std::endl;
231  std::cout << "------------------------" << std::endl;
232  std::cout << "5x5" << std::endl;
233  M.resize(5,5,false);
234  std::cout << M << std::endl;
235  std::cout << "3x2" << std::endl;
236  M.resize(3,2,false);
237  std::cout << M << std::endl;
238  std::cout << "2x2" << std::endl;
239  M.resize(2,2,false);
240  std::cout << M << std::endl;
241  std::cout << "------------------------" << std::endl;
242 
244  vpMatrix A(1,6),B;
245 
246  A=1.0;
247  //vMe=1.0;
248  B=A*vMe;
249 
250  std::cout << "------------------------" << std::endl;
251  std::cout << "--- TEST vpRowVector * vpColVector" << std::endl;
252  std::cout << "------------------------" << std::endl;
253  vpRowVector r(3);
254  r[0] = 2;
255  r[1] = 3;
256  r[2] = 4;
257 
258  vpColVector c(3);
259  c[0] = 1;
260  c[1] = 2;
261  c[2] = -1;
262 
263  double rc = r * c;
264 
265  r.print(std::cout, 2, "r");
266  c.print(std::cout, 2, "c");
267  std::cout << "r * c = " << rc << std::endl;
268 
269  std::cout << "------------------------" << std::endl;
270  std::cout << "--- TEST vpRowVector * vpMatrix" << std::endl;
271  std::cout << "------------------------" << std::endl;
272  M.resize(3,3) ;
273  M.eye(3);
274 
275  M[1][0] = 1.5;
276  M[2][0] = 2.3;
277 
278  vpRowVector rM = r * M;
279 
280  r.print(std::cout, 2, "r");
281  M.print(std::cout, 10, "M");
282  std::cout << "r * M = " << rM << std::endl;
283 
284  std::cout << "------------------------" << std::endl;
285  std::cout << "--- TEST vpGEMM " << std::endl;
286  std::cout << "------------------------" << std::endl;
287  M.resize(3,3) ;
288  M.eye(3);
289  vpMatrix N(3, 3);
290  N[0][0] = 2;
291  N[1][0] = 1.2;
292  N[1][2] = 0.6;
293  N[2][2] = 0.25;
294 
295  vpMatrix C(3, 3);
296  C.eye(3);
297 
298  vpMatrix D;
299 
300  //realise the operation D = 2 * M^T * N + 3 C
301  vpGEMM(M, N, 2, C, 3, D, VP_GEMM_A_T);
302  std::cout << D << std::endl;
303 
304  std::cout << "All tests succeed" << std::endl;
305  return 0;
306  }
307  }
308  catch(vpException &e) {
309  std::cout << "Catch an exception: " << e << std::endl;
310  return 1;
311  }
312 }
313 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, const bool binary=false, char *header=NULL)
Definition: vpMatrix.h:523
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:70
error that can be emited by ViSP classes.
Definition: vpException.h:73
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
Implementation of a rotation matrix and operations on such kind of matrices.
void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols)
Definition: vpMatrix.cpp:137
Implementation of a velocity twist matrix and operations on such kind of matrices.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Definition: vpMatrix.cpp:2634
static double rad(double deg)
Definition: vpMath.h:104
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpRowVector.h:205
void vpGEMM(const vpArray2D< double > &A, const vpArray2D< double > &B, const double &alpha, const vpArray2D< double > &C, const double &beta, vpArray2D< double > &D, const unsigned int &ops=0)
Definition: vpGEMM.h:358
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
int print(std::ostream &s, unsigned int length, char const *intro=0) const
static bool saveMatrixYAML(const std::string &filename, const vpArray2D< double > &M, const char *header="")
Definition: vpMatrix.h:572
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, const bool binary=false, const char *header="")
Definition: vpMatrix.h:555
void eye()
Definition: vpMatrix.cpp:194
static bool loadMatrixYAML(const std::string &filename, vpArray2D< double > &M, char *header=NULL)
Definition: vpMatrix.h:538