ViSP  2.9.0
testPoseFeatures.cpp
1 /****************************************************************************
2  *
3  * $Id: testPoseFeatures.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 from visual features by virtual visual servoing.
36  *
37  * Authors:
38  * Aurelien Yol
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 #include <visp/vpHomogeneousMatrix.h>
44 #include <visp/vpPoint.h>
45 #include <visp/vpDisplay.h>
46 #include <visp/vpDisplayX.h>
47 #include <visp/vpImage.h>
48 #include <visp/vpCameraParameters.h>
49 #include <visp/vpPoseFeatures.h>
50 #include <visp/vpPose.h>
51 #include <iostream>
52 #include <vector>
53 #include <visp/vpPose.h>
54 #include <limits>
55 
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
65 class vp_createPointClass{
66 public:
67  int value;
68 
69  vp_createPointClass() : value(0){}
70 
71  int vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
72  value += 1;
74  return value;
75  }
76 };
77 
78 void vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
80 }
81 
82 void vp_createLine(vpFeatureLine &fp,const vpLine &v){
84 }
85 #endif
86 #endif
87 
88 int main()
89 {
90  try {
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  vpPoint pts[6];
102 
103  double val = 0.25;
104  double val2 = 0.0;
105 
106  //2D Point Feature
107  pts[0].setWorldCoordinates(0.0,-val,val2);
108  pts[1].setWorldCoordinates(0.0,val,val2);
109  pts[2].setWorldCoordinates(-val,val,val2);
110 
111  //Segment Feature
112  pts[3].setWorldCoordinates(-val,-val/2.0,val2);
113  pts[4].setWorldCoordinates(val,val/2.0,val2);
114 
115  //3D point Feature
116  pts[5].setWorldCoordinates(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  pose.computePose(cMo_est);
189  // pose.computePose(cMo_est, vpPoseFeatures::ROBUST_VIRTUAL_VS);
190 
191 
192  std::cout << "\nEstimated pose from visual features : " << std::endl;
193  pose_est.buildFrom(cMo_est);
194  std::cout << pose_est.t() << std::endl;
195 
196  std::cout << "\nResulting covariance (Diag): " << std::endl;
197  vpMatrix covariance = pose.getCovarianceMatrix();
198  std::cout << covariance[0][0] << " " << covariance[1][1] << " "
199  << covariance[2][2] << " " << covariance[3][3] << " "
200  << covariance[4][4] << " " << covariance[5][5] << " " << std::endl;
201 
202  int test_fail = 0;
203  for(unsigned int i=0; i<6; i++) {
204  if (std::fabs(pose_ref[i]-pose_est[i]) > 0.001)
205  test_fail = 1;
206  }
207 
208  std::cout << "\nPose is " << (test_fail ? "badly" : "well") << " estimated" << std::endl;
209 
210  return test_fail;
211  }
212  catch(vpException e) {
213  std::cout << "Catch an exception: " << e << std::endl;
214  return 1;
215  }
216 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void setVerbose(const bool &mode)
void setVVSIterMax(const unsigned int &val)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
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:98
error that can be emited by ViSP classes.
Definition: vpException.h:76
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:65
void addFeatureEllipse(const vpCircle &)
vpPoseVector buildFrom(const vpHomogeneousMatrix &M)
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:124
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
vpRowVector t() const
transpose of Vector
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:100
void setCovarianceComputation(const bool &flag)
vpMatrix getCovarianceMatrix() const
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
void addSpecificFeature(RetType(*fct_ptr)(ArgsFunc...), Args &&...args)
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:61
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74