惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
N
News and Events Feed by Topic
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Cloudflare Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tailwind CSS Blog
Vercel News
Vercel News
V
Vulnerabilities – Threatpost
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
I
Intezer
Schneier on Security
Schneier on Security
Martin Fowler
Martin Fowler
J
Java Code Geeks
K
Kaspersky official blog
H
Heimdal Security Blog
O
OpenAI News
I
InfoQ
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
云风的 BLOG
云风的 BLOG
Hacker News - Newest:
Hacker News - Newest: "LLM"
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
S
Security @ Cisco Blogs
Security Latest
Security Latest
PCI Perspectives
PCI Perspectives
H
Hacker News: Front Page
C
CERT Recently Published Vulnerability Notes
博客园_首页
The Last Watchdog
The Last Watchdog
罗磊的独立博客
L
LINUX DO - 热门话题
U
Unit 42
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Scott Helme
Scott Helme

博客园 - 我才是银古

第16章:常见问题、排错与最佳实践 第15章:扩展生态、MCAD 与外部集成 第12章:实战案例:机械结构与 3D 打印零件 第14章:构建、测试、调试与贡献流程 第13章:OpenSCAD 源码架构与核心执行流程 第11章:预览、渲染、网格精度与性能优化 第09章:列表推导、递归与算法建模 第08章:参数化零件库与复用设计 第10章:导入导出、命令行与自动化 第06章:CSG 布尔建模方法 第07章:二维图形、拉伸、旋转与投影 第05章:基础几何、坐标系与变换 第04章:参数、变量、函数、模块与作用域 OpenSCAD 教程目录 第03章:OpenSCAD 语言基础 第02章:安装、环境配置与开发工作流 第01章:OpenSCAD 项目全景与学习路线 第02章:源码获取、编译与开发环境配置 第01章:OCCT项目全景与学习路线 第18章:二次开发实战与综合案例 第18章:综合实战案例 第17章:数据交换与协同 第16章:源码架构与二次开发 第15章:插件与自定义工作台开发 第14章:Python脚本宏与自动化 第13章:FEM仿真分析 第12章:CAM数控加工 第11章:SurfaceMesh与逆向工程 第10章:Draft二维绘图与BIM建筑 第09章:工程图TechDraw 第07章:参数化表达式与Spreadsheet 第08章:装配设计Assembly 第06章:Part工作台与几何内核 第05章:PartDesign实体特征建模 第04章:草图Sketcher约束建模 第02章:安装版本与工作环境配置 第03章:界面工作台与基础操作 第01章:项目全景与学习路线 第十二章:插件开发、研究功能与最佳实践 第十章:定时任务与自动化(Cron) 第七章:技能、记忆与自学习闭环 第八章:MCP 集成与上下文文件 第六章:工具系统与终端后端 第五章:模型供应商与配置体系 Hermes Agent 教程目录 第十一章:语音、视觉、浏览器与子代理协作 第四章:CLI/TUI 与会话管理 第十二章:学习路线、实战方案与最佳实践 第十一章:源码结构、开发调试与插件开发 第十章:自动化、远程访问、日志与排障 第九章:Control UI、节点、Canvas 与语音能力 第七章:工具、技能、插件与能力扩展 第八章:安全模型、访问控制与沙箱实践 第六章:Agent 工作区、会话与多智能体路由 第五章:多通道消息接入与聊天平台配置 第四章:配置体系、模型接入与认证管理 第三章:Gateway 架构、协议与运行机制 第二章:安装、环境准备与快速上手 第一章:OpenClaw 项目概览与核心定位 oh-my-openagent 教程目录 09-命令模型回退与配置参考 10-实战案例最佳实践与故障排除 05-工作模式-Ultrawork-Prometheus-Atlas 08-Hooks与MCP系统 06-Category与Skill系统 07-核心工具链 04-智能体全景详解 03-安装与环境配置 02-整体架构与多模型编排机制 01-项目简介与核心理念 01-项目概览与学习路线 02-安装部署与工具适配 03-Skill机制与using-superpowers 05-TDD系统化调试与完成前验证 04-需求澄清方案设计与计划编写 07-并行智能体子智能体与Git-Worktree 第六章:代码审查、反馈处理与分支收尾 08-中国特色Skills与本土团队落地 09-MCP构建工作流执行与自定义Skill 第23章:FreeCAD-Python-API Clipper2 C# 源码解读教程 第19章:PolyTree 多边形树结构 第20章:实际应用与最佳实践 第18章:Minkowski 和与差 第17章:RectClip 矩形裁剪优化 第16章:ClipperOffset 偏移类详解 第15章:填充规则详解 第14章:布尔运算执行流程 第13章:ClipperD 浮点裁剪类 第11章:OutRec 与 OutPt 输出结构 第9章:Active 活动边结构 第10章:Vertex 顶点与 LocalMinima 局部极小值 第12章:Clipper64 裁剪类详解 第7章:高精度运算与128位整数 第8章:ClipperBase 基类详解 第5章:枚举类型与常量定义 第6章:InternalClipper 内部工具类 第2章:核心数据结构 - Point64、PointD 第3章:路径与多边形表示 - Path64、PathD、Paths64、PathsD 第4章:矩形边界 - Rect64、RectD
第15章:孔洞检测与处理
我才是银古 · 2026-06-24 · via 博客园 - 我才是银古

