Improving contour partitioning

At the heart of several tools that I have developed in the past lays a contour partitioning algorithm. It was robust and could handle arbitrary contour shapes, but recently I got a bug report assigned related to CloseHoles tool, and after a careful debugging I found that the partition was indeed robust, but not “clean” enough, it could sometimes create fan like splits which was actually undesired.
What started as a routine bug hunt ended up improving greatly the code for a much more neat contour partition algorithm.

Before
Before
After: new algorithm
After: new algorithm
Improving contour partitioning