NFP - No Fit Polygon
A Rust library for computing the No Fit Polygon (Minkowski sum) of two convex polygons for nesting and packing optimization.
What is NFP?
The No Fit Polygon defines the forbidden region where one polygon cannot be placed relative to another without collision. When polygon B's origin is outside the NFP, the polygons do not overlap. When it's inside, they do collide.
Features
- ✅ Convex polygon support with Minkowski sum via vertex combinations and convex hull
- ✅ Fast computation using Graham scan algorithm
- ✅ Validated output - CCW orientation, convex, no self-intersections
- ✅ Automatic orientation correction - handles CCW/CW input
- ✅ Zero-copy collision detection - use point-in-polygon test on NFP
Installation
Add this to your Cargo.toml:
[]
= "0.3"
Quick Start
use *;
// Two convex polygons at origin (CCW orientation)
let square_a = vec!;
let square_b = vec!;
// Compute NFP
match nfp
Collision Detection
Once you have the NFP, detect collisions by testing if B's origin is inside or outside:
use *;
// Compute NFP first
let square_a = vec!;
let square_b = vec!;
let nfp = nfp.unwrap;
// Check if placing B at (15.0, 15.0) causes collision
let test_position = point;
if point_in_polygon else
Requirements
- Both input polygons must be convex
- Both polygons must have at least 3 vertices
- Polygons should be in counter-clockwise (CCW) orientation (auto-corrected if needed)
- Polygons must be closed (last point connects to first conceptually)
API Reference
NFPConvex::nfp(poly_a, poly_b) -> Result<Vec<Point>, NfpError>
Computes the No Fit Polygon for two convex polygons using Minkowski sum.
Arguments:
poly_a- First convex polygon as slice of pointspoly_b- Second convex polygon as slice of points
Returns:
Ok(Vec<Point>)- NFP as counter-clockwise convex polygonErr(NfpError)- If input validation fails
Time Complexity: O(n·m log(n·m)) where n = |A| vertices, m = |B| vertices
NfpError
Error enumeration for NFP operations.
Variants:
EmptyPolygon- One or both input polygons are emptyInsufficientVertices- One or both polygons have fewer than 3 vertices
Example:
use *;
match nfp
Point
2D coordinate point from the togo geometry library.
Fields:
x: f64- X coordinatey: f64- Y coordinate
point(x, y) -> Point
Helper function to create a new Point.
use *;
let p = point;
assert_eq!;
assert_eq!;
Algorithm
The implementation uses the Minkowski Sum via Vertex Combinations:
- Validate inputs (non-empty, convex, ≥3 vertices)
- Ensure counter-clockwise orientation
- Negate polygon B (reflection through origin)
- Generate all vertex sum combinations: {a + b | a ∈ A, b ∈ -B}
- Compute convex hull of sum points using Graham scan
- Return convex hull boundary as NFP
Limitations
- Current version supports Convex polygons only
References
- Minkowski sum: https://en.wikipedia.org/wiki/Minkowski_addition
- No-Fit Polygon: https://en.wikipedia.org/wiki/No-fit_polygon
- Graham scan: https://en.wikipedia.org/wiki/Graham_scan
Related Projects
NFP is part of the Nest2D collection of nesting and packing tools.