Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testDisplacement.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  * Tests transformation within various representations of rotation.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpMath.h>
52 #include <visp3/core/vpRotationMatrix.h>
53 #include <visp3/core/vpThetaUVector.h>
54 #include <visp3/vision/vpHomography.h>
55 
56 bool test(const std::string &s, const vpHomography &H, const std::vector<double> &bench)
57 {
58  static unsigned int cpt = 0;
59  std::cout << "** Test " << ++cpt << std::endl;
60  std::cout << s << "(" << H.getRows() << "," << H.getCols() << ") = \n[" << H << "]" << std::endl;
61  if (bench.size() != H.size()) {
62  std::cout << "Test fails: bad size wrt bench" << std::endl;
63  return false;
64  }
65  for (unsigned int i = 0; i < H.size(); i++) {
66  if (std::fabs(H.data[i] - bench[i]) > std::fabs(H.data[i]) * std::numeric_limits<double>::epsilon()) {
67  std::cout << "Test fails: bad content" << std::endl;
68  return false;
69  }
70  }
71 
72  return true;
73 }
74 
75 int main()
76 {
77  try {
78  {
79  vpHomography H;
80  H.eye();
81  std::vector<double> bench(9, 0);
82  bench[0] = bench[4] = bench[8] = 1.;
83  int err = 1;
84  if (test("H", H, bench) == false)
85  return err;
86  if (test("H", H / H[2][2], bench) == false)
87  return err;
88  }
89  {
91 
92  std::cout << "Initialization " << std::endl;
93  // std::cout << tu << std::endl ;
94 
95  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl;
96  vpRotationMatrix R(tu);
97 
98  // pure rotation
100  M.insert(R);
101 
102  std::cout << "M" << std::endl << M << std::endl;
103  vpPlane p(0, 0, 1, 1);
104 
105  vpHomography H(M, p);
106 
107  std::cout << "H" << std::endl << H << std::endl;
108 
109  vpColVector n;
111 
112  H.computeDisplacement(R, T, n);
113 
114  std::cout << "R" << std::endl << R;
115  std::cout << "T" << std::endl << T.t() << std::endl;
116  std::cout << "n" << std::endl << n.t() << std::endl;
117  }
118  std::cout << "------------------------------------------------------" << std::endl;
119 
120  {
122 
123  std::cout << "Initialization " << std::endl;
124  // std::cout << tu << std::endl ;
125 
126  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl;
127  vpRotationMatrix R(tu);
128 
129  // pure rotation
131  M.insert(R);
132 
133  M[0][3] = 0.21;
134  M[1][3] = 0.31;
135  M[2][3] = 0.5;
136 
137  std::cout << "M" << std::endl << M << std::endl;
138  vpPlane p(0, 0, 1, 1);
139 
140  vpHomography H(M, p);
141 
142  std::cout << "H" << std::endl << H << std::endl;
143 
144  vpColVector n;
146 
147  H.computeDisplacement(R, T, n);
148 
149  std::cout << "R" << std::endl << R;
150  std::cout << "T" << std::endl << T.t() << std::endl;
151  std::cout << "n" << std::endl << n.t() << std::endl;
152  }
153 
154  std::cout << "------------------------------------------------------" << std::endl;
155  {
156  vpThetaUVector tu(vpMath::rad(-190), vpMath::rad(12), vpMath::rad(-45));
157 
158  vpRotationMatrix R(tu);
159 
160  // pure rotation
162  M.insert(R);
163 
164  M[0][3] = 0.21;
165  M[1][3] = -0.31;
166  M[2][3] = 0.5;
167 
168  std::cout << "M" << std::endl << M << std::endl;
169  vpPlane p(0.4, -0.5, 0.5, 1);
170 
171  vpHomography H(M, p);
172 
173  std::cout << "H" << std::endl << H << std::endl;
174 
175  vpColVector n;
177  H.computeDisplacement(R, T, n);
178 
179  std::cout << "R" << std::endl << R;
180  std::cout << "T" << std::endl << T.t() << std::endl;
181  std::cout << "n" << std::endl << n.t() << std::endl;
182 
183  vpPlane p1(n[0], n[1], n[2], 1.0);
184  H.buildFrom(R, T, p1);
185  std::cout << "H" << std::endl << H << std::endl;
186  }
187  std::cout << "All tests succeed" << std::endl;
188  return 0;
189  } catch (const vpException &e) {
190  std::cout << "Catch an exception: " << e << std::endl;
191  return 1;
192  }
193 }
void buildFrom(const vpRotationMatrix &aRb, const vpTranslationVector &atb, const vpPlane &bP)
Construction from Translation and rotation and a plane.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRowVector t() const
error that can be emited by ViSP classes.
Definition: vpException.h:71
unsigned int getRows() const
Definition: vpArray2D.h:289
vpRowVector t() const
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:145
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:291
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
Implementation of a rotation matrix and operations on such kind of matrices.
unsigned int getCols() const
Definition: vpArray2D.h:279
void insert(const vpRotationMatrix &R)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
static double rad(double deg)
Definition: vpMath.h:108
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.