Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
vpMbtFaceDepthDense.cpp
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 dense features for a particular face.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpCPUFeatures.h>
37 #include <visp3/mbt/vpMbtFaceDepthDense.h>
38 
39 #ifdef VISP_HAVE_PCL
40 #include <pcl/common/point_tests.h>
41 #endif
42 
43 #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2)
44 #include <emmintrin.h>
45 #define VISP_HAVE_SSE2 1
46 #endif
47 
48 // https://stackoverflow.com/a/40765925
49 #if !defined(__FMA__) && defined(__AVX2__)
50 #define __FMA__ 1
51 #endif
52 
53 #if defined _WIN32 && defined(_M_ARM64)
54 #define _ARM64_DISTINCT_NEON_TYPES
55 #include <Intrin.h>
56 #include <arm_neon.h>
57 #define VISP_HAVE_NEON 1
58 #elif (defined(__ARM_NEON__) || defined (__ARM_NEON)) && defined(__aarch64__)
59 #include <arm_neon.h>
60 #define VISP_HAVE_NEON 1
61 #endif
62 
63 #define USE_SIMD_CODE 1
64 
65 #if VISP_HAVE_SSE2 && USE_SIMD_CODE
66 #define USE_SSE 1
67 #else
68 #define USE_SSE 0
69 #endif
70 
71 #if VISP_HAVE_NEON && USE_SIMD_CODE
72 #define USE_NEON 1
73 #else
74 #define USE_NEON 0
75 #endif
76 
77 #if (VISP_HAVE_OPENCV_VERSION >= 0x040101 || (VISP_HAVE_OPENCV_VERSION < 0x040000 && VISP_HAVE_OPENCV_VERSION >= 0x030407)) && USE_SIMD_CODE
78 #define USE_OPENCV_HAL 1
79 #include <opencv2/core/simd_intrinsics.hpp>
80 #include <opencv2/core/hal/intrin.hpp>
81 #endif
82 
83 #if !USE_OPENCV_HAL && (USE_SSE || USE_NEON)
84 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
85 #include <cstdint>
86 #endif
87 
88 namespace
89 {
90 #if USE_SSE
91 inline void v_load_deinterleave(const uint64_t *ptr, __m128i &a, __m128i &b, __m128i &c)
92 {
93  __m128i t0 = _mm_loadu_si128((const __m128i *)ptr); // a0, b0
94  __m128i t1 = _mm_loadu_si128((const __m128i *)(ptr + 2)); // c0, a1
95  __m128i t2 = _mm_loadu_si128((const __m128i *)(ptr + 4)); // b1, c1
96 
97  t1 = _mm_shuffle_epi32(t1, 0x4e); // a1, c0
98 
99  a = _mm_unpacklo_epi64(t0, t1);
100  b = _mm_unpacklo_epi64(_mm_unpackhi_epi64(t0, t0), t2);
101  c = _mm_unpackhi_epi64(t1, t2);
102 }
103 
104 inline void v_load_deinterleave(const double *ptr, __m128d &a0, __m128d &b0, __m128d &c0)
105 {
106  __m128i a1, b1, c1;
107  v_load_deinterleave((const uint64_t *)ptr, a1, b1, c1);
108  a0 = _mm_castsi128_pd(a1);
109  b0 = _mm_castsi128_pd(b1);
110  c0 = _mm_castsi128_pd(c1);
111 }
112 
113 inline __m128d v_combine_low(const __m128d &a, const __m128d &b)
114 {
115  __m128i a1 = _mm_castpd_si128(a), b1 = _mm_castpd_si128(b);
116  return _mm_castsi128_pd(_mm_unpacklo_epi64(a1, b1));
117 }
118 
119 inline __m128d v_combine_high(const __m128d &a, const __m128d &b)
120 {
121  __m128i a1 = _mm_castpd_si128(a), b1 = _mm_castpd_si128(b);
122  return _mm_castsi128_pd(_mm_unpackhi_epi64(a1, b1));
123 }
124 
125 inline __m128d v_fma(const __m128d &a, const __m128d &b, const __m128d &c)
126 {
127 #if __FMA__
128  return _mm_fmadd_pd(a, b, c);
129 #else
130  return _mm_add_pd(_mm_mul_pd(a, b), c);
131 #endif
132 }
133 #else
134 inline void v_load_deinterleave(const double *ptr, float64x2_t &a0, float64x2_t &b0, float64x2_t &c0)
135 {
136  float64x2x3_t v = vld3q_f64(ptr);
137  a0 = v.val[0];
138  b0 = v.val[1];
139  c0 = v.val[2];
140 }
141 
142 inline float64x2_t v_combine_low(const float64x2_t &a, const float64x2_t &b)
143 {
144  return vcombine_f64(vget_low_f64(a), vget_low_f64(b));
145 }
146 
147 inline float64x2_t v_combine_high(const float64x2_t &a, const float64x2_t &b)
148 {
149  return vcombine_f64(vget_high_f64(a), vget_high_f64(b));
150 }
151 
152 inline float64x2_t v_fma(const float64x2_t &a, const float64x2_t &b, const float64x2_t &c)
153 {
154  return vfmaq_f64(c, a, b);
155 }
156 #endif
157 }
158 #endif // !USE_OPENCV_HAL && (USE_SSE || USE_NEON)
159 
161  : m_cam(), m_clippingFlag(vpPolygon3D::NO_CLIPPING), m_distFarClip(100), m_distNearClip(0.001), m_hiddenFace(nullptr),
162  m_planeObject(), m_polygon(nullptr), m_useScanLine(false),
163  m_depthDenseFilteringMethod(DEPTH_OCCUPANCY_RATIO_FILTERING), m_depthDenseFilteringMaxDist(3.0),
164  m_depthDenseFilteringMinDist(0.8), m_depthDenseFilteringOccupancyRatio(0.3), m_isTrackedDepthDenseFace(true),
165  m_isVisible(false), m_listOfFaceLines(), m_planeCamera(), m_pointCloudFace(), m_polygonLines()
166 { }
167 
169 {
170  for (size_t i = 0; i < m_listOfFaceLines.size(); i++) {
171  delete m_listOfFaceLines[i];
172  }
173 }
174 
190  vpUniRand &rand_gen, int polygon, std::string name)
191 {
192  // Build a PolygonLine to be able to easily display the lines model
193  PolygonLine polygon_line;
194 
195  // Add polygon
196  polygon_line.m_poly.setNbPoint(2);
197  polygon_line.m_poly.addPoint(0, P1);
198  polygon_line.m_poly.addPoint(1, P2);
199 
200  polygon_line.m_poly.setClipping(m_clippingFlag);
201  polygon_line.m_poly.setNearClippingDistance(m_distNearClip);
202  polygon_line.m_poly.setFarClippingDistance(m_distFarClip);
203 
204  polygon_line.m_p1 = &polygon_line.m_poly.p[0];
205  polygon_line.m_p2 = &polygon_line.m_poly.p[1];
206 
207  m_polygonLines.push_back(polygon_line);
208 
209  // suppress line already in the model
210  bool already_here = false;
212 
213  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
214  ++it) {
215  l = *it;
216  if ((samePoint(*(l->p1), P1) && samePoint(*(l->p2), P2)) || (samePoint(*(l->p1), P2) && samePoint(*(l->p2), P1))) {
217  already_here = true;
218  l->addPolygon(polygon);
219  l->hiddenface = faces;
221  }
222  }
223 
224  if (!already_here) {
225  l = new vpMbtDistanceLine;
226 
228  l->buildFrom(P1, P2, rand_gen);
229  l->addPolygon(polygon);
230  l->hiddenface = faces;
232 
233  l->setIndex((unsigned int)m_listOfFaceLines.size());
234  l->setName(name);
235 
238 
241 
244 
245  m_listOfFaceLines.push_back(l);
246  }
247 }
248 
249 #ifdef VISP_HAVE_PCL
251  const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud,
252  unsigned int stepX, unsigned int stepY
253 #if DEBUG_DISPLAY_DEPTH_DENSE
254  ,
255  vpImage<unsigned char> &debugImage,
256  std::vector<std::vector<vpImagePoint> > &roiPts_vec
257 #endif
258  ,
259  const vpImage<bool> *mask)
260 {
261  unsigned int width = point_cloud->width, height = point_cloud->height;
262  m_pointCloudFace.clear();
263 
264  if (point_cloud->width == 0 || point_cloud->height == 0)
265  return false;
266 
267  std::vector<vpImagePoint> roiPts;
268  double distanceToFace;
269  computeROI(cMo, width, height, roiPts
270 #if DEBUG_DISPLAY_DEPTH_DENSE
271  ,
272  roiPts_vec
273 #endif
274  ,
275  distanceToFace);
276 
277  if (roiPts.size() <= 2) {
278 #ifndef NDEBUG
279  std::cerr << "Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
280 #endif
281  return false;
282  }
283 
286  return false;
287  }
288 
289  vpPolygon polygon_2d(roiPts);
290  vpRect bb = polygon_2d.getBoundingBox();
291 
292  unsigned int top = (unsigned int)std::max<double>(0.0, bb.getTop());
293  unsigned int bottom = (unsigned int)std::min<double>((double)height, std::max<double>(0.0, bb.getBottom()));
294  unsigned int left = (unsigned int)std::max<double>(0.0, bb.getLeft());
295  unsigned int right = (unsigned int)std::min<double>((double)width, std::max<double>(0.0, bb.getRight()));
296 
297  bb.setTop(top);
298  bb.setBottom(bottom);
299  bb.setLeft(left);
300  bb.setRight(right);
301 
302  if (bb.getHeight() < 0 || bb.getWidth() < 0) {
303  return false;
304  }
305 
306  m_pointCloudFace.reserve((size_t)(bb.getWidth() * bb.getHeight()));
307 
308  int totalTheoreticalPoints = 0, totalPoints = 0;
309  for (unsigned int i = top; i < bottom; i += stepY) {
310  for (unsigned int j = left; j < right; j += stepX) {
311  if ((m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
312  j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
313  m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs()[i][j] == m_polygon->getIndex())
314  : polygon_2d.isInside(vpImagePoint(i, j)))) {
315  totalTheoreticalPoints++;
316 
317  if (vpMeTracker::inRoiMask(mask, i, j) && pcl::isFinite((*point_cloud)(j, i)) && (*point_cloud)(j, i).z > 0) {
318  totalPoints++;
319 
320  m_pointCloudFace.push_back((*point_cloud)(j, i).x);
321  m_pointCloudFace.push_back((*point_cloud)(j, i).y);
322  m_pointCloudFace.push_back((*point_cloud)(j, i).z);
323 
324 #if DEBUG_DISPLAY_DEPTH_DENSE
325  debugImage[i][j] = 255;
326 #endif
327  }
328  }
329  }
330  }
331 
332  if (totalPoints == 0 || ((m_depthDenseFilteringMethod & DEPTH_OCCUPANCY_RATIO_FILTERING) &&
333  totalPoints / (double)totalTheoreticalPoints < m_depthDenseFilteringOccupancyRatio)) {
334  return false;
335  }
336 
337  return true;
338 }
339 #endif
340 
342  unsigned int height, const std::vector<vpColVector> &point_cloud,
343  unsigned int stepX, unsigned int stepY
344 #if DEBUG_DISPLAY_DEPTH_DENSE
345  ,
346  vpImage<unsigned char> &debugImage,
347  std::vector<std::vector<vpImagePoint> > &roiPts_vec
348 #endif
349  ,
350  const vpImage<bool> *mask)
351 {
352  m_pointCloudFace.clear();
353 
354  if (width == 0 || height == 0)
355  return 0;
356 
357  std::vector<vpImagePoint> roiPts;
358  double distanceToFace;
359  computeROI(cMo, width, height, roiPts
360 #if DEBUG_DISPLAY_DEPTH_DENSE
361  ,
362  roiPts_vec
363 #endif
364  ,
365  distanceToFace);
366 
367  if (roiPts.size() <= 2) {
368 #ifndef NDEBUG
369  std::cerr << "Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
370 #endif
371  return false;
372  }
373 
376  return false;
377  }
378 
379  vpPolygon polygon_2d(roiPts);
380  vpRect bb = polygon_2d.getBoundingBox();
381 
382  unsigned int top = (unsigned int)std::max<double>(0.0, bb.getTop());
383  unsigned int bottom = (unsigned int)std::min<double>((double)height, std::max<double>(0.0, bb.getBottom()));
384  unsigned int left = (unsigned int)std::max<double>(0.0, bb.getLeft());
385  unsigned int right = (unsigned int)std::min<double>((double)width, std::max<double>(0.0, bb.getRight()));
386 
387  bb.setTop(top);
388  bb.setBottom(bottom);
389  bb.setLeft(left);
390  bb.setRight(right);
391 
392  m_pointCloudFace.reserve((size_t)(bb.getWidth() * bb.getHeight()));
393 
394  int totalTheoreticalPoints = 0, totalPoints = 0;
395  for (unsigned int i = top; i < bottom; i += stepY) {
396  for (unsigned int j = left; j < right; j += stepX) {
397  if ((m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
398  j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
399  m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs()[i][j] == m_polygon->getIndex())
400  : polygon_2d.isInside(vpImagePoint(i, j)))) {
401  totalTheoreticalPoints++;
402 
403  if (vpMeTracker::inRoiMask(mask, i, j) && point_cloud[i * width + j][2] > 0) {
404  totalPoints++;
405 
406  m_pointCloudFace.push_back(point_cloud[i * width + j][0]);
407  m_pointCloudFace.push_back(point_cloud[i * width + j][1]);
408  m_pointCloudFace.push_back(point_cloud[i * width + j][2]);
409 
410 #if DEBUG_DISPLAY_DEPTH_DENSE
411  debugImage[i][j] = 255;
412 #endif
413  }
414  }
415  }
416  }
417 
418  if (totalPoints == 0 || ((m_depthDenseFilteringMethod & DEPTH_OCCUPANCY_RATIO_FILTERING) &&
419  totalPoints / (double)totalTheoreticalPoints < m_depthDenseFilteringOccupancyRatio)) {
420  return false;
421  }
422 
423  return true;
424 }
425 
427  unsigned int height, const vpMatrix &point_cloud,
428  unsigned int stepX, unsigned int stepY
429 #if DEBUG_DISPLAY_DEPTH_DENSE
430  ,
431  vpImage<unsigned char> &debugImage,
432  std::vector<std::vector<vpImagePoint> > &roiPts_vec
433 #endif
434  ,
435  const vpImage<bool> *mask)
436 {
437  m_pointCloudFace.clear();
438 
439  if (width == 0 || height == 0)
440  return 0;
441 
442  std::vector<vpImagePoint> roiPts;
443  double distanceToFace;
444  computeROI(cMo, width, height, roiPts
445 #if DEBUG_DISPLAY_DEPTH_DENSE
446  ,
447  roiPts_vec
448 #endif
449  ,
450  distanceToFace);
451 
452  if (roiPts.size() <= 2) {
453 #ifndef NDEBUG
454  std::cerr << "Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
455 #endif
456  return false;
457  }
458 
461  return false;
462  }
463 
464  vpPolygon polygon_2d(roiPts);
465  vpRect bb = polygon_2d.getBoundingBox();
466 
467  unsigned int top = (unsigned int)std::max<double>(0.0, bb.getTop());
468  unsigned int bottom = (unsigned int)std::min<double>((double)height, std::max<double>(0.0, bb.getBottom()));
469  unsigned int left = (unsigned int)std::max<double>(0.0, bb.getLeft());
470  unsigned int right = (unsigned int)std::min<double>((double)width, std::max<double>(0.0, bb.getRight()));
471 
472  bb.setTop(top);
473  bb.setBottom(bottom);
474  bb.setLeft(left);
475  bb.setRight(right);
476 
477  m_pointCloudFace.reserve((size_t)(bb.getWidth() * bb.getHeight()));
478 
479  int totalTheoreticalPoints = 0, totalPoints = 0;
480  for (unsigned int i = top; i < bottom; i += stepY) {
481  for (unsigned int j = left; j < right; j += stepX) {
482  if ((m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
483  j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
484  m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs()[i][j] == m_polygon->getIndex())
485  : polygon_2d.isInside(vpImagePoint(i, j)))) {
486  totalTheoreticalPoints++;
487 
488  if (vpMeTracker::inRoiMask(mask, i, j) && point_cloud[i * width + j][2] > 0) {
489  totalPoints++;
490 
491  m_pointCloudFace.push_back(point_cloud[i * width + j][0]);
492  m_pointCloudFace.push_back(point_cloud[i * width + j][1]);
493  m_pointCloudFace.push_back(point_cloud[i * width + j][2]);
494 
495 #if DEBUG_DISPLAY_DEPTH_DENSE
496  debugImage[i][j] = 255;
497 #endif
498  }
499  }
500  }
501  }
502 
503  if (totalPoints == 0 || ((m_depthDenseFilteringMethod & DEPTH_OCCUPANCY_RATIO_FILTERING) &&
504  totalPoints / (double)totalTheoreticalPoints < m_depthDenseFilteringOccupancyRatio)) {
505  return false;
506  }
507 
508  return true;
509 }
510 
512 
514 {
515  // Compute lines visibility, only for display
516  vpMbtDistanceLine *line;
517  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
518  ++it) {
519  line = *it;
520  bool isvisible = false;
521 
522  for (std::list<int>::const_iterator itindex = line->Lindex_polygon.begin(); itindex != line->Lindex_polygon.end();
523  ++itindex) {
524  int index = *itindex;
525  if (index == -1) {
526  isvisible = true;
527  }
528  else {
529  if (line->hiddenface->isVisible((unsigned int)index)) {
530  isvisible = true;
531  }
532  }
533  }
534 
535  // Si la ligne n'appartient a aucune face elle est tout le temps visible
536  if (line->Lindex_polygon.empty())
537  isvisible = true; // Not sure that this can occur
538 
539  if (isvisible) {
540  line->setVisible(true);
541  }
542  else {
543  line->setVisible(false);
544  }
545  }
546 }
547 
549  vpColVector &error)
550 {
551  if (m_pointCloudFace.empty()) {
552  L.resize(0, 0);
553  error.resize(0);
554  return;
555  }
556 
557  L.resize(getNbFeatures(), 6, false, false);
558  error.resize(getNbFeatures(), false);
559 
560  // Transform the plane equation for the current pose
563 
564  double nx = m_planeCamera.getA();
565  double ny = m_planeCamera.getB();
566  double nz = m_planeCamera.getC();
567  double D = m_planeCamera.getD();
568 
569 #if defined(VISP_HAVE_SIMDLIB)
571 #else
572  bool useSIMD = vpCPUFeatures::checkSSE2();
573 #endif
574 #if USE_OPENCV_HAL
575  useSIMD = true;
576 #endif
577 #if !USE_SSE && !USE_NEON && !USE_OPENCV_HAL
578  useSIMD = false;
579 #endif
580 
581  if (useSIMD) {
582 #if USE_SSE || USE_NEON || USE_OPENCV_HAL
583  size_t cpt = 0;
584  if (getNbFeatures() >= 2) {
585  double *ptr_point_cloud = &m_pointCloudFace[0];
586  double *ptr_L = L.data;
587  double *ptr_error = error.data;
588 
589 #if USE_OPENCV_HAL
590  const cv::v_float64x2 vnx = cv::v_setall_f64(nx);
591  const cv::v_float64x2 vny = cv::v_setall_f64(ny);
592  const cv::v_float64x2 vnz = cv::v_setall_f64(nz);
593  const cv::v_float64x2 vd = cv::v_setall_f64(D);
594 #elif USE_SSE
595  const __m128d vnx = _mm_set1_pd(nx);
596  const __m128d vny = _mm_set1_pd(ny);
597  const __m128d vnz = _mm_set1_pd(nz);
598  const __m128d vd = _mm_set1_pd(D);
599 #else
600  const float64x2_t vnx = vdupq_n_f64(nx);
601  const float64x2_t vny = vdupq_n_f64(ny);
602  const float64x2_t vnz = vdupq_n_f64(nz);
603  const float64x2_t vd = vdupq_n_f64(D);
604 #endif
605 
606  for (; cpt <= m_pointCloudFace.size() - 6; cpt += 6, ptr_point_cloud += 6) {
607 #if USE_OPENCV_HAL
608  cv::v_float64x2 vx, vy, vz;
609  cv::v_load_deinterleave(ptr_point_cloud, vx, vy, vz);
610 
611 #if (VISP_HAVE_OPENCV_VERSION >= 0x040900)
612  cv::v_float64x2 va1 = cv::v_sub(cv::v_mul(vnz, vy), cv::v_mul(vny, vz)); // vnz*vy - vny*vz
613  cv::v_float64x2 va2 = cv::v_sub(cv::v_mul(vnx, vz), cv::v_mul(vnz, vx)); // vnx*vz - vnz*vx
614  cv::v_float64x2 va3 = cv::v_sub(cv::v_mul(vny, vx), cv::v_mul(vnx, vy)); // vny*vx - vnx*vy
615 #else
616  cv::v_float64x2 va1 = vnz*vy - vny*vz;
617  cv::v_float64x2 va2 = vnx*vz - vnz*vx;
618  cv::v_float64x2 va3 = vny*vx - vnx*vy;
619 #endif
620 
621  cv::v_float64x2 vnxy = cv::v_combine_low(vnx, vny);
622  cv::v_store(ptr_L, vnxy);
623  ptr_L += 2;
624  vnxy = cv::v_combine_low(vnz, va1);
625  cv::v_store(ptr_L, vnxy);
626  ptr_L += 2;
627  vnxy = cv::v_combine_low(va2, va3);
628  cv::v_store(ptr_L, vnxy);
629  ptr_L += 2;
630 
631  vnxy = cv::v_combine_high(vnx, vny);
632  cv::v_store(ptr_L, vnxy);
633  ptr_L += 2;
634  vnxy = cv::v_combine_high(vnz, va1);
635  cv::v_store(ptr_L, vnxy);
636  ptr_L += 2;
637  vnxy = cv::v_combine_high(va2, va3);
638  cv::v_store(ptr_L, vnxy);
639  ptr_L += 2;
640 
641 #if (VISP_HAVE_OPENCV_VERSION >= 0x040900)
642  cv::v_float64x2 verr = cv::v_add(vd, cv::v_muladd(vnx, vx, cv::v_muladd(vny, vy, cv::v_mul(vnz, vz))));
643 #else
644  cv::v_float64x2 verr = vd + cv::v_muladd(vnx, vx, cv::v_muladd(vny, vy, vnz*vz));
645 #endif
646 
647  cv::v_store(ptr_error, verr);
648  ptr_error += 2;
649 #elif USE_SSE
650  __m128d vx, vy, vz;
651  v_load_deinterleave(ptr_point_cloud, vx, vy, vz);
652 
653  __m128d va1 = _mm_sub_pd(_mm_mul_pd(vnz, vy), _mm_mul_pd(vny, vz));
654  __m128d va2 = _mm_sub_pd(_mm_mul_pd(vnx, vz), _mm_mul_pd(vnz, vx));
655  __m128d va3 = _mm_sub_pd(_mm_mul_pd(vny, vx), _mm_mul_pd(vnx, vy));
656 
657  __m128d vnxy = v_combine_low(vnx, vny);
658  _mm_storeu_pd(ptr_L, vnxy);
659  ptr_L += 2;
660  vnxy = v_combine_low(vnz, va1);
661  _mm_storeu_pd(ptr_L, vnxy);
662  ptr_L += 2;
663  vnxy = v_combine_low(va2, va3);
664  _mm_storeu_pd(ptr_L, vnxy);
665  ptr_L += 2;
666 
667  vnxy = v_combine_high(vnx, vny);
668  _mm_storeu_pd(ptr_L, vnxy);
669  ptr_L += 2;
670  vnxy = v_combine_high(vnz, va1);
671  _mm_storeu_pd(ptr_L, vnxy);
672  ptr_L += 2;
673  vnxy = v_combine_high(va2, va3);
674  _mm_storeu_pd(ptr_L, vnxy);
675  ptr_L += 2;
676 
677  const __m128d verror = _mm_add_pd(vd, v_fma(vnx, vx, v_fma(vny, vy, _mm_mul_pd(vnz, vz))));
678  _mm_storeu_pd(ptr_error, verror);
679  ptr_error += 2;
680 #else
681  float64x2_t vx, vy, vz;
682  v_load_deinterleave(ptr_point_cloud, vx, vy, vz);
683 
684  float64x2_t va1 = vsubq_f64(vmulq_f64(vnz, vy), vmulq_f64(vny, vz));
685  float64x2_t va2 = vsubq_f64(vmulq_f64(vnx, vz), vmulq_f64(vnz, vx));
686  float64x2_t va3 = vsubq_f64(vmulq_f64(vny, vx), vmulq_f64(vnx, vy));
687 
688  float64x2_t vnxy = v_combine_low(vnx, vny);
689  vst1q_f64(ptr_L, vnxy);
690  ptr_L += 2;
691  vnxy = v_combine_low(vnz, va1);
692  vst1q_f64(ptr_L, vnxy);
693  ptr_L += 2;
694  vnxy = v_combine_low(va2, va3);
695  vst1q_f64(ptr_L, vnxy);
696  ptr_L += 2;
697 
698  vnxy = v_combine_high(vnx, vny);
699  vst1q_f64(ptr_L, vnxy);
700  ptr_L += 2;
701  vnxy = v_combine_high(vnz, va1);
702  vst1q_f64(ptr_L, vnxy);
703  ptr_L += 2;
704  vnxy = v_combine_high(va2, va3);
705  vst1q_f64(ptr_L, vnxy);
706  ptr_L += 2;
707 
708  const float64x2_t verror = vaddq_f64(vd, v_fma(vnx, vx, v_fma(vny, vy, vmulq_f64(vnz, vz))));
709  vst1q_f64(ptr_error, verror);
710  ptr_error += 2;
711 #endif
712  }
713  }
714 
715  for (; cpt < m_pointCloudFace.size(); cpt += 3) {
716  double x = m_pointCloudFace[cpt];
717  double y = m_pointCloudFace[cpt + 1];
718  double z = m_pointCloudFace[cpt + 2];
719 
720  double _a1 = (nz * y) - (ny * z);
721  double _a2 = (nx * z) - (nz * x);
722  double _a3 = (ny * x) - (nx * y);
723 
724  // L
725  L[(unsigned int)(cpt / 3)][0] = nx;
726  L[(unsigned int)(cpt / 3)][1] = ny;
727  L[(unsigned int)(cpt / 3)][2] = nz;
728  L[(unsigned int)(cpt / 3)][3] = _a1;
729  L[(unsigned int)(cpt / 3)][4] = _a2;
730  L[(unsigned int)(cpt / 3)][5] = _a3;
731 
732  vpColVector normal(3);
733  normal[0] = nx;
734  normal[1] = ny;
735  normal[2] = nz;
736 
737  vpColVector pt(3);
738  pt[0] = x;
739  pt[1] = y;
740  pt[2] = z;
741 
742  // Error
743  error[(unsigned int)(cpt / 3)] = D + (normal.t() * pt);
744  }
745 #endif
746  }
747  else {
748  vpColVector normal(3);
749  normal[0] = nx;
750  normal[1] = ny;
751  normal[2] = nz;
752  vpColVector pt(3);
753 
754  unsigned int idx = 0;
755  for (size_t i = 0; i < m_pointCloudFace.size(); i += 3, idx++) {
756  double x = m_pointCloudFace[i];
757  double y = m_pointCloudFace[i + 1];
758  double z = m_pointCloudFace[i + 2];
759 
760  double _a1 = (nz * y) - (ny * z);
761  double _a2 = (nx * z) - (nz * x);
762  double _a3 = (ny * x) - (nx * y);
763 
764  // L
765  L[idx][0] = nx;
766  L[idx][1] = ny;
767  L[idx][2] = nz;
768  L[idx][3] = _a1;
769  L[idx][4] = _a2;
770  L[idx][5] = _a3;
771 
772  pt[0] = x;
773  pt[1] = y;
774  pt[2] = z;
775  // Error
776  error[idx] = D + (normal.t() * pt);
777  }
778  }
779 }
780 
781 void vpMbtFaceDepthDense::computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height,
782  std::vector<vpImagePoint> &roiPts
783 #if DEBUG_DISPLAY_DEPTH_DENSE
784  ,
785  std::vector<std::vector<vpImagePoint> > &roiPts_vec
786 #endif
787  ,
788  double &distanceToFace)
789 {
790  if (m_useScanLine || m_clippingFlag > 2)
791  m_cam.computeFov(width, height);
792 
793  if (m_useScanLine) {
794  for (std::vector<PolygonLine>::iterator it = m_polygonLines.begin(); it != m_polygonLines.end(); ++it) {
795  it->m_p1->changeFrame(cMo);
796  it->m_p2->changeFrame(cMo);
797 
798  vpImagePoint ip1, ip2;
799 
800  it->m_poly.changeFrame(cMo);
801  it->m_poly.computePolygonClipped(m_cam);
802 
803  if (it->m_poly.polyClipped.size() == 2 &&
804  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::NEAR_CLIPPING) == 0) &&
805  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::FAR_CLIPPING) == 0) &&
806  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::DOWN_CLIPPING) == 0) &&
807  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::UP_CLIPPING) == 0) &&
808  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::LEFT_CLIPPING) == 0) &&
809  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::RIGHT_CLIPPING) == 0)) {
810 
811  std::vector<std::pair<vpPoint, vpPoint> > linesLst;
812  m_hiddenFace->computeScanLineQuery(it->m_poly.polyClipped[0].first, it->m_poly.polyClipped[1].first, linesLst,
813  true);
814 
815  vpPoint faceCentroid;
816 
817  for (unsigned int i = 0; i < linesLst.size(); i++) {
818  linesLst[i].first.project();
819  linesLst[i].second.project();
820 
821  vpMeterPixelConversion::convertPoint(m_cam, linesLst[i].first.get_x(), linesLst[i].first.get_y(), ip1);
822  vpMeterPixelConversion::convertPoint(m_cam, linesLst[i].second.get_x(), linesLst[i].second.get_y(), ip2);
823 
824  it->m_imPt1 = ip1;
825  it->m_imPt2 = ip2;
826 
827  roiPts.push_back(ip1);
828  roiPts.push_back(ip2);
829 
830  faceCentroid.set_X(faceCentroid.get_X() + linesLst[i].first.get_X() + linesLst[i].second.get_X());
831  faceCentroid.set_Y(faceCentroid.get_Y() + linesLst[i].first.get_Y() + linesLst[i].second.get_Y());
832  faceCentroid.set_Z(faceCentroid.get_Z() + linesLst[i].first.get_Z() + linesLst[i].second.get_Z());
833 
834 #if DEBUG_DISPLAY_DEPTH_DENSE
835  std::vector<vpImagePoint> roiPts_;
836  roiPts_.push_back(ip1);
837  roiPts_.push_back(ip2);
838  roiPts_vec.push_back(roiPts_);
839 #endif
840  }
841 
842  if (linesLst.empty()) {
843  distanceToFace = std::numeric_limits<double>::max();
844  }
845  else {
846  faceCentroid.set_X(faceCentroid.get_X() / (2 * linesLst.size()));
847  faceCentroid.set_Y(faceCentroid.get_Y() / (2 * linesLst.size()));
848  faceCentroid.set_Z(faceCentroid.get_Z() / (2 * linesLst.size()));
849 
850  distanceToFace =
851  sqrt(faceCentroid.get_X() * faceCentroid.get_X() + faceCentroid.get_Y() * faceCentroid.get_Y() +
852  faceCentroid.get_Z() * faceCentroid.get_Z());
853  }
854  }
855  }
856  }
857  else {
858  // Get polygon clipped
859  m_polygon->getRoiClipped(m_cam, roiPts, cMo);
860 
861  // Get 3D polygon clipped
862  std::vector<vpPoint> polygonsClipped;
863  m_polygon->getPolygonClipped(polygonsClipped);
864 
865  if (polygonsClipped.empty()) {
866  distanceToFace = std::numeric_limits<double>::max();
867  }
868  else {
869  vpPoint faceCentroid;
870 
871  for (size_t i = 0; i < polygonsClipped.size(); i++) {
872  faceCentroid.set_X(faceCentroid.get_X() + polygonsClipped[i].get_X());
873  faceCentroid.set_Y(faceCentroid.get_Y() + polygonsClipped[i].get_Y());
874  faceCentroid.set_Z(faceCentroid.get_Z() + polygonsClipped[i].get_Z());
875  }
876 
877  faceCentroid.set_X(faceCentroid.get_X() / polygonsClipped.size());
878  faceCentroid.set_Y(faceCentroid.get_Y() / polygonsClipped.size());
879  faceCentroid.set_Z(faceCentroid.get_Z() / polygonsClipped.size());
880 
881  distanceToFace = sqrt(faceCentroid.get_X() * faceCentroid.get_X() + faceCentroid.get_Y() * faceCentroid.get_Y() +
882  faceCentroid.get_Z() * faceCentroid.get_Z());
883  }
884 
885 #if DEBUG_DISPLAY_DEPTH_DENSE
886  roiPts_vec.push_back(roiPts);
887 #endif
888  }
889 }
890 
892  const vpCameraParameters &cam, const vpColor &col, unsigned int thickness,
893  bool displayFullModel)
894 {
895  std::vector<std::vector<double> > models =
896  getModelForDisplay(I.getWidth(), I.getHeight(), cMo, cam, displayFullModel);
897 
898  for (size_t i = 0; i < models.size(); i++) {
899  vpImagePoint ip1(models[i][1], models[i][2]);
900  vpImagePoint ip2(models[i][3], models[i][4]);
901  vpDisplay::displayLine(I, ip1, ip2, col, thickness);
902  }
903 }
904 
906  const vpCameraParameters &cam, const vpColor &col, unsigned int thickness,
907  bool displayFullModel)
908 {
909  std::vector<std::vector<double> > models =
910  getModelForDisplay(I.getWidth(), I.getHeight(), cMo, cam, displayFullModel);
911 
912  for (size_t i = 0; i < models.size(); i++) {
913  vpImagePoint ip1(models[i][1], models[i][2]);
914  vpImagePoint ip2(models[i][3], models[i][4]);
915  vpDisplay::displayLine(I, ip1, ip2, col, thickness);
916  }
917 }
918 
920  const vpCameraParameters & /*cam*/, const double /*scale*/,
921  const unsigned int /*thickness*/)
922 { }
923 
925  const vpCameraParameters & /*cam*/, const double /*scale*/,
926  const unsigned int /*thickness*/)
927 { }
928 
940 std::vector<std::vector<double> > vpMbtFaceDepthDense::getModelForDisplay(unsigned int width, unsigned int height,
941  const vpHomogeneousMatrix &cMo,
942  const vpCameraParameters &cam,
943  bool displayFullModel)
944 {
945  std::vector<std::vector<double> > models;
946 
947  if ((m_polygon->isVisible() && m_isTrackedDepthDenseFace) || displayFullModel) {
949 
950  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
951  ++it) {
952  vpMbtDistanceLine *line = *it;
953  std::vector<std::vector<double> > lineModels =
954  line->getModelForDisplay(width, height, cMo, cam, displayFullModel);
955  models.insert(models.end(), lineModels.begin(), lineModels.end());
956  }
957  }
958 
959  return models;
960 }
961 
971 bool vpMbtFaceDepthDense::samePoint(const vpPoint &P1, const vpPoint &P2) const
972 {
973  double dx = fabs(P1.get_oX() - P2.get_oX());
974  double dy = fabs(P1.get_oY() - P2.get_oY());
975  double dz = fabs(P1.get_oZ() - P2.get_oZ());
976 
977  if (dx <= std::numeric_limits<double>::epsilon() && dy <= std::numeric_limits<double>::epsilon() &&
978  dz <= std::numeric_limits<double>::epsilon())
979  return true;
980  else
981  return false;
982 }
983 
985 {
986  m_cam = camera;
987 
988  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
989  ++it) {
990  (*it)->setCameraParameters(camera);
991  }
992 }
993 
995 {
996  m_useScanLine = v;
997 
998  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
999  ++it) {
1000  (*it)->useScanLine = v;
1001  }
1002 }
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
Generic class defining intrinsic camera parameters.
void computeFov(const unsigned int &w, const unsigned int &h)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpRowVector t() const
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
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
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
vpMbScanLine & getMbScanLineRenderer()
bool isVisible(unsigned int i)
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
Manage the line of a polygon used in the model-based tracker.
void setIndex(unsigned int i)
vpPoint * p2
The second extremity.
std::list< int > Lindex_polygon
Index of the faces which contain the line.
void buildFrom(vpPoint &_p1, vpPoint &_p2, vpUniRand &rand_gen)
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
bool useScanLine
Use scanline rendering.
vpPoint * p1
The first extremity.
void setCameraParameters(const vpCameraParameters &camera)
void setName(const std::string &line_name)
void setVisible(bool _isvisible)
void addPolygon(const int &index)
vpMbtPolygon & getPolygon()
double m_depthDenseFilteringMinDist
Minimum distance threshold.
vpMbHiddenFaces< vpMbtPolygon > * m_hiddenFace
Pointer to the list of faces.
bool m_isVisible
Visibility flag.
double m_distFarClip
Distance for near clipping.
std::vector< double > m_pointCloudFace
List of depth points inside the face.
vpPlane m_planeObject
Plane equation described in the object frame.
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
void setCameraParameters(const vpCameraParameters &camera)
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
void computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height, std::vector< vpImagePoint > &roiPts, double &distanceToFace)
unsigned int getNbFeatures() const
bool samePoint(const vpPoint &P1, const vpPoint &P2) const
void computeInteractionMatrixAndResidu(const vpHomogeneousMatrix &cMo, vpMatrix &L, vpColVector &error)
void setScanLineVisibilityTest(bool v)
vpMbtPolygon * m_polygon
Polygon defining the face.
bool m_useScanLine
Scan line visibility.
bool m_isTrackedDepthDenseFace
Flag to define if the face should be tracked or not.
double m_depthDenseFilteringMaxDist
Maximum distance threshold.
bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, const pcl::PointCloud< pcl::PointXYZ >::ConstPtr &point_cloud, unsigned int stepX, unsigned int stepY, const vpImage< bool > *mask=nullptr)
std::vector< PolygonLine > m_polygonLines
Polygon lines used for scan-line visibility.
int m_depthDenseFilteringMethod
Method to use to consider or not the face.
unsigned int m_clippingFlag
Flags specifying which clipping to used.
std::vector< vpMbtDistanceLine * > m_listOfFaceLines
vpCameraParameters m_cam
Camera intrinsic parameters.
double m_depthDenseFilteringOccupancyRatio
Ratio between available depth points and theoretical number of points.
double m_distNearClip
Distance for near clipping.
void displayFeature(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double scale=0.05, unsigned int thickness=1)
void addLine(vpPoint &p1, vpPoint &p2, vpMbHiddenFaces< vpMbtPolygon > *const faces, vpUniRand &rand_gen, int polygon=-1, std::string name="")
virtual bool isVisible(const vpHomogeneousMatrix &cMo, double alpha, const bool &modulo=false, const vpCameraParameters &cam=vpCameraParameters(), unsigned int width=0, unsigned int height=0)
int getIndex() const
Definition: vpMbtPolygon.h:91
static bool inRoiMask(const vpImage< bool > *mask, unsigned int i, unsigned int j)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
void changeFrame(const vpHomogeneousMatrix &cMo)
Definition: vpPlane.cpp:372
double getD() const
Definition: vpPlane.h:106
double getA() const
Definition: vpPlane.h:100
double getC() const
Definition: vpPlane.h:104
double getB() const
Definition: vpPlane.h:102
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:454
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:447
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:458
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:486
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:488
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:449
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:490
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:456
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:445
Implements a 3D polygon with render functionalities like clipping.
Definition: vpPolygon3D.h:55
void setFarClippingDistance(const double &dist)
Definition: vpPolygon3D.h:189
void setNearClippingDistance(const double &dist)
Definition: vpPolygon3D.h:202
void setClipping(const unsigned int &flags)
Definition: vpPolygon3D.h:182
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
void getPolygonClipped(std::vector< std::pair< vpPoint, unsigned int > > &poly)
Defines a generic 2D polygon.
Definition: vpPolygon.h:97
vpRect getBoundingBox() const
Definition: vpPolygon.h:171
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
Definition: vpPolygon.cpp:394
Defines a rectangle in the plane.
Definition: vpRect.h:76
double getWidth() const
Definition: vpRect.h:224
void setTop(double pos)
Definition: vpRect.h:354
double getLeft() const
Definition: vpRect.h:170
void setLeft(double pos)
Definition: vpRect.h:318
void setRight(double pos)
Definition: vpRect.h:345
double getRight() const
Definition: vpRect.h:176
double getBottom() const
Definition: vpRect.h:94
double getHeight() const
Definition: vpRect.h:163
void setBottom(double pos)
Definition: vpRect.h:285
double getTop() const
Definition: vpRect.h:189
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:123
VISP_EXPORT bool checkSSE2()
VISP_EXPORT bool checkNeon()