Clipper1

From znlgis/opengis-skills · View interactive version

Clipper1 is an AI agent skill from the GitHub repository znlgis/opengis-skills. Use when maintaining legacy code on the Clipper 1.x API — IntPoint integer paths with a scale factor, Clipper.AddPath/Execute for ctUnion/ctIntersection/ctDifference/ctXor, ClipperOffset for jtRound/jtMiter buffering, PolyTree hole hierarchy, and the pyclipper (1.4.0) / js-clipper bindings. Clipper1

How to use this skill

Skill file

项目地址(V6 官方主页): <https://sourceforge.net/p/polyclipping/>

官方文档: <https://sourceforge.net/p/polyclipping/wiki/Home/>

最新版本: V6 末版 6.4.2(冻结,不再新增功能;pyclipper 1.4.0 内嵌的即为该版,截至 2026-11,见 SourceForge)。原 GitHub 仓库 github.com/AngusJohnson/Clipper 已重定向到 Clipper2

包管理器: NuGet Clipper 6.4.0(Angus Johnson 发布)| PyPI pyclipper 1.4.0(封装 Clipper 6.4.2)| npm js-clipper 1.0.1

许可证: Boost Software License 1.0

概述

Clipper1 主要特征:

新项目优先使用 Clipper2,其性能与 API 均优于 Clipper1。本 SKILL 主要用于维护遗留代码。


安装

# C#(NuGet 包作者为 Angus Johnson,最新 6.4.0,略旧于 C++ V6 末版 6.4.2)
dotnet add package Clipper

# JavaScript
npm install js-clipper          # 1.0.1;或 clipper-lib(6.4.2,与 C++ V6 末版同版号)

# Python
pip install pyclipper           # 1.4.0,内嵌 Clipper 6.4.2

验证:

python -c "import pyclipper; print(pyclipper.__version__)"

C++ 通常直接拷贝 clipper.hpp/.cpp 到工程。


核心 API

核心数据结构

using ClipperLib;
using IntPoint = ClipperLib.IntPoint;

const long Scale = 1000000;     // 浮点 → 整数缩放因子

List<IntPoint> path = new() {
    new IntPoint(0, 0),
    new IntPoint(100 * Scale, 0),
    new IntPoint(100 * Scale, 100 * Scale),
    new IntPoint(0, 100 * Scale),
};
List<List<IntPoint>> paths = new() { path };

布尔运算

var clipper = new Clipper();
clipper.AddPath(subject, PolyType.ptSubject, true);   // true = 闭合
clipper.AddPath(clip,    PolyType.ptClip,    true);

var solution = new List<List<IntPoint>>();
clipper.Execute(ClipType.ctUnion, solution,
    PolyFillType.pftNonZero, PolyFillType.pftNonZero);

ClipType:ctIntersection、ctUnion、ctDifference、ctXor

PolyFillType:pftEvenOdd、pftNonZero、pftPositive、pftNegative


多边形偏移(ClipperOffset)

var co = new ClipperOffset();
co.AddPaths(paths, JoinType.jtRound, EndType.etClosedPolygon);

var solution = new List<List<IntPoint>>();
co.Execute(ref solution, 10 * Scale);     // 正:外扩;负:内缩

JoinType:jtSquare、jtMiter、jtRound

EndType:etClosedPolygon、etClosedLine、etOpenButt、etOpenSquare、etOpenRound


PolyTree(保留环层级)

var tree = new PolyTree();
clipper.Execute(ClipType.ctUnion, tree,
    PolyFillType.pftNonZero, PolyFillType.pftNonZero);

foreach (var node in tree.Childs)        // 顶层是外环
    foreach (var hole in node.Childs)    // 子节点是孔
        Console.WriteLine(hole.Contour.Count);

实用方法

double area    = Clipper.Area(path);
bool   ccw     = Clipper.Orientation(path);
var    cleaned = Clipper.CleanPolygon(path, 1.415);    // 去重/共线
var    simp    = Clipper.SimplifyPolygon(path, PolyFillType.pftNonZero);
var    reverse = Clipper.ReversePath(path);

Python(pyclipper)

import pyclipper

scale = 1_000_000
subj  = [[(0,0),(100,0),(100,100),(0,100)]]
clip  = [[(50,50),(150,50),(150,150),(50,150)]]

