Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
vpMbtFaceDepthNormal.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Manage depth normal features for a particular face.
33  *
34 *****************************************************************************/
35 
36 #ifndef _vpMbtFaceDepthNormal_h_
37 #define _vpMbtFaceDepthNormal_h_
38 
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
43 #include <pcl/point_cloud.h>
44 #include <pcl/point_types.h>
45 #endif
46 
47 #include <visp3/core/vpPlane.h>
48 #include <visp3/mbt/vpMbTracker.h>
49 #include <visp3/mbt/vpMbtDistanceLine.h>
50 
51 #define DEBUG_DISPLAY_DEPTH_NORMAL 0
52 
53 BEGIN_VISP_NAMESPACE
54 class VISP_EXPORT vpMbtFaceDepthNormal
55 {
56 public:
61  {
63  MEAN_CENTROID
64  };
65 
70  {
71  ROBUST_FEATURE_ESTIMATION = 0,
72  ROBUST_SVD_PLANE_ESTIMATION = 1,
73 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
74  PCL_PLANE_ESTIMATION = 2
75 #endif
76  };
77 
81  unsigned int m_clippingFlag;
83  double m_distFarClip;
94 
96  virtual ~vpMbtFaceDepthNormal();
97 
98  void addLine(vpPoint &p1, vpPoint &p2, vpMbHiddenFaces<vpMbtPolygon> *const faces, vpUniRand &rand_gen,
99  int polygon = -1, std::string name = "");
100 
101 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
102  bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
103  const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud,
104  vpColVector &desired_features, unsigned int stepX, unsigned int stepY
105 #if DEBUG_DISPLAY_DEPTH_NORMAL
106  ,
107  vpImage<unsigned char> &debugImage, std::vector<std::vector<vpImagePoint> > &roiPts_vec
108 #endif
109  ,
110  const vpImage<bool> *mask = nullptr);
111 #endif
112  bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
113  const std::vector<vpColVector> &point_cloud, vpColVector &desired_features,
114  unsigned int stepX, unsigned int stepY
115 #if DEBUG_DISPLAY_DEPTH_NORMAL
116  ,
117  vpImage<unsigned char> &debugImage, std::vector<std::vector<vpImagePoint> > &roiPts_vec
118 #endif
119  ,
120  const vpImage<bool> *mask = nullptr);
121  bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
122  const vpMatrix &point_cloud, vpColVector &desired_features,
123  unsigned int stepX, unsigned int stepY
124 #if DEBUG_DISPLAY_DEPTH_NORMAL
125  ,
126  vpImage<unsigned char> &debugImage, std::vector<std::vector<vpImagePoint> > &roiPts_vec
127 #endif
128  ,
129  const vpImage<bool> *mask = nullptr);
130 
131  void computeInteractionMatrix(const vpHomogeneousMatrix &cMo, vpMatrix &L, vpColVector &features);
132 
133  void computeVisibility();
134  void computeVisibilityDisplay();
135 
136  bool planeIsInvalid(const vpHomogeneousMatrix &cMo, double maxAngle);
137 
138  void computeNormalVisibility(double nx, double ny, double nz, const vpColVector &centroid_point,
139  vpColVector &face_normal);
140 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
141  void computeNormalVisibility(float nx, float ny, float nz, const pcl::PointXYZ &centroid_point,
142  pcl::PointXYZ &face_normal);
143 #endif
144  void computeNormalVisibility(double nx, double ny, double nz, const vpHomogeneousMatrix &cMo,
145  const vpCameraParameters &camera, vpColVector &correct_normal, vpPoint &centroid);
146 
147  void display(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
148  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
149  void display(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
150  const vpColor &col, unsigned int thickness = 1, bool displayFullModel = false);
151 
152  void displayFeature(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
153  double scale = 0.05, unsigned int thickness = 1);
154  void displayFeature(const vpImage<vpRGBa> &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
155  double scale = 0.05, unsigned int thickness = 1);
156 
157  std::vector<std::vector<double> > getFeaturesForDisplay(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
158  double scale = 0.05);
159 
160  std::vector<std::vector<double> > getModelForDisplay(unsigned int width, unsigned int height,
161  const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
162  bool displayFullModel = false);
163 
164  inline bool isTracked() const { return m_isTrackedDepthNormalFace; }
165 
166  inline bool isVisible() const { return m_polygon->isvisible; }
167 
168  void setCameraParameters(const vpCameraParameters &camera);
169 
170  inline void setFaceCentroidMethod(const vpFaceCentroidType &method) { m_faceCentroidMethod = method; }
171 
172  inline void setFeatureEstimationMethod(const vpFeatureEstimationType &method) { m_featureEstimationMethod = method; }
173 
174  inline void setPclPlaneEstimationMethod(int method) { m_pclPlaneEstimationMethod = method; }
175 
176  inline void setPclPlaneEstimationRansacMaxIter(int maxIter) { m_pclPlaneEstimationRansacMaxIter = maxIter; }
177 
178  inline void setPclPlaneEstimationRansacThreshold(double threshold)
179  {
180  m_pclPlaneEstimationRansacThreshold = threshold;
181  }
182 
183  void setScanLineVisibilityTest(bool v);
184 
185  inline void setTracked(bool tracked) { m_isTrackedDepthNormalFace = tracked; }
186 
187 private:
188  class PolygonLine
189  {
190  public:
192  vpPoint *m_p1;
194  vpPoint *m_p2;
196  vpMbtPolygon m_poly;
198  vpImagePoint m_imPt1;
200  vpImagePoint m_imPt2;
201 
202  PolygonLine() : m_p1(nullptr), m_p2(nullptr), m_poly(), m_imPt1(), m_imPt2() { }
203 
204  PolygonLine(const PolygonLine &polyLine)
205  : m_p1(nullptr), m_p2(nullptr), m_poly(polyLine.m_poly), m_imPt1(polyLine.m_imPt1), m_imPt2(polyLine.m_imPt2)
206  {
207  m_p1 = &m_poly.p[0];
208  m_p2 = &m_poly.p[1];
209  }
210 
211  PolygonLine &operator=(PolygonLine other)
212  {
213  swap(*this, other);
214 
215  return *this;
216  }
217 
218  void swap(PolygonLine &first, PolygonLine &second)
219  {
220  using std::swap;
221  swap(first.m_p1, second.m_p1);
222  swap(first.m_p2, second.m_p2);
223  swap(first.m_poly, second.m_poly);
224  swap(first.m_imPt1, second.m_imPt1);
225  swap(first.m_imPt2, second.m_imPt2);
226  }
227  };
228 
229  template <class T> class Mat33
230  {
231  public:
232  std::vector<T> data;
233 
234  Mat33() : data(9) { }
235 
236  inline T operator[](const size_t i) const { return data[i]; }
237 
238  inline T &operator[](const size_t i) { return data[i]; }
239 
240  Mat33 inverse() const
241  {
242  // determinant
243  T det = data[0] * (data[4] * data[8] - data[7] * data[5]) - data[1] * (data[3] * data[8] - data[5] * data[6]) +
244  data[2] * (data[3] * data[7] - data[4] * data[6]);
245  T invdet = 1 / det;
246 
247  Mat33<T> minv;
248  minv[0] = (data[4] * data[8] - data[7] * data[5]) * invdet;
249  minv[1] = (data[2] * data[7] - data[1] * data[8]) * invdet;
250  minv[2] = (data[1] * data[5] - data[2] * data[4]) * invdet;
251  minv[3] = (data[5] * data[6] - data[3] * data[8]) * invdet;
252  minv[4] = (data[0] * data[8] - data[2] * data[6]) * invdet;
253  minv[5] = (data[3] * data[2] - data[0] * data[5]) * invdet;
254  minv[6] = (data[3] * data[7] - data[6] * data[4]) * invdet;
255  minv[7] = (data[6] * data[1] - data[0] * data[7]) * invdet;
256  minv[8] = (data[0] * data[4] - data[3] * data[1]) * invdet;
257 
258  return minv;
259  }
260  };
261 
262 protected:
278  std::vector<vpMbtDistanceLine *> m_listOfFaceLines;
289  std::vector<PolygonLine> m_polygonLines;
290 
291 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
292  bool computeDesiredFeaturesPCL(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud_face,
293  vpColVector &desired_features, vpColVector &desired_normal,
294  vpColVector &centroid_point);
295 #endif
296  void computeDesiredFeaturesRobustFeatures(const std::vector<double> &point_cloud_face_custom,
297  const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
298  vpColVector &desired_features, vpColVector &desired_normal,
299  vpColVector &centroid_point);
300  void computeDesiredFeaturesSVD(const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
301  vpColVector &desired_features, vpColVector &desired_normal,
302  vpColVector &centroid_point);
303  void computeDesiredNormalAndCentroid(const vpHomogeneousMatrix &cMo, const vpColVector &desired_normal,
304  const vpColVector &centroid_point);
305 
306  bool computePolygonCentroid(const std::vector<vpPoint> &points, vpPoint &centroid);
307 
308  void computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
309  std::vector<vpImagePoint> &roiPts
310 #if DEBUG_DISPLAY_DEPTH_NORMAL
311  ,
312  std::vector<std::vector<vpImagePoint> > &roiPts_vec
313 #endif
314  );
315 
316  void estimateFeatures(const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
317  vpColVector &x_estimated, std::vector<double> &weights);
318 
319  void estimatePlaneEquationSVD(const std::vector<double> &point_cloud_face, const vpHomogeneousMatrix &cMo,
320  vpColVector &plane_equation_estimated, vpColVector &centroid);
321 
322  bool samePoint(const vpPoint &P1, const vpPoint &P2) const;
323 };
324 END_VISP_NAMESPACE
325 
326 #ifdef VISP_HAVE_NLOHMANN_JSON
327 #include VISP_NLOHMANN_JSON(json.hpp)
328 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_SEGMENTATION) && defined(VISP_HAVE_PCL_FILTERS)
329 NLOHMANN_JSON_SERIALIZE_ENUM(VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::vpFeatureEstimationType, {
330  {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_FEATURE_ESTIMATION, "robust"},
331  {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_SVD_PLANE_ESTIMATION, "robustSVD"},
332  {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::PCL_PLANE_ESTIMATION, "pcl"}
333 });
334 #else
335 NLOHMANN_JSON_SERIALIZE_ENUM(VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::vpFeatureEstimationType, {
336  {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_FEATURE_ESTIMATION, "robust"},
337  {VISP_NAMESPACE_ADDRESSING vpMbtFaceDepthNormal::ROBUST_SVD_PLANE_ESTIMATION, "robustSVD"}
338 });
339 #endif
340 #endif
341 
342 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
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: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
double m_pclPlaneEstimationRansacThreshold
PCL plane estimation RANSAC threshold.
int m_pclPlaneEstimationMethod
PCL plane estimation method.
double m_distNearClip
Distance for near clipping.
double m_distFarClip
Distance for near clipping.
vpMbHiddenFaces< vpMbtPolygon > * m_hiddenFace
Pointer to the list of faces.
std::vector< vpMbtDistanceLine * > m_listOfFaceLines
void setPclPlaneEstimationMethod(int method)
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)
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.
vpFaceCentroidType m_faceCentroidMethod
Method to compute the face centroid for the current features.
int m_pclPlaneEstimationRansacMaxIter
PCL pane estimation max number of iterations.
unsigned int m_clippingFlag
Flags specifying which clipping to used.
void setTracked(bool tracked)
vpPoint m_faceDesiredNormal
Face (normalized) normal (computed from the sensor)
void setFeatureEstimationMethod(const vpFeatureEstimationType &method)
std::vector< PolygonLine > m_polygonLines
void setFaceCentroidMethod(const vpFaceCentroidType &method)
void setPclPlaneEstimationRansacMaxIter(int maxIter)
bool m_useScanLine
Scan line visibility.
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:60
bool isvisible
flag to specify whether the face is visible or not
Definition: vpMbtPolygon.h:66
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:57
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