Surface tools, extensions, analysis, Class-A, projection Mesh repair, curvature, smooth, parameterize, boolean2D Assembly: large, interference, mass, BOM, explode B-Rep: healing, pattern, trim, defeature, recognition Direct modeling, FFD, blend, draft, profiles Drawing, dimensioning, PMI, sheet metal CAM, format I/O, reverse engineering NURBS operations, IGA prep, ICP, convex hull, voronoi Topology compression, transaction, license, material Selection, feature builder, sketch, appearance, equation ~200+ new functions · 80提案全部实现
95 lines
3.3 KiB
C++
95 lines
3.3 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);
|
||
|
||
} // namespace vde::boolean
|