Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testPose.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  * Compute the pose of a 3D object using the Dementhon, Lagrange and
32  * Non-Linear approach.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/vision/vpPose.h>
41 #include <visp3/core/vpPoint.h>
42 #include <visp3/core/vpMath.h>
43 #include <visp3/core/vpTranslationVector.h>
44 #include <visp3/core/vpRxyzVector.h>
45 #include <visp3/core/vpRotationMatrix.h>
46 #include <visp3/core/vpHomogeneousMatrix.h>
47 #include <visp3/core/vpDebug.h>
48 
49 #include <stdlib.h>
50 #include <stdio.h>
51 
52 #define L 0.035
53 
62 void print_pose(const vpHomogeneousMatrix &cMo, const std::string &legend);
63 int compare_pose(const vpPose &pose, const vpHomogeneousMatrix &cMo_ref, const vpHomogeneousMatrix &cMo_est,
64  const std::string &legend);
65 
66 // print the resulting estimated pose
67 void print_pose(const vpHomogeneousMatrix &cMo, const std::string &legend)
68 {
69  vpPoseVector cpo = vpPoseVector(cMo);
70 
71  std::cout << std::endl << legend << "\n "
72  << "tx = " << cpo[0] << "\n "
73  << "ty = " << cpo[1] << "\n "
74  << "tz = " << cpo[2] << "\n "
75  << "tux = vpMath::rad(" << vpMath::deg(cpo[3]) << ")\n "
76  << "tuy = vpMath::rad(" << vpMath::deg(cpo[4]) << ")\n "
77  << "tuz = vpMath::rad(" << vpMath::deg(cpo[5]) << ")\n"
78  << std::endl;
79 }
80 
81 // test if pose is well estimated
82 int compare_pose(const vpPose &pose, const vpHomogeneousMatrix &cMo_ref, const vpHomogeneousMatrix &cMo_est,
83  const std::string &legend)
84 {
85  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
86  vpPoseVector pose_est = vpPoseVector(cMo_est);
87 
88  int fail = 0;
89 
90  // Test done on the 3D pose
91  for(unsigned int i=0; i<6; i++) {
92  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
93  fail = 1;
94  }
95 
96  std::cout << "Based on 3D parameters " << legend << " is " << (fail ? "badly" : "well") << " estimated" << std::endl;
97 
98  // Test done on the residual
99  double r = pose.computeResidual(cMo_est);
100  if (pose.listP.size() < 4) {
101  fail = 1;
102  std::cout << "Not enough point" << std::endl;
103  return fail;
104  }
105  r = sqrt(r)/pose.listP.size();
106  //std::cout << "Residual on each point (meter): " << r << std::endl;
107  fail = (r > 0.1) ? 1 : 0;
108  std::cout << "Based on 2D residual (" << r << ") " << legend << " is " << (fail ? "badly" : "well") << " estimated" << std::endl;
109  return fail;
110 }
111 
112 int main()
113 {
114  try {
115  vpPoint P[5] ; // Point to be tracked
116  vpPose pose ;
117  pose.clearPoint() ;
118 
119  P[0].setWorldCoordinates(-L,-L, 0 ) ;
120  P[1].setWorldCoordinates(L,-L, 0 ) ;
121  P[2].setWorldCoordinates(L,L, 0 ) ;
122  P[3].setWorldCoordinates(-2*L, 3*L, 0 ) ;
123  P[4].setWorldCoordinates(-L,L, 0.01 ) ;
124 
125  int test_fail = 0, fail = 0;
126  vpPoseVector cpo_ref = vpPoseVector(0.01, 0.02, 0.25, vpMath::rad(5), 0,vpMath::rad(10));
127  vpHomogeneousMatrix cMo_ref(cpo_ref) ;
128  vpHomogeneousMatrix cMo ; // will contain the estimated pose
129 
130  for(int i=0 ; i < 5 ; i++) {
131  P[i].project(cMo_ref) ;
132  //P[i].print();
133  pose.addPoint(P[i]) ; // and added to the pose computation class
134  }
135 
136  // Let's go ...
137  print_pose(cMo_ref, std::string("Reference pose")); // print the reference pose
138 
139  std::cout <<"-------------------------------------------------"<<std::endl ;
140  pose.computePose(vpPose::LAGRANGE, cMo) ;
141 
142  print_pose(cMo, std::string("Pose estimated by Lagrange"));
143  fail = compare_pose(pose, cMo_ref, cMo, "pose by Lagrange");
144  test_fail |= fail;
145 
146  std::cout <<"--------------------------------------------------"<<std::endl ;
147  pose.computePose(vpPose::DEMENTHON, cMo) ;
148 
149  print_pose(cMo, std::string("Pose estimated by Dementhon"));
150  fail = compare_pose(pose, cMo_ref, cMo, "pose by Dementhon");
151  test_fail |= fail;
152 
153  std::cout <<"--------------------------------------------------"<<std::endl ;
155  pose.setRansacThreshold(0.01);
156  pose.computePose(vpPose::RANSAC, cMo) ;
157 
158  print_pose(cMo, std::string("Pose estimated by Ransac"));
159  fail = compare_pose(pose, cMo_ref, cMo, "pose by Ransac");
160  test_fail |= fail;
161 
162  std::cout <<"--------------------------------------------------"<<std::endl ;
164 
165  print_pose(cMo, std::string("Pose estimated by Lagrange than Lowe"));
166  fail = compare_pose(pose, cMo_ref, cMo, "pose by Lagrange than Lowe");
167  test_fail |= fail;
168 
169  std::cout <<"--------------------------------------------------"<<std::endl ;
171 
172  print_pose(cMo, std::string("Pose estimated by Dementhon than Lowe"));
173  fail = compare_pose(pose, cMo_ref, cMo, "pose by Dementhon than Lowe");
174  test_fail |= fail;
175 
176  // Now Virtual Visual servoing
177 
178  std::cout <<"--------------------------------------------------"<<std::endl ;
179  pose.computePose(vpPose::VIRTUAL_VS, cMo) ;
180 
181  print_pose(cMo, std::string("Pose estimated by VVS"));
182  fail = compare_pose(pose, cMo_ref, cMo, "pose by VVS");
183  test_fail |= fail;
184 
185  std::cout <<"-------------------------------------------------"<<std::endl ;
187 
188  print_pose(cMo, std::string("Pose estimated by Dementhon than by VVS"));
189  fail = compare_pose(pose, cMo_ref, cMo, "pose by Dementhon than by VVS");
190  test_fail |= fail;
191 
192  std::cout <<"-------------------------------------------------"<<std::endl ;
194 
195  print_pose(cMo, std::string("Pose estimated by Lagrange than by VVS"));
196  fail = compare_pose(pose, cMo_ref, cMo, "pose by Lagrange than by VVS");
197  test_fail |= fail;
198 
199  std::cout << "\nGlobal pose estimation test " << (test_fail ? "fail" : "is ok") << std::endl;
200 
201  return test_fail;
202  }
203  catch(vpException &e) {
204  std::cout << "Catch an exception: " << e << std::endl;
205  return 1;
206  }
207 }
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:73
void setRansacThreshold(const double &t)
Definition: vpPose.h:227
std::list< vpPoint > listP
Array of point (use here class vpPoint)
Definition: vpPose.h:101
Class that defines what is a point.
Definition: vpPoint.h:59
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:76
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:372
static double rad(double deg)
Definition: vpMath.h:104
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:226
static double deg(double rad)
Definition: vpMath.h:97
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:145
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
Definition: vpPose.cpp:337
void clearPoint()
Definition: vpPose.cpp:130