ViSP  3.0.0
testDisplacement.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Tests transformation within various representations of rotation.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
48 #include <visp3/core/vpMath.h>
49 #include <visp3/core/vpRotationMatrix.h>
50 #include <visp3/vision/vpHomography.h>
51 #include <visp3/core/vpDebug.h>
52 #include <visp3/core/vpThetaUVector.h>
53 #include <stdlib.h>
54 #include <stdio.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 int main()
75 {
76  try {
77  int err = 1;
78  {
79  vpHomography H;
80  H.eye();
81  std::vector<double> bench(9, 0);
82  bench[0] = bench[4] = bench[8] = 1.;
83  if (test("H", H, bench) == false)
84  return err;
85  if (test("H", H/H[2][2], bench) == false)
86  return err;
87  }
88  {
90 
91  std::cout << "Initialization " <<std::endl ;
92  // std::cout << tu << std::endl ;
93 
94  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl ;
95  vpRotationMatrix R(tu) ;
96 
97  // pure rotation
99  M.insert(R) ;
100 
101  std::cout << "M" <<std::endl <<M << std::endl ;
102  vpPlane p(0,0,1,1) ;
103 
104  vpHomography H(M,p) ;
105 
106  std::cout << "H" <<std::endl <<H << std::endl ;
107 
108  vpColVector n ;
110 
111  H.computeDisplacement(R,T,n) ;
112 
113  std::cout << "R" <<std::endl << R ;
114  std::cout << "T" <<std::endl << T.t() << std::endl;
115  std::cout << "n" <<std::endl << n.t() << std::endl;
116  }
117  std::cout <<"------------------------------------------------------" << std::endl ;
118 
119  {
121 
122  std::cout << "Initialization " << std::endl ;
123  // std::cout << tu << std::endl ;
124 
125  std::cout << "From vpThetaUVector to vpRotationMatrix " << std::endl ;
126  vpRotationMatrix R(tu) ;
127 
128  // pure rotation
130  M.insert(R) ;
131 
132  M[0][3] = 0.21 ;
133  M[1][3] = 0.31 ;
134  M[2][3] = 0.5 ;
135 
136  std::cout << "M" << std::endl << M << std::endl ;
137  vpPlane p(0,0,1,1) ;
138 
139  vpHomography H(M,p) ;
140 
141  std::cout << "H" << std::endl << H << std::endl ;
142 
143  vpColVector n ;
145 
146  H.computeDisplacement(R,T,n) ;
147 
148  std::cout << "R" <<std::endl << R ;
149  std::cout << "T" <<std::endl << T.t() << std::endl;
150  std::cout << "n" <<std::endl << n.t() << std::endl;
151  }
152 
153  std::cout <<"------------------------------------------------------" << std::endl ;
154  {
155  vpThetaUVector tu(vpMath::rad(-190), vpMath::rad(12), vpMath::rad(-45)) ;
156 
157  vpRotationMatrix R(tu) ;
158 
159  // pure rotation
161  M.insert(R) ;
162 
163  M[0][3] = 0.21 ;
164  M[1][3] = -0.31 ;
165  M[2][3] = 0.5 ;
166 
167  std::cout << "M" << std::endl << M << std::endl ;
168  vpPlane p(0.4,-0.5,0.5,1) ;
169 
170  vpHomography H(M,p) ;
171 
172  std::cout << "H" << std::endl << H << std::endl ;
173 
174  vpColVector n ;
176  H.computeDisplacement(R,T,n) ;
177 
178  std::cout << "R" <<std::endl << R ;
179  std::cout << "T" <<std::endl << T.t() << std::endl;
180  std::cout << "n" <<std::endl << n.t() << std::endl;
181 
182  vpPlane p1(n[0],n[1],n[2],1.0) ;
183  H.buildFrom(R,T,p1) ;
184  std::cout << "H" << std::endl << H << std::endl ;
185  }
186  std::cout << "All tests succeed" << std::endl;
187  return 0;
188  }
189  catch(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.
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
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
Implementation of a rotation matrix and operations on such kind of matrices.
void insert(const vpRotationMatrix &R)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
vpRowVector t() const
vpRowVector t() const
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
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.