executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
clipper2Use when performing high-performance 2D polygon boolean operations (union, intersection, difference, XOR) and offsetting via C++, C#, or Python bindings. Clipper2: the modern polygon clipping library with robust numerical handling.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: clipper2 description: "Use when performing high-performance 2D polygon boolean operations (union, intersection, difference, XOR) and offsetting via C++, C#, or Python bindings. Clipper2: the modern polygon clipping library with robust numerical handling." tags:
项目地址: https://github.com/AngusJohnson/Clipper2
官方文档: http://www.angusj.com/clipper2/Docs/Overview.htm
最新稳定版: 2.0.1(截至2026年9月,tag
Clipper2_2.0.1)NuGet:
Clipper2许可证: Boost Software License 1.0
Clipper2 相对 Clipper1 的提升:
PathsD)与整数(Paths64)双模式Union/Intersect/Difference/Xor 顶级函数dotnet add package Clipper2 # 提供 Clipper2Lib 命名空间
# vcpkg
vcpkg install clipper2
# 或直接 add subdirectory CPP/Clipper2Lib 到 CMake
npm install clipper2-js # 第三方原生 TS/JS 移植;另有 WASM 移植 js-angusj-clipper
using Clipper2Lib;
PointD pd = new(10.5, 20.7); // 浮点点
PathD path = new() { new(0,0), new(10,0), new(10,10), new(0,10) };
PathsD subj = new() { path }; // 多个 path
PathsD clip = new() { /* ... */ };
整数版本:Point64 / Path64 / Paths64,避免浮点误差。
using Clipper2Lib;
PathsD a = new() { Clipper.MakePath(new[] { 0d,0, 100,0, 100,100, 0,100 }) };
PathsD b = new() { Clipper.MakePath(new[] { 50d,50, 150,50, 150,150, 50,150 }) };
PathsD u = Clipper.Union(a, b, FillRule.NonZero);
PathsD i = Clipper.Intersect(a, b, FillRule.NonZero);
PathsD d = Clipper.Difference(a, b, FillRule.NonZero);
PathsD x = Clipper.Xor(a, b, FillRule.NonZero);
FillRule:
EvenOdd(奇偶)NonZero(非零)Positive(正向缠绕)Negative(负向缠绕)PathsD outline = new() {
Clipper.MakePath(new[] { 0d,0, 100,0, 100,100, 0,100 })
};
// 向外偏移 10 单位
PathsD outer = Clipper.InflatePaths(outline, 10,
JoinType.Round, // Square / Miter / Round
EndType.Polygon); // Polygon / Joined / Butt / Square / Round
// 向内偏移:负数
PathsD inner = Clipper.InflatePaths(outline, -10,
JoinType.Miter, EndType.Polygon);
EndType:
Polygon 闭合多边形Joined 开放多线段,端点首尾相连Butt / Square / Round 开放线段端帽需要更精细控制(PolyTree、ZFill 回调等):
ClipperD clipper = new(2); // 2 = 浮点保留 2 位小数精度
clipper.AddSubject(subj);
clipper.AddClip(clip);
PolyTreeD tree = new();
clipper.Execute(ClipType.Union, FillRule.NonZero, tree);
PathsD result = tree.GetAllPaths(FillRule.NonZero);
PolyTree 保留外环/内孔的层级关系,对于带孔多边形非常重要。
double area = Clipper.Area(path); // 有符号面积
PointD c = Clipper.Centroid(path); // 形心
RectD bbox = Clipper.GetBounds(paths); // 包围盒
PathsD simp = Clipper.SimplifyPaths(paths, 0.5); // 简化(容差)
PathsD ramer = Clipper.RamerDouglasPeucker(paths, 0.5); // RDP 简化
PathsD trim = Clipper.TrimCollinear(paths); // 去共线点
// 闵可夫斯基和/差(轮廓膨胀工具)
PathD mink = Clipper.MinkowskiSum(pattern, path, false);
#include "clipper2/clipper.h"
using namespace Clipper2Lib;
PathsD subj{ MakePathD({0,0, 100,0, 100,100, 0,100}) };
PathsD clip{ MakePathD({50,50, 150,50, 150,150, 50,150}) };
auto u = Union(subj, clip, FillRule::NonZero);
auto inflated = InflatePaths(u, 10.0, JoinType::Round, EndType::Polygon);
Clipper.MakePath 或手工构造 PathD/Path64 列表NonZero)Clipper.Union(a, b, FillRule.NonZero)ClipperD 类 + PolyTreeDSimplifyPaths 或 TrimCollinear 清理Clipper.InflatePaths(paths, offset, JoinType.Round, EndType.Polygon) 生成刀路偏置SimplifyPaths,去除多余顶点TrimCollinear| 问题 | 解决 |
|---|---|
| 输出空集 | 检查 FillRule、方向(逆时针 vs 顺时针)、闭合性 |
| 偏移变形 | 调整 JoinType 与 MiterLimit |
| 浮点精度异常 | 改用 Path64 或 ClipperD(precision) |
| 与 Clipper1 不兼容 | API 已变化,参考迁移指南 |
| 库 | 语言 | 主要特性 |
|---|---|---|
| Clipper2 | C++/C#/Delphi/JS | 高性能布尔+偏移,AGG 兼容 |
| CGAL Boolean | C++ | 精确算术,复杂但慢 |
| JTS / NTS | Java/.NET | 完整空间分析,速度中等 |
| Boost.Polygon | C++ | 仅整数,高性能 |
Union/Intersect/Difference/Xor/InflatePaths),比 Clipper1 的类实例方式更简洁。浮点模式直接用 PathsD,整数模式用 Paths64(指定 scale)。FillRule 默认用 NonZero;② InflatePaths 的 JoinType 和 MiterLimit 影响偏移质量——圆形连接(Round)最稳健;③ 浮点模式下 ClipperD(precision) 的 precision 参数控制内部整数转换精度;④ PolyTree 保留孔洞层级。Clipper.Union(pathsA, pathsB, FillRule.NonZero) → Clipper.InflatePaths(result, offset, JoinType.Round, EndType.Polygon) → Clipper.SimplifyPaths(result, tolerance)。