使用+,更新k,r,切比路面宽一点
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/octree/octree.h>
#include<fstream>
#include <string>
#include <vector>
#include <liblas/liblas.hpp>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
using namespace std;
void loadLasFile(string s, pcl::PointCloud<pcl::PointXYZ>& cloud);
void loadLasFile(string s, pcl::PointCloud<pcl::PointXYZ>& cloud) {
std::ifstream ifs(s, std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
unsigned long int nbPoints = reader.GetHeader().GetPointRecordsCount();
cloud.width = nbPoints;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);
int i = 0;
uint16_t r1, g1, b1;
int r2, g2, b2;
uint32_t rgb;
while (reader.ReadNextPoint()) {
cloud.points[i].x = (reader.GetPoint().GetX());
cloud.points[i].y = (reader.GetPoint().GetY());
cloud.points[i].z = (reader.GetPoint().GetZ());
i++;
}
}
float computeRange(pcl::PointCloud<pcl::PointXYZ>& trail, float r, int index, int k) {
float x1, y1, x2, y2, a1, a2, a3, b1, b2, b3;
x2 = 0; y2 = 1;
a1 = trail.points[index].x;
a2 = trail.points[index].y;
a3 = trail.points[index].z;
b1 = trail.points[index + k].x;
b2 = trail.points[index + k].y;
b3 = trail.points[index + k].z;
x1 = a1 - b1;
y1 = a2 - b2;
float cosa = (x1 * x2 + y1 * y2) / (sqrt(x1 * x1 + y1 * y1)+sqrt(x2 * x2 + y2 * y2));
float sina = sqrt(1 - cosa * cosa);
float range = r / sina;
return range;
}
int main() {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr trail(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr result(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr seedCloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr colored(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr tempCloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointXYZ tempPnt;
std::vector<int>pointIdx;
float r = 10;
int k = 7;
cout << "读取点云" << endl;
pcl::io::loadPCDFile("o-sample100.pcd", *cloud);
cout << "读取轨迹线" << endl;
pcl::io::loadPCDFile("trail.pcd", *trail);
cout << "原始点数: " << cloud->width << " \t" << "轨迹线点数: " << trail->width << endl;
cout << "构建八叉树" << endl;
float resolution = 128.0f;
pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(resolution);
octree.setInputCloud(cloud);
octree.addPointsFromInputCloud();
cout << "构建完成" << endl;
for (size_t i = 0;i + k < trail->points.size(); i += k) {
cout << "-------------------------------------------------------------" << endl;
float range = computeRange(*trail, r, i, k);
float center = trail->points[i].x;
float Ymin = min(trail->points[i].y, trail->points[i + k].y);
float Ymax = max(trail->points[i].y, trail->points[i + k].y);
float Zmin = -10;
float Zmax = max(trail->points[i].z, trail->points[i + k].z);
cout << "range: " << range << endl;
Eigen::Vector3f Emin(center - range, Ymin, Zmin);
Eigen::Vector3f Emax(center + range, Ymax, Zmax);
octree.boxSearch(Emin, Emax, pointIdx);
cout << i << "\t这段种子数:\t" << pointIdx.size() << endl;
pcl::copyPointCloud(*cloud, pointIdx, *tempCloud);
*seedCloud += *tempCloud;
}
cout << "切割后的点云: seedCloud.pcd" << endl;
pcl::io::savePCDFileASCII("0seedCloud.pcd", *seedCloud);
cout << "保存成功" << endl;
return 0;
}
初版
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/octree/octree.h>
#include<fstream>
#include <string>
#include <vector>
#include <LasOperator.h>
#include <liblas/liblas.hpp>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
using namespace std;
void loadLasFile(string s, pcl::PointCloud<pcl::PointXYZRGB>& cloud);
void loadLasFile(string s, pcl::PointCloud<pcl::PointXYZRGB>& cloud) {
std::ifstream ifs(s, std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
unsigned long int nbPoints = reader.GetHeader().GetPointRecordsCount();
cloud.width = nbPoints;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);
int i = 0;
uint16_t r1, g1, b1;
int r2, g2, b2;
uint32_t rgb;
while (reader.ReadNextPoint()) {
cloud.points[i].x = (reader.GetPoint().GetX());
cloud.points[i].y = (reader.GetPoint().GetY());
cloud.points[i].z = (reader.GetPoint().GetZ());
r1 = (reader.GetPoint().GetColor().GetRed());
g1 = (reader.GetPoint().GetColor().GetGreen());
b1 = (reader.GetPoint().GetColor().GetBlue());
r2 = ceil(((float)r1 / 65536) * (float)256);
g2 = ceil(((float)g1 / 65536) * (float)256);
b2 = ceil(((float)b1 / 65536) * (float)256);
rgb = ((int)r2) << 16 | ((int)g2) << 8 | ((int)b2);
cloud.points[i].rgb = *reinterpret_cast<float*>(&rgb);
i++;
}
}
float computeRange(pcl::PointCloud<pcl::PointXYZRGB>& trail, float r, int index) {
float x1, y1, x2, y2, a1, a2, a3, b1, b2, b3;
x2 = 0; y2 = 1;
a1 = trail.points[index].x;
a2 = trail.points[index].y;
a3 = trail.points[index].z;
b1 = trail.points[index + 20].x;
b2 = trail.points[index + 20].y;
b3 = trail.points[index + 20].z;
x1 = a1 - b1;
y1 = a2 - b2;
float cosa = (x1 * x2 + y1 * y2) / (sqrt(x1 * x1 + y1 * y1) + sqrt(x2 * x2 + y2 * y2));
float sina = sqrt(1 - cosa * cosa);
float range = r / sina;
return range;
}
int main() {
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr trail(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr result(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr seedCloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr colored(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr tempCloud(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointXYZRGB tempPnt;
std::vector<int>pointIdx;
float r = 10;
cout << "读取点云" << endl;
loadLasFile("sample10.las", *cloud);
cout << "读取轨迹线" << endl;
pcl::io::loadPCDFile("trail.pcd", *trail);
cout << "原始点数: " << cloud->width << " \t" << "轨迹线点数: " << trail->width << endl;
cout << "构建八叉树" << endl;
float resolution = 128.0f;
pcl::octree::OctreePointCloudSearch<pcl::PointXYZRGB> octree(resolution);
octree.setInputCloud(cloud);
octree.addPointsFromInputCloud();
for (size_t i = 0;i + 20 <= trail->points.size(); i += 20) {
cout << "-------------------------------------------------------------" << endl;
float range = computeRange(*trail, r, i);
float center = trail->points[i].x;
float Ymin = min(trail->points[i].y, trail->points[i + 20].y);
float Ymax = max(trail->points[i].y, trail->points[i + 20].y);
float Zmin = -10;
float Zmax = max(trail->points[i].z, trail->points[i + 20].z);
cout << "range: " << range << endl;
Eigen::Vector3f Emin(trail->points[i].x - range, Ymin, Zmin);
Eigen::Vector3f Emax(trail->points[i].x + range, Ymax - 0.001, Zmax);
octree.boxSearch(Emin, Emax, pointIdx);
cout << i << "\t这段种子数:\t" << pointIdx.size() << endl;
pcl::copyPointCloud(*cloud, pointIdx, *tempCloud);
*seedCloud += *tempCloud;
}
cout << "切割后的点云: seedCloud.pcd" << endl;
pcl::io::savePCDFileASCII("0seedCloud.pcd", *seedCloud);
cout << "保存成功" << endl;
return 0;
}