Visual Servoing Platform  version 3.6.1 under development (2024-05-08)
testDisplacement.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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  * Tests transformation within various representations of rotation.
32  */
33 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpRotationMatrix.h>
48 #include <visp3/core/vpThetaUVector.h>
49 #include <visp3/vision/vpHomography.h>
50 
51 bool test(const std::string &s, const vpHomography &H, const std::vector<double> &bench)
52 {
53  static unsigned int cpt = 0;
54  std::cout << "** Test " << ++cpt << std::endl;
55  std::cout << s << "(" << H.getRows() << "," << H.getCols() << ") = \n[" << H << "]" << std::endl;
56  if (bench.size() != H.size()) {
57  std::cout << "Test fails: bad size wrt bench" << std::endl;
58  return false;
59  }
60  for (unsigned int i = 0; i < H.size(); i++) {
61  if (std::fabs(H.data[i] - bench[i]) > std::fabs(H.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 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
73  try {
74  {
75  vpHomography H;
76  H.eye();
77  std::vector<double> bench(9, 0);
78  bench[0] = bench[4] = bench[8] = 1.;
79  int err = 1;
80  if (test("H", H, bench) == false)
81  return err;
82  if (test("H", H / H[2][2], bench) == false)
83  return err;
84  }
85  {
87 
88  std::cout << "Initialization " << std::endl;
89  // std::cout << tu << std::endl ;
90 
91  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl;
92  vpRotationMatrix R(tu);
93 
94  // pure rotation
96  M.insert(R);
97 
98  std::cout << "M" << std::endl << M << std::endl;
99  vpPlane p(0, 0, 1, 1);
100 
101  vpHomography H(M, p);
102 
103  std::cout << "H" << std::endl << H << std::endl;
104 
105  vpColVector n;
107 
108  H.computeDisplacement(R, T, n);
109 
110  std::cout << "R" << std::endl << R;
111  std::cout << "T" << std::endl << T.t() << std::endl;
112  std::cout << "n" << std::endl << n.t() << std::endl;
113  }
114  std::cout << "------------------------------------------------------" << std::endl;
115 
116  {
118 
119  std::cout << "Initialization " << std::endl;
120  // std::cout << tu << std::endl ;
121 
122  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl;
123  vpRotationMatrix R(tu);
124 
125  // pure rotation
127  M.insert(R);
128 
129  M[0][3] = 0.21;
130  M[1][3] = 0.31;
131  M[2][3] = 0.5;
132 
133  std::cout << "M" << std::endl << M << std::endl;
134  vpPlane p(0, 0, 1, 1);
135 
136  vpHomography H(M, p);
137 
138  std::cout << "H" << std::endl << H << std::endl;
139 
140  vpColVector n;
142 
143  H.computeDisplacement(R, T, n);
144 
145  std::cout << "R" << std::endl << R;
146  std::cout << "T" << std::endl << T.t() << std::endl;
147  std::cout << "n" << std::endl << n.t() << std::endl;
148  }
149 
150  std::cout << "------------------------------------------------------" << std::endl;
151  {
152  vpThetaUVector tu(vpMath::rad(-190), vpMath::rad(12), vpMath::rad(-45));
153 
154  vpRotationMatrix R(tu);
155 
156  // pure rotation
158  M.insert(R);
159 
160  M[0][3] = 0.21;
161  M[1][3] = -0.31;
162  M[2][3] = 0.5;
163 
164  std::cout << "M" << std::endl << M << std::endl;
165  vpPlane p(0.4, -0.5, 0.5, 1);
166 
167  vpHomography H(M, p);
168 
169  std::cout << "H" << std::endl << H << std::endl;
170 
171  vpColVector n;
173  H.computeDisplacement(R, T, n);
174 
175  std::cout << "R" << std::endl << R;
176  std::cout << "T" << std::endl << T.t() << std::endl;
177  std::cout << "n" << std::endl << n.t() << std::endl;
178 
179  vpPlane p1(n[0], n[1], n[2], 1.0);
180  H.buildFrom(R, T, p1);
181  std::cout << "H" << std::endl << H << std::endl;
182  }
183  std::cout << "All tests succeed" << std::endl;
184  return EXIT_SUCCESS;
185  } catch (const vpException &e) {
186  std::cout << "Catch an exception: " << e << std::endl;
187  return EXIT_FAILURE;
188  }
189 #else
190  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
191  return EXIT_SUCCESS;
192 #endif
193 }
unsigned int getCols() const
Definition: vpArray2D.h:327
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
unsigned int getRows() const
Definition: vpArray2D.h:337
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
void insert(const vpRotationMatrix &R)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:168
void buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP)
Construction from translation and rotation and a plane.
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static double rad(double deg)
Definition: vpMath.h:127
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:54
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
vpRowVector t() const