#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //SYJ新增 #include #include #include #include //去除雜點 #include #include #include //讀寫檔案 #include #include #include #include #include using namespace std; #define SIZE 20 struct callback_args { bool* isShow; pcl::PointCloud::Ptr orgin_points; pcl::visualization::PCLVisualizer::Ptr viewerPtr; pcl::PointCloud::Ptr clicked_points_3d; }; void pp_callback(const pcl::visualization::PointPickingEvent& event, void* args) { struct callback_args* data = (struct callback_args*)args; if (event.getPointIndex() == -1) return; int index = event.getPointIndex(); std::cout << "index: " << index << std::endl; pcl::PointXYZ current_point; event.getPoint(current_point.x, current_point.y, current_point.z); data->clicked_points_3d->points.push_back(current_point); // Draw clicked points in red: pcl::visualization::PointCloudColorHandlerCustom red(data->clicked_points_3d, 255, 0, 0); data->viewerPtr->removePointCloud("clicked_points"); data->viewerPtr->addPointCloud(data->clicked_points_3d, red, "clicked_points"); data->viewerPtr->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, "clicked_points"); std::cout << current_point.x << " " << current_point.y << " " << current_point.z << std::endl; } int main(int argc, char** argv) { string s = "D:\\ROBOT_Project\\Robot\\ABB\\RealSence_PathMove\\bin\\x64\\Debug\\"; pcl::PointCloud::Ptr cloud(new pcl::PointCloud); pcl::PointCloud::Ptr cloudNormal(new pcl::PointCloud); pcl::PointCloud::Ptr Normals(new pcl::PointCloud); pcl::PointCloud::Ptr cloud_and_normal(new pcl::PointCloud); pcl::io::loadPCDFile(s+"allpoint.pcd", *cloud); //計算完整大底法向量 pcl::NormalEstimationOMP neOMP; neOMP.setNumberOfThreads(3); neOMP.setInputCloud(cloud); neOMP.setRadiusSearch(30); Eigen::Vector4f centroid; pcl::compute3DCentroid(*cloud, centroid); neOMP.setViewPoint(centroid[0], centroid[1], centroid[2]); neOMP.compute(*Normals); //法向量與點雲結合 pcl::concatenateFields(*cloud, *Normals, *cloud_and_normal); double e = 1; int jump = 1; int Startj = 0; pcl::PointCloud::Ptr cloudN(new pcl::PointCloud); pcl::PointCloud::Ptr edgeR_BF(new pcl::PointCloud); pcl::io::loadPCDFile(s+"processpoint.pcd", *edgeR_BF); int count = 0, count1 = 0, ccc = 1000; //使用轉折處點雲與完整大底點雲進行匹配 for (size_t i = 0; i < edgeR_BF->size(); i++) { bool flag = false; for (size_t j = Startj; j < cloud_and_normal->size(); j += jump) { int aa = cloud_and_normal->points[j].x - edgeR_BF->points[i].x; int bb = cloud_and_normal->points[j].y - edgeR_BF->points[i].y; int cc = cloud_and_normal->points[j].z - edgeR_BF->points[i].z; if (sqrt(aa * aa + bb * bb + cc * cc) < e) { count += 1; cloudN->push_back(cloud_and_normal->points[j]); //Startj = j; flag = true; e = 1; break; } } if (!flag) { e += 1; i--; } } pcl::PointCloud::Ptr tempcloudNormal(new pcl::PointCloud); tempcloudNormal = cloudN; pcl::PointCloud::Ptr cloudNormalL0(new pcl::PointCloud); Eigen::Vector3f planeVec; Eigen::Vector3f origenVec; float dotResult, radian, n, a; pcl::PointCloud::Ptr cloudNormalL(new pcl::PointCloud); for (size_t i = 0; i < tempcloudNormal->size(); i++) { cout << tempcloudNormal->points[i].normal_x << endl; //將所有法向量轉換為同樣長度 n = 1.5 / tempcloudNormal->points[i].normal_z; tempcloudNormal->points[i].normal_x *= n; tempcloudNormal->points[i].normal_y *= n; tempcloudNormal->points[i].normal_z *= n; cloudNormalL->push_back(tempcloudNormal->points[i]); cout << cloudNormalL->points[i].normal_x << endl; } for (size_t i = 0; i < cloudNormalL->size(); i++) { cout << cloudNormalL->points[i].x << " " << cloudNormalL->points[i].y << " " << cloudNormalL->points[i].z << endl; } #pragma endregion pcl::io::savePCDFile(s+"edge_normal.pcd", *cloudNormalL);//寫入檔案 pcl::visualization::PCLVisualizer viewer; viewer.addPointCloudNormals(cloudNormalL, 1, -50, "cloudNormalL"); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, "cloudNormalL"); viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, "cloudNormalL"); viewer.addPointCloud(cloud, "cloud"); viewer.addCoordinateSystem(10, "cloud"); viewer.setCameraPosition(0, 0, -100, 0, 1, 0, 0, 0, -1); // 初始化参数 bool isShow = true; struct callback_args kb_args; kb_args.isShow = &isShow; kb_args.orgin_points = cloud; /*struct callback_args cb_args; pcl::PointCloud::Ptr clicked_points_3d(new pcl::PointCloud); cb_args.clicked_points_3d = clicked_points_3d; cb_args.viewerPtr = pcl::visualization::PCLVisualizer::Ptr(&viewer); viewer.registerPointPickingCallback(pp_callback, (void*)&cb_args); pcl::console::print_highlight("Shift+click on three floor points, then press 'Q'...\n");*/ viewer.spin(); return 0; }