Local preservation snapshot, not a new deployment or simulation acceptance.
37 lines
1.6 KiB
C++
37 lines
1.6 KiB
C++
// PCL segmentation only: source indices and plane, with no motion authority.
|
|
#include <pcl/point_types.h>
|
|
#include <pcl/segmentation/sac_segmentation.h>
|
|
#include <cmath>
|
|
|
|
extern "C" int pcl_ground_indices(const float *xyz, int count, float angle,
|
|
float distance, int *output,
|
|
float *coefficients) noexcept {
|
|
try {
|
|
if (count < 3 || count > 30000 || !std::isfinite(angle) || angle <= 0 ||
|
|
!std::isfinite(distance) || distance <= 0) return -1;
|
|
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
|
|
cloud->reserve(count);
|
|
for (int i = 0; i < count; ++i) {
|
|
const float *p = xyz + 3*i;
|
|
if (!std::isfinite(p[0]) || !std::isfinite(p[1]) || !std::isfinite(p[2])) return -1;
|
|
cloud->push_back(pcl::PointXYZ(p[0], p[1], p[2]));
|
|
}
|
|
pcl::SACSegmentation<pcl::PointXYZ> filter;
|
|
filter.setInputCloud(cloud);
|
|
filter.setModelType(pcl::SACMODEL_PERPENDICULAR_PLANE);
|
|
filter.setMethodType(pcl::SAC_RANSAC);
|
|
filter.setAxis(Eigen::Vector3f::UnitZ());
|
|
filter.setEpsAngle(angle);
|
|
filter.setDistanceThreshold(distance);
|
|
filter.setOptimizeCoefficients(true);
|
|
filter.setMaxIterations(100);
|
|
pcl::PointIndices indices;
|
|
pcl::ModelCoefficients model;
|
|
filter.segment(indices, model);
|
|
if (model.values.size() != 4) return 0;
|
|
for (int i = 0; i < 4; ++i) coefficients[i] = model.values[i];
|
|
for (size_t i = 0; i < indices.indices.size(); ++i) output[i] = indices.indices[i];
|
|
return static_cast<int>(indices.indices.size());
|
|
} catch (...) { return -1; }
|
|
}
|