Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testPoseFeatures.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 from visual features by virtual visual servoing.
32  *
33  * Authors:
34  * Aurelien Yol
35  *
36  *****************************************************************************/
37 
38 #include <iostream>
39 #include <vector>
40 #include <limits>
41 
42 #include <visp3/core/vpConfig.h>
43 #include <visp3/core/vpHomogeneousMatrix.h>
44 #include <visp3/core/vpPoint.h>
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpCameraParameters.h>
47 #include <visp3/vision/vpPoseFeatures.h>
48 #include <visp3/vision/vpPose.h>
49 
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 
59 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
60 class vp_createPointClass{
61 public:
62  int value;
63 
64  vp_createPointClass() : value(0){}
65 
66  int vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
67  value += 1;
69  return value;
70  }
71 };
72 
73 void vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
75 }
76 
77 void vp_createLine(vpFeatureLine &fp,const vpLine &v){
79 }
80 #endif
81 #endif
82 
83 
84 int test_pose(bool use_robust)
85 {
86  if (use_robust)
87  std::cout << "** Test robust pose estimation from features\n" << std::endl;
88  else
89  std::cout << "** Test pose estimation from features\n" << std::endl;
90 
91  vpImage<unsigned char> I(600,600);
92 
93  vpHomogeneousMatrix cMo_ref(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(60));
94  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
95 
96  std::cout << "Reference pose used to create the visual features : " << std::endl;
97  std::cout << pose_ref.t() << std::endl;
98 
99  vpPoseFeatures pose;
100 
101  std::vector<vpPoint> pts;
102 
103  double val = 0.25;
104  double val2 = 0.0;
105 
106  //2D Point Feature
107  pts.push_back( vpPoint(0.0,-val,val2) );
108  pts.push_back( vpPoint(0.0,val,val2) );
109  pts.push_back( vpPoint(-val,val,val2) );
110 
111  //Segment Feature
112  pts.push_back( vpPoint(-val,-val/2.0,val2) );
113  pts.push_back( vpPoint(val,val/2.0,val2) );
114 
115  //3D point Feature
116  pts.push_back( vpPoint(0.0,0.0,-1.5) );
117 
118  //Line Feature
119  vpLine line;
120  line.setWorldCoordinates(0.0,1.0,0.0,.0,
121  0.0,0.0,1.0,0.0);
122 
123  //Vanishing Point Feature
124  vpLine l1;
125  l1.setWorldCoordinates(0.0,1.0,0.2,0.0,
126  1.0,0.0,0.0,-0.25);
127 
128  vpLine l2;
129  l2.setWorldCoordinates(0.0,1.0,0.2,0.0,
130  -1.0,0.0,0.0,-0.25);
131 
132  //Ellipse Feature
133  vpCircle circle;
134  circle.setWorldCoordinates(0.0, 0.0, 1.0 , 0.0, 0.0, 0.0, 0.25);
135 
136  pts[0].project(cMo_ref);
137  pts[1].project(cMo_ref);
138  pts[2].project(cMo_ref);
139 
140  pts[3].project(cMo_ref);
141  pts[4].project(cMo_ref);
142 
143  pts[5].project(cMo_ref);
144 
145  line.project(cMo_ref);
146 
147  l1.project(cMo_ref);
148  l2.project(cMo_ref);
149 
150  circle.project(cMo_ref);
151 
152  pose.addFeaturePoint(pts[0]);
153  // pose.addFeaturePoint(pts[1]);
154  pose.addFeaturePoint(pts[2]);
155 
156  pose.addFeaturePoint3D(pts[5]);
157 
158  pose.addFeatureVanishingPoint(l1,l2);
159 
160  // pose.addFeatureSegment(pts[3],pts[4]);
161  //
162  // pose.addFeatureLine(line);
163 
164  pose.addFeatureEllipse(circle);
165 
166 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
167  vpFeaturePoint fp;
168  vpFeatureLine fl;
169  vpFeatureSegment fs;
170  void (*ptr)(vpFeatureSegment&, vpPoint&, vpPoint&) = &vpFeatureBuilder::create;
171  vp_createPointClass cpClass;
172  int (vp_createPointClass::*ptrClass)(vpFeaturePoint&, const vpPoint&) = &vp_createPointClass::vp_createPoint;
173  pose.addSpecificFeature(&cpClass, ptrClass, fp, pts[1]);
174  pose.addSpecificFeature(&vp_createLine, fl, line);
175  pose.addSpecificFeature(ptr, fs, pts[3], pts[4]);
176 #endif
177 
178  pose.setVerbose(true);
179  pose.setLambda(0.6);
180  pose.setVVSIterMax(200);
181  pose.setCovarianceComputation(true);
182 
183  vpHomogeneousMatrix cMo_est(0.4, 0.3, 1.5, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
184  vpPoseVector pose_est = vpPoseVector(cMo_est);
185  std::cout << "\nPose used as initialisation of the pose computation : " << std::endl;
186  std::cout << pose_est.t() << std::endl;
187 
188  if (! use_robust)
189  pose.computePose(cMo_est);
190  else
192 
193  if (! use_robust)
194  std::cout << "\nEstimated pose from visual features : " << std::endl;
195  else
196  std::cout << "\nRobust estimated pose from visual features : " << std::endl;
197 
198  pose_est.buildFrom(cMo_est);
199  std::cout << pose_est.t() << std::endl;
200 
201  std::cout << "\nResulting covariance (Diag): " << std::endl;
202  vpMatrix covariance = pose.getCovarianceMatrix();
203  std::cout << covariance[0][0] << " " << covariance[1][1] << " "
204  << covariance[2][2] << " " << covariance[3][3] << " "
205  << covariance[4][4] << " " << covariance[5][5] << " " << std::endl;
206 
207  int test_fail = 0;
208  for(unsigned int i=0; i<6; i++) {
209  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
210  test_fail = 1;
211  }
212 
213  std::cout << "\nPose is " << (test_fail ? "badly" : "well") << " estimated\n" << std::endl;
214 
215  return test_fail;
216 }
217 
218 int main()
219 {
220  try {
221  if (test_pose(false))
222  return -1;
223 
224  if (test_pose(true))
225  return -1;
226 
227  return 0;
228  }
229  catch(vpException &e) {
230  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
231  return -1;
232  }
233 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
void setVerbose(const bool &mode)
void setVVSIterMax(const unsigned int &val)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setWorldCoordinates(const double &A1, const double &B1, const double &C1, const double &D1, const double &A2, const double &B2, const double &C2, const double &D2)
Definition: vpLine.cpp:94
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void addFeatureVanishingPoint(const vpPoint &)
void setLambda(const double &val)
void addFeaturePoint3D(const vpPoint &)
void addFeaturePoint(const vpPoint &)
Class that defines what is a point.
Definition: vpPoint.h:59
void addFeatureEllipse(const vpCircle &)
void computePose(vpHomogeneousMatrix &cMo, const vpPoseFeaturesMethodType &type=VIRTUAL_VS)
Class that defines a line in the object frame, the camera frame and the image plane. All the parameters must be set in meter.
Definition: vpLine.h:105
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
Class that defines a 2D line visual feature which is composed by two parameters that are and ...
static double rad(double deg)
Definition: vpMath.h:104
void setCovarianceComputation(const bool &flag)
vpMatrix getCovarianceMatrix() const
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
void addSpecificFeature(RetType(*fct_ptr)(ArgsFunc...), Args &&...args)
vpPoseVector buildFrom(const double tx, const double ty, const double tz, const double tux, const double tuy, const double tuz)
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Tools for pose computation from any feature.This class allows to estimate a pose by virtual visual se...
Class that defines what is a circle.
Definition: vpCircle.h:57
vpRowVector t() const
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:62