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