Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testPoseFeatures.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 <vector>
36 
37 #include <visp3/core/vpCameraParameters.h>
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpHomogeneousMatrix.h>
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpPoint.h>
42 #include <visp3/vision/vpPose.h>
43 #include <visp3/vision/vpPoseFeatures.h>
44 
52 #ifdef ENABLE_VISP_NAMESPACE
53 using namespace VISP_NAMESPACE_NAME;
54 #endif
55 
56 #if defined(VISP_HAVE_MODULE_VISUAL_FEATURES) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 class vp_createPointClass
59 {
60 public:
61  int value;
62 
63  vp_createPointClass() : value(0) { }
64 
65  int vp_createPoint(vpFeaturePoint &fp, const vpPoint &v)
66  {
67  value += 1;
69  return value;
70  }
71 };
72 
73 void vp_createPoint(vpFeaturePoint &fp, const vpPoint &v) { vpFeatureBuilder::create(fp, v); }
74 
75 void vp_createLine(vpFeatureLine &fp, const vpLine &v) { vpFeatureBuilder::create(fp, v); }
76 #endif
77 
78 int test_pose(bool use_robust)
79 {
80  if (use_robust)
81  std::cout << "** Test robust pose estimation from features\n" << std::endl;
82  else
83  std::cout << "** Test pose estimation from features\n" << std::endl;
84 
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, 0.0, 0.0, 1.0, 0.0);
115 
116  // Vanishing Point Feature
117  vpLine l1;
118  l1.setWorldCoordinates(0.0, 1.0, 0.2, 0.0, 1.0, 0.0, 0.0, -0.25);
119 
120  vpLine l2;
121  l2.setWorldCoordinates(0.0, 1.0, 0.2, 0.0, -1.0, 0.0, 0.0, -0.25);
122 
123  // Ellipse Feature
124  vpCircle circle;
125  circle.setWorldCoordinates(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.25);
126 
127  pts[0].project(cMo_ref);
128  pts[1].project(cMo_ref);
129  pts[2].project(cMo_ref);
130 
131  pts[3].project(cMo_ref);
132  pts[4].project(cMo_ref);
133 
134  pts[5].project(cMo_ref);
135 
136  line.project(cMo_ref);
137 
138  l1.project(cMo_ref);
139  l2.project(cMo_ref);
140 
141  circle.project(cMo_ref);
142 
143  pose.addFeaturePoint(pts[0]);
144  // pose.addFeaturePoint(pts[1]);
145  pose.addFeaturePoint(pts[2]);
146 
147  pose.addFeaturePoint3D(pts[5]);
148 
149  pose.addFeatureVanishingPoint(l1, l2);
150 
151  // pose.addFeatureSegment(pts[3],pts[4]);
152  //
153  // pose.addFeatureLine(line);
154 
155  pose.addFeatureEllipse(circle);
156 
157  vpFeaturePoint fp;
158  vpFeatureLine fl;
159  vpFeatureSegment fs;
161  vp_createPointClass cpClass;
162  int (vp_createPointClass::*ptrClass)(vpFeaturePoint &, const vpPoint &) = &vp_createPointClass::vp_createPoint;
163  pose.addSpecificFeature(&cpClass, ptrClass, fp, pts[1]);
164  pose.addSpecificFeature(&vp_createLine, fl, line);
165  pose.addSpecificFeature(ptr, fs, pts[3], pts[4]);
166 
167  pose.setVerbose(true);
168  pose.setLambda(0.6);
169  pose.setVVSIterMax(200);
170  pose.setCovarianceComputation(true);
171 
172  vpHomogeneousMatrix cMo_est(0.4, 0.3, 1.5, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
173  vpPoseVector pose_est = vpPoseVector(cMo_est);
174  std::cout << "\nPose used as initialisation of the pose computation : " << std::endl;
175  std::cout << pose_est.t() << std::endl;
176 
177  if (!use_robust)
178  pose.computePose(cMo_est);
179  else
180  pose.computePose(cMo_est, vpPoseFeatures::ROBUST_VIRTUAL_VS);
181 
182  if (!use_robust)
183  std::cout << "\nEstimated pose from visual features : " << std::endl;
184  else
185  std::cout << "\nRobust estimated pose from visual features : " << std::endl;
186 
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] << " " << covariance[2][2] << " " << covariance[3][3] << " "
193  << covariance[4][4] << " " << covariance[5][5] << " " << std::endl;
194 
195  int test_fail = 0;
196  for (unsigned int i = 0; i < 6; i++) {
197  if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
198  test_fail = 1;
199  }
200 
201  std::cout << "\nPose is " << (test_fail ? "badly" : "well") << " estimated\n" << std::endl;
202 
203  return test_fail;
204 }
205 #endif
206 
207 int main()
208 {
209 #if defined(VISP_HAVE_MODULE_VISUAL_FEATURES) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) \
210  && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
211  try {
212  if (test_pose(false))
213  return EXIT_FAILURE;
214 
215  if (test_pose(true))
216  return EXIT_FAILURE;
217 
218  return EXIT_SUCCESS;
219  }
220  catch (const vpException &e) {
221  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
222  return EXIT_FAILURE;
223  }
224 #else
225  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV and enable c++11 min standard " << std::endl;
226  return EXIT_SUCCESS;
227 #endif
228 }
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:87
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpCircle.cpp:57
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const std::string & getStringMessage() const
Definition: vpException.cpp:67
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
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:103
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:83
static double rad(double deg)
Definition: vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
vpRowVector t() const
vpPoseVector & buildFrom(const double &tx, const double &ty, const double &tz, const double &tux, const double &tuy, const double &tuz)