第15章:孔洞检测与处理

15.1 概述

在多边形布尔运算中,正确识别和处理孔洞是至关重要的。本章将分析 Clipper 如何确定多边形是外轮廓还是孔洞,以及如何维护它们之间的层次关系。

15.2 孔洞的定义

15.2.1 几何定义

  • 外轮廓:逆时针方向的闭合路径,包围"内部"区域
  • 孔洞:顺时针方向的闭合路径,从外轮廓中"挖出"区域
外轮廓(逆时针)        孔洞(顺时针)
    ┌───────────┐         ┌───────┐
    │           │         │ ┌───┐ │
    │           │         │ │   │ │
    │           │         │ └───┘ │
    └───────────┘         └───────┘
         ↺                    ↻

15.2.2 面积符号

public static double Area(Path poly)
{
    int cnt = (int)poly.Count;
    if (cnt < 3) return 0;
    double a = 0;
    for (int i = 0, j = cnt - 1; i < cnt; ++i)
    {
        a += ((double)poly[j].X + poly[i].X) * 
             ((double)poly[j].Y - poly[i].Y);
        j = i;
    }
    return -a * 0.5;
}
  • 正面积:逆时针(外轮廓)
  • 负面积:顺时针(孔洞)

15.3 SetHoleState

在创建新输出记录时设置孔洞状态:

private void SetHoleState(TEdge e, OutRec outRec)
{
    TEdge e2 = e.PrevInAEL;
    TEdge eTmp = null;
    
    while (e2 != null)
    {
        if (e2.OutIdx >= 0 && e2.WindDelta != 0)
        {
            if (eTmp == null)
                eTmp = e2;
            else if (eTmp.OutIdx == e2.OutIdx)
                eTmp = null;  // 配对边,不计入
        }
        e2 = e2.PrevInAEL;
    }

    if (eTmp == null)
    {
        outRec.FirstLeft = null;
        outRec.IsHole = false;
    }
    else
    {
        outRec.FirstLeft = m_PolyOuts[eTmp.OutIdx];
        outRec.IsHole = !outRec.FirstLeft.IsHole;
    }
}

15.3.1 算法原理

从当前边向左遍历 AEL,计算经过的边数:

  • 偶数边:外轮廓
  • 奇数边:孔洞
        e1     e2     e3     e4
AEL: ───│──────│──────│──────│──── 
        │      │      │      │
        ▼      ▼      ▼      ▼
      外轮廓  孔洞   外轮廓  孔洞

新边从 e4 右侧开始:
经过 e4(1)→ e3(2)→ e2(3)→ e1(4)
4 条边,偶数 → 新边是孔洞

15.3.2 配对边处理

if (eTmp.OutIdx == e2.OutIdx)
    eTmp = null;  // 配对边

如果遇到属于同一输出记录的两条边,它们相互抵消。

15.4 FixHoleLinkage

修复孔洞链接:

private void FixHoleLinkage(OutRec outRec)
{
    // 确保 FirstLeft 指向有效的外轮廓
    if (outRec.FirstLeft == null ||                
        (outRec.IsHole != outRec.FirstLeft.IsHole &&
        outRec.FirstLeft.Pts != null)) 
        return;

    OutRec orfl = outRec.FirstLeft;
    while (orfl != null && 
           ((orfl.IsHole == outRec.IsHole) || orfl.Pts == null))
        orfl = orfl.FirstLeft;
    outRec.FirstLeft = orfl;
}

15.4.1 修复逻辑

