U-Nesting
2D/3D Spatial Optimization Engine - High-performance nesting and bin packing algorithms in Rust with C FFI support
Overview
U-Nesting provides domain-agnostic spatial optimization algorithms for 2D nesting and 3D bin packing problems:
- 2D Nesting - Optimal polygon placement on bounded surfaces
- 3D Bin Packing - Optimal volume arrangement in containers
- Genetic Algorithm - Metaheuristic optimization for complex layouts
- NFP/NFR Computation - Precise collision-free placement
Design Philosophy
U-Nesting is a pure computation engine with no domain-specific logic. Industry context (manufacturing, textile, logistics, etc.) is determined by consuming applications.
┌─────────────────────────────────────────┐
│ Consuming Applications │
│ (Manufacturing, Textile, Logistics) │
└─────────────────┬───────────────────────┘
│ Domain Context
▼
┌─────────────────────────────────────────┐
│ U-Nesting Engine │
│ Pure Geometry + Optimization Math │
│ (Domain Agnostic) │
└─────────────────────────────────────────┘
Features
- 🚀 High Performance - Written in Rust with parallel computation via Rayon
- 🎯 Domain Agnostic - Abstract models adaptable to any spatial optimization
- 📐 2D Support - Polygon nesting with NFP, holes, and curves
- 📦 3D Support - Box and mesh packing with physical constraints
- 🔌 C FFI Support - Use from C#, Python, or any language with C bindings
- 📦 Zero Domain Dependencies - Pure mathematical optimization
Demo
Sample Dataset
A test dataset with 9 different polygon shapes and 50 total pieces on a 500×500 boundary:
Algorithm Comparison
Optimization results using different algorithms on the same dataset:
| Algorithm | Result |
|---|---|
| BLF (Bottom-Left Fill) | |
| NFP (No-Fit Polygon Guided) | |
| GA (Genetic Algorithm) | |
| BRKGA (Biased Random-Key GA) | |
| SA (Simulated Annealing) | |
| GDRR (Greedy Descent with Restarts) | |
| ALNS (Adaptive Large Neighborhood Search) |
Installation
From crates.io
[]
= "0.1" # 2D only (default)
= { = "0.1", = ["3d"] } # 2D + 3D
From GitHub
[]
= { = "https://github.com/iyulab/U-Nesting" }
Quick Start
2D Nesting
use ;
// Define geometries to place
let geometries = vec!;
// Define boundary
let boundary = rectangle;
// Configure and run
let config = default
.with_spacing
.with_margin;
let result = new.solve;
println!;
3D Bin Packing
use ;
// Define geometries to place
let geometries = vec!;
// Define boundary
let boundary = box_shape
.with_max_mass;
// Configure and run
let config = default
.with_gravity
.with_stability;
let result = new.solve;
println!;
Core Concepts
| Concept | Description | 2D | 3D |
|---|---|---|---|
| Geometry | Shape to be placed | Polygon | Box, Mesh |
| Boundary | Containing region | Rectangle, Polygon | Box, Cylinder |
| Placement | Position + orientation | x, y, θ | x, y, z, rotation |
| Spacing | Gap between geometries | Float | Float |
| Margin | Offset from boundary edge | Float | Float |
| Constraint | Placement rules | Rotation, Direction | Orientation, Stability |
Module Structure
u-nesting/
├── core/ # Shared abstractions
│ ├── traits.rs # Geometry, Boundary, Solver
│ ├── ga.rs # Genetic algorithm framework
│ ├── config.rs # Common configuration
│ └── result.rs # Unified result types
│
├── d2/ # 2D Module
│ ├── geometry.rs # Polygon, Point, Segment
│ ├── boundary.rs # 2D boundary definitions
│ ├── nfp.rs # No Fit Polygon
│ ├── nester.rs # Placement algorithms
│ └── io.rs # Import/Export
│
├── d3/ # 3D Module
│ ├── geometry.rs # Box, Mesh, AABB
│ ├── boundary.rs # 3D boundary definitions
│ ├── nfr.rs # No Fit Region
│ ├── packer.rs # Placement algorithms
│ ├── physics.rs # Gravity, stability
│ └── io.rs # Import/Export
│
└── ffi/ # C FFI interface
Algorithms
2D Algorithms
| Algorithm | Description | Quality | Speed |
|---|---|---|---|
| BLF (Bottom-Left Fill) | Greedy placement at bottom-left positions | ★★★☆☆ | ★★★★★ |
| NFP (No-Fit Polygon Guided) | NFP-based collision-free placement | ★★★★☆ | ★★★☆☆ |
| GA (Genetic Algorithm) | Sequence optimization with crossover/mutation | ★★★★★ | ★★☆☆☆ |
| BRKGA (Biased Random-Key GA) | Random-key encoding with elite inheritance | ★★★★★ | ★★☆☆☆ |
| SA (Simulated Annealing) | Temperature-based neighborhood search | ★★★★☆ | ★★★☆☆ |
| GDRR (Greedy Descent with Random Restarts) | Local search with restart diversification | ★★★★☆ | ★★★☆☆ |
| ALNS (Adaptive Large Neighborhood Search) | Destroy-repair with operator selection | ★★★★★ | ★★☆☆☆ |
3D Algorithms
| Algorithm | Description | Quality | Speed |
|---|---|---|---|
| Extreme Point | Placement at extreme points | ★★★☆☆ | ★★★★★ |
| Layer Packing | Layer-based bottom-up placement | ★★★☆☆ | ★★★★☆ |
| Genetic Algorithm | Sequence and rotation optimization | ★★★★★ | ★★☆☆☆ |
Configuration
2D Configuration
let config = Config2D ;
3D Configuration
let config = Config3D ;
FFI Interface
JSON Request (2D)
JSON Request (3D)
C Interface
extern int ;
extern void ;
// C# example
[LibraryImport("u_nesting")]
public static partial int unesting_solve(string request, out IntPtr result);
Result Structure
SolveResult
Performance
2D Benchmarks (GA, 500 generations)
| Geometries | Complexity | Time | Utilization |
|---|---|---|---|
| 20 | Simple | 200ms | 92% |
| 100 | Mixed | 2s | 88% |
| 500 | Complex | 15s | 85% |
3D Benchmarks (Extreme Point)
| Geometries | Complexity | Time | Utilization |
|---|---|---|---|
| 50 | Uniform | 100ms | 85% |
| 200 | Mixed | 1.5s | 78% |
| 100 | Constrained | 3s | 72% |
Architecture
┌──────────────────────────────────────────────┐
│ U-Nesting Engine │
├──────────────────────────────────────────────┤
│ Core: Traits, GA Framework, Config │
├─────────────────────┬────────────────────────┤
│ 2D Module │ 3D Module │
├─────────────────────┼────────────────────────┤
│ Polygon, NFP │ Box, Mesh, NFR │
│ BLF, GA Nester │ EP, LAFF, GA Packer │
└─────────────────────┴────────────────────────┘
▲ ▲
│ │
┌─────────┴────────────────────┴───────────────┐
│ Consuming Applications │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Sheet │ │ Mold │ │Container│ ... │
│ │ Metal │ │ Design │ │ Loading │ │
│ └─────────┘ └─────────┘ └─────────┘ │
└──────────────────────────────────────────────┘
License
Licensed under either of:
- MIT license (LICENSE-MIT)
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
Related Projects
One-liner:
Domain-agnostic 2D/3D spatial optimization engine in Rust.