- VDE-1341 mesh: to_mcm ear-clips n>3 faces (Newell normal + 2D ear clipping, fallback to fan on degeneracy) and maps local triangle indices back to face vertices; empty HalfedgeMesh/PointCloud bounds return a finite zero box; faces_range()/vertex_one_ring() implemented (FaceRange iterator, adjacency from vertex outgoing half-edges). - VDE-1342 FeatureNode: last_error()/last_evaluate_ok() diagnostics, stronger boolean success predicate, invalid primitive/edit parameters throw std::invalid_argument instead of being silently clamped. - VDE-1343 edit_feature: face_ids range validation before the no-op early return, analytic hole location first, warnings on geometric fallback, 15% radial consistency check between reference faces and matched hole. - VDE-1344 rebind_entity: id hit now verified against the anchor distance (vertex/edge/face), falls back to nearest-anchor match, returns -1 when still mismatched; Body rebinds through anchor's nearest face. - Tests: test_mesh_1177_fixes 11 cases, test_entity_1177_fixes 12 cases; registry 8,339 -> 8,362; full CTest 8,362/8,362 passed, 0 failed, 35 SKIP, 635.55 s; release checks 14/14 PASS, report-check latest=224. - Docs: report 224 (self-verification), verifier report 223 (1.0.175 PASS) included, RegressionExtreme count corrected to 10/10 (P3-1).
267 lines
12 KiB
C++
267 lines
12 KiB
C++
#pragma once
|
||
/**
|
||
* @file point_cloud.h
|
||
* @brief 点云数据结构与处理算法
|
||
*
|
||
* 提供点云的存储(顶点/法线/颜色/强度)、PLY/E57/LAS/PTX 加载,
|
||
* 以及体素降采样、离群点剔除、法线估计、曲率感知采样与平滑滤波。
|
||
*
|
||
* @ingroup mesh
|
||
*/
|
||
#include "vde/core/point.h"
|
||
#include "vde/core/aabb.h"
|
||
#include <vector>
|
||
#include <string>
|
||
#include <cstdint>
|
||
#include <cstddef>
|
||
#include <array>
|
||
|
||
namespace vde::mesh {
|
||
using core::Point3D;
|
||
using core::Vector3D;
|
||
using core::AABB3D;
|
||
|
||
/**
|
||
* @brief 点云导出状态(VDE 数据交换补全:诚实失败 / 降级语义)
|
||
* @ingroup mesh
|
||
*/
|
||
enum class PointCloudIoStatus {
|
||
Ok = 0, ///< 成功写出
|
||
Unsupported,///< 格式在当前构建中不支持写出(明确诊断,不静默)
|
||
Error ///< 参数/IO 错误
|
||
};
|
||
|
||
/// 点云导出结果:status + 人类可读说明
|
||
struct PointCloudIoResult {
|
||
PointCloudIoStatus status = PointCloudIoStatus::Error;
|
||
std::string message;
|
||
|
||
[[nodiscard]] bool ok() const noexcept { return status == PointCloudIoStatus::Ok; }
|
||
[[nodiscard]] bool unsupported() const noexcept {
|
||
return status == PointCloudIoStatus::Unsupported;
|
||
}
|
||
};
|
||
/**
|
||
* @brief E57 写出选项(FMT-12)
|
||
*
|
||
* 默认(false)保持既有 VDE 最小 E57 格式;true 写标准 CompressedVector
|
||
* 分段(ASTM E2807 bitPack codec)。page_crc=true 时再套 1024B 物理页层
|
||
* (每页 1020B 数据 + 4B CRC-32C);默认 false 保持 FMT-12 首增量无页行为。
|
||
* @ingroup mesh
|
||
*/
|
||
struct E57WriteOptions {
|
||
bool standard_compressed_vector = false; ///< true=标准 CompressedVector 分段
|
||
bool page_crc = false; ///< true=1024B 物理页 + 页尾 CRC-32C
|
||
};
|
||
|
||
/**
|
||
* @brief E57 读取选项(页层容错)
|
||
*
|
||
* verify_page_crc=true:校验物理页 CRC,损坏时抛异常(明确检测)。
|
||
* false:跳过 CRC 校验,仅剥离 4B 页尾,用于损坏页仍能读取的容错场景。
|
||
* @ingroup mesh
|
||
*/
|
||
struct E57ReadOptions {
|
||
bool verify_page_crc = true; ///< 是否校验 page CRC
|
||
};
|
||
|
||
// ───────────────────────────────────────────────────────────
|
||
// PointCloud 点云类
|
||
// ───────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* @brief 点云数据结构
|
||
*
|
||
* 存储顶点位置、法线、颜色(可选)及包围盒信息。
|
||
* 支持 PLY/E57/PTX 加载,体素降采样、统计离群点剔除、kNN 法线估计。
|
||
*
|
||
* @ingroup mesh
|
||
*/
|
||
class PointCloud {
|
||
public:
|
||
std::vector<Point3D> vertices; ///< 顶点位置
|
||
std::vector<Vector3D> normals; ///< 顶点法线(归一化,空数组表示未计算)
|
||
std::vector<std::array<uint8_t, 3>> colors; ///< RGB 颜色(0-255,空数组表示无颜色)
|
||
std::vector<double> intensities; ///< 反射强度(空数组表示无强度)
|
||
|
||
PointCloud() = default;
|
||
|
||
/// 从顶点列表构造(无颜色/法线)
|
||
explicit PointCloud(std::vector<Point3D> pts) : vertices(std::move(pts)) {}
|
||
|
||
/// 顶点数
|
||
[[nodiscard]] size_t size() const { return vertices.size(); }
|
||
/// 是否有法线
|
||
[[nodiscard]] bool has_normals() const { return !normals.empty(); }
|
||
/// 是否有颜色
|
||
[[nodiscard]] bool has_colors() const { return !colors.empty(); }
|
||
/// 是否有强度
|
||
[[nodiscard]] bool has_intensity() const { return !intensities.empty(); }
|
||
|
||
/// 包围盒;空点云返回原点退化盒(extent = 0)
|
||
[[nodiscard]] AABB3D bounds() const;
|
||
|
||
// ── I/O ──────────────────────────────────────────────
|
||
|
||
/// 加载 PLY 点云文件(ASCII 或 binary little-endian)
|
||
static PointCloud load_ply(const std::string& path);
|
||
|
||
/// 加载 E57 点云文件(ASTM E2807:XML header + 二进制点数据)
|
||
static PointCloud load_e57(const std::string& path);
|
||
|
||
/// 加载 E57;options.verify_page_crc 控制物理页 CRC 校验(仅页层文件)。
|
||
static PointCloud load_e57(const std::string& path, const E57ReadOptions& options);
|
||
|
||
/// 加载 LAS/LAZ 点云文件(ASPRS LAS 1.2/1.3/1.4,点格式 0-10)。
|
||
/// LAZ:支持 LASzip pointwise / pointwise-chunked(compressor 1/2),
|
||
/// 点格式 0/2(POINT10 v2 [+ RGB12 v2]);其它 LAZ 变体明确抛诊断。
|
||
static PointCloud load_las(const std::string& path);
|
||
|
||
/// 加载 PTX 格式点云(Leica 扫描仪原始格式)
|
||
static PointCloud load_ptx(const std::string& path);
|
||
|
||
/// 保存为 PLY 文件
|
||
bool save_ply(const std::string& path) const;
|
||
|
||
// ── 点云导出(VDE-1222/A-9 数据交换补全)──────────────
|
||
|
||
/// 保存为 ASPRS LAS 1.4 二进制(点格式 2:含 RGB;无颜色时点格式 0)。
|
||
/// 坐标量化 scale=1e-6(必要时按包围盒扩大以避免 int32 溢出),
|
||
/// 写→读回坐标误差 ≤ 5e-7,颜色/点数精确;空点云返回 Error。
|
||
[[nodiscard]] PointCloudIoResult save_las(const std::string& path) const;
|
||
|
||
/// 保存为 Leica PTX 文本(单 scan:num_cols num_rows + 3 行矩阵 + 每点
|
||
/// "x y z intensity r g b",双精度 17 位有效数字)。
|
||
/// 注意 PTX 读取器会跳过 (0,0,0) 点(该格式用全零表示无效点)。
|
||
[[nodiscard]] PointCloudIoResult save_ptx(const std::string& path) const;
|
||
|
||
/// 保存为 E57 默认(旧最小格式):ASTM-E57 48B 头 + XML prototype +
|
||
/// 原始二进制点记录(x/y/z,可选法线、RGB 颜色、强度);旧行为/字节布局不变,
|
||
/// 外部工具兼容性有限;需要 CompressedVector 分段用带 options 的重载或
|
||
/// save_e57_standard()。
|
||
[[nodiscard]] PointCloudIoResult save_e57(const std::string& path) const;
|
||
/// 保存为 E57,按 options 选择旧最小格式或标准 CompressedVector 分段;
|
||
/// page_crc=true 时标准流再套 1024B 物理页(1020B + CRC-32C),首增量采用
|
||
/// VDE 自兼容逻辑偏移(外部物理偏移语义尚未完全验证)。
|
||
[[nodiscard]] PointCloudIoResult save_e57(const std::string& path,
|
||
const E57WriteOptions& options) const;
|
||
/// 保存为标准 E57 CompressedVector 分段(FMT-12 首增量)。
|
||
[[nodiscard]] PointCloudIoResult save_e57_standard(const std::string& path) const;
|
||
|
||
/// 保存为真实 LAZ 1.2(LASzip 兼容,零外部依赖):
|
||
/// LAS 1.2 头(点格式置 bit7=compressed)+ LASzip Special VLR(id 22204,
|
||
/// user id "laszip encoded")+ 压缩数据块。压缩方式为 compressor 2
|
||
/// (pointwise + chunked):每 chunk 首点原始存储,其余点使用 POINT10 v2
|
||
/// (+ RGB12 v2)算术编码;chunk table 按规范用 IntegerCompressor(32,2) 编码。
|
||
/// 支持 LAS 点格式 0/2(xyz+intensity [+RGB]),量化 scale=1e-6(坐标回读
|
||
/// 误差 ≤5e-7、颜色/强度精确)。已用第三方 laspy/lazrs 解码互验。
|
||
/// 读取端 load_las() 可自动识别并回读本函数产物。
|
||
[[nodiscard]] PointCloudIoResult save_laz(const std::string& path) const;
|
||
|
||
// ── 滤波 / 降采样 ────────────────────────────────────
|
||
|
||
/**
|
||
* @brief 体素降采样
|
||
* @param voxel_size 体素边长
|
||
* @return 降采样后的点云(每个体素用质心代替)
|
||
*/
|
||
[[nodiscard]] PointCloud voxel_downsample(double voxel_size) const;
|
||
|
||
/**
|
||
* @brief 统计离群点剔除(SOR)
|
||
* @param k 邻域点数(默认 6)
|
||
* @param std_ratio 标准差倍率阈值(默认 1.0),超过 mean+k*std_ratio 的点标记为离群
|
||
* @return 剔除离群点后的点云
|
||
*/
|
||
[[nodiscard]] PointCloud statistical_outlier_removal(int k = 6, double std_ratio = 1.0) const;
|
||
|
||
// ── 法线估计 ──────────────────────────────────────────
|
||
|
||
/**
|
||
* @brief kNN 法线估计
|
||
*
|
||
* 对每个点,找到 k 个最近邻点,用 PCA 估计法线(最小特征值对应方向)。
|
||
* 法线方向通过视角向量(点→原点方向)统一朝向。
|
||
*
|
||
* @param k 最近邻点数(≥ 3)
|
||
* @return 估计了法线的新点云
|
||
*/
|
||
[[nodiscard]] PointCloud normal_estimation(int k = 10) const;
|
||
|
||
// ── 曲率感知采样 ──────────────────────────────────────
|
||
|
||
/**
|
||
* @brief 曲率感知降采样
|
||
*
|
||
* 计算每点局部曲率近似,按曲率加权采样:高曲率区域保留更多点。
|
||
*
|
||
* @param target_count 目标点数
|
||
* @return 采样后的索引数组
|
||
*/
|
||
[[nodiscard]] std::vector<size_t> curvature_aware_sampling(size_t target_count) const;
|
||
|
||
// ── 平滑滤波 ──────────────────────────────────────────
|
||
|
||
/**
|
||
* @brief 双边滤波平滑
|
||
*
|
||
* 对点云位置进行法向加权的双边滤波去噪,保持尖锐特征。
|
||
*
|
||
* @param sigma_c 空间高斯 σ(0 = 自动按平均点距计算)
|
||
* @param sigma_n 法向高斯 σ(弧度)
|
||
* @param iterations 迭代次数
|
||
* @return 平滑后的新点云
|
||
*/
|
||
[[nodiscard]] PointCloud smoothing_filter(double sigma_c = 0.0,
|
||
double sigma_n = 0.3,
|
||
int iterations = 3) const;
|
||
|
||
/**
|
||
* @brief 双边法向感知平滑
|
||
*
|
||
* 在每次迭代中先通过 kNN PCA 重新估计法线,再用法线进行双边位置修正。
|
||
* 相比 smoothing_filter(仅迭代后重新估计法线),本方法对噪声较大的原始
|
||
* 扫描数据(法线不可靠)能产生更平滑的结果。
|
||
*
|
||
* @param k_normal 法线重新估计的邻域大小(默认 10)
|
||
* @param sigma_c 空间高斯 σ(0 = 自动按平均点距计算)
|
||
* @param sigma_n 法向高斯 σ(弧度)
|
||
* @param iterations 双边迭代次数
|
||
* @return 平滑后的新点云
|
||
*/
|
||
[[nodiscard]] PointCloud normal_aware_smoothing(
|
||
int k_normal = 10,
|
||
double sigma_c = 0.0,
|
||
double sigma_n = 0.3,
|
||
int iterations = 3) const;
|
||
|
||
private:
|
||
/** 构建 k-d 树索引,返回每个顶点的 k 最近邻索引 */
|
||
[[nodiscard]] std::vector<std::vector<size_t>> k_nearest_neighbors(size_t k) const;
|
||
|
||
/** 协方差矩阵 PCA:计算三个特征值,返回最小特征值对应的特征向量 */
|
||
[[nodiscard]] static Vector3D pca_normal(const std::vector<Point3D>& local_pts);
|
||
};
|
||
|
||
// ── GPU 加速(VDE-1278,可选) ─────────────────────────────
|
||
|
||
/**
|
||
* @brief GPU(OpenCL)点云 kNN 法线/SOR 批核是否真实可用
|
||
*
|
||
* 首次调用探测并缓存:无 OpenCL 设备、GPU 后端未编译、点云核编译失败
|
||
* (如设备不支持 64 位原子)时返回 false。返回 false 时 normal_estimation /
|
||
* statistical_outlier_removal 始终走 CPU 实现,绝不谎报 GPU。
|
||
*/
|
||
[[nodiscard]] bool point_cloud_gpu_available();
|
||
|
||
/**
|
||
* @brief GPU 点云路径的最小点数阈值(默认 20000)
|
||
*
|
||
* 点数达到阈值才尝试 GPU 批核,小点云直接走 CPU,避免传输开销倒挂。
|
||
* 每次调用读取环境变量 VDE_GPU_CLOUD_MIN_POINTS(十进制无符号整数)覆盖
|
||
* 默认值,供测试与基准强制开关;环境变量非法时回退默认值。
|
||
*/
|
||
[[nodiscard]] std::size_t point_cloud_gpu_min_points();
|
||
|
||
} // namespace vde::mesh
|