确保孔洞的 FirstLeft 指向一个:

  1. 有效的输出记录(Pts != null)
  2. 不同类型的记录(孔洞的 FirstLeft 应该是外轮廓)

15.5 FirstLeft 链

FirstLeft 建立了输出记录之间的父子关系:

PolyTree 结构:
Root
├── OutRec1 (外轮廓)
│   ├── OutRec2 (孔洞, FirstLeft = OutRec1)
│   │   └── OutRec3 (岛屿, FirstLeft = OutRec2)
│   └── OutRec4 (孔洞, FirstLeft = OutRec1)
└── OutRec5 (外轮廓)

15.6 FixupFirstLefts 方法系列

15.6.1 FixupFirstLefts1

private void FixupFirstLefts1(OutRec OldOutRec, OutRec NewOutRec)
{ 
    foreach (OutRec outRec in m_PolyOuts)
    {
        OutRec firstLeft = ParseFirstLeft(outRec.FirstLeft);
        if (outRec.Pts != null && firstLeft == OldOutRec)
        {
            if (Poly2ContainsPoly1(outRec.Pts, NewOutRec.Pts))
                outRec.FirstLeft = NewOutRec;
        }
    }
}

当一个多边形被分割时,更新子多边形的 FirstLeft。

15.6.2 FixupFirstLefts2

private void FixupFirstLefts2(OutRec innerOutRec, OutRec outerOutRec)
{
    OutRec orfl = outerOutRec.FirstLeft;
    foreach (OutRec outRec in m_PolyOuts)
    {
        if (outRec.Pts == null || outRec == outerOutRec || outRec == innerOutRec) 
            continue;
        OutRec firstLeft = ParseFirstLeft(outRec.FirstLeft);
        if (firstLeft != orfl && firstLeft != innerOutRec && firstLeft != outerOutRec) 
            continue;
        if (Poly2ContainsPoly1(outRec.Pts, innerOutRec.Pts))
            outRec.FirstLeft = innerOutRec;
        else if (Poly2ContainsPoly1(outRec.Pts, outerOutRec.Pts))
            outRec.FirstLeft = outerOutRec;
        else if (outRec.FirstLeft == innerOutRec || outRec.FirstLeft == outerOutRec) 
            outRec.FirstLeft = orfl;
    }
}

当一个多边形被分割成内外两部分时使用。

15.6.3 FixupFirstLefts3

private void FixupFirstLefts3(OutRec OldOutRec, OutRec NewOutRec)
{
    foreach (OutRec outRec in m_PolyOuts)
    {
        OutRec firstLeft = ParseFirstLeft(outRec.FirstLeft);
        if (outRec.Pts != null && firstLeft == OldOutRec) 
            outRec.FirstLeft = NewOutRec;
    }
}

简单替换 FirstLeft 引用。

15.7 Poly2ContainsPoly1

判断一个多边形是否包含另一个:

private static bool Poly2ContainsPoly1(OutPt outPt1, OutPt outPt2)
{
    OutPt op = outPt1;
    do
    {
        int res = PointInPolygon(op.Pt, outPt2);
        if (res >= 0) return res > 0;
        op = op.Next;
    }
    while (op != outPt1);
    return true;
}

遍历第一个多边形的所有点,检查是否在第二个多边形内部。

15.8 PointInPolygon

点在多边形内判断:

public static int PointInPolygon(IntPoint pt, Path path)
{
    // returns 0 if false, +1 if true, -1 if pt ON polygon boundary
    int result = 0, cnt = path.Count;
    if (cnt < 3) return 0;
    IntPoint ip = path[0];
    
    for (int i = 1; i <= cnt; ++i)
    {
        IntPoint ipNext = (i == cnt ? path[0] : path[i]);
        
        if (ipNext.Y == pt.Y)
        {
            if ((ipNext.X == pt.X) || (ip.Y == pt.Y &&
                ((ipNext.X > pt.X) == (ip.X < pt.X)))) 
                return -1;  // 在边界上
        }
        
        if ((ip.Y < pt.Y) != (ipNext.Y < pt.Y))
        {
            if (ip.X >= pt.X)
            {
                if (ipNext.X > pt.X) 
                    result = 1 - result;
                else
                {
                    double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) -
                               (double)(ipNext.X - pt.X) * (ip.Y - pt.Y);
                    if (d == 0) return -1;
                    else if ((d > 0) == (ipNext.Y > ip.Y)) 
                        result = 1 - result;
                }
            }
            else
            {
                if (ipNext.X > pt.X)
                {
                    double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) -
                               (double)(ipNext.X - pt.X) * (ip.Y - pt.Y);
                    if (d == 0) return -1;
                    else if ((d > 0) == (ipNext.Y > ip.Y)) 
                        result = 1 - result;
                }
            }
        }
        ip = ipNext;
    }
    return result;
}

