Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
51
52#ifdef ENABLE_VISP_NAMESPACE
53using 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
58class vp_createPointClass
59{
60public:
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
73void vp_createPoint(vpFeaturePoint &fp, const vpPoint &v);
74void vp_createLine(vpFeatureLine &fp, const vpLine &v);
75
76void vp_createPoint(vpFeaturePoint &fp, const vpPoint &v) { vpFeatureBuilder::create(fp, v); }
77
78void vp_createLine(vpFeatureLine &fp, const vpLine &v) { vpFeatureBuilder::create(fp, v); }
79#endif
80
81int test_pose(bool use_robust);
82
83int test_pose(bool use_robust)
84{
85 if (use_robust)
86 std::cout << "** Test robust pose estimation from features\n" << std::endl;
87 else
88 std::cout << "** Test pose estimation from features\n" << std::endl;
89
90 vpImage<unsigned char> I(600, 600);
91
92 vpHomogeneousMatrix cMo_ref(0., 0., 1., vpMath::rad(0), vpMath::rad(0), vpMath::rad(60));
93 vpPoseVector pose_ref = vpPoseVector(cMo_ref);
94
95 std::cout << "Reference pose used to create the visual features : " << std::endl;
96 std::cout << pose_ref.t() << std::endl;
97
98 vpPoseFeatures pose;
99
100 std::vector<vpPoint> pts;
101
102 double val = 0.25;
103 double val2 = 0.0;
104
105 // 2D Point Feature
106 pts.push_back(vpPoint(0.0, -val, val2));
107 pts.push_back(vpPoint(0.0, val, val2));
108 pts.push_back(vpPoint(-val, val, val2));
109
110 // Segment Feature
111 pts.push_back(vpPoint(-val, -val / 2.0, val2));
112 pts.push_back(vpPoint(val, val / 2.0, val2));
113
114 // 3D point Feature
115 pts.push_back(vpPoint(0.0, 0.0, -1.5));
116
117 // Line Feature
118 vpLine line;
119 line.setWorldCoordinates(0.0, 1.0, 0.0, .0, 0.0, 0.0, 1.0, 0.0);
120
121 // Vanishing Point Feature
122 vpLine l1;
123 l1.setWorldCoordinates(0.0, 1.0, 0.2, 0.0, 1.0, 0.0, 0.0, -0.25);
124
125 vpLine l2;
126 l2.setWorldCoordinates(0.0, 1.0, 0.2, 0.0, -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_ref);
133 pts[1].project(cMo_ref);
134 pts[2].project(cMo_ref);
135
136 pts[3].project(cMo_ref);
137 pts[4].project(cMo_ref);
138
139 pts[5].project(cMo_ref);
140
141 line.project(cMo_ref);
142
143 l1.project(cMo_ref);
144 l2.project(cMo_ref);
145
146 circle.project(cMo_ref);
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
163 vpFeatureLine fl;
166 vp_createPointClass cpClass;
167 int (vp_createPointClass::*ptrClass)(vpFeaturePoint &, const vpPoint &) = &vp_createPointClass::vp_createPoint;
168 pose.addSpecificFeature(&cpClass, ptrClass, fp, pts[1]);
169 pose.addSpecificFeature(&vp_createLine, fl, line);
170 pose.addSpecificFeature(ptr, fs, pts[3], pts[4]);
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 if (!use_robust)
183 pose.computePose(cMo_est);
184 else
186
187 if (!use_robust)
188 std::cout << "\nEstimated pose from visual features : " << std::endl;
189 else
190 std::cout << "\nRobust estimated pose from visual features : " << std::endl;
191
192 pose_est.buildFrom(cMo_est);
193 std::cout << pose_est.t() << std::endl;
194
195 std::cout << "\nResulting covariance (Diag): " << std::endl;
196 vpMatrix covariance = pose.getCovarianceMatrix();
197 std::cout << covariance[0][0] << " " << covariance[1][1] << " " << covariance[2][2] << " " << covariance[3][3] << " "
198 << covariance[4][4] << " " << covariance[5][5] << " " << std::endl;
199
200 int test_fail = 0;
201 for (unsigned int i = 0; i < 6; i++) {
202 if (std::fabs(pose_ref[i] - pose_est[i]) > 0.001)
203 test_fail = 1;
204 }
205
206 std::cout << "\nPose is " << (test_fail ? "badly" : "well") << " estimated\n" << std::endl;
207
208 return test_fail;
209}
210#endif
211
212int main()
213{
214#if defined(VISP_HAVE_MODULE_VISUAL_FEATURES) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) \
215 && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
216 try {
217 if (test_pose(false))
218 return EXIT_FAILURE;
219
220 if (test_pose(true))
221 return EXIT_FAILURE;
222
223 return EXIT_SUCCESS;
224 }
225 catch (const vpException &e) {
226 std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
227 return EXIT_FAILURE;
228 }
229#else
230 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV and enable c++11 min standard " << std::endl;
231 return EXIT_SUCCESS;
232#endif
233}
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:58
error that can be emitted by ViSP classes.
Definition vpException.h:60
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.
Definition of the vpImage class member functions.
Definition vpImage.h:131
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:87
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
Tools for pose computation from any feature.
void setVerbose(const bool &mode)
void addFeatureEllipse(const vpCircle &c)
void addFeaturePoint3D(const vpPoint &p)
void setVVSIterMax(const unsigned int &val)
void addFeaturePoint(const vpPoint &p)
void setCovarianceComputation(const bool &flag)
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.
vpRowVector t() const
vpPoseVector & buildFrom(const double &tx, const double &ty, const double &tz, const double &tux, const double &tuy, const double &tuz)