pc = pyclipper.Pyclipper()
pc.AddPaths(pyclipper.scale_to_clipper(subj, scale),
            pyclipper.PT_SUBJECT, True)
pc.AddPaths(pyclipper.scale_to_clipper(clip, scale),
            pyclipper.PT_CLIP, True)
sol = pc.Execute(pyclipper.CT_UNION,
                 pyclipper.PFT_NONZERO,
                 pyclipper.PFT_NONZERO)
sol = pyclipper.scale_from_clipper(sol, scale)

# 偏移
pco = pyclipper.PyclipperOffset()
pco.AddPaths(pyclipper.scale_to_clipper(subj, scale),
             pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
sol = pco.Execute(10 * scale)

C++ 用法

#include "clipper.hpp"
using namespace ClipperLib;

Paths subj{{ {0,0},{1000,0},{1000,1000},{0,1000} }};
Paths clip{{ {500,500},{1500,500},{1500,1500},{500,1500} }};
Clipper c;
c.AddPaths(subj, ptSubject, true);
c.AddPaths(clip, ptClip, true);
Paths solution;
c.Execute(ctUnion, solution, pftNonZero, pftNonZero);

典型工作流

工作流一:CAD/GIS 中多边形布尔运算

  1. 确定浮点精度需求,选定 scale 系数(如 1,000,000 表示微米精度)
  2. 将输入几何(Polygon/MultiLine 等)转换为 List<IntPoint> 格式,乘以 scale 取整
  3. 创建 Clipper 实例,AddPath 添加 Subject 和 Clip
  4. 调用 Execute 执行布尔运算(Union/Intersect/Difference/Xor)
  5. 将结果坐标除以 scale 还原为浮点坐标
  6. 如需保留孔洞关系,使用 PolyTree 替代 List<List<IntPoint>>

工作流二:多边形轮廓偏移(生成安全区/缓冲区)

  1. 构建多边形路径(闭合路径用 etClosedPolygon,开放用 etOpenButt 等)
  2. 创建 ClipperOffset,通过 AddPaths 添加输入
  3. 调用 Execute,正值为外扩(膨胀),负值为内缩(侵蚀)
  4. 检查结果是否自交——偏移前可先用 SimplifyPolygon 清理输入
  5. 输出偏移后的多边形用于刀路生成、安全距离计算等

与 Clipper2 主要差异

| 维度 | Clipper1 | Clipper2 |

|------|----------|----------|

| 数值类型 | 整数 + scale | 整数 / 浮点双模式 |

| 性能 | 较慢 | 显著更快 |

| API | Clipper 类 | 顶层 Union/Intersect/... |

| 简化 | SimplifyPolygon | SimplifyPaths 更准确 |

| 维护 | 已停止新功能 | 活跃维护 |

迁移要点:


常见问题

| 报错 / 现象 | 原因 | 处置 |

|------------|------|------|

| 坐标整体偏移或舍入失真 | Clipper1 用 IntPoint 整数坐标,浮点输入必须定标 | 按坐标范围加大 scale(如 1e6 级),运算后再除回 |

| 偏移结果出现自相交/碎片 | 输入多边形本身存在自交或极窄边 | 偏移前先 SimplifyPolygon/CleanPolygon |

| 输出 path 首尾点重复 | 调用方按旧习惯手动闭合了轮廓 | Clipper 自己闭环,去掉重复首点 |

| Mac/Linux 编译失败 | 平台与默认整数位宽的宏不一致 | 按目标平台启用 use_int32 / use_xyz |

| 布尔结果丢了孔洞层级 | 出参是扁平 List<List<IntPoint>>,只保留外环与孔的顺序信息 | 改用 PolyTree(顶层 Childs 为外环、其子节点为孔) |

| 外扩后尖角被削平或出现毛刺 | 默认 JoinType 与目标形状不匹配 | 尖角用 jtMiter(配合 MiterLimit)、圆弧用 jtRound、平角用 jtSquare;内缩毛刺先 CleanPolygon |

| 开放线偏移结果变成闭合条带 | EndType 误用 etClosedPolygon | 改 etOpenButt/etOpenSquare/etOpenRound |

| 面积正负号看不懂 | Clipper.Area 的符号表示朝向 | 正为外环、负为孔;需要翻转用 ReversePath,判断用 Orientation |


AI 使用建议


相关技能


参考资源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.