36 #include <visp3/core/vpCPUFeatures.h> 37 #include <visp3/mbt/vpMbtFaceDepthNormal.h> 38 #include <visp3/mbt/vpMbtTukeyEstimator.h> 41 #include <pcl/common/centroid.h> 42 #include <pcl/filters/extract_indices.h> 43 #include <pcl/segmentation/sac_segmentation.h> 46 #if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2) 47 #include <emmintrin.h> 48 #define VISP_HAVE_SSE2 1 51 #define USE_SSE_CODE 1 52 #if VISP_HAVE_SSE2 && USE_SSE_CODE 59 : m_cam(), m_clippingFlag(
vpPolygon3D::NO_CLIPPING), m_distFarClip(100), m_distNearClip(0.001), m_hiddenFace(NULL),
60 m_planeObject(), m_polygon(NULL), m_useScanLine(false), m_faceActivated(false),
61 m_faceCentroidMethod(GEOMETRIC_CENTROID), m_faceDesiredCentroid(), m_faceDesiredNormal(),
62 m_featureEstimationMethod(ROBUST_FEATURE_ESTIMATION), m_isTrackedDepthNormalFace(true), m_isVisible(false),
63 m_listOfFaceLines(), m_planeCamera(),
64 m_pclPlaneEstimationMethod(2),
65 m_pclPlaneEstimationRansacMaxIter(200), m_pclPlaneEstimationRansacThreshold(0.001), m_polygonLines()
94 PolygonLine polygon_line;
97 polygon_line.m_poly.setNbPoint(2);
98 polygon_line.m_poly.addPoint(0, P1);
99 polygon_line.m_poly.addPoint(1, P2);
105 polygon_line.m_p1 = &polygon_line.m_poly.p[0];
106 polygon_line.m_p2 = &polygon_line.m_poly.p[1];
111 bool already_here =
false;
153 const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &point_cloud,
156 #
if DEBUG_DISPLAY_DEPTH_NORMAL
159 std::vector<std::vector<vpImagePoint> > &roiPts_vec
166 if (width == 0 || height == 0)
169 std::vector<vpImagePoint> roiPts;
173 #
if DEBUG_DISPLAY_DEPTH_NORMAL
179 if (roiPts.size() <= 2) {
181 std::cerr <<
"Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
189 unsigned int top = (
unsigned int)std::max(0.0, bb.
getTop());
190 unsigned int bottom = (
unsigned int)std::min((
double)height, std::max(0.0, bb.
getBottom()));
191 unsigned int left = (
unsigned int)std::max(0.0, bb.
getLeft());
192 unsigned int right = (
unsigned int)std::min((
double)width, std::max(0.0, bb.
getRight()));
200 pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_face(
new pcl::PointCloud<pcl::PointXYZ>);
201 std::vector<double> point_cloud_face_vec, point_cloud_face_custom;
217 double prev_x, prev_y, prev_z;
220 double x = 0.0, y = 0.0;
221 for (
unsigned int i = top; i < bottom; i += stepY) {
222 for (
unsigned int j = left; j < right; j += stepX) {
223 if (
vpMeTracker::inMask(mask, i, j) && pcl::isFinite((*point_cloud)(j, i)) && (*point_cloud)(j, i).z > 0 &&
224 (
m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
225 j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
230 point_cloud_face->push_back((*point_cloud)(j, i));
233 point_cloud_face_vec.push_back((*point_cloud)(j, i).x);
234 point_cloud_face_vec.push_back((*point_cloud)(j, i).y);
235 point_cloud_face_vec.push_back((*point_cloud)(j, i).z);
247 prev_z = (*point_cloud)(j, i).z;
250 point_cloud_face_custom.push_back(prev_x);
251 point_cloud_face_custom.push_back(x);
253 point_cloud_face_custom.push_back(prev_y);
254 point_cloud_face_custom.push_back(y);
256 point_cloud_face_custom.push_back(prev_z);
257 point_cloud_face_custom.push_back((*point_cloud)(j, i).z);
261 point_cloud_face_custom.push_back(x);
262 point_cloud_face_custom.push_back(y);
263 point_cloud_face_custom.push_back((*point_cloud)(j, i).z);
268 #if DEBUG_DISPLAY_DEPTH_NORMAL 269 debugImage[i][j] = 255;
276 if (checkSSE2 && push) {
277 point_cloud_face_custom.push_back(prev_x);
278 point_cloud_face_custom.push_back(prev_y);
279 point_cloud_face_custom.push_back(prev_z);
283 if (point_cloud_face->empty() && point_cloud_face_custom.empty() && point_cloud_face_vec.empty()) {
298 desired_normal, centroid_point);
313 const std::vector<vpColVector> &point_cloud,
316 #
if DEBUG_DISPLAY_DEPTH_NORMAL
319 std::vector<std::vector<vpImagePoint> > &roiPts_vec
326 if (width == 0 || height == 0)
329 std::vector<vpImagePoint> roiPts;
333 #
if DEBUG_DISPLAY_DEPTH_NORMAL
339 if (roiPts.size() <= 2) {
341 std::cerr <<
"Error: roiPts.size() <= 2 in computeDesiredFeatures" << std::endl;
349 unsigned int top = (
unsigned int)std::max(0.0, bb.
getTop());
350 unsigned int bottom = (
unsigned int)std::min((
double)height, std::max(0.0, bb.
getBottom()));
351 unsigned int left = (
unsigned int)std::max(0.0, bb.
getLeft());
352 unsigned int right = (
unsigned int)std::min((
double)width, std::max(0.0, bb.
getRight()));
360 std::vector<double> point_cloud_face, point_cloud_face_custom;
372 double prev_x, prev_y, prev_z;
375 double x = 0.0, y = 0.0;
376 for (
unsigned int i = top; i < bottom; i += stepY) {
377 for (
unsigned int j = left; j < right; j += stepX) {
379 (
m_useScanLine ? (i < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
380 j < m_hiddenFace->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
384 point_cloud_face.push_back(point_cloud[i * width + j][0]);
385 point_cloud_face.push_back(point_cloud[i * width + j][1]);
386 point_cloud_face.push_back(point_cloud[i * width + j][2]);
398 prev_z = point_cloud[i * width + j][2];
401 point_cloud_face_custom.push_back(prev_x);
402 point_cloud_face_custom.push_back(x);
404 point_cloud_face_custom.push_back(prev_y);
405 point_cloud_face_custom.push_back(y);
407 point_cloud_face_custom.push_back(prev_z);
408 point_cloud_face_custom.push_back(point_cloud[i * width + j][2]);
412 point_cloud_face_custom.push_back(x);
413 point_cloud_face_custom.push_back(y);
414 point_cloud_face_custom.push_back(point_cloud[i * width + j][2]);
418 #if DEBUG_DISPLAY_DEPTH_NORMAL 419 debugImage[i][j] = 255;
426 if (checkSSE2 && push) {
427 point_cloud_face_custom.push_back(prev_x);
428 point_cloud_face_custom.push_back(prev_y);
429 point_cloud_face_custom.push_back(prev_z);
433 if (point_cloud_face.empty() && point_cloud_face_custom.empty()) {
442 pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_face_pcl(
new pcl::PointCloud<pcl::PointXYZ>);
443 point_cloud_face_pcl->reserve(point_cloud_face.size() / 3);
445 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
446 point_cloud_face_pcl->push_back(
447 pcl::PointXYZ(point_cloud_face[3 * i], point_cloud_face[3 * i + 1], point_cloud_face[3 * i + 2]));
457 desired_normal, centroid_point);
476 pcl::ModelCoefficients::Ptr coefficients(
new pcl::ModelCoefficients);
477 pcl::PointIndices::Ptr inliers(
new pcl::PointIndices);
479 pcl::SACSegmentation<pcl::PointXYZ> seg;
481 seg.setOptimizeCoefficients(
true);
483 seg.setModelType(pcl::SACMODEL_PLANE);
488 seg.setInputCloud(point_cloud_face);
489 seg.segment(*inliers, *coefficients);
491 pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_face_extracted(
new pcl::PointCloud<pcl::PointXYZ>);
493 pcl::ExtractIndices<pcl::PointXYZ> extract;
496 extract.setInputCloud(point_cloud_face);
497 extract.setIndices(inliers);
498 extract.setNegative(
false);
499 extract.filter(*point_cloud_face_extracted);
501 #if PCL_VERSION_COMPARE(>=, 1, 7, 2) 502 pcl::PointXYZ centroid_point_pcl;
503 if (pcl::computeCentroid(*point_cloud_face_extracted, centroid_point_pcl)) {
504 pcl::PointXYZ face_normal;
506 centroid_point_pcl, face_normal);
508 desired_features.
resize(3,
false);
509 desired_features[0] = -coefficients->values[0] / coefficients->values[3];
510 desired_features[1] = -coefficients->values[1] / coefficients->values[3];
511 desired_features[2] = -coefficients->values[2] / coefficients->values[3];
513 desired_normal[0] = face_normal.x;
514 desired_normal[1] = face_normal.y;
515 desired_normal[2] = face_normal.z;
517 centroid_point[0] = centroid_point_pcl.x;
518 centroid_point[1] = centroid_point_pcl.y;
519 centroid_point[2] = centroid_point_pcl.z;
521 std::cerr <<
"Cannot compute centroid!" << std::endl;
525 std::cerr <<
"Cannot compute centroid using PCL " << PCL_VERSION_PRETTY <<
"!" << std::endl;
528 }
catch (
const pcl::PCLException &e) {
529 std::cerr <<
"Catch a PCL exception: " << e.what() << std::endl;
538 const std::vector<double> &point_cloud_face,
544 std::vector<double> weights;
549 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
550 centroid_point[0] += weights[i] * point_cloud_face[3 * i];
551 centroid_point[1] += weights[i] * point_cloud_face[3 * i + 1];
552 centroid_point[2] += weights[i] * point_cloud_face[3 * i + 2];
557 centroid_point[0] /= den;
558 centroid_point[1] /= den;
559 centroid_point[2] /= den;
572 desired_features.
resize(3,
false);
573 desired_features[0] = -plane_equation_SVD[0] / plane_equation_SVD[3];
574 desired_features[1] = -plane_equation_SVD[1] / plane_equation_SVD[3];
575 desired_features[2] = -plane_equation_SVD[2] / plane_equation_SVD[3];
587 centroid_cam[0] = centroid_point[0];
588 centroid_cam[1] = centroid_point[1];
589 centroid_cam[2] = centroid_point[2];
597 face_normal_cam[0] = desired_normal[0];
598 face_normal_cam[1] = desired_normal[1];
599 face_normal_cam[2] = desired_normal[2];
600 face_normal_cam[3] = 1;
608 if (points_.empty()) {
612 if (points_.size() < 2) {
613 centroid = points_[0];
617 std::vector<vpPoint> points = points_;
618 points.push_back(points_.front());
620 double A1 = 0.0, A2 = 0.0, c_x1 = 0.0, c_x2 = 0.0, c_y = 0.0, c_z = 0.0;
622 for (
size_t i = 0; i < points.size() - 1; i++) {
624 c_x1 += (points[i].get_X() + points[i + 1].get_X()) *
625 (points[i].get_X() * points[i + 1].get_Y() - points[i + 1].get_X() * points[i].get_Y());
626 c_y += (points[i].get_Y() + points[i + 1].get_Y()) *
627 (points[i].get_X() * points[i + 1].get_Y() - points[i + 1].get_X() * points[i].get_Y());
628 A1 += points[i].
get_X() * points[i + 1].get_Y() - points[i + 1].get_X() * points[i].get_Y();
631 c_x2 += (points[i].get_X() + points[i + 1].get_X()) *
632 (points[i].get_X() * points[i + 1].get_Z() - points[i + 1].get_X() * points[i].get_Z());
633 c_z += (points[i].get_Z() + points[i + 1].get_Z()) *
634 (points[i].get_X() * points[i + 1].get_Z() - points[i + 1].get_X() * points[i].get_Z());
635 A2 += points[i].get_X() * points[i + 1].get_Z() - points[i + 1].get_X() * points[i].get_Z();
644 centroid.
set_X(c_x1);
646 centroid.
set_X(c_x2);
656 unsigned int height, std::vector<vpImagePoint> &roiPts
657 #
if DEBUG_DISPLAY_DEPTH_NORMAL
659 std::vector<std::vector<vpImagePoint> > &roiPts_vec
668 it->m_p1->changeFrame(cMo);
669 it->m_p2->changeFrame(cMo);
673 it->m_poly.changeFrame(cMo);
674 it->m_poly.computePolygonClipped(
m_cam);
676 if (it->m_poly.polyClipped.size() == 2 &&
684 std::vector<std::pair<vpPoint, vpPoint> > linesLst;
688 for (
unsigned int i = 0; i < linesLst.size(); i++) {
689 linesLst[i].first.project();
690 linesLst[i].second.project();
698 roiPts.push_back(ip1);
699 roiPts.push_back(ip2);
701 #if DEBUG_DISPLAY_DEPTH_NORMAL 702 std::vector<vpImagePoint> roiPts_;
703 roiPts_.push_back(ip1);
704 roiPts_.push_back(ip2);
705 roiPts_vec.push_back(roiPts_);
714 #if DEBUG_DISPLAY_DEPTH_NORMAL 715 roiPts_vec.push_back(roiPts);
729 bool isvisible =
false;
733 int index = *itindex;
766 std::vector<vpImagePoint> roiPts;
769 std::vector<vpPoint> polyPts;
777 e4[0] = -centroid.
get_X();
778 e4[1] = -centroid.
get_Y();
779 e4[2] = -centroid.
get_Z();
782 double centroid_x = 0.0;
783 double centroid_y = 0.0;
784 double centroid_z = 0.0;
786 for (
size_t i = 0; i < polyPts.size(); i++) {
787 centroid_x += polyPts[i].get_X();
788 centroid_y += polyPts[i].get_Y();
789 centroid_z += polyPts[i].get_Z();
792 centroid_x /= polyPts.
size();
793 centroid_y /= polyPts.size();
794 centroid_z /= polyPts.size();
801 centroid.
set_X(centroid_x);
802 centroid.
set_Y(centroid_y);
803 centroid.
set_Z(centroid_z);
806 correct_normal.
resize(3,
false);
808 if (angle < M_PI_2) {
809 correct_normal = faceNormal;
811 correct_normal[0] = -faceNormal[0];
812 correct_normal[1] = -faceNormal[1];
813 correct_normal[2] = -faceNormal[2];
819 const pcl::PointXYZ ¢roid_point, pcl::PointXYZ &face_normal)
828 e4[0] = -centroid_point.x;
829 e4[1] = -centroid_point.y;
830 e4[2] = -centroid_point.z;
834 if (angle < M_PI_2) {
835 face_normal = pcl::PointXYZ(faceNormal[0], faceNormal[1], faceNormal[2]);
837 face_normal = pcl::PointXYZ(-faceNormal[0], -faceNormal[1], -faceNormal[2]);
845 face_normal.
resize(3,
false);
855 if (angle >= M_PI_2) {
856 face_normal[0] = -face_normal[0];
857 face_normal[1] = -face_normal[1];
858 face_normal[2] = -face_normal[2];
864 L.
resize(3, 6,
false,
false);
877 features.
resize(3,
false);
878 features[0] = -ux / D;
879 features[1] = -uy / D;
880 features[2] = -uz / D;
883 L[0][0] = ux * ux / D2;
884 L[0][1] = ux * uy / D2;
885 L[0][2] = ux * uz / D2;
891 L[1][0] = ux * uy / D2;
892 L[1][1] = uy * uy / D2;
893 L[1][2] = uy * uz / D2;
899 L[2][0] = ux * uz / D2;
900 L[2][1] = uy * uz / D2;
901 L[2][2] = uz * uz / D2;
909 bool displayFullModel)
913 for (
size_t i = 0; i < models.size(); i++) {
922 bool displayFullModel)
926 for (
size_t i = 0; i < models.size(); i++) {
935 unsigned int thickness)
951 pt_extremity.
set_X(pt_centroid.
get_X() + pt_normal.get_X() * scale);
952 pt_extremity.
set_Y(pt_centroid.
get_Y() + pt_normal.get_Y() * scale);
953 pt_extremity.
set_Z(pt_centroid.
get_Z() + pt_normal.get_Z() * scale);
978 pt_extremity.
set_X(pt_centroid.
get_X() + correct_normal[0] * scale);
979 pt_extremity.
set_Y(pt_centroid.
get_Y() + correct_normal[1] * scale);
980 pt_extremity.
set_Z(pt_centroid.
get_Z() + correct_normal[2] * scale);
991 unsigned int thickness)
1004 pt_normal.project();
1007 pt_extremity.
set_X(pt_centroid.
get_X() + pt_normal.get_X() * scale);
1008 pt_extremity.
set_Y(pt_centroid.
get_Y() + pt_normal.get_Y() * scale);
1009 pt_extremity.
set_Z(pt_centroid.
get_Z() + pt_normal.get_Z() * scale);
1034 pt_extremity.
set_X(pt_centroid.
get_X() + correct_normal[0] * scale);
1035 pt_extremity.
set_Y(pt_centroid.
get_Y() + correct_normal[1] * scale);
1036 pt_extremity.
set_Z(pt_centroid.
get_Z() + correct_normal[2] * scale);
1048 vpMbtTukeyEstimator<double> tukey_robust;
1049 std::vector<double> residues(point_cloud_face.size() / 3);
1051 w.resize(point_cloud_face.size() / 3, 1.0);
1053 unsigned int max_iter = 30, iter = 0;
1054 double error = 0.0, prev_error = -1.0;
1055 double A = 0.0, B = 0.0, C = 0.0;
1057 Mat33<double> ATA_3x3;
1066 while (std::fabs(error - prev_error) > 1e-6 && (iter < max_iter)) {
1083 if (point_cloud_face.size() / 3 >= 2) {
1084 const double *ptr_point_cloud = &point_cloud_face[0];
1085 const __m128d vA = _mm_set1_pd(A);
1086 const __m128d vB = _mm_set1_pd(B);
1087 const __m128d vC = _mm_set1_pd(C);
1088 const __m128d vones = _mm_set1_pd(1.0);
1090 double *ptr_residues = &residues[0];
1092 for (; cpt <= point_cloud_face.size() - 6; cpt += 6, ptr_point_cloud += 6, ptr_residues += 2) {
1093 const __m128d vxi = _mm_loadu_pd(ptr_point_cloud);
1094 const __m128d vyi = _mm_loadu_pd(ptr_point_cloud + 2);
1095 const __m128d vZi = _mm_loadu_pd(ptr_point_cloud + 4);
1096 const __m128d vinvZi = _mm_div_pd(vones, vZi);
1099 _mm_add_pd(_mm_add_pd(_mm_mul_pd(vA, vxi), _mm_mul_pd(vB, vyi)), _mm_sub_pd(vC, vinvZi));
1100 _mm_storeu_pd(ptr_residues, tmp);
1104 for (; cpt < point_cloud_face.size(); cpt += 3) {
1105 double xi = point_cloud_face[cpt];
1106 double yi = point_cloud_face[cpt + 1];
1107 double Zi = point_cloud_face[cpt + 2];
1109 residues[cpt / 3] = (A * xi + B * yi + C - 1 / Zi);
1113 tukey_robust.MEstimator(residues, w, 1e-2);
1115 __m128d vsum_wi2_xi2 = _mm_setzero_pd();
1116 __m128d vsum_wi2_yi2 = _mm_setzero_pd();
1117 __m128d vsum_wi2 = _mm_setzero_pd();
1118 __m128d vsum_wi2_xi_yi = _mm_setzero_pd();
1119 __m128d vsum_wi2_xi = _mm_setzero_pd();
1120 __m128d vsum_wi2_yi = _mm_setzero_pd();
1122 __m128d vsum_wi2_xi_Zi = _mm_setzero_pd();
1123 __m128d vsum_wi2_yi_Zi = _mm_setzero_pd();
1124 __m128d vsum_wi2_Zi = _mm_setzero_pd();
1128 if (point_cloud_face.size() / 3 >= 2) {
1129 const double *ptr_point_cloud = &point_cloud_face[0];
1130 double *ptr_w = &w[0];
1132 const __m128d vones = _mm_set1_pd(1.0);
1134 for (; cpt <= point_cloud_face.size() - 6; cpt += 6, ptr_point_cloud += 6, ptr_w += 2) {
1135 const __m128d vwi2 = _mm_mul_pd(_mm_loadu_pd(ptr_w), _mm_loadu_pd(ptr_w));
1137 const __m128d vxi = _mm_loadu_pd(ptr_point_cloud);
1138 const __m128d vyi = _mm_loadu_pd(ptr_point_cloud + 2);
1139 const __m128d vZi = _mm_loadu_pd(ptr_point_cloud + 4);
1140 const __m128d vinvZi = _mm_div_pd(vones, vZi);
1142 vsum_wi2_xi2 = _mm_add_pd(vsum_wi2_xi2, _mm_mul_pd(vwi2, _mm_mul_pd(vxi, vxi)));
1143 vsum_wi2_yi2 = _mm_add_pd(vsum_wi2_yi2, _mm_mul_pd(vwi2, _mm_mul_pd(vyi, vyi)));
1144 vsum_wi2 = _mm_add_pd(vsum_wi2, vwi2);
1145 vsum_wi2_xi_yi = _mm_add_pd(vsum_wi2_xi_yi, _mm_mul_pd(vwi2, _mm_mul_pd(vxi, vyi)));
1146 vsum_wi2_xi = _mm_add_pd(vsum_wi2_xi, _mm_mul_pd(vwi2, vxi));
1147 vsum_wi2_yi = _mm_add_pd(vsum_wi2_yi, _mm_mul_pd(vwi2, vyi));
1149 const __m128d vwi2_invZi = _mm_mul_pd(vwi2, vinvZi);
1150 vsum_wi2_xi_Zi = _mm_add_pd(vsum_wi2_xi_Zi, _mm_mul_pd(vxi, vwi2_invZi));
1151 vsum_wi2_yi_Zi = _mm_add_pd(vsum_wi2_yi_Zi, _mm_mul_pd(vyi, vwi2_invZi));
1152 vsum_wi2_Zi = _mm_add_pd(vsum_wi2_Zi, vwi2_invZi);
1157 _mm_storeu_pd(vtmp, vsum_wi2_xi2);
1158 double sum_wi2_xi2 = vtmp[0] + vtmp[1];
1160 _mm_storeu_pd(vtmp, vsum_wi2_yi2);
1161 double sum_wi2_yi2 = vtmp[0] + vtmp[1];
1163 _mm_storeu_pd(vtmp, vsum_wi2);
1164 double sum_wi2 = vtmp[0] + vtmp[1];
1166 _mm_storeu_pd(vtmp, vsum_wi2_xi_yi);
1167 double sum_wi2_xi_yi = vtmp[0] + vtmp[1];
1169 _mm_storeu_pd(vtmp, vsum_wi2_xi);
1170 double sum_wi2_xi = vtmp[0] + vtmp[1];
1172 _mm_storeu_pd(vtmp, vsum_wi2_yi);
1173 double sum_wi2_yi = vtmp[0] + vtmp[1];
1175 _mm_storeu_pd(vtmp, vsum_wi2_xi_Zi);
1176 double sum_wi2_xi_Zi = vtmp[0] + vtmp[1];
1178 _mm_storeu_pd(vtmp, vsum_wi2_yi_Zi);
1179 double sum_wi2_yi_Zi = vtmp[0] + vtmp[1];
1181 _mm_storeu_pd(vtmp, vsum_wi2_Zi);
1182 double sum_wi2_Zi = vtmp[0] + vtmp[1];
1184 for (; cpt < point_cloud_face.size(); cpt += 3) {
1185 double wi2 = w[cpt / 3] * w[cpt / 3];
1187 double xi = point_cloud_face[cpt];
1188 double yi = point_cloud_face[cpt + 1];
1189 double Zi = point_cloud_face[cpt + 2];
1190 double invZi = 1.0 / Zi;
1192 sum_wi2_xi2 += wi2 * xi * xi;
1193 sum_wi2_yi2 += wi2 * yi * yi;
1195 sum_wi2_xi_yi += wi2 * xi * yi;
1196 sum_wi2_xi += wi2 * xi;
1197 sum_wi2_yi += wi2 * yi;
1199 sum_wi2_xi_Zi += wi2 * xi * invZi;
1200 sum_wi2_yi_Zi += wi2 * yi * invZi;
1201 sum_wi2_Zi += wi2 * invZi;
1204 ATA_3x3[0] = sum_wi2_xi2;
1205 ATA_3x3[1] = sum_wi2_xi_yi;
1206 ATA_3x3[2] = sum_wi2_xi;
1207 ATA_3x3[3] = sum_wi2_xi_yi;
1208 ATA_3x3[4] = sum_wi2_yi2;
1209 ATA_3x3[5] = sum_wi2_yi;
1210 ATA_3x3[6] = sum_wi2_xi;
1211 ATA_3x3[7] = sum_wi2_yi;
1212 ATA_3x3[8] = sum_wi2;
1214 Mat33<double> minv = ATA_3x3.inverse();
1216 A = minv[0] * sum_wi2_xi_Zi + minv[1] * sum_wi2_yi_Zi + minv[2] * sum_wi2_Zi;
1217 B = minv[3] * sum_wi2_xi_Zi + minv[4] * sum_wi2_yi_Zi + minv[5] * sum_wi2_Zi;
1218 C = minv[6] * sum_wi2_xi_Zi + minv[7] * sum_wi2_yi_Zi + minv[8] * sum_wi2_Zi;
1226 __m128d verror = _mm_set1_pd(0.0);
1227 if (point_cloud_face.size() / 3 >= 2) {
1228 const double *ptr_point_cloud = &point_cloud_face[0];
1229 const __m128d vA = _mm_set1_pd(A);
1230 const __m128d vB = _mm_set1_pd(B);
1231 const __m128d vC = _mm_set1_pd(C);
1232 const __m128d vones = _mm_set1_pd(1.0);
1234 double *ptr_residues = &residues[0];
1236 for (; cpt <= point_cloud_face.size() - 6; cpt += 6, ptr_point_cloud += 6, ptr_residues += 2) {
1237 const __m128d vxi = _mm_loadu_pd(ptr_point_cloud);
1238 const __m128d vyi = _mm_loadu_pd(ptr_point_cloud + 2);
1239 const __m128d vZi = _mm_loadu_pd(ptr_point_cloud + 4);
1240 const __m128d vinvZi = _mm_div_pd(vones, vZi);
1242 const __m128d tmp = _mm_add_pd(_mm_add_pd(_mm_mul_pd(vA, vxi), _mm_mul_pd(vB, vyi)), _mm_sub_pd(vC, vinvZi));
1243 verror = _mm_add_pd(verror, _mm_mul_pd(tmp, tmp));
1245 _mm_storeu_pd(ptr_residues, tmp);
1249 _mm_storeu_pd(vtmp, verror);
1250 error = vtmp[0] + vtmp[1];
1252 for (
size_t idx = cpt; idx < point_cloud_face.size(); idx += 3) {
1253 double xi = point_cloud_face[idx];
1254 double yi = point_cloud_face[idx + 1];
1255 double Zi = point_cloud_face[idx + 2];
1257 error +=
vpMath::sqr(A * xi + B * yi + C - 1 / Zi);
1258 residues[idx / 3] = (A * xi + B * yi + C - 1 / Zi);
1261 error /= point_cloud_face.size() / 3;
1267 while (std::fabs(error - prev_error) > 1e-6 && (iter < max_iter)) {
1283 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1284 double xi = point_cloud_face[3 * i];
1285 double yi = point_cloud_face[3 * i + 1];
1286 double Zi = point_cloud_face[3 * i + 2];
1288 residues[i] = (A * xi + B * yi + C - 1 / Zi);
1292 tukey_robust.MEstimator(residues, w, 1e-2);
1295 double sum_wi2_xi2 = 0.0, sum_wi2_yi2 = 0.0, sum_wi2 = 0.0;
1296 double sum_wi2_xi_yi = 0.0, sum_wi2_xi = 0.0, sum_wi2_yi = 0.0;
1298 double sum_wi2_xi_Zi = 0.0, sum_wi2_yi_Zi = 0.0, sum_wi2_Zi = 0.0;
1300 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1301 double wi2 = w[i] * w[i];
1303 double xi = point_cloud_face[3 * i];
1304 double yi = point_cloud_face[3 * i + 1];
1305 double Zi = point_cloud_face[3 * i + 2];
1306 double invZi = 1 / Zi;
1308 sum_wi2_xi2 += wi2 * xi * xi;
1309 sum_wi2_yi2 += wi2 * yi * yi;
1311 sum_wi2_xi_yi += wi2 * xi * yi;
1312 sum_wi2_xi += wi2 * xi;
1313 sum_wi2_yi += wi2 * yi;
1315 sum_wi2_xi_Zi += wi2 * xi * invZi;
1316 sum_wi2_yi_Zi += wi2 * yi * invZi;
1317 sum_wi2_Zi += wi2 * invZi;
1320 ATA_3x3[0] = sum_wi2_xi2;
1321 ATA_3x3[1] = sum_wi2_xi_yi;
1322 ATA_3x3[2] = sum_wi2_xi;
1323 ATA_3x3[3] = sum_wi2_xi_yi;
1324 ATA_3x3[4] = sum_wi2_yi2;
1325 ATA_3x3[5] = sum_wi2_yi;
1326 ATA_3x3[6] = sum_wi2_xi;
1327 ATA_3x3[7] = sum_wi2_yi;
1328 ATA_3x3[8] = sum_wi2;
1330 Mat33<double> minv = ATA_3x3.inverse();
1332 A = minv[0] * sum_wi2_xi_Zi + minv[1] * sum_wi2_yi_Zi + minv[2] * sum_wi2_Zi;
1333 B = minv[3] * sum_wi2_xi_Zi + minv[4] * sum_wi2_yi_Zi + minv[5] * sum_wi2_Zi;
1334 C = minv[6] * sum_wi2_xi_Zi + minv[7] * sum_wi2_yi_Zi + minv[8] * sum_wi2_Zi;
1340 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1341 double xi = point_cloud_face[3 * i];
1342 double yi = point_cloud_face[3 * i + 1];
1343 double Zi = point_cloud_face[3 * i + 2];
1345 error +=
vpMath::sqr(A * xi + B * yi + C - 1 / Zi);
1346 residues[i] = (A * xi + B * yi + C - 1 / Zi);
1349 error /= point_cloud_face.size() / 3;
1355 x_estimated.
resize(3,
false);
1365 unsigned int max_iter = 10;
1366 double prev_error = 1e3;
1367 double error = 1e3 - 1;
1369 std::vector<double> weights(point_cloud_face.size() / 3, 1.0);
1370 std::vector<double> residues(point_cloud_face.size() / 3);
1371 vpMatrix M((
unsigned int)(point_cloud_face.size() / 3), 3);
1372 vpMbtTukeyEstimator<double> tukey;
1375 for (
unsigned int iter = 0; iter < max_iter && std::fabs(error - prev_error) > 1e-6; iter++) {
1377 tukey.MEstimator(residues, weights, 1e-4);
1389 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1390 residues[i] = std::fabs(A * point_cloud_face[3 * i] + B * point_cloud_face[3 * i + 1] +
1391 C * point_cloud_face[3 * i + 2] + D) /
1392 sqrt(A * A + B * B + C * C);
1395 tukey.MEstimator(residues, weights, 1e-4);
1396 plane_equation_estimated.
resize(4,
false);
1400 double centroid_x = 0.0, centroid_y = 0.0, centroid_z = 0.0;
1401 double total_w = 0.0;
1403 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1404 centroid_x += weights[i] * point_cloud_face[3 * i];
1405 centroid_y += weights[i] * point_cloud_face[3 * i + 1];
1406 centroid_z += weights[i] * point_cloud_face[3 * i + 2];
1407 total_w += weights[i];
1410 centroid_x /= total_w;
1411 centroid_y /= total_w;
1412 centroid_z /= total_w;
1415 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1416 M[(
unsigned int)i][0] = weights[i] * (point_cloud_face[3 * i] - centroid_x);
1417 M[(
unsigned int)i][1] = weights[i] * (point_cloud_face[3 * i + 1] - centroid_y);
1418 M[(
unsigned int)i][2] = weights[i] * (point_cloud_face[3 * i + 2] - centroid_z);
1427 double smallestSv = W[0];
1428 unsigned int indexSmallestSv = 0;
1429 for (
unsigned int i = 1; i < W.
size(); i++) {
1430 if (W[i] < smallestSv) {
1432 indexSmallestSv = i;
1436 normal = V.
getCol(indexSmallestSv);
1439 double A = normal[0], B = normal[1], C = normal[2];
1440 double D = -(A * centroid_x + B * centroid_y + C * centroid_z);
1443 plane_equation_estimated[0] = A;
1444 plane_equation_estimated[1] = B;
1445 plane_equation_estimated[2] = C;
1446 plane_equation_estimated[3] = D;
1451 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1452 residues[i] = std::fabs(A * point_cloud_face[3 * i] + B * point_cloud_face[3 * i + 1] +
1453 C * point_cloud_face[3 * i + 2] + D) /
1454 sqrt(A * A + B * B + C * C);
1455 error += weights[i] * residues[i];
1461 tukey.MEstimator(residues, weights, 1e-4);
1464 centroid.
resize(3,
false);
1465 double total_w = 0.0;
1467 for (
size_t i = 0; i < point_cloud_face.size() / 3; i++) {
1468 centroid[0] += weights[i] * point_cloud_face[3 * i];
1469 centroid[1] += weights[i] * point_cloud_face[3 * i + 1];
1470 centroid[2] += weights[i] * point_cloud_face[3 * i + 2];
1471 total_w += weights[i];
1474 centroid[0] /= total_w;
1475 centroid[1] /= total_w;
1476 centroid[2] /= total_w;
1479 double A = normal[0], B = normal[1], C = normal[2];
1480 double D = -(A * centroid[0] + B * centroid[1] + C * centroid[2]);
1483 plane_equation_estimated[0] = A;
1484 plane_equation_estimated[1] = B;
1485 plane_equation_estimated[2] = C;
1486 plane_equation_estimated[3] = D;
1498 std::vector<std::vector<double> > features;
1511 pt_normal.project();
1514 pt_extremity.
set_X(pt_centroid.
get_X() + pt_normal.get_X() * scale);
1515 pt_extremity.
set_Y(pt_centroid.
get_Y() + pt_normal.get_Y() * scale);
1516 pt_extremity.
set_Z(pt_centroid.
get_Z() + pt_normal.get_Z() * scale);
1523 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 1524 std::vector<double> params = {2,
1525 im_centroid.
get_i(),
1526 im_centroid.
get_j(),
1527 im_extremity.
get_i(),
1528 im_extremity.
get_j()};
1530 std::vector<double> params;
1531 params.push_back(2);
1532 params.push_back(im_centroid.
get_i());
1533 params.push_back(im_centroid.
get_j());
1534 params.push_back(im_extremity.
get_i());
1535 params.push_back(im_extremity.
get_j());
1537 features.push_back(params);
1555 pt_extremity.
set_X(pt_centroid.
get_X() + correct_normal[0] * scale);
1556 pt_extremity.
set_Y(pt_centroid.
get_Y() + correct_normal[1] * scale);
1557 pt_extremity.
set_Z(pt_centroid.
get_Z() + correct_normal[2] * scale);
1563 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 1564 std::vector<double> params = {3,
1565 im_centroid.
get_i(),
1566 im_centroid.
get_j(),
1567 im_extremity.
get_i(),
1568 im_extremity.
get_j()};
1570 std::vector<double> params;
1571 params.push_back(3);
1572 params.push_back(im_centroid.
get_i());
1573 params.push_back(im_centroid.
get_j());
1574 params.push_back(im_extremity.
get_i());
1575 params.push_back(im_extremity.
get_j());
1577 features.push_back(params);
1598 bool displayFullModel)
1600 std::vector<std::vector<double> > models;
1608 std::vector<std::vector<double> > lineModels = line->
getModelForDisplay(width, height, cMo, cam, displayFullModel);
1609 models.insert(models.end(), lineModels.begin(), lineModels.end());
1631 if (dx <= std::numeric_limits<double>::epsilon() && dy <= std::numeric_limits<double>::epsilon() &&
1632 dz <= std::numeric_limits<double>::epsilon())
1644 (*it)->setCameraParameters(camera);
1654 (*it)->useScanLine = v;
vpFeatureEstimationType m_featureEstimationMethod
Method to estimate the desired features.
void svd(vpColVector &w, vpMatrix &V)
Used to indicate that a value is not in the allowed range.
Implementation of a matrix and operations on matrices.
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
Implements a 3D polygon with render functionnalities like clipping.
void computeVisibilityDisplay()
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
void setWorldCoordinates(double oX, double oY, double oZ)
void setVisible(bool _isvisible)
void buildFrom(vpPoint &_p1, vpPoint &_p2, vpUniRand &rand_gen)
bool isVisible(unsigned int i)
void addLine(vpPoint &p1, vpPoint &p2, vpMbHiddenFaces< vpMbtPolygon > *const faces, vpUniRand &rand_gen, int polygon=-1, std::string name="")
unsigned int getWidth() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
bool m_isTrackedDepthNormalFace
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
std::list< int > Lindex_polygon
Index of the faces which contain the line.
void setFarClippingDistance(const double &dist)
int m_pclPlaneEstimationMethod
PCL plane estimation method.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
unsigned int m_clippingFlag
Flags specifying which clipping to used.
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const
Class to define RGB colors available for display functionnalities.
int m_pclPlaneEstimationRansacMaxIter
PCL pane estimation max number of iterations.
vpPoint * p1
The first extremity.
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 ¢roid_point)
double get_oY() const
Get the point oY coordinate in the object frame.
error that can be emited by ViSP classes.
vpMbScanLine & getMbScanLineRenderer()
Manage the line of a polygon used in the model-based tracker.
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
unsigned int size() const
Return the number of elements of the 2D array.
double get_y() const
Get the point y coordinate in the image plane.
double m_distNearClip
Distance for near clipping.
vpMbtPolygon & getPolygon()
void computeROI(const vpHomogeneousMatrix &cMo, unsigned int width, unsigned int height, std::vector< vpImagePoint > &roiPts)
bool m_useScanLine
Scan line visibility.
std::vector< std::vector< double > > getFeaturesForDisplay(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double scale=0.05)
vpCameraParameters m_cam
Camera intrinsic parameters.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
bool computePolygonCentroid(const std::vector< vpPoint > &points, vpPoint ¢roid)
void setCameraParameters(const vpCameraParameters &camera)
void set_X(double cX)
Set the point cX coordinate in the camera frame.
bool m_faceActivated
True if the face should be considered by the tracker.
vpMbtPolygon * m_polygon
Polygon defining the face.
Defines a generic 2D polygon.
vpRect getBoundingBox() const
void computeNormalVisibility(double nx, double ny, double nz, const vpColVector ¢roid_point, vpColVector &face_normal)
vpColVector & normalize()
std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
void computeInteractionMatrix(const vpHomogeneousMatrix &cMo, vpMatrix &L, vpColVector &features)
void setScanLineVisibilityTest(bool v)
vpPoint * p2
The second extremity.
void changeFrame(const vpHomogeneousMatrix &cMo)
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static double sqr(double x)
bool computeDesiredFeaturesPCL(const pcl::PointCloud< pcl::PointXYZ >::ConstPtr &point_cloud_face, vpColVector &desired_features, vpColVector &desired_normal, vpColVector ¢roid_point)
VISP_EXPORT bool checkSSE2()
void getPolygonClipped(std::vector< std::pair< vpPoint, unsigned int > > &poly)
vpMbHiddenFaces< vpMbtPolygon > * m_hiddenFace
Pointer to the list of faces.
Generic class defining intrinsic camera parameters.
double get_oZ() const
Get the point oZ coordinate in the object frame.
double get_x() const
Get the point x coordinate in the image plane.
vpPoint m_faceDesiredNormal
Face (normalized) normal (computed from the sensor)
double m_distFarClip
Distance for near clipping.
double get_Y() const
Get the point cY coordinate in the camera frame.
virtual bool isVisible(const vpHomogeneousMatrix &cMo, double alpha, const bool &modulo=false, const vpCameraParameters &cam=vpCameraParameters(), unsigned int width=0, unsigned int height=0)
std::vector< vpMbtDistanceLine * > m_listOfFaceLines
void setClipping(const unsigned int &flags)
bool samePoint(const vpPoint &P1, const vpPoint &P2) const
std::vector< PolygonLine > m_polygonLines
double get_Z() const
Get the point cZ coordinate in the camera frame.
vpFaceCentroidType m_faceCentroidMethod
Method to compute the face centroid for the current features.
void setName(const std::string &line_name)
void setCameraParameters(const vpCameraParameters &camera)
double get_oX() const
Get the point oX coordinate in the object frame.
void resize(unsigned int i, bool flagNullify=true)
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
void estimatePlaneEquationSVD(const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &plane_equation_estimated, vpColVector ¢roid)
void computeDesiredFeaturesSVD(const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &desired_features, vpColVector &desired_normal, vpColVector ¢roid_point)
void computeDesiredNormalAndCentroid(const vpHomogeneousMatrix &cMo, const vpColVector &desired_normal, const vpColVector ¢roid_point)
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Implementation of column vector and the associated operations.
static bool inMask(const vpImage< bool > *mask, unsigned int i, unsigned int j)
static double dotProd(const vpColVector &a, const vpColVector &b)
void setRight(double pos)
vpHomogeneousMatrix inverse() const
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
void displayFeature(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double scale=0.05, unsigned int thickness=1)
void addPolygon(const int &index)
void setNearClippingDistance(const double &dist)
unsigned int getHeight() const
vpPoint m_faceDesiredCentroid
Desired centroid (computed from the sensor)
Defines a rectangle in the plane.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Class for generating random numbers with uniform probability density.
Compute the geometric centroid.
virtual ~vpMbtFaceDepthNormal()
vpPlane m_planeObject
Plane equation described in the object frame.
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=NULL)
void estimateFeatures(const std::vector< double > &point_cloud_face, const vpHomogeneousMatrix &cMo, vpColVector &x_estimated, std::vector< double > &weights)
double get_X() const
Get the point cX coordinate in the camera frame.
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)
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
bool useScanLine
Use scanline rendering.
void setIndex(unsigned int i)
vpColVector getCol(unsigned int j) const
static const vpColor blue
double m_pclPlaneEstimationRansacThreshold
PCL plane estimation RANSAC threshold.
void computeFov(const unsigned int &w, const unsigned int &h)