Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMbtFaceDepthNormal.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Manage depth normal features for a particular face.
32 */
33
34#ifndef VP_MBT_FACE_DEPTH_NORMAL_H
35#define VP_MBT_FACE_DEPTH_NORMAL_H
36
37#include <iostream>
38
39#include <visp3/core/vpConfig.h>
40#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
41#include <pcl/point_cloud.h>
42#include <pcl/point_types.h>
43#endif
44
45#include <visp3/core/vpPlane.h>
46#include <visp3/mbt/vpMbTracker.h>
47#include <visp3/mbt/vpMbtDistanceLine.h>
48
49#define DEBUG_DISPLAY_DEPTH_NORMAL 0
50
68class VISP_EXPORT vpMbtFaceDepthNormal
69{
70public:
79
84 {
87#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
89#endif
90 };
91
95 unsigned int m_clippingFlag;
108
111 virtual ~vpMbtFaceDepthNormal();
113
114 void addLine(vpPoint &p1, vpPoint &p2, vpMbHiddenFaces<vpMbtPolygon> *const faces, vpUniRand &rand_gen,
115 int polygon = -1, std::string name = "");
116
117#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
118 bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
119 const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud,
120 vpColVector &desired_features, unsigned int stepX, unsigned int stepY
121#if DEBUG_DISPLAY_DEPTH_NORMAL
122 ,
123 vpImage<unsigned char> &debugImage, std::vector<std::vector<vpImagePoint> > &roiPts_vec
124#endif
125 ,
126 const vpImage<bool> *mask = nullptr);
127#endif
128 bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
129 const std::vector<vpColVector> &point_cloud, vpColVector &desired_features,
130 unsigned int stepX, unsigned int stepY
131#if DEBUG_DISPLAY_DEPTH_NORMAL
132 ,
133 vpImage<unsigned char> &debugImage, std::vector<std::vector<vpImagePoint> > &roiPts_vec
134#endif
135 ,
136 const vpImage<bool> *mask = nullptr);
137 bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
138 const vpMatrix &point_cloud, vpColVector &desired_features,
139 unsigned int stepX, unsigned int stepY
140#if DEBUG_DISPLAY_DEPTH_NORMAL
141 ,
142 vpImage<unsigned char> &debugImage, std::vector<std::vector<vpImagePoint> > &roiPts_vec
143#endif
144 ,
145 const vpImage<bool> *mask = nullptr);
146
148
149 void computeVisibility();
151
152 bool planeIsInvalid(const vpHomogeneousMatrix &cMo, double maxAngle);
153
154 void computeNormalVisibility(double nx, double ny, double nz, const vpColVector &centroid_point,
155 vpColVector &face_normal);
156#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
157 void computeNormalVisibility(float nx, float ny, float nz, const pcl::PointXYZ &centroid_point,
158 pcl::PointXYZ &face_normal);
159#endif
160 void computeNormalVisibility(double nx, double ny, double nz, const vpHomogeneousMatrix &cMo,
161 const vpCameraParameters &camera, vpColVector &correct_normal, vpPoint &centroid);
162
163 void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
164 const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
165 void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
166 const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
167
169 double scale = 0.05, unsigned int thickness = 1);
170 void displayFeature(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
171 double scale = 0.05, unsigned int thickness = 1);
172
173 std::vector<std::vector<double> > getFeaturesForDisplay(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
174 double scale = 0.05);
175
176 std::vector<std::vector<double> > getModelForDisplay(unsigned int width, unsigned int height,
177 const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
178 bool displayFullModel = false);
179
180 inline bool isTracked() const { return m_isTrackedDepthNormalFace; }
181
182 inline bool isVisible() const { return m_polygon->isvisible; }
183
184 void setCameraParameters(const vpCameraParameters &camera);
185
186 inline void setFaceCentroidMethod(const vpFaceCentroidType &method) { m_faceCentroidMethod = method; }
187
189
190 inline void setPclPlaneEstimationMethod(int method) { m_pclPlaneEstimationMethod = method; }
191
193
194 inline void setPclPlaneEstimationRansacThreshold(double threshold)
195 {
197 }
198
199 void setScanLineVisibilityTest(bool v);
200
201 inline void setTracked(bool tracked) { m_isTrackedDepthNormalFace = tracked; }
202
203private:
204 class PolygonLine
205 {
206 public:
208 vpPoint *m_p1;
210 vpPoint *m_p2;
212 vpMbtPolygon m_poly;
214 vpImagePoint m_imPt1;
216 vpImagePoint m_imPt2;
217
218 PolygonLine() : m_p1(nullptr), m_p2(nullptr), m_poly(), m_imPt1(), m_imPt2() { }
219
220 PolygonLine(const PolygonLine &polyLine)
221 : m_p1(nullptr), m_p2(nullptr), m_poly(polyLine.m_poly), m_imPt1(polyLine.m_imPt1), m_imPt2(polyLine.m_imPt2)
222 {
223 m_p1 = &m_poly.p[0];
224 m_p2 = &m_poly.p[1];
225 }
226
227 PolygonLine &operator=(PolygonLine other)
228 {
229 swap(*this, other);
230
231 return *this;
232 }
233
234 void swap(PolygonLine &first, PolygonLine &second)
235 {
236 using std::swap;
237 swap(first.m_p1, second.m_p1);
238 swap(first.m_p2, second.m_p2);
239 swap(first.m_poly, second.m_poly);
240 swap(first.m_imPt1, second.m_imPt1);
241 swap(first.m_imPt2, second.m_imPt2);
242 }
243 };
244
245 template <class T> class Mat33
246 {
247 public:
248 std::vector<T> data;
249
250 Mat33() : data(9) { }
251
252 inline T operator[](const size_t i) const { return data[i]; }
253
254 inline T &operator[](const size_t i) { return data[i]; }
255
256 Mat33 inverse() const
257 {
258 // determinant
259 T det = data[0] * (data[4] * data[8] - data[7] * data[5]) - data[1] * (data[3] * data[8] - data[5] * data[6]) +
260 data[2] * (data[3] * data[7] - data[4] * data[6]);
261 T invdet = 1 / det;
262
263 Mat33<T> minv;
264 minv[0] = (data[4] * data[8] - data[7] * data[5]) * invdet;
265 minv[1] = (data[2] * data[7] - data[1] * data[8]) * invdet;
266 minv[2] = (data[1] * data[5] - data[2] * data[4]) * invdet;
267 minv[3] = (data[5] * data[6] - data[3] * data[8]) * invdet;
268 minv[4] = (data[0] * data[8] - data[2] * data[6]) * invdet;
269 minv[5] = (data[3] * data[2] - data[0] * data[5]) * invdet;
270 minv[6] = (data[3] * data[7] - data[6] * data[4]) * invdet;
271 minv[7] = (data[6] * data[1] - data[0] * data[7]) * invdet;
272 minv[8] = (data[0] * data[4] - data[3] * data[1]) * invdet;
273
274 return minv;
275 }
276 };
277
278protected:
294 std::vector<vpMbtDistanceLine *> m_listOfFaceLines;
305 std::vector<PolygonLine> m_polygonLines;
306
307#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
308 bool computeDesiredFeaturesPCL(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud_face,
309 vpColVector &desired_features, vpColVector &desired_normal,
310 vpColVector &centroid_point);
311#endif
312 void computeDesiredFeaturesRobustFeatures(const std::vector<double> &point_cloud_face_custom,
313 const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
314 vpColVector &desired_features, vpColVector &desired_normal,
315 vpColVector &centroid_point);
316 void computeDesiredFeaturesSVD(const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
317 vpColVector &desired_features, vpColVector &desired_normal,
318 vpColVector &centroid_point);
319 void computeDesiredNormalAndCentroid(const vpHomogeneousMatrix &cMo, const vpColVector &desired_normal,
320 const vpColVector &centroid_point);
321
322 bool computePolygonCentroid(const std::vector<vpPoint> &points, vpPoint &centroid);
323
324 void computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
325 std::vector<vpImagePoint> &roiPts
326#if DEBUG_DISPLAY_DEPTH_NORMAL
327 ,
328 std::vector<std::vector<vpImagePoint> > &roiPts_vec
329#endif
330 );
331
332 void estimateFeatures(const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
333 vpColVector &x_estimated, std::vector<double> &weights);
334
335 void estimatePlaneEquationSVD(const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
336 vpColVector &plane_equation_estimated, vpColVector &centroid);
337
338 bool samePoint(const vpPoint &P1, const vpPoint &P2) const;
339};
340END_VISP_NAMESPACE
341
342#ifdef VISP_HAVE_NLOHMANN_JSON
343#include VISP_NLOHMANN_JSON(json.hpp)
344
345#if defined(__clang__)
346// Mute warning : declaration requires an exit-time destructor [-Wexit-time-destructors]
347// message : expanded from macro 'NLOHMANN_JSON_SERIALIZE_ENUM'
348# pragma clang diagnostic push
349# pragma clang diagnostic ignored "-Wexit-time-destructors"
350#endif
351
352#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
353NLOHMANN_JSON_SERIALIZE_ENUM(VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::vpFeatureEstimationType, {
354 {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_FEATURE_ESTIMATION, "robust"},
355 {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_SVD_PLANE_ESTIMATION, "robustSVD"},
356 {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::PCL_PLANE_ESTIMATION, "pcl"}
357});
358#else
359NLOHMANN_JSON_SERIALIZE_ENUM(VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::vpFeatureEstimationType, {
360 {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_FEATURE_ESTIMATION, "robust"},
361 {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_SVD_PLANE_ESTIMATION, "robustSVD"}
362});
363#endif
364
365#if defined(__clang__)
366# pragma clang diagnostic pop
367#endif
368
369#endif
370
371#endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Implementation of the polygons management for the model-based trackers.
double m_pclPlaneEstimationRansacThreshold
PCL plane estimation RANSAC threshold.
int m_pclPlaneEstimationMethod
PCL plane estimation method.
double m_distNearClip
Distance for near clipping.
void computeInteractionMatrix(const vpHomogeneousMatrix &cMo, vpMatrix &L, vpColVector &features)
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
void addLine(vpPoint &p1, vpPoint &p2, vpMbHiddenFaces< vpMbtPolygon > *const faces, vpUniRand &rand_gen, int polygon=-1, std::string name="")
void computeDesiredFeaturesSVD(const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &desired_features, vpColVector &desired_normal, vpColVector &centroid_point)
double m_distFarClip
Distance for near clipping.
bool samePoint(const vpPoint &P1, const vpPoint &P2) const
void displayFeature(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double scale=0.05, unsigned int thickness=1)
vpMbHiddenFaces< vpMbtPolygon > * m_hiddenFace
Pointer to the list of faces.
std::vector< std::vector< double > > getFeaturesForDisplay(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double scale=0.05)
bool computePolygonCentroid(const std::vector< vpPoint > &points, vpPoint &centroid)
void computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height, std::vector< vpImagePoint > &roiPts)
void estimatePlaneEquationSVD(const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &plane_equation_estimated, vpColVector &centroid)
std::vector< vpMbtDistanceLine * > m_listOfFaceLines
void computeDesiredNormalAndCentroid(const vpHomogeneousMatrix &cMo, const vpColVector &desired_normal, const vpColVector &centroid_point)
void setPclPlaneEstimationMethod(int method)
vpMbtFaceDepthNormal & operator=(const vpMbtFaceDepthNormal &mbt_face)
vpFeatureEstimationType m_featureEstimationMethod
Method to estimate the desired features.
void setPclPlaneEstimationRansacThreshold(double threshold)
vpMbtPolygon * m_polygon
Polygon defining the face.
vpPoint m_faceDesiredCentroid
Desired centroid (computed from the sensor).
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
vpCameraParameters m_cam
Camera intrinsic parameters.
@ ROBUST_FEATURE_ESTIMATION
Robust scheme to estimate the normal of the plane.
@ ROBUST_SVD_PLANE_ESTIMATION
Use SVD and robust scheme to estimate the normal of the plane.
@ PCL_PLANE_ESTIMATION
Use PCL to estimate the normal of the plane.
vpPlane m_planeObject
Plane equation described in the object frame.
bool m_faceActivated
True if the face should be considered by the tracker.
@ GEOMETRIC_CENTROID
Compute the geometric centroid.
@ MEAN_CENTROID
Compute the mean centroid.
vpFaceCentroidType m_faceCentroidMethod
Method to compute the face centroid for the current features.
bool planeIsInvalid(const vpHomogeneousMatrix &cMo, double maxAngle)
int m_pclPlaneEstimationRansacMaxIter
PCL pane estimation max number of iterations.
unsigned int m_clippingFlag
Flags specifying which clipping to used.
void computeDesiredFeaturesRobustFeatures(const std::vector< double > &point_cloud_face_custom, const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &desired_features, vpColVector &desired_normal, vpColVector &centroid_point)
void setTracked(bool tracked)
vpPoint m_faceDesiredNormal
Face (normalized) normal (computed from the sensor).
void estimateFeatures(const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &x_estimated, std::vector< double > &weights)
void setFeatureEstimationMethod(const vpFeatureEstimationType &method)
std::vector< PolygonLine > m_polygonLines
void setFaceCentroidMethod(const vpFaceCentroidType &method)
bool computeDesiredFeaturesPCL(const pcl::PointCloud< pcl::PointXYZ >::ConstPtr &point_cloud_face, vpColVector &desired_features, vpColVector &desired_normal, vpColVector &centroid_point)
void setPclPlaneEstimationRansacMaxIter(int maxIter)
void computeNormalVisibility(double nx, double ny, double nz, const vpColVector &centroid_point, vpColVector &face_normal)
bool m_useScanLine
Scan line visibility.
bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height, const pcl::PointCloud< pcl::PointXYZ >::ConstPtr &point_cloud, vpColVector &desired_features, unsigned int stepX, unsigned int stepY, const vpImage< bool > *mask=nullptr)
Implementation of a polygon of the model used by the model-based tracker.
This class defines the container for a plane geometrical structure.
Definition vpPlane.h:56
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
vpPoint * p
corners in the object frame
Definition vpPolygon3D.h:79
Class for generating random numbers with uniform probability density.
Definition vpUniRand.h:127