polygonclippingrs

A Rust crate to compute boolean operations (i.e., intersection, union, difference, and XOR) of two polygons.

Polygon representation

Polygons are represented as a set of "contours". Each contour is a loop of vertices. Points contained by an even number of contours are considered outside the polygon, and points contained by an odd number of contours are considered inside the polygon. Note that "polygon" is not quite correct since this includes "multipolygons" - essentially two completely disjoint shapes.

Invalid/malformed polygons

This implementation does not account for "malformed" polygons. The behavior in these cases is undefined. Some malformed polygons include:

Algorithm

This is an implementation of the paper: Francisco Martínez, Carlos Ogayar, Juan R. Jiménez, Antonio J. Rueda, A simple algorithm for Boolean operations on polygons, Advances in Engineering Software, Volume 64, 2013, Pages 11-19, ISSN 0965-9978, https://doi.org/10.1016/j.advengsoft.2013.04.004. (https://www.sciencedirect.com/science/article/pii/S0965997813000379)

Differences to the original algorithm

These are intentional changes to the original algorithm.

Deficiencies to the original algorithm

These are problems in the implementation that could be addressed in the future.

A personal note

This paper is quite clever, and the general idea is fairly intuitive. However, the paper seems to hide critical information in single sentences that seem benign (e.g., events must be sorted by non-vertical edges first), and more importantly leaves a lot of figuring out the details of the algorithm to the reader. This made it confusing when my version required significant differences to the pseudo-code in the paper. In addition, some flags are described but not how to compute them, and the "special cases" are treated as footnotes rather than parts of the algorithm that take lots of time and work to restructure and figure out.

Alright, I'm done complaining.

License

Licensed under the MIT license.