ViSP  2.6.2
testPoseFeatures.cpp
1 /****************************************************************************
2  *
3  * $Id: testPoseFeatures.cpp 3820 2012-06-27 13:13:29Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
65 public:
66  int value;
67 
68  vp_createPointClass() : value(0){}
69 
71  value += 1;
73  return value;
74  }
75 };
76 
77 void vp_createPoint(vpFeaturePoint &fp,const vpPoint &v){
79 }
80 
81 void vp_createLine(vpFeatureLine &fp,const vpLine &v){
83 }
84 #endif
85 
86 int main()
87 {
88  vpImage<unsigned char> I(600,600);
89 
90  vpHomogeneousMatrix cMo(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(60));
91 
92  std::cout << "cMo used for the projection : " << std::endl;
93  std::cout << vpPoseVector(cMo).t() << std::endl;
94 
95  vpPoseFeatures pose;
96 
97  vpPoint pts[6];
98 
99  double val = 0.25;
100  double val2 = 0.0;
101 
102  //2D Point Feature
103  pts[0].setWorldCoordinates(0.0,-val,val2);
104  pts[1].setWorldCoordinates(0.0,val,val2);
105  pts[2].setWorldCoordinates(-val,val,val2);
106 
107  //Segment Feature
108  pts[3].setWorldCoordinates(-val,-val/2.0,val2);
109  pts[4].setWorldCoordinates(val,val/2.0,val2);
110 
111  //3D point Feature
112  pts[5].setWorldCoordinates(0.0,0.0,-1.5);
113 
114  //Line Feature
115  vpLine line;
116  line.setWorldCoordinates(0.0,1.0,0.0,.0,
117  0.0,0.0,1.0,0.0);
118 
119  //Vanishing Point Feature
120  vpLine l1;
121  l1.setWorldCoordinates(0.0,1.0,0.2,0.0,
122  1.0,0.0,0.0,-0.25);
123 
124  vpLine l2;
125  l2.setWorldCoordinates(0.0,1.0,0.2,0.0,
126  -1.0,0.0,0.0,-0.25);
127 
128  //Ellipse Feature
129  vpCircle circle;
130  circle.setWorldCoordinates(0.0, 0.0, 1.0 , 0.0, 0.0, 0.0, 0.25);
131 
132  pts[0].project(cMo);
133  pts[1].project(cMo);
134  pts[2].project(cMo);
135 
136  pts[3].project(cMo);
137  pts[4].project(cMo);
138 
139  pts[5].project(cMo);
140 
141  line.project(cMo);
142 
143  l1.project(cMo);
144  l2.project(cMo);
145 
146  circle.project(cMo);
147 
148  pose.addFeaturePoint(pts[0]);
149 // pose.addFeaturePoint(pts[1]);
150  pose.addFeaturePoint(pts[2]);
151 
152  pose.addFeaturePoint3D(pts[5]);
153 
154  pose.addFeatureVanishingPoint(l1,l2);
155 
156 // pose.addFeatureSegment(pts[3],pts[4]);
157 //
158 // pose.addFeatureLine(line);
159 
160  pose.addFeatureEllipse(circle);
161 
162 #ifdef VISP_HAVE_CPP11_COMPATIBILITY
163  vpFeaturePoint fp;
164  vpFeatureLine fl;
165  vpFeatureSegment fs;
166  void (*ptr)(vpFeatureSegment&, vpPoint&, vpPoint&) = &vpFeatureBuilder::create;
167  vp_createPointClass cpClass;
168  int (vp_createPointClass::*ptrClass)(vpFeaturePoint&, const vpPoint&) = &vp_createPointClass::vp_createPoint;
169  pose.addSpecificFeature(&cpClass, ptrClass, fp, pts[1]);
170  pose.addSpecificFeature(&vp_createLine, fl, line);
171  pose.addSpecificFeature(ptr, fs, pts[3], pts[4]);
172 #endif
173 
174  pose.setVerbose(true);
175  pose.setLambda(0.6);
176  pose.setVVSIterMax(200);
177  pose.setCovarianceComputation(true);
178 
179  vpHomogeneousMatrix cMo2(0.4, 0.3, 1.5, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
180  std::cout << "cMo used as initialisation of the pose computation : " << std::endl;
181  std::cout << vpPoseVector(cMo2).t() << std::endl;
182 
183  pose.computePose(cMo2);
184 // pose.computePose(cMo2, vpPoseFeatures::ROBUST_VIRTUAL_VS);
185 
186  std::cout << "Resulting cMo : " << std::endl;
187  std::cout << vpPoseVector(cMo2).t() << std::endl;
188 
189  std::cout << "Resulting covariance (Diag): " << std::endl;
190  vpMatrix covariance = pose.getCovarianceMatrix();
191  std::cout << covariance[0][0] << " "
192  << covariance[1][1] << " "
193  << covariance[2][2] << " "
194  << covariance[3][3] << " "
195  << covariance[4][4] << " "
196  << covariance[5][5] << " " << std::endl;
197 
198  int result = 0; // Ok
199  for(unsigned int i = 0 ; i < 6 ; i++){
200  if(fabs(vpPoseVector(cMo2)[i] - vpPoseVector(cMo)[i]) > 1e-4){
201  std::cout << "Bad pose estimation" << std::endl;
202  result = -1;
203  break;
204  }
205  }
206 
207  if (result == 0)
208  std::cout << "Pose well estimed" << std::endl;
209 
210  return result;
211 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void setVerbose(const bool &mode)
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
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 &)
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 setVVSIterMax(const int &val)
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)
int vp_createPoint(vpFeaturePoint &fp, const vpPoint &v)
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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