Skip to main content

PyNest is a parallel SVG nesting engine I built to solve a practical version of the 2D irregular bin-packing problem: placing arbitrary vector shapes inside a fixed sheet area while minimizing material waste and ensuring zero overlap. Unlike simple rectangle packing, nesting irregular polygons introduces geometric complexity, rotation handling, and a rapidly growing search space. PyNest approaches this using computational geometry, heuristic placement, and parallel optimization.

The process starts by parsing SVG files and normalizing all shapes into polygonal representations. Since SVG paths often contain Bézier curves, they are discretized into linear segments so that collision detection can operate purely on polygons. For each shape, I compute and cache metadata such as area, axis-aligned bounding boxes, and rotated variants. Precomputing rotations avoids repeated trigonometric transformations during placement evaluation and significantly reduces overhead.

Collision detection is handled in two stages. First, fast bounding box checks eliminate most invalid placements. Only when bounding boxes overlap does the system perform precise polygon intersection tests using edge intersection and point-in-polygon checks. This layered strategy keeps geometric computation manageable as the number of shapes increases.

For placement, PyNest uses a bottom-left heuristic. Each shape is evaluated across candidate positions and allowed rotations, prioritizing the lowest valid vertical position and then the leftmost horizontal position. While greedy placement is deterministic and efficient, it is rarely optimal. To improve layout quality, the engine runs multiple iterations with different shape orderings and scores each layout based on used height and wasted area.

To explore more of the solution space within reasonable time, PyNest parallelizes the optimization phase. Multiple worker processes test different permutations simultaneously, and the best-scoring layout is selected. This parallel search improves packing density without making runtime impractical.

The final output is a reconstructed SVG where each shape is transformed using computed translation and rotation matrices. The result is directly usable in fabrication workflows such as laser cutting or CNC routing.

PyNest reflects a balance between geometric correctness and practical performance. Instead of pursuing computationally expensive exact solutions, it combines heuristic placement, caching strategies, and parallel execution to produce efficient layouts suitable for real-world manufacturing scenarios.