实现 VDE-382 剩余三项提案: - multi_polygon_offset(polygons, distance):各轮廓偏移后,对包围盒重叠者用 boolean_2d 并集合并,保留所有互不相交的连通分量。 - variable_offset(poly, per_vertex_distances):逐顶点距离沿角平分线偏移, 长度不符/非有限值返回空,全 0 原样返回。 - concentric_offsets(boundary, stepover, max_passes):自边界向内逐层内缩, 生成同心刀路层(CNC 型腔铣),层为空即停止。 - include/vde/boolean/polygon_offset.h:声明与文档 - src/boolean/polygon_offset.cpp:实现(新增包围盒重叠判定 helper) - tests/boolean/test_polygon_offset.cpp:VDE382Test 6 项 回归:test_polygon_offset 38/38 全绿。 更新 VDE-382.md 与 audit-proposal-batch.md(已实现 24→25 / 缺失 258→255)。
140 lines
5.4 KiB
C++
140 lines
5.4 KiB
C++
/**
|
||
* @file polygon_offset.h
|
||
* @brief 多边形等距偏移 (Polygon Offset / Inset)
|
||
*
|
||
* 基于直骨架 (Straight Skeleton) 近似的多边形
|
||
* 膨胀/收缩操作。用于 2D 轮廓生成、刀具路径补偿等。
|
||
*
|
||
* @ingroup boolean
|
||
*/
|
||
|
||
#pragma once
|
||
#include "vde/core/polygon.h"
|
||
#include <vector>
|
||
|
||
namespace vde::boolean {
|
||
using core::Point2D;
|
||
using core::Polygon2D;
|
||
|
||
/**
|
||
* @brief 多边形等距偏移
|
||
*
|
||
* 将多边形的每条边沿其法线方向平移指定距离,
|
||
* 在转角处自动生成圆弧/斜角连接。
|
||
*
|
||
* 算法:
|
||
* 1. 对每条边计算单位外法线
|
||
* 2. 沿法线平移 distance 距离
|
||
* 3. 相邻偏移边求交形成新的顶点
|
||
* 4. 检测自交并移除退化区域
|
||
*
|
||
* - 正距离 = 外扩(膨胀/Inflation)
|
||
* - 负距离 = 内缩(偏置/Deflation)
|
||
*
|
||
* @param poly 原始多边形
|
||
* @param distance 偏移距离(正=外扩,负=内缩)
|
||
* @return 偏移后的多边形列表(内缩或自交可能产生多个碎片)
|
||
*
|
||
* @note 使用直骨架近似而非精确圆弧偏移,转角处为斜角连接。
|
||
* 大距离负偏移可能导致多边形完全消失(返回空列表)。
|
||
*
|
||
* @code{.cpp}
|
||
* Polygon2D rect = {{0,0}, {10,0}, {10,5}, {0,5}};
|
||
* // 外扩 2 单位
|
||
* auto inflated = polygon_offset(rect, 2.0);
|
||
* // 内缩 1 单位(生成工具路径)
|
||
* auto toolpath = polygon_offset(rect, -1.0);
|
||
* @endcode
|
||
*
|
||
* @see boolean_2d
|
||
*/
|
||
std::vector<Polygon2D> polygon_offset(const Polygon2D& poly, double distance);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-382: arc_joins + multi_contour_offset + offset_quality_check
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// Join type for offset corners
|
||
enum class OffsetJoinType { Miter, Round, Bevel };
|
||
|
||
/// Offset quality metrics
|
||
struct OffsetQuality {
|
||
bool self_intersection_free = true;
|
||
double min_edge_length = 0.0;
|
||
double max_edge_length = 0.0;
|
||
double area_ratio = 0.0; ///< offset area / original area
|
||
int vertex_count_in = 0;
|
||
int vertex_count_out = 0;
|
||
bool is_valid = true;
|
||
std::string error_message;
|
||
};
|
||
|
||
/// Polygon offset with arc (rounded) joins at corners
|
||
/// @param poly Input polygon
|
||
/// @param distance Offset distance (positive=outward, negative=inward)
|
||
/// @param arc_segments Number of segments per 90° arc (default 8)
|
||
/// @return Offset polygon list
|
||
[[nodiscard]] std::vector<Polygon2D> polygon_offset_arc(const Polygon2D& poly,
|
||
double distance, int arc_segments = 8);
|
||
|
||
/// Multi-contour offset: offset each contour independently
|
||
/// @param contours Input polygon contours
|
||
/// @param distance Offset distance
|
||
/// @return Offset contour list (same order as input)
|
||
[[nodiscard]] std::vector<std::vector<Polygon2D>> multi_contour_offset(
|
||
const std::vector<Polygon2D>& contours, double distance);
|
||
|
||
/// Quality check for offset result
|
||
/// @param original Original polygon
|
||
/// @param offset_result Offset result (first polygon)
|
||
/// @return Quality metrics
|
||
[[nodiscard]] OffsetQuality offset_quality_check(const Polygon2D& original,
|
||
const std::vector<Polygon2D>& offset_result);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-382 提案:多轮廓偏移 / 变距离偏移 / 同心刀路
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 多轮廓同时偏移(VDE-382 提案 2)
|
||
*
|
||
* 对一组多边形各自偏移后,把**互相交叠**(包围盒重叠)的结果用并集合并,
|
||
* 保留所有互不相交的连通分量(PCB 走线间距、多岛轮廓等场景)。
|
||
*
|
||
* @param polygons 输入多边形列表
|
||
* @param distance 偏移距离(正=外扩,负=内缩)
|
||
* @return 合并后的偏移多边形列表
|
||
*/
|
||
[[nodiscard]] std::vector<Polygon2D> multi_polygon_offset(
|
||
const std::vector<Polygon2D>& polygons, double distance);
|
||
|
||
/**
|
||
* @brief 变距离多边形偏移(VDE-382 提案 3)
|
||
*
|
||
* 每个顶点使用各自的偏移距离(沿该顶点角平分线移动)。沿边的偏移距离
|
||
* 由两端顶点距离线性过渡(由逐顶点偏移一阶近似)。
|
||
*
|
||
* @param poly 原始多边形
|
||
* @param per_vertex_distances 逐顶点偏移距离(长度须等于顶点数)
|
||
* @return 偏移后的多边形列表;长度不符或含非有限值时返回空列表
|
||
*/
|
||
[[nodiscard]] std::vector<Polygon2D> variable_offset(
|
||
const Polygon2D& poly,
|
||
const std::vector<double>& per_vertex_distances);
|
||
|
||
/**
|
||
* @brief 同心偏移刀路(VDE-382 提案 4)
|
||
*
|
||
* 自边界向内以 `stepover` 逐层偏置,生成同心刀路层(CNC 型腔铣)。
|
||
* 第 0 层为第一次内缩结果,其后逐层内缩;某层为空即停止。
|
||
*
|
||
* @param boundary 边界多边形
|
||
* @param stepover 每层内缩步距(>0)
|
||
* @param max_passes 最大层数
|
||
* @return 由外到内的各层多边形列表
|
||
*/
|
||
[[nodiscard]] std::vector<std::vector<Polygon2D>> concentric_offsets(
|
||
const Polygon2D& boundary, double stepover, int max_passes);
|
||
|
||
} // namespace vde::boolean
|