// // Created by zx on 2021/5/11. // #include "point_tool.h" /* void rotation_matrix_to_rpy() { //2# 92.0084 112.664 179.263 //3# 85.2597 73.8993 -1.70915 //4# 91.93 112.146 -179.65 //5# 32.9435 67.8446 -52.9897 //6# 124.098 119.42 -150.147 //8# 37.3443 111.887 130.948 // mat_r00:0.0229183 // mat_r01:-0.9985044 // mat_r02:0.0496393 // mat_r03:5287.829 // mat_r10:0.2763783 // mat_r11:0.0540452 // mat_r12:0.9595283 // mat_r13:-2399.535 // mat_r20:-0.9607757 // mat_r21:-0.0082715 // mat_r22:0.2772034 // mat_r23:6674.29500 Eigen::Matrix3d mat; mat<<0.0229183, -0.9985044, 0.0496393, 0.2763783, 0.0540452, 0.9595283, -0.9607757, -0.0082715, 0.2772034; Eigen::Vector3d euler_angles = mat.eulerAngles(2, 1, 0); std::cout<>value) { data[i]=value; } else { return false; } } point.x=data[0]; point.y=data[1]; point.z=data[2]; return true; } pcl::PointCloud::Ptr ReadTxtCloud(std::string file) { std::ifstream fin(file.c_str()); const int line_length=64; char str[line_length]={0}; pcl::PointCloud::Ptr cloud(new pcl::PointCloud); while(fin.getline(str,line_length)) { pcl::PointXYZ point; if(string2point(str,point)) { cloud->push_back(point); } } return cloud; } void save_cloud_txt(pcl::PointCloud::Ptr cloud,std::string file) { FILE* pf=fopen(file.c_str(),"w"); for(int i=0;isize();++i) { pcl::PointXYZ point=cloud->points[i]; fprintf(pf,"%.3f %.3f %.3f\n",point.x,point.y,point.z); } fclose(pf); } void save_cloud_txt(pcl::PointCloud::Ptr cloud,std::string file) { FILE* pf=fopen(file.c_str(),"w"); for(int i=0;isize();++i) { pcl::PointXYZRGB point=cloud->points[i]; fprintf(pf,"%.3f %.3f %.3f %d %d %d\n",point.x,point.y,point.z,point.r,point.g,point.b); } fclose(pf); } //欧式聚类******************************************************* std::vector::Ptr> segmentation(pcl::PointCloud::Ptr sor_cloud) { std::vector ece_inlier; pcl::search::KdTree::Ptr tree(new pcl::search::KdTree); pcl::EuclideanClusterExtraction ece; ece.setInputCloud(sor_cloud); ece.setClusterTolerance(0.2); ece.setMinClusterSize(20); ece.setMaxClusterSize(20000); ece.setSearchMethod(tree); ece.extract(ece_inlier); std::vector::Ptr> segmentation_clouds; for (int i = 0; i < ece_inlier.size(); i++) { pcl::PointCloud::Ptr cloud_copy(new pcl::PointCloud); std::vector ece_inlier_ext = ece_inlier[i].indices; copyPointCloud(*sor_cloud, ece_inlier_ext, *cloud_copy);//按照索引提取点云数据 segmentation_clouds.push_back(cloud_copy); } return segmentation_clouds; } double distance(cv::Point2f p1, cv::Point2f p2) { return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0)); } bool isRect(std::vector& points) { if (points.size() == 4) { double L[3] = {0.0}; L[0] = distance(points[0], points[1]); L[1] = distance(points[1], points[2]); L[2] = distance(points[0], points[2]); double max_l = L[0]; double l1 = L[1]; double l2 = L[2]; cv::Point2f ps = points[0], pt = points[1]; cv::Point2f pc = points[2]; for (int i = 1; i < 3; ++i) { if (L[i] > max_l) { max_l = L[i]; l1 = L[abs(i + 1) % 3]; l2 = L[abs(i + 2) % 3]; ps = points[i % 3]; pt = points[(i + 1) % 3]; pc = points[(i + 2) % 3]; } } //直角边与坐标轴的夹角 <20° float thresh=20.0*M_PI/180.0; cv::Point2f vct(pt.x-pc.x,pt.y-pc.y); float angle=atan2(vct.y,vct.x); if(!(fabs(angle)= 0.15) { // char description[255]={0}; // sprintf(description,"angle cos value(%.2f) >0.13 ",cosa); // std::cout<1.900 || length >3.300 ||length < 2.200) { // char description[255]={0}; // sprintf(description,"width<1400 || width >1900 || length >3300 ||length < 2200 l:%.1f,w:%.1f",length,width); // std::cout< max_l * 0.1 || distance(center1, center2) > 0.150) { // std::cout << "d:" << d << " maxl:" << max_l << " center1:" << center1 << " center2:" << center2< max_l * 0.1 || distance(center1, center2) > 0.150 "); // std::cout< max_l) { max_index = i; max_l = L[i]; l1 = L[abs(i + 1) % 3]; l2 = L[abs(i + 2) % 3]; ps = points[i % 3]; pt = points[(i + 1) % 3]; pc = points[(i + 2) % 3]; } } //直角边与坐标轴的夹角 <20° float thresh=20.0*M_PI/180.0; cv::Point2f vct(pt.x-pc.x,pt.y-pc.y); float angle=atan2(vct.y,vct.x); if(!(fabs(angle)= 0.15) { // char description[255]={0}; // sprintf(description,"3 wheels angle cos value(%.2f) >0.13 ",cosa); // std::cout<2.100 && l<3.300 && w>1.400 && w<2.100) { //生成第四个点 cv::Point2f vec1=ps-pc; cv::Point2f vec2=pt-pc; cv::Point2f point4=(vec1+vec2)+pc; points.push_back(point4); // char description[255]={0}; // sprintf(description,"3 wheels rectangle cos angle=%.2f,L=%.1f, w=%.1f",cosa,l,w); // std::cout<::Ptr &pointcloud) { // std::lock_guard lck(m_cloud_mutex); pointcloud->clear(); std::ifstream in(filename); if (!in.is_open()) { std::cout << "failed to open file " << filename << std::endl; return false; } while (!in.eof()) { std::string t_linestr; if (getline(in, t_linestr)) { std::vector str_vector; std::stringstream ss(t_linestr); std::string num_str; while (getline(ss, num_str, ' ')) { str_vector.push_back(num_str); } if (str_vector.size() != 3) { std::cout << "unsupported point cloud / cannot find point x y z value: " << filename << std::endl; return false; } pcl::PointXYZ t_cloud; t_cloud.x = stod(str_vector[0]); t_cloud.y = stod(str_vector[1]); t_cloud.z = stod(str_vector[2]); pointcloud->push_back(t_cloud); } } in.close(); std::cout << "file read finished with points " << pointcloud->size() << std::endl; return true; } */