Visual Servoing Platform  version 3.3.1 under development (2020-12-02)
vpMbtFaceDepthDense.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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 #define USE_SSE_CODE 1
49 #if VISP_HAVE_SSE2 && USE_SSE_CODE
50 #define USE_SSE 1
51 #else
52 #define USE_SSE 0
53 #endif
54 
56  : m_cam(), m_clippingFlag(vpPolygon3D::NO_CLIPPING), m_distFarClip(100), m_distNearClip(0.001), m_hiddenFace(NULL),
57  m_planeObject(), m_polygon(NULL), m_useScanLine(false),
58  m_depthDenseFilteringMethod(DEPTH_OCCUPANCY_RATIO_FILTERING), m_depthDenseFilteringMaxDist(3.0),
59  m_depthDenseFilteringMinDist(0.8), m_depthDenseFilteringOccupancyRatio(0.3), m_isTrackedDepthDenseFace(true),
60  m_isVisible(false), m_listOfFaceLines(), m_planeCamera(), m_pointCloudFace(), m_polygonLines()
61 {
62 }
63 
65 {
66  for (size_t i = 0; i < m_listOfFaceLines.size(); i++) {
67  delete m_listOfFaceLines[i];
68  }
69 }
70 
85  std::string name)
86 {
87  // Build a PolygonLine to be able to easily display the lines model
88  PolygonLine polygon_line;
89 
90  // Add polygon
91  polygon_line.m_poly.setNbPoint(2);
92  polygon_line.m_poly.addPoint(0, P1);
93  polygon_line.m_poly.addPoint(1, P2);
94 
95  polygon_line.m_poly.setClipping(m_clippingFlag);
96  polygon_line.m_poly.setNearClippingDistance(m_distNearClip);
97  polygon_line.m_poly.setFarClippingDistance(m_distFarClip);
98 
99  polygon_line.m_p1 = &polygon_line.m_poly.p[0];
100  polygon_line.m_p2 = &polygon_line.m_poly.p[1];
101 
102  m_polygonLines.push_back(polygon_line);
103 
104  // suppress line already in the model
105  bool already_here = false;
107 
108  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
109  ++it) {
110  l = *it;
111  if ((samePoint(*(l->p1), P1) && samePoint(*(l->p2), P2)) || (samePoint(*(l->p1), P2) && samePoint(*(l->p2), P1))) {
112  already_here = true;
113  l->addPolygon(polygon);
114  l->hiddenface = faces;
116  }
117  }
118 
119  if (!already_here) {
120  l = new vpMbtDistanceLine;
121 
123  l->buildFrom(P1, P2);
124  l->addPolygon(polygon);
125  l->hiddenface = faces;
127 
128  l->setIndex((unsigned int)m_listOfFaceLines.size());
129  l->setName(name);
130 
133 
134  if ((m_clippingFlag & vpPolygon3D::NEAR_CLIPPING) == vpPolygon3D::NEAR_CLIPPING)
136 
137  if ((m_clippingFlag & vpPolygon3D::FAR_CLIPPING) == vpPolygon3D::FAR_CLIPPING)
139 
140  m_listOfFaceLines.push_back(l);
141  }
142 }
143 
144 #ifdef VISP_HAVE_PCL
146  const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud,
147  unsigned int stepX, unsigned int stepY
148 #if DEBUG_DISPLAY_DEPTH_DENSE
149  ,
150  vpImage<unsigned char> &debugImage,
151  std::vector<std::vector<vpImagePoint> > &roiPts_vec
152 #endif
153  , const vpImage<bool> *mask
154 )
155 {
156  unsigned int width = point_cloud->width, height = point_cloud->height;
157  m_pointCloudFace.clear();
158 
159  if (point_cloud->width == 0 || point_cloud->height == 0)
160  return false;
161 
162  std::vector<vpImagePoint> roiPts;
163  double distanceToFace;
164  computeROI(cMo, width, height, roiPts
165 #if DEBUG_DISPLAY_DEPTH_DENSE
166  ,
167  roiPts_vec
168 #endif
169  ,
170  distanceToFace);
171 
172  if (roiPts.size() <= 2) {
173 #ifndef NDEBUG
174  std::cerr << "Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
175 #endif
176  return false;
177  }
178 
181  return false;
182  }
183 
184  vpPolygon polygon_2d(roiPts);
185  vpRect bb = polygon_2d.getBoundingBox();
186 
187  unsigned int top = (unsigned int)std::max(0.0, bb.getTop());
188  unsigned int bottom = (unsigned int)std::min((double)height, std::max(0.0, bb.getBottom()));
189  unsigned int left = (unsigned int)std::max(0.0, bb.getLeft());
190  unsigned int right = (unsigned int)std::min((double)width, std::max(0.0, bb.getRight()));
191 
192  bb.setTop(top);
193  bb.setBottom(bottom);
194  bb.setLeft(left);
195  bb.setRight(right);
196 
197  if (bb.getHeight() < 0 || bb.getWidth() < 0) {
198  return false;
199  }
200 
201  m_pointCloudFace.reserve((size_t)(bb.getWidth() * bb.getHeight()));
202 
203  bool checkSSE2 = vpCPUFeatures::checkSSE2();
204 #if !USE_SSE
205  checkSSE2 = false;
206 #else
207  bool push = false;
208  double prev_x = 0.0, prev_y = 0.0, prev_z = 0.0;
209 #endif
210 
211  int totalTheoreticalPoints = 0, totalPoints = 0;
212  for (unsigned int i = top; i < bottom; i += stepY) {
213  for (unsigned int j = left; j < right; j += stepX) {
214  if ((m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
215  j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
216  m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs()[i][j] == m_polygon->getIndex())
217  : polygon_2d.isInside(vpImagePoint(i, j)))) {
218  totalTheoreticalPoints++;
219 
220  if (vpMeTracker::inMask(mask, i, j) && pcl::isFinite((*point_cloud)(j, i)) && (*point_cloud)(j, i).z > 0) {
221  totalPoints++;
222 
223  if (checkSSE2) {
224 #if USE_SSE
225  if (!push) {
226  push = true;
227  prev_x = (*point_cloud)(j, i).x;
228  prev_y = (*point_cloud)(j, i).y;
229  prev_z = (*point_cloud)(j, i).z;
230  } else {
231  push = false;
232  m_pointCloudFace.push_back(prev_x);
233  m_pointCloudFace.push_back((*point_cloud)(j, i).x);
234 
235  m_pointCloudFace.push_back(prev_y);
236  m_pointCloudFace.push_back((*point_cloud)(j, i).y);
237 
238  m_pointCloudFace.push_back(prev_z);
239  m_pointCloudFace.push_back((*point_cloud)(j, i).z);
240  }
241 #endif
242  } else {
243  m_pointCloudFace.push_back((*point_cloud)(j, i).x);
244  m_pointCloudFace.push_back((*point_cloud)(j, i).y);
245  m_pointCloudFace.push_back((*point_cloud)(j, i).z);
246  }
247 
248 #if DEBUG_DISPLAY_DEPTH_DENSE
249  debugImage[i][j] = 255;
250 #endif
251  }
252  }
253  }
254  }
255 
256 #if USE_SSE
257  if (checkSSE2 && push) {
258  m_pointCloudFace.push_back(prev_x);
259  m_pointCloudFace.push_back(prev_y);
260  m_pointCloudFace.push_back(prev_z);
261  }
262 #endif
263 
264  if (totalPoints == 0 || ((m_depthDenseFilteringMethod & DEPTH_OCCUPANCY_RATIO_FILTERING) &&
265  totalPoints / (double)totalTheoreticalPoints < m_depthDenseFilteringOccupancyRatio)) {
266  return false;
267  }
268 
269  return true;
270 }
271 #endif
272 
274  unsigned int height, const std::vector<vpColVector> &point_cloud,
275  unsigned int stepX, unsigned int stepY
276 #if DEBUG_DISPLAY_DEPTH_DENSE
277  ,
278  vpImage<unsigned char> &debugImage,
279  std::vector<std::vector<vpImagePoint> > &roiPts_vec
280 #endif
281  , const vpImage<bool> *mask
282 )
283 {
284  m_pointCloudFace.clear();
285 
286  if (width == 0 || height == 0)
287  return 0;
288 
289  std::vector<vpImagePoint> roiPts;
290  double distanceToFace;
291  computeROI(cMo, width, height, roiPts
292 #if DEBUG_DISPLAY_DEPTH_DENSE
293  ,
294  roiPts_vec
295 #endif
296  ,
297  distanceToFace);
298 
299  if (roiPts.size() <= 2) {
300 #ifndef NDEBUG
301  std::cerr << "Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
302 #endif
303  return false;
304  }
305 
308  return false;
309  }
310 
311  vpPolygon polygon_2d(roiPts);
312  vpRect bb = polygon_2d.getBoundingBox();
313 
314  unsigned int top = (unsigned int)std::max(0.0, bb.getTop());
315  unsigned int bottom = (unsigned int)std::min((double)height, std::max(0.0, bb.getBottom()));
316  unsigned int left = (unsigned int)std::max(0.0, bb.getLeft());
317  unsigned int right = (unsigned int)std::min((double)width, std::max(0.0, bb.getRight()));
318 
319  bb.setTop(top);
320  bb.setBottom(bottom);
321  bb.setLeft(left);
322  bb.setRight(right);
323 
324  m_pointCloudFace.reserve((size_t)(bb.getWidth() * bb.getHeight()));
325 
326  bool checkSSE2 = vpCPUFeatures::checkSSE2();
327 #if !USE_SSE
328  checkSSE2 = false;
329 #else
330  bool push = false;
331  double prev_x = 0.0, prev_y = 0.0, prev_z = 0.0;
332 #endif
333 
334  int totalTheoreticalPoints = 0, totalPoints = 0;
335  for (unsigned int i = top; i < bottom; i += stepY) {
336  for (unsigned int j = left; j < right; j += stepX) {
337  if ((m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
338  j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
339  m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs()[i][j] == m_polygon->getIndex())
340  : polygon_2d.isInside(vpImagePoint(i, j)))) {
341  totalTheoreticalPoints++;
342 
343  if (vpMeTracker::inMask(mask, i, j) && point_cloud[i * width + j][2] > 0) {
344  totalPoints++;
345 
346  if (checkSSE2) {
347 #if USE_SSE
348  if (!push) {
349  push = true;
350  prev_x = point_cloud[i * width + j][0];
351  prev_y = point_cloud[i * width + j][1];
352  prev_z = point_cloud[i * width + j][2];
353  } else {
354  push = false;
355  m_pointCloudFace.push_back(prev_x);
356  m_pointCloudFace.push_back(point_cloud[i * width + j][0]);
357 
358  m_pointCloudFace.push_back(prev_y);
359  m_pointCloudFace.push_back(point_cloud[i * width + j][1]);
360 
361  m_pointCloudFace.push_back(prev_z);
362  m_pointCloudFace.push_back(point_cloud[i * width + j][2]);
363  }
364 #endif
365  } else {
366  m_pointCloudFace.push_back(point_cloud[i * width + j][0]);
367  m_pointCloudFace.push_back(point_cloud[i * width + j][1]);
368  m_pointCloudFace.push_back(point_cloud[i * width + j][2]);
369  }
370 
371 #if DEBUG_DISPLAY_DEPTH_DENSE
372  debugImage[i][j] = 255;
373 #endif
374  }
375  }
376  }
377  }
378 
379 #if USE_SSE
380  if (checkSSE2 && push) {
381  m_pointCloudFace.push_back(prev_x);
382  m_pointCloudFace.push_back(prev_y);
383  m_pointCloudFace.push_back(prev_z);
384  }
385 #endif
386 
387  if (totalPoints == 0 || ((m_depthDenseFilteringMethod & DEPTH_OCCUPANCY_RATIO_FILTERING) &&
388  totalPoints / (double)totalTheoreticalPoints < m_depthDenseFilteringOccupancyRatio)) {
389  return false;
390  }
391 
392  return true;
393 }
394 
396 
398 {
399  // Compute lines visibility, only for display
400  vpMbtDistanceLine *line;
401  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
402  ++it) {
403  line = *it;
404  bool isvisible = false;
405 
406  for (std::list<int>::const_iterator itindex = line->Lindex_polygon.begin(); itindex != line->Lindex_polygon.end();
407  ++itindex) {
408  int index = *itindex;
409  if (index == -1) {
410  isvisible = true;
411  } else {
412  if (line->hiddenface->isVisible((unsigned int)index)) {
413  isvisible = true;
414  }
415  }
416  }
417 
418  // Si la ligne n'appartient a aucune face elle est tout le temps visible
419  if (line->Lindex_polygon.empty())
420  isvisible = true; // Not sure that this can occur
421 
422  if (isvisible) {
423  line->setVisible(true);
424  } else {
425  line->setVisible(false);
426  }
427  }
428 }
429 
431  vpColVector &error)
432 {
433  if (m_pointCloudFace.empty()) {
434  L.resize(0, 0);
435  error.resize(0);
436  return;
437  }
438 
439  L.resize(getNbFeatures(), 6, false, false);
440  error.resize(getNbFeatures(), false);
441 
442  // Transform the plane equation for the current pose
445 
446  double nx = m_planeCamera.getA();
447  double ny = m_planeCamera.getB();
448  double nz = m_planeCamera.getC();
449  double D = m_planeCamera.getD();
450 
451  bool checkSSE2 = vpCPUFeatures::checkSSE2();
452 #if !USE_SSE
453  checkSSE2 = false;
454 #endif
455 
456  if (checkSSE2) {
457 #if USE_SSE
458  size_t cpt = 0;
459  if (getNbFeatures() >= 2) {
460  double *ptr_point_cloud = &m_pointCloudFace[0];
461  double *ptr_L = L.data;
462  double *ptr_error = error.data;
463 
464  const __m128d vnx = _mm_set1_pd(nx);
465  const __m128d vny = _mm_set1_pd(ny);
466  const __m128d vnz = _mm_set1_pd(nz);
467  const __m128d vd = _mm_set1_pd(D);
468 
469  double tmp_a1[2], tmp_a2[2], tmp_a3[2];
470 
471  for (; cpt <= m_pointCloudFace.size() - 6; cpt += 6, ptr_point_cloud += 6) {
472  const __m128d vx = _mm_loadu_pd(ptr_point_cloud);
473  const __m128d vy = _mm_loadu_pd(ptr_point_cloud + 2);
474  const __m128d vz = _mm_loadu_pd(ptr_point_cloud + 4);
475 
476  const __m128d va1 = _mm_sub_pd(_mm_mul_pd(vnz, vy), _mm_mul_pd(vny, vz));
477  const __m128d va2 = _mm_sub_pd(_mm_mul_pd(vnx, vz), _mm_mul_pd(vnz, vx));
478  const __m128d va3 = _mm_sub_pd(_mm_mul_pd(vny, vx), _mm_mul_pd(vnx, vy));
479 
480  _mm_storeu_pd(tmp_a1, va1);
481  _mm_storeu_pd(tmp_a2, va2);
482  _mm_storeu_pd(tmp_a3, va3);
483 
484  *ptr_L = nx;
485  ptr_L++;
486  *ptr_L = ny;
487  ptr_L++;
488  *ptr_L = nz;
489  ptr_L++;
490  *ptr_L = tmp_a1[0];
491  ptr_L++;
492  *ptr_L = tmp_a2[0];
493  ptr_L++;
494  *ptr_L = tmp_a3[0];
495  ptr_L++;
496 
497  *ptr_L = nx;
498  ptr_L++;
499  *ptr_L = ny;
500  ptr_L++;
501  *ptr_L = nz;
502  ptr_L++;
503  *ptr_L = tmp_a1[1];
504  ptr_L++;
505  *ptr_L = tmp_a2[1];
506  ptr_L++;
507  *ptr_L = tmp_a3[1];
508  ptr_L++;
509 
510  const __m128d verror =
511  _mm_add_pd(_mm_add_pd(vd, _mm_mul_pd(vnx, vx)), _mm_add_pd(_mm_mul_pd(vny, vy), _mm_mul_pd(vnz, vz)));
512  _mm_storeu_pd(ptr_error, verror);
513  ptr_error += 2;
514  }
515  }
516 
517  for (; cpt < m_pointCloudFace.size(); cpt += 3) {
518  double x = m_pointCloudFace[cpt];
519  double y = m_pointCloudFace[cpt + 1];
520  double z = m_pointCloudFace[cpt + 2];
521 
522  double _a1 = (nz * y) - (ny * z);
523  double _a2 = (nx * z) - (nz * x);
524  double _a3 = (ny * x) - (nx * y);
525 
526  // L
527  L[(unsigned int)(cpt / 3)][0] = nx;
528  L[(unsigned int)(cpt / 3)][1] = ny;
529  L[(unsigned int)(cpt / 3)][2] = nz;
530  L[(unsigned int)(cpt / 3)][3] = _a1;
531  L[(unsigned int)(cpt / 3)][4] = _a2;
532  L[(unsigned int)(cpt / 3)][5] = _a3;
533 
534  vpColVector normal(3);
535  normal[0] = nx;
536  normal[1] = ny;
537  normal[2] = nz;
538 
539  vpColVector pt(3);
540  pt[0] = x;
541  pt[1] = y;
542  pt[2] = z;
543 
544  // Error
545  error[(unsigned int)(cpt / 3)] = D + (normal.t() * pt);
546  }
547 #endif
548  } else {
549  vpColVector normal(3);
550  normal[0] = nx;
551  normal[1] = ny;
552  normal[2] = nz;
553  vpColVector pt(3);
554 
555  unsigned int idx = 0;
556  for (size_t i = 0; i < m_pointCloudFace.size(); i += 3, idx++) {
557  double x = m_pointCloudFace[i];
558  double y = m_pointCloudFace[i + 1];
559  double z = m_pointCloudFace[i + 2];
560 
561  double _a1 = (nz * y) - (ny * z);
562  double _a2 = (nx * z) - (nz * x);
563  double _a3 = (ny * x) - (nx * y);
564 
565  // L
566  L[idx][0] = nx;
567  L[idx][1] = ny;
568  L[idx][2] = nz;
569  L[idx][3] = _a1;
570  L[idx][4] = _a2;
571  L[idx][5] = _a3;
572 
573  pt[0] = x;
574  pt[1] = y;
575  pt[2] = z;
576  // Error
577  error[idx] = D + (normal.t() * pt);
578  }
579  }
580 }
581 
582 void vpMbtFaceDepthDense::computeROI(const vpHomogeneousMatrix &cMo, unsigned int width,
583  unsigned int height, std::vector<vpImagePoint> &roiPts
584 #if DEBUG_DISPLAY_DEPTH_DENSE
585  ,
586  std::vector<std::vector<vpImagePoint> > &roiPts_vec
587 #endif
588  ,
589  double &distanceToFace)
590 {
591  if (m_useScanLine || m_clippingFlag > 2)
592  m_cam.computeFov(width, height);
593 
594  if (m_useScanLine) {
595  for (std::vector<PolygonLine>::iterator it = m_polygonLines.begin(); it != m_polygonLines.end(); ++it) {
596  it->m_p1->changeFrame(cMo);
597  it->m_p2->changeFrame(cMo);
598 
599  vpImagePoint ip1, ip2;
600 
601  it->m_poly.changeFrame(cMo);
602  it->m_poly.computePolygonClipped(m_cam);
603 
604  if (it->m_poly.polyClipped.size() == 2 &&
605  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::NEAR_CLIPPING) == 0) &&
606  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::FAR_CLIPPING) == 0) &&
607  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::DOWN_CLIPPING) == 0) &&
608  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::UP_CLIPPING) == 0) &&
609  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::LEFT_CLIPPING) == 0) &&
610  ((it->m_poly.polyClipped[1].second & it->m_poly.polyClipped[0].second & vpPolygon3D::RIGHT_CLIPPING) == 0)) {
611 
612  std::vector<std::pair<vpPoint, vpPoint> > linesLst;
613  m_hiddenFace->computeScanLineQuery(it->m_poly.polyClipped[0].first, it->m_poly.polyClipped[1].first, linesLst,
614  true);
615 
616  vpPoint faceCentroid;
617 
618  for (unsigned int i = 0; i < linesLst.size(); i++) {
619  linesLst[i].first.project();
620  linesLst[i].second.project();
621 
622  vpMeterPixelConversion::convertPoint(m_cam, linesLst[i].first.get_x(), linesLst[i].first.get_y(), ip1);
623  vpMeterPixelConversion::convertPoint(m_cam, linesLst[i].second.get_x(), linesLst[i].second.get_y(), ip2);
624 
625  it->m_imPt1 = ip1;
626  it->m_imPt2 = ip2;
627 
628  roiPts.push_back(ip1);
629  roiPts.push_back(ip2);
630 
631  faceCentroid.set_X(faceCentroid.get_X() + linesLst[i].first.get_X() + linesLst[i].second.get_X());
632  faceCentroid.set_Y(faceCentroid.get_Y() + linesLst[i].first.get_Y() + linesLst[i].second.get_Y());
633  faceCentroid.set_Z(faceCentroid.get_Z() + linesLst[i].first.get_Z() + linesLst[i].second.get_Z());
634 
635 #if DEBUG_DISPLAY_DEPTH_DENSE
636  std::vector<vpImagePoint> roiPts_;
637  roiPts_.push_back(ip1);
638  roiPts_.push_back(ip2);
639  roiPts_vec.push_back(roiPts_);
640 #endif
641  }
642 
643  if (linesLst.empty()) {
644  distanceToFace = std::numeric_limits<double>::max();
645  } else {
646  faceCentroid.set_X(faceCentroid.get_X() / (2 * linesLst.size()));
647  faceCentroid.set_Y(faceCentroid.get_Y() / (2 * linesLst.size()));
648  faceCentroid.set_Z(faceCentroid.get_Z() / (2 * linesLst.size()));
649 
650  distanceToFace =
651  sqrt(faceCentroid.get_X() * faceCentroid.get_X() + faceCentroid.get_Y() * faceCentroid.get_Y() +
652  faceCentroid.get_Z() * faceCentroid.get_Z());
653  }
654  }
655  }
656  } else {
657  // Get polygon clipped
658  m_polygon->getRoiClipped(m_cam, roiPts, cMo);
659 
660  // Get 3D polygon clipped
661  std::vector<vpPoint> polygonsClipped;
662  m_polygon->getPolygonClipped(polygonsClipped);
663 
664  if (polygonsClipped.empty()) {
665  distanceToFace = std::numeric_limits<double>::max();
666  } else {
667  vpPoint faceCentroid;
668 
669  for (size_t i = 0; i < polygonsClipped.size(); i++) {
670  faceCentroid.set_X(faceCentroid.get_X() + polygonsClipped[i].get_X());
671  faceCentroid.set_Y(faceCentroid.get_Y() + polygonsClipped[i].get_Y());
672  faceCentroid.set_Z(faceCentroid.get_Z() + polygonsClipped[i].get_Z());
673  }
674 
675  faceCentroid.set_X(faceCentroid.get_X() / polygonsClipped.size());
676  faceCentroid.set_Y(faceCentroid.get_Y() / polygonsClipped.size());
677  faceCentroid.set_Z(faceCentroid.get_Z() / polygonsClipped.size());
678 
679  distanceToFace = sqrt(faceCentroid.get_X() * faceCentroid.get_X() + faceCentroid.get_Y() * faceCentroid.get_Y() +
680  faceCentroid.get_Z() * faceCentroid.get_Z());
681  }
682 
683 #if DEBUG_DISPLAY_DEPTH_DENSE
684  roiPts_vec.push_back(roiPts);
685 #endif
686  }
687 }
688 
690  const vpCameraParameters &cam, const vpColor &col, unsigned int thickness,
691  bool displayFullModel)
692 {
693  std::vector<std::vector<double> > models = getModelForDisplay(I.getWidth(), I.getHeight(), cMo, cam, displayFullModel);
694 
695  for (size_t i = 0; i < models.size(); i++) {
696  vpImagePoint ip1(models[i][1], models[i][2]);
697  vpImagePoint ip2(models[i][3], models[i][4]);
698  vpDisplay::displayLine(I, ip1, ip2, col, thickness);
699  }
700 }
701 
703  const vpCameraParameters &cam, const vpColor &col, unsigned int thickness,
704  bool displayFullModel)
705 {
706  std::vector<std::vector<double> > models = getModelForDisplay(I.getWidth(), I.getHeight(), cMo, cam, displayFullModel);
707 
708  for (size_t i = 0; i < models.size(); i++) {
709  vpImagePoint ip1(models[i][1], models[i][2]);
710  vpImagePoint ip2(models[i][3], models[i][4]);
711  vpDisplay::displayLine(I, ip1, ip2, col, thickness);
712  }
713 }
714 
716  const vpCameraParameters & /*cam*/, const double /*scale*/,
717  const unsigned int /*thickness*/)
718 {
719 }
720 
722  const vpCameraParameters & /*cam*/, const double /*scale*/,
723  const unsigned int /*thickness*/)
724 {
725 }
726 
738 std::vector<std::vector<double> > vpMbtFaceDepthDense::getModelForDisplay(unsigned int width, unsigned int height,
739  const vpHomogeneousMatrix &cMo,
740  const vpCameraParameters &cam,
741  bool displayFullModel)
742 {
743  std::vector<std::vector<double> > models;
744 
745  if ((m_polygon->isVisible() && m_isTrackedDepthDenseFace) || displayFullModel) {
747 
748  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
749  ++it) {
750  vpMbtDistanceLine *line = *it;
751  std::vector<std::vector<double> > lineModels = line->getModelForDisplay(width, height, cMo, cam, displayFullModel);
752  models.insert(models.end(), lineModels.begin(), lineModels.end());
753  }
754  }
755 
756  return models;
757 }
758 
768 bool vpMbtFaceDepthDense::samePoint(const vpPoint &P1, const vpPoint &P2) const
769 {
770  double dx = fabs(P1.get_oX() - P2.get_oX());
771  double dy = fabs(P1.get_oY() - P2.get_oY());
772  double dz = fabs(P1.get_oZ() - P2.get_oZ());
773 
774  if (dx <= std::numeric_limits<double>::epsilon() && dy <= std::numeric_limits<double>::epsilon() &&
775  dz <= std::numeric_limits<double>::epsilon())
776  return true;
777  else
778  return false;
779 }
780 
782 {
783  m_cam = camera;
784 
785  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
786  ++it) {
787  (*it)->setCameraParameters(camera);
788  }
789 }
790 
792 {
793  m_useScanLine = v;
794 
795  for (std::vector<vpMbtDistanceLine *>::const_iterator it = m_listOfFaceLines.begin(); it != m_listOfFaceLines.end();
796  ++it) {
797  (*it)->useScanLine = v;
798  }
799 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:156
double m_distNearClip
Distance for near clipping.
double getTop() const
Definition: vpRect.h:193
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
void setScanLineVisibilityTest(bool v)
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:449
Implements a 3D polygon with render functionnalities like clipping.
Definition: vpPolygon3D.h:59
bool samePoint(const vpPoint &P1, const vpPoint &P2) const
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:304
void setVisible(bool _isvisible)
bool isVisible(unsigned int i)
int getIndex() const
Definition: vpMbtPolygon.h:101
std::vector< double > m_pointCloudFace
List of depth points inside the face.
vpCameraParameters m_cam
Camera intrinsic parameters.
Implementation of an homogeneous matrix and operations on such kind of matrices.
int m_depthDenseFilteringMethod
Method to use to consider or not the face.
std::list< int > Lindex_polygon
Index of the faces which contain the line.
void setFarClippingDistance(const double &dist)
Definition: vpPolygon3D.h:194
void setCameraParameters(const vpCameraParameters &camera)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class to define RGB colors available for display functionnalities.
Definition: vpColor.h:157
void setLeft(double pos)
Definition: vpRect.h:322
bool m_useScanLine
Scan line visibility.
void displayFeature(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double scale=0.05, unsigned int thickness=1)
vpPoint * p1
The first extremity.
vpMbScanLine & getMbScanLineRenderer()
Manage the line of a polygon used in the model-based tracker.
vpRowVector t() const
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:483
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:145
double m_depthDenseFilteringMinDist
Minimum distance threshold.
vpMbtPolygon & getPolygon()
bool m_isTrackedDepthDenseFace
Flag to define if the face should be tracked or not.
void addLine(vpPoint &p1, vpPoint &p2, vpMbHiddenFaces< vpMbtPolygon > *const faces, int polygon=-1, std::string name="")
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:447
double getRight() const
Definition: vpRect.h:180
vpRect getBoundingBox() const
Definition: vpPolygon.h:177
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
bool m_isVisible
Visibility flag.
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:479
vpMbtPolygon * m_polygon
Polygon defining the face.
vpPlane m_planeObject
Plane equation described in the object frame.
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
void setTop(double pos)
Definition: vpRect.h:358
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
double getWidth() const
Definition: vpRect.h:228
double getD() const
Definition: vpPlane.h:108
unsigned int m_clippingFlag
Flags specifying which clipping to used.
vpPoint * p2
The second extremity.
void changeFrame(const vpHomogeneousMatrix &cMo)
Definition: vpPlane.cpp:354
void computeInteractionMatrixAndResidu(const vpHomogeneousMatrix &cMo, vpMatrix &L, vpColVector &error)
VISP_EXPORT bool checkSSE2()
double getB() const
Definition: vpPlane.h:104
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
Definition: vpPolygon.cpp:309
double m_distFarClip
Distance for near clipping.
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
void getPolygonClipped(std::vector< std::pair< vpPoint, unsigned int > > &poly)
Generic class defining intrinsic camera parameters.
double m_depthDenseFilteringOccupancyRatio
Ratio between available depth points and theoretical number of points.
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:451
double getLeft() const
Definition: vpRect.h:174
std::vector< vpMbtDistanceLine * > m_listOfFaceLines
unsigned int getNbFeatures() const
virtual bool isVisible(const vpHomogeneousMatrix &cMo, double alpha, const bool &modulo=false, const vpCameraParameters &cam=vpCameraParameters(), unsigned int width=0, unsigned int height=0)
double m_depthDenseFilteringMaxDist
Maximum distance threshold.
void computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height, std::vector< vpImagePoint > &roiPts, double &distanceToFace)
void setClipping(const unsigned int &flags)
Definition: vpPolygon3D.h:187
double getA() const
Definition: vpPlane.h:102
void setName(const std::string &line_name)
void setCameraParameters(const vpCameraParameters &camera)
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:438
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
unsigned int getHeight() const
Definition: vpImage.h:188
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:481
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
static bool inMask(const vpImage< bool > *mask, unsigned int i, unsigned int j)
void setRight(double pos)
Definition: vpRect.h:349
bool computeDesiredFeatures(const vpHomogeneousMatrix &cMo, const pcl::PointCloud< pcl::PointXYZ >::ConstPtr &point_cloud, unsigned int stepX, unsigned int stepY, const vpImage< bool > *mask=NULL)
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
double getHeight() const
Definition: vpRect.h:167
double getC() const
Definition: vpPlane.h:106
std::vector< PolygonLine > m_polygonLines
Polygon lines used for scan-line visibility.
void addPolygon(const int &index)
void setNearClippingDistance(const double &dist)
Definition: vpPolygon3D.h:207
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:442
vpMbHiddenFaces< vpMbtPolygon > * m_hiddenFace
Pointer to the list of faces.
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:440
unsigned int getWidth() const
Definition: vpImage.h:246
double getBottom() const
Definition: vpRect.h:98
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
void setBottom(double pos)
Definition: vpRect.h:289
bool useScanLine
Use scanline rendering.
void buildFrom(vpPoint &_p1, vpPoint &_p2)
void setIndex(unsigned int i)
void computeFov(const unsigned int &w, const unsigned int &h)