Visual Servoing Platform  version 3.0.0
testPoseFeatures.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  * Compute the pose from visual features by virtual visual servoing.
32  *
33  * Authors:
34  * Aurelien Yol
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpHomogeneousMatrix.h>
40 #include <visp3/core/vpPoint.h>
41 #include <visp3/core/vpImage.h>
42 #include <visp3/core/vpCameraParameters.h>
43 #include <visp3/vision/vpPoseFeatures.h>
44 #include <visp3/vision/vpPose.h>
45 #include <iostream>
46 #include <vector>
47 #include <visp3/vision/vpPose.h>
48 #include <limits>
49 
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
59 class vp_createPointClass{
60 public:
61  int value;
62 
63  vp_createPointClass() : value(0){}
64 
65  int vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
66  value += 1;
68  return value;
69  }
70 };
71 
72 void vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
74 }
75 
76 void vp_createLine(vpFeatureLine &fp,const vpLine &v){
78 }
79 #endif
80 #endif
81 
82 int main()
83 {
84  try {
85  vpImage<unsigned char> I(600,600);
86 
87  vpHomogeneousMatrix cMo_ref(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(60));
88  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
89 
90  std::cout << "Reference pose used to create the visual features : " << std::endl;
91  std::cout << pose_ref.t() << std::endl;
92 
93  vpPoseFeatures pose;
94 
95  std::vector<vpPoint> pts;
96 
97  double val = 0.25;
98  double val2 = 0.0;
99 
100  //2D Point Feature
101  pts.push_back( vpPoint(0.0,-val,val2) );
102  pts.push_back( vpPoint(0.0,val,val2) );
103  pts.push_back( vpPoint(-val,val,val2) );
104 
105  //Segment Feature
106  pts.push_back( vpPoint(-val,-val/2.0,val2) );
107  pts.push_back( vpPoint(val,val/2.0,val2) );
108 
109  //3D point Feature
110  pts.push_back( vpPoint(0.0,0.0,-1.5) );
111 
112  //Line Feature
113  vpLine line;
114  line.setWorldCoordinates(0.0,1.0,0.0,.0,
115  0.0,0.0,1.0,0.0);
116 
117  //Vanishing Point Feature
118  vpLine l1;
119  l1.setWorldCoordinates(0.0,1.0,0.2,0.0,
120  1.0,0.0,0.0,-0.25);
121 
122  vpLine l2;
123  l2.setWorldCoordinates(0.0,1.0,0.2,0.0,
124  -1.0,0.0,0.0,-0.25);
125 
126  //Ellipse Feature
127  vpCircle circle;
128  circle.setWorldCoordinates(0.0, 0.0, 1.0 , 0.0, 0.0, 0.0, 0.25);
129 
130  pts[0].project(cMo_ref);
131  pts[1].project(cMo_ref);
132  pts[2].project(cMo_ref);
133 
134  pts[3].project(cMo_ref);
135  pts[4].project(cMo_ref);
136 
137  pts[5].project(cMo_ref);
138 
139  line.project(cMo_ref);
140 
141  l1.project(cMo_ref);
142  l2.project(cMo_ref);
143 
144  circle.project(cMo_ref);
145 
146  pose.addFeaturePoint(pts[0]);
147  // pose.addFeaturePoint(pts[1]);
148  pose.addFeaturePoint(pts[2]);
149 
150  pose.addFeaturePoint3D(pts[5]);
151 
152  pose.addFeatureVanishingPoint(l1,l2);
153 
154  // pose.addFeatureSegment(pts[3],pts[4]);
155  //
156  // pose.addFeatureLine(line);
157 
158  pose.addFeatureEllipse(circle);
159 
160 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
161  vpFeaturePoint fp;
162  vpFeatureLine fl;
163  vpFeatureSegment fs;
164  void (*ptr)(vpFeatureSegment&, vpPoint&, vpPoint&) = &vpFeatureBuilder::create;
165  vp_createPointClass cpClass;
166  int (vp_createPointClass::*ptrClass)(vpFeaturePoint&, const vpPoint&) = &vp_createPointClass::vp_createPoint;
167  pose.addSpecificFeature(&cpClass, ptrClass, fp, pts[1]);
168  pose.addSpecificFeature(&vp_createLine, fl, line);
169  pose.addSpecificFeature(ptr, fs, pts[3], pts[4]);
170 #endif
171 
172  pose.setVerbose(true);
173  pose.setLambda(0.6);
174  pose.setVVSIterMax(200);
175  pose.setCovarianceComputation(true);
176 
177  vpHomogeneousMatrix cMo_est(0.4, 0.3, 1.5, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
178  vpPoseVector pose_est = vpPoseVector(cMo_est);
179  std::cout << "\nPose used as initialisation of the pose computation : " << std::endl;
180  std::cout << pose_est.t() << std::endl;
181 
182  pose.computePose(cMo_est);
183  // pose.computePose(cMo_est, vpPoseFeatures::ROBUST_VIRTUAL_VS);
184 
185 
186  std::cout << "\nEstimated pose from visual features : " << std::endl;
187  pose_est.buildFrom(cMo_est);
188  std::cout << pose_est.t() << std::endl;
189 
190  std::cout << "\nResulting covariance (Diag): " << std::endl;
191  vpMatrix covariance = pose.getCovarianceMatrix();
192  std::cout << covariance[0][0] << " " << covariance[1][1] << " "
193  << covariance[2][2] << " " << covariance[3][3] << " "
194  << covariance[4][4] << " " << covariance[5][5] << " " << std::endl;
195 
196  int test_fail = 0;
197  for(unsigned int i=0; i<6; i++) {
198  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
199  test_fail = 1;
200  }
201 
202  std::cout << "\nPose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
203 
204  return test_fail;
205  }
206  catch(vpException e) {
207  std::cout << "Catch an exception: " << e << std::endl;
208  return 1;
209  }
210 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
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:120
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)
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