voidEuclideanCluster::segmentByDistance(const pcl::PointCloud<pcl::PointXYZI>::Ptr in,pcl::PointCloud<pcl::PointXYZI>::Ptr &outCloudPtr, std::vector<pcl::PointCloud<pcl::PointXYZI>::Ptr>&points_vector){// cluster the pointcloud according to the distance of the points using different thresholds (not only one for the entire pc)// in this way, the points farther in the pc will also be clusteredstd::vector<pcl::PointIndices> clusterIndices;if(!use_multiple_thres_){cluster_vector(in, clusterIndices);for(auto it = clusterIndices.begin(); it != clusterIndices.end();++it){pcl::PointCloud<pcl::PointXYZI>::Ptr temp_cluster(new pcl::PointCloud<pcl::PointXYZI>);pcl::copyPointCloud(*in, it->indices,*temp_cluster);*outCloudPtr +=*temp_cluster;points_vector.push_back(temp_cluster);}}else{std::vector<pcl::PointCloud<pcl::PointXYZI>::Ptr>cloud_segments_array(7);for(unsignedint i =0; i < cloud_segments_array.size(); i++){pcl::PointCloud<pcl::PointXYZI>::Ptr tmp_cloud(new pcl::PointCloud<pcl::PointXYZI>);cloud_segments_array[i]= tmp_cloud;}for(unsignedint i =0; i < in->points.size(); i++){pcl::PointXYZI current_point;current_point.x = in->points[i].x;current_point.y = in->points[i].y;current_point.z = in->points[i].z;current_point.intensity = in->points[i].intensity;float origin_distance =sqrt(pow(current_point.x,2)+pow(current_point.y,2));if(origin_distance < clustering_ranges_[0]){cloud_segments_array[0]->points.push_back(current_point);}elseif(origin_distance < clustering_ranges_[1]){cloud_segments_array[1]->points.push_back(current_point);}elseif(origin_distance < clustering_ranges_[2]){cloud_segments_array[2]->points.push_back(current_point);}elseif(origin_distance < clustering_ranges_[3]){cloud_segments_array[3]->points.push_back(current_point);}else{cloud_segments_array[4]->points.push_back(current_point);}}std::vector<std::thread>thread_vec(cloud_segments_array.size());for(unsignedint i =0; i < cloud_segments_array.size(); i++){std::promise<std::vector<pcl::PointIndices>> promiseObj;std::shared_future<std::vector<pcl::PointIndices>> futureObj = promiseObj.get_future();thread_vec[i]= std::thread(&EuclideanCluster::clusterIndicesMultiThread,this, cloud_segments_array[i], std::ref(clustering_distances_[i]), std::ref(promiseObj));clusterIndices = futureObj.get();for(int j =0; j < clusterIndices.size(); j++){//每次聚類得出的indices為輸入點云對應的索引pcl::PointCloud<pcl::PointXYZI>::Ptr temp_cluster(new pcl::PointCloud<pcl::PointXYZI>);pcl::copyPointCloud(*cloud_segments_array[i], clusterIndices[j],*temp_cluster);*outCloudPtr +=*temp_cluster;points_vector.push_back(temp_cluster);}}for(int i =0; i < thread_vec.size(); i++){thread_vec[i].join();}}}
voidBoundingBox::getBoundingBox(std_msgs::Header header,std::vector<pcl::PointCloud<pcl::PointXYZI>::Ptr>&points_vector,autoware_msgs::CloudClusterArray &inOutClusters){std::vector<BoundingBoxPtr> Clusters;for(int i =0; i < points_vector.size(); i++){// pcl::PointCloud<pcl::PointXYZI>::Ptr temp_cluster(new pcl::PointCloud<pcl::PointXYZI>);// pcl::copyPointCloud(*in, it->indices, *temp_cluster);// *outCloudPtr += *temp_cluster;BoundingBoxPtr cluster(newBoundingBox());cluster->SetCloud(header, points_vector[i], inEstimatePose_);Clusters.push_back(cluster);}// Clusters can be merged or checked in here// check for mergable clustersstd::vector<BoundingBoxPtr> midClusters;std::vector<BoundingBoxPtr> finalClusters;if(Clusters.size()>0)checkAllForMerge(header, Clusters, midClusters, clusterMergeThreshold_, inEstimatePose_);elsemidClusters = Clusters;if(midClusters.size()>0)checkAllForMerge(header, midClusters, finalClusters, clusterMergeThreshold_, inEstimatePose_);elsefinalClusters = midClusters;// Get final PointCloud to be publishedfor(unsignedint i =0; i < Clusters.size(); i++){if(Clusters[i]->validCluster_){autoware_msgs::CloudCluster cloudCluster;Clusters[i]->ToROSMessage(header, cloudCluster);inOutClusters.clusters.push_back(cloudCluster);}}inOutClusters.header = header;}