15.8.1 算法:射线法

从点发出水平射线,计算与多边形边的交叉次数:

  • 奇数:内部
  • 偶数:外部
  • 在边上:返回 -1

15.9 方向修正

在 ExecuteInternal 的后处理阶段:

foreach (OutRec outRec in m_PolyOuts)
{
    if (outRec.Pts == null || outRec.IsOpen) continue;
    if ((outRec.IsHole ^ ReverseSolution) == (Area(outRec) > 0))
        ReversePolyPtLinks(outRec.Pts);
}

15.9.1 正确方向

IsHole ReverseSolution 应有面积符号
false false > 0(逆时针)
true false < 0(顺时针)
false true < 0(顺时针)
true true > 0(逆时针)

15.10 BuildResult2 中的层次构建

private void BuildResult2(PolyTree polytree)
{
    polytree.Clear();

    // 第一遍:创建所有 PolyNode
    polytree.m_AllPolys.Capacity = m_PolyOuts.Count;
    for (int i = 0; i < m_PolyOuts.Count; i++)
    {
        OutRec outRec = m_PolyOuts[i];
        // ...
        FixHoleLinkage(outRec);
        PolyNode pn = new PolyNode();
        polytree.m_AllPolys.Add(pn);
        outRec.PolyNode = pn;
        // ... 填充顶点 ...
    }

    // 第二遍:建立父子关系
    polytree.m_Childs.Capacity = m_PolyOuts.Count;
    for (int i = 0; i < m_PolyOuts.Count; i++)
    {
        OutRec outRec = m_PolyOuts[i];
        if (outRec.PolyNode == null) continue;
        else if (outRec.IsOpen)
        {
            outRec.PolyNode.IsOpen = true;
            polytree.AddChild(outRec.PolyNode);
        }
        else if (outRec.FirstLeft != null && 
                 outRec.FirstLeft.PolyNode != null)
            outRec.FirstLeft.PolyNode.AddChild(outRec.PolyNode);
        else
            polytree.AddChild(outRec.PolyNode);
    }
}

15.11 使用示例

Clipper clipper = new Clipper();

// 添加带孔的多边形
Path outer = new Path();
outer.Add(new IntPoint(0, 0));
outer.Add(new IntPoint(200, 0));
outer.Add(new IntPoint(200, 200));
outer.Add(new IntPoint(0, 200));

Path hole = new Path();
hole.Add(new IntPoint(50, 50));
hole.Add(new IntPoint(50, 150));
hole.Add(new IntPoint(150, 150));
hole.Add(new IntPoint(150, 50));

clipper.AddPath(outer, PolyType.ptSubject, true);
clipper.AddPath(hole, PolyType.ptSubject, true);

// 使用 PolyTree 获取层次结构
PolyTree tree = new PolyTree();
clipper.Execute(ClipType.ctUnion, tree);

// 遍历结果
void ProcessNode(PolyNode node, int depth)
{
    string indent = new string(' ', depth * 2);
    if (node.Contour.Count > 0)
    {
        string type = node.IsHole ? "孔洞" : "外轮廓";
        Console.WriteLine($"{indent}{type}: {node.Contour.Count}点");
    }
    foreach (var child in node.Childs)
        ProcessNode(child, depth + 1);
}

ProcessNode(tree, 0);

15.12 本章小结

本章详细分析了 Clipper 的孔洞处理:

  1. 孔洞识别

    • 基于面积符号判断方向
    • SetHoleState 使用 AEL 计数
  2. 层次关系

    • FirstLeft 链建立父子关系
    • FixupFirstLefts 系列修复链接
  3. 包含测试

    • Poly2ContainsPoly1 判断包含
    • PointInPolygon 点在多边形内
  4. 方向修正

    • 确保外轮廓逆时针
    • 确保孔洞顺时针
  5. 结果构建

    • BuildResult2 构建 PolyTree
    • 保留完整的层次结构

上一章:输出多边形构建 | 返回目录 | 下一章:填充规则详解