Visual Servoing Platform  version 3.6.1 under development (2024-05-04)
testPoseFeatures.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
34 #include <iostream>
35 #include <limits>
36 #include <vector>
37 
38 #include <visp3/core/vpCameraParameters.h>
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpHomogeneousMatrix.h>
41 #include <visp3/core/vpImage.h>
42 #include <visp3/core/vpPoint.h>
43 #include <visp3/vision/vpPose.h>
44 #include <visp3/vision/vpPoseFeatures.h>
45 
53 #if defined(VISP_HAVE_MODULE_VISUAL_FEATURES) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
54 #ifndef DOXYGEN_SHOULD_SKIP_THIS
55 class vp_createPointClass
56 {
57 public:
58  int value;
59 
60  vp_createPointClass() : value(0) { }
61 
62  int vp_createPoint(vpFeaturePoint &fp, const vpPoint &v)
63  {
64  value += 1;
66  return value;
67  }
68 };
69 
70 void vp_createPoint(vpFeaturePoint &fp, const vpPoint &v) { vpFeatureBuilder::create(fp, v); }
71 
72 void vp_createLine(vpFeatureLine &fp, const vpLine &v) { vpFeatureBuilder::create(fp, v); }
73 #endif
74 
75 int test_pose(bool use_robust)
76 {
77  if (use_robust)
78  std::cout << "** Test robust pose estimation from features\n" << std::endl;
79  else
80  std::cout << "** Test pose estimation from features\n" << std::endl;
81 
82  vpImage<unsigned char> I(600, 600);
83 
84  vpHomogeneousMatrix cMo_ref(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(60));
85  vpPoseVector pose_ref = vpPoseVector(cMo_ref);
86 
87  std::cout << "Reference pose used to create the visual features : " << std::endl;
88  std::cout << pose_ref.t() << std::endl;
89 
90  vpPoseFeatures pose;
91 
92  std::vector<vpPoint> pts;
93 
94  double val = 0.25;
95  double val2 = 0.0;
96 
97  // 2D Point Feature
98  pts.push_back(vpPoint(0.0, -val, val2));
99  pts.push_back(vpPoint(0.0, val, val2));
100  pts.push_back(vpPoint(-val, val, val2));
101 
102  // Segment Feature
103  pts.push_back(vpPoint(-val, -val / 2.0, val2));
104  pts.push_back(vpPoint(val, val / 2.0, val2));
105 
106  // 3D point Feature
107  pts.push_back(vpPoint(0.0, 0.0, -1.5));
108 
109  // Line Feature
110  vpLine line;
111  line.setWorldCoordinates(0.0, 1.0, 0.0, .0, 0.0, 0.0, 1.0, 0.0);
112 
113  // Vanishing Point Feature
114  vpLine l1;
115  l1.setWorldCoordinates(0.0, 1.0, 0.2, 0.0, 1.0, 0.0, 0.0, -0.25);
116 
117  vpLine l2;
118  l2.setWorldCoordinates(0.0, 1.0, 0.2, 0.0, -1.0, 0.0, 0.0, -0.25);
119 
120  // Ellipse Feature
121  vpCircle circle;
122  circle.setWorldCoordinates(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.25);
123 
124  pts[0].project(cMo_ref);
125  pts[1].project(cMo_ref);
126  pts[2].project(cMo_ref);
127 
128  pts[3].project(cMo_ref);
129  pts[4].project(cMo_ref);
130 
131  pts[5].project(cMo_ref);
132 
133  line.project(cMo_ref);
134 
135  l1.project(cMo_ref);
136  l2.project(cMo_ref);
137 
138  circle.project(cMo_ref);
139 
140  pose.addFeaturePoint(pts[0]);
141  // pose.addFeaturePoint(pts[1]);
142  pose.addFeaturePoint(pts[2]);
143 
144  pose.addFeaturePoint3D(pts[5]);
145 
146  pose.addFeatureVanishingPoint(l1, l2);
147 
148  // pose.addFeatureSegment(pts[3],pts[4]);
149  //
150  // pose.addFeatureLine(line);
151 
152  pose.addFeatureEllipse(circle);
153 
154  vpFeaturePoint fp;
155  vpFeatureLine fl;
156  vpFeatureSegment fs;
158  vp_createPointClass cpClass;
159  int (vp_createPointClass::*ptrClass)(vpFeaturePoint &, const vpPoint &) = &vp_createPointClass::vp_createPoint;
160  pose.addSpecificFeature(&cpClass, ptrClass, fp, pts[1]);
161  pose.addSpecificFeature(&vp_createLine, fl, line);
162  pose.addSpecificFeature(ptr, fs, pts[3], pts[4]);
163 
164  pose.setVerbose(true);
165  pose.setLambda(0.6);
166  pose.setVVSIterMax(200);
167  pose.setCovarianceComputation(true);
168 
169  vpHomogeneousMatrix cMo_est(0.4, 0.3, 1.5, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
170  vpPoseVector pose_est = vpPoseVector(cMo_est);
171  std::cout << "\nPose used as initialisation of the pose computation : " << std::endl;
172  std::cout << pose_est.t() << std::endl;
173 
174  if (!use_robust)
175  pose.computePose(cMo_est);
176  else
178 
179  if (!use_robust)
180  std::cout << "\nEstimated pose from visual features : " << std::endl;
181  else
182  std::cout << "\nRobust estimated pose from visual features : " << std::endl;
183 
184  pose_est.buildFrom(cMo_est);
185  std::cout << pose_est.t() << std::endl;
186 
187  std::cout << "\nResulting covariance (Diag): " << std::endl;
188  vpMatrix covariance = pose.getCovarianceMatrix();
189  std::cout << covariance[0][0] << " " << covariance[1][1] << " " << covariance[2][2] << " " << covariance[3][3] << " "
190  << covariance[4][4] << " " << covariance[5][5] << " " << std::endl;
191 
192  int test_fail = 0;
193  for (unsigned int i = 0; i < 6; i++) {
194  if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
195  test_fail = 1;
196  }
197 
198  std::cout << "\nPose is " << (test_fail ? "badly" : "well") << " estimated\n" << std::endl;
199 
200  return test_fail;
201 }
202 #endif
203 
204 int main()
205 {
206 #if defined(VISP_HAVE_MODULE_VISUAL_FEATURES) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) \
207  && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
208  try {
209  if (test_pose(false))
210  return EXIT_FAILURE;
211 
212  if (test_pose(true))
213  return EXIT_FAILURE;
214 
215  return EXIT_SUCCESS;
216  }
217  catch (const vpException &e) {
218  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
219  return EXIT_FAILURE;
220  }
221 #else
222  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV and enable c++11 min standard " << std::endl;
223  return EXIT_SUCCESS;
224 #endif
225 }
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:86
void setWorldCoordinates(const vpColVector &oP) vp_override
Definition: vpCircle.cpp:57
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const std::string & getStringMessage() const
Definition: vpException.cpp:66
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines a 2D line visual feature which is composed by two parameters that are and ,...
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 3D line in the object frame and allows forward projection of the line in the cam...
Definition: vpLine.h:101
void setWorldCoordinates(const double &oA1, const double &oB1, const double &oC1, const double &oD1, const double &oA2, const double &oB2, const double &oC2, const double &oD2)
Definition: vpLine.cpp:82
static double rad(double deg)
Definition: vpMath.h:127
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
Tools for pose computation from any feature.
void setVerbose(const bool &mode)
void addFeaturePoint3D(const vpPoint &p)
void setVVSIterMax(const unsigned int &val)
void addFeaturePoint(const vpPoint &p)
void setCovarianceComputation(const bool &flag)
void addFeatureEllipse(const vpCircle &)
void addSpecificFeature(RetType(*fct_ptr)(ArgsFunc...), Args &&...args)
void addFeatureVanishingPoint(const vpPoint &p)
void setLambda(const double &val)
void computePose(vpHomogeneousMatrix &cMo, const vpPoseFeaturesMethodType &type=VIRTUAL_VS)
vpMatrix getCovarianceMatrix() const
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
vpRowVector t() const
vpPoseVector buildFrom(double tx, double ty, double tz, double tux, double tuy, double tuz)