ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testPose.cpp
1 /****************************************************************************
2  *
3  * $Id: testPose.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Compute the pose of a 3D object using the Dementhon, Lagrange and
36  * Non-Linear approach.
37  *
38  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 #include <visp/vpPose.h>
45 #include <visp/vpPoint.h>
46 #include <visp/vpMath.h>
47 #include <visp/vpTranslationVector.h>
48 #include <visp/vpRxyzVector.h>
49 #include <visp/vpRotationMatrix.h>
50 #include <visp/vpHomogeneousMatrix.h>
51 #include <visp/vpDebug.h>
52 #include <visp/vpParseArgv.h>
53 
54 #include <stdlib.h>
55 #include <stdio.h>
56 
57 // List of allowed command line options
58 #define GETOPTARGS "h"
59 
60 #define L 0.035
61 
74 void usage(const char *name, const char *badparam)
75 {
76  fprintf(stdout, "\n\
77 Compute the pose of a 3D object using the Dementhon, Lagrange and\n\
78 Non-Linear approach.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-h]\n", name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: Default\n\
85  -h\n\
86  Print the help.\n");
87 
88  if (badparam)
89  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
90 }
98 bool getOptions(int argc, const char **argv)
99 {
100  const char *optarg;
101  int c;
102  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
103 
104  switch (c) {
105  case 'h': usage(argv[0], NULL); return false; break;
106 
107  default:
108  usage(argv[0], optarg);
109  return false; break;
110  }
111  }
112 
113  if ((c == 1) || (c == -1)) {
114  // standalone param or error
115  usage(argv[0], NULL);
116  std::cerr << "ERROR: " << std::endl;
117  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
118  return false;
119  }
120 
121  return true;
122 }
123 
124 // print the resulting estimated pose
125 void print_pose(const vpHomogeneousMatrix &cMo, const std::string &legend)
126 {
127  vpPoseVector cpo = vpPoseVector(cMo);
128 
129  std::cout << std::endl << legend << "\n "
130  << "tx = " << cpo[0] << "\n "
131  << "ty = " << cpo[1] << "\n "
132  << "tz = " << cpo[2] << "\n "
133  << "tux = vpMath::rad(" << vpMath::deg(cpo[3]) << ")\n "
134  << "tuy = vpMath::rad(" << vpMath::deg(cpo[4]) << ")\n "
135  << "tuz = vpMath::rad(" << vpMath::deg(cpo[5]) << ")\n"
136  << std::endl;
137 }
138 
139 // test if pose is well estimated
140 int compare_pose(const vpPose &pose, const vpHomogeneousMatrix &cMo_ref, const vpHomogeneousMatrix &cMo_est, const std::string &legend)
141 {
142  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
143  vpPoseVector pose_est = vpPoseVector(cMo_est);
144 
145  int fail = 0;
146 
147  // Test done on the 3D pose
148  for(unsigned int i=0; i<6; i++) {
149  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
150  fail = 1;
151  }
152  std::cout << "Based on 3D parameters " << legend << " is " << (fail ? "badly" : "well") << " estimated" << std::endl;
153 
154  // Test done on the residual
155  double r = pose.computeResidual(cMo_est);
156  r = sqrt(r)/pose.listP.size();
157  //std::cout << "Residual on each point (meter): " << r << std::endl;
158  fail = (r > 0.1) ? 1 : 0;
159  std::cout << "Based on 2D residual (" << r << ") " << legend << " is " << (fail ? "badly" : "well") << " estimated" << std::endl;
160  return fail;
161 }
162 
163 int
164 main(int argc, const char ** argv)
165 {
166  // Read the command line options
167  if (getOptions(argc, argv) == false) {
168  exit (-1);
169  }
170 
171  vpPoint P[5] ; // Point to be tracked
172  vpPose pose ;
173  pose.clearPoint() ;
174 
175  P[0].setWorldCoordinates(-L,-L, 0 ) ;
176  P[1].setWorldCoordinates(L,-L, 0 ) ;
177  P[2].setWorldCoordinates(L,L, 0 ) ;
178  P[3].setWorldCoordinates(-2*L, 3*L, 0 ) ;
179  P[4].setWorldCoordinates(-L,L, 0.01 ) ;
180  //P[3].setWorldCoordinates(-L,L, 0 ) ;
181 
182  int test_fail = 0, fail = 0;
183  vpPoseVector cpo_ref = vpPoseVector(0.01, 0.02, 0.25, vpMath::rad(5), 0,vpMath::rad(10));
184  vpHomogeneousMatrix cMo_ref(cpo_ref) ;
185  vpHomogeneousMatrix cMo ; // will contain the estimated pose
186 
187  for(int i=0 ; i < 5 ; i++) {
188  P[i].project(cMo_ref) ;
189  //P[i].print();
190  pose.addPoint(P[i]) ; // and added to the pose computation class
191  }
192 
193  // Let's go ...
194  print_pose(cMo_ref, std::string("Reference pose")); // print the reference pose
195 
196  std::cout <<"-------------------------------------------------"<<std::endl ;
197  pose.computePose(vpPose::LAGRANGE, cMo) ;
198 
199  print_pose(cMo, std::string("Pose estimated by Lagrange"));
200  fail = compare_pose(pose, cMo_ref, cMo, "pose by Lagrange");
201  test_fail |= fail;
202 
203  std::cout <<"--------------------------------------------------"<<std::endl ;
204  pose.computePose(vpPose::DEMENTHON, cMo) ;
205 
206  print_pose(cMo, std::string("Pose estimated by Dementhon"));
207  fail = compare_pose(pose, cMo_ref, cMo, "pose by Dementhon");
208  test_fail |= fail;
209 
210  std::cout <<"--------------------------------------------------"<<std::endl ;
212  pose.setRansacThreshold(0.01);
213  pose.computePose(vpPose::RANSAC, cMo) ;
214 
215  print_pose(cMo, std::string("Pose estimated by Ransac"));
216  fail = compare_pose(pose, cMo_ref, cMo, "pose by Ransac");
217  test_fail |= fail;
218 
219  std::cout <<"--------------------------------------------------"<<std::endl ;
221 
222  print_pose(cMo, std::string("Pose estimated by Lagrange than Lowe"));
223  fail = compare_pose(pose, cMo_ref, cMo, "pose by Lagrange than Lowe");
224  test_fail |= fail;
225 
226  std::cout <<"--------------------------------------------------"<<std::endl ;
228 
229  print_pose(cMo, std::string("Pose estimated by Dementhon than Lowe"));
230  fail = compare_pose(pose, cMo_ref, cMo, "pose by Dementhon than Lowe");
231  test_fail |= fail;
232 
233  // Now Virtual Visual servoing
234 
235  std::cout <<"--------------------------------------------------"<<std::endl ;
236  pose.computePose(vpPose::VIRTUAL_VS, cMo) ;
237 
238  print_pose(cMo, std::string("Pose estimated by VVS"));
239  fail = compare_pose(pose, cMo_ref, cMo, "pose by VVS");
240  test_fail |= fail;
241 
242  std::cout <<"-------------------------------------------------"<<std::endl ;
244 
245  print_pose(cMo, std::string("Pose estimated by Dementhon than by VVS"));
246  fail = compare_pose(pose, cMo_ref, cMo, "pose by Dementhon than by VVS");
247  test_fail |= fail;
248 
249  std::cout <<"-------------------------------------------------"<<std::endl ;
251 
252  print_pose(cMo, std::string("Pose estimated by Lagrange than by VVS"));
253  fail = compare_pose(pose, cMo_ref, cMo, "pose by Lagrange than by VVS");
254  test_fail |= fail;
255 
256  std::cout << "\nGlobal pose estimation test " << (test_fail ? "fail" : "is ok") << std::endl;
257 
258  return test_fail;
259 }