返回资讯中心
外部精选
软件工程
#开发者实时流

Martinez-Rueda 多边形裁剪算法笔记:从线段到路径

来源标题:Notes on the Martinez-Rueda Polygon Clipping Algorithm: Segments to Paths

这份笔记讲解 Martinez-Rueda 多边形裁剪算法如何把线段组织成路径。内容聚焦于从线段到路径的转换环节,并梳理该算法的相关要点。

Hacker News 最新the_origami_fox2026/11/30 · 今天16 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

The Martinez-Rueda algorithm computes boolean operations between polygons. It works at the segment level. This article describes the graph faces computation algorithm which is used to convert those segments to paths (lists of ordered points).

This is part of a group of articles on the Martinez-Rueda Polygon algorithm. The other articles are:

The Martinez-Rueda algorithm is a generic, versatile algorithm for calculating boolean operations between polygons such as intersections (also known as polygon clipping) as well as unions, differences, and XORs. This is useful for graphic art programs (e.g. Inkscape) and also geospatial/geographic information systems (GIS). It can also be used for computer games, but often simpler, faster algorithms are used there instead.

I have implemented Martinez-Rueda in my PolygonAlgorithms.jl Julia package. I recently updated this package to use graph faces computation to convert segments to paths. See Pull Request 35. This closed issue #24 Handle improper self-intersecting polygons. This is the focus of the blog post.

The full description of the Martinez-Rueda algorithm can be found in their paper A new algorithm for computing Boolean operations on polygons (2009). At a high level, the main idea of the Martinez-Rueda algorithm is to annotate segments with filled above and filled below during a line sweep, and then to use those annotations to select the relevant segments in the output polygon.

The algorithm outputs a collection of segments. However in most cases the input is a path - an ordered list of points - and the desired output is also a path. Therefore the challenge is to map segments to paths.

So if the output of Martinez-Rueda is a list of segments, which is a list of pairs of points:

then the task is to convert it to a list of paths, which is a list of lists of points:

A follow-up paper, A simple algorithm for Boolean operations on polygons (2013), describes an algorithm called computeContour. It relies on the fact that the line sweep processes events in sorted order, left to right and then bottom to top, so its output is already sorted. They also store a pointer in each event to its connected event (start/end of the same edge). (My code does this as well.) Together, this means the next edge can be found in constant time, because events at the same point will always sit next to each other in the output. However, the paper notes this only works when exactly two edges meet at each vertex.

My original code did not use this algorithm. Instead it used a “segment chaining” algorithm based on this blog post https://sean.fun/a/polygon-clipping-pt2/. Here is the description from the blog post:

The basic algorithm is to keep a list of open chains.

When we process a segment, search to see if it can be added to the end of any open chains.

If it can’t be added to a chain, then create a new open chain with the single segment inside of it.

If it can be added to two open chains, then combine the chains into a single open chain.

Otherwise, if it is only added to a single open chain, then we’ll need to check if adding the segment causes the chain to close. If so, close the chain and add it to the region list (and remove it from the list of open chains).

This algorithm looks simple but relies on the same assumption as computeContour: exactly two segments meet at each vertex. For improper polygons this is not true. This resulted in a common “There are still open chains at the end of processing all segments” error at the end of the algorithm that end users could not address easily.

Here are some examples where the segment chaining algorithm fails or produces inconsistent results:

There are workarounds to this issue, however I eventually decided to instead implement a totally new algorithm which does not have this issue at all. This is the graph faces computation algorithm.

My main source for this algorithm was this answer on StackOverflow.1

Given a directed graph of edges $(u, v)$, the task of computing the faces of the graph is known as “graph face computation”. A “face” is a region of the plane bounded by a loop of half-edges. A graph can have multiple interior faces and one exterior face per disconnected region, which covers all interior faces in that region. In total the faces have double the area of the graph. The algorithm returns all faces, and so the step afterwards is to discern which ones are relevant. See the Segments to Paths section. The algorithm is linear in the number of edges $E$: $\mathcal{O}(E)$. (This assumes the number of half-edges per node is low, so the sorting cost can be ignored.)

Graph faces computation has the following advantages over segment chaining:

The first step of the algorithm is to convert the segments to a directed graph of $\text{nodes} \rightarrow \text{half-edges}$. “Directed” means that the half-edge $(u, v)$ from $u$ to $v$ is different to the half-edge $(v, u)$ from $v$ to $u$. The algorithm requires that the half-edges are sorted clockwise around each node, so that is done here as well.

The simplest implementation of snap_to_grid is to round the points to a given decimal place e.g. to 6 decimal places. My code uses a slightly more complex version that additionally checks if any rounded points already exist in half the spacing, e.g. $0.5 \times 10^{-6}$, and snaps to that point instead. This is to prevent aggressive rounding if there are trailing numeric errors, e.g. it snaps 0.007812500005 to 0.007812 instead of rounding to 0.007813. The sort_clockwise function compares angles calculated using atan.

The main insight of the algorithm is the incredibly simple observation that for the edge $(X, Y)$, if $Z$ is the next neighbor clockwise around the node $X$, then the half-edge $(Z, X)$ comes before $(X, Y)$. Put another way, if we know the half-edges around the node $X$ are $[(X, Y),(X, Z),…]$ in that order, then we know that $(Z, X)$ connects to $(X, Y)$. Therefore we can map every edge to the edge that comes after it, and then we can follow this map to walk all edges in the graph. This will return all exterior faces as counter-clockwise and all interior faces as clockwise.

First, loop over all nodes and map all connections:

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →