u_nesting_d2/lib.rs
1//! # U-Nesting 2D
2//!
3//! 2D nesting algorithms for the U-Nesting spatial optimization engine.
4//!
5//! This crate provides polygon-based 2D nesting with NFP (No-Fit Polygon) computation
6//! and various placement algorithms.
7//!
8//! ## Features
9//!
10//! - Polygon geometry with holes support
11//! - Multiple placement strategies (BLF, NFP-guided, GA, BRKGA, SA)
12//! - Convex hull and convexity detection
13//! - Configurable rotation and mirroring constraints
14//! - NFP-based collision-free placement
15//! - Spatial indexing for fast queries
16//!
17//! ## Quick Start
18//!
19//! ```rust
20//! use u_nesting_d2::{Geometry2D, Boundary2D, Nester2D, Config, Strategy, Solver};
21//!
22//! // Create geometries
23//! let rect = Geometry2D::rectangle("rect1", 100.0, 50.0)
24//! .with_quantity(5)
25//! .with_rotations_deg(vec![0.0, 90.0]);
26//!
27//! // Create boundary
28//! let boundary = Boundary2D::rectangle(500.0, 300.0);
29//!
30//! // Configure and solve
31//! let config = Config::new()
32//! .with_strategy(Strategy::NfpGuided)
33//! .with_spacing(2.0);
34//!
35//! let nester = Nester2D::new(config);
36//! let result = nester.solve(&[rect], &boundary).unwrap();
37//!
38//! println!("Placed {} items, utilization: {:.1}%",
39//! result.placements.len(),
40//! result.utilization * 100.0);
41//! ```
42//!
43//! ## Geometry Creation
44//!
45//! ```rust
46//! use u_nesting_d2::Geometry2D;
47//!
48//! // Rectangle
49//! let rect = Geometry2D::rectangle("r1", 100.0, 50.0);
50//!
51//! // Circle (approximated)
52//! let circle = Geometry2D::circle("c1", 25.0, 32);
53//!
54//! // L-shape
55//! let l_shape = Geometry2D::l_shape("l1", 100.0, 80.0, 30.0, 30.0);
56//!
57//! // Custom polygon
58//! let custom = Geometry2D::new("custom")
59//! .with_polygon(vec![(0.0, 0.0), (100.0, 0.0), (50.0, 80.0)])
60//! .with_quantity(3);
61//! ```
62
63pub mod alns_nesting;
64pub mod boundary;
65pub mod brkga_nesting;
66pub mod ga_nesting;
67pub mod gdrr_nesting;
68pub mod geometry;
69#[cfg(feature = "milp")]
70pub mod milp_solver;
71pub mod nester;
72pub mod nfp;
73#[cfg(feature = "milp")]
74pub mod nfp_cm_solver;
75pub mod nfp_sliding;
76pub mod placement_utils;
77pub mod sa_nesting;
78pub mod spatial_index;
79
80/// Computes valid placement bounds and clamps a position to keep geometry within boundary.
81///
82/// Returns `Some((clamped_x, clamped_y))` if the geometry can fit in the boundary,
83/// `None` if the geometry is too large to fit.
84///
85/// # Arguments
86/// * `x`, `y` - The proposed placement position for the geometry's origin
87/// * `geom_aabb` - The AABB `(min, max)` of the geometry at the given rotation
88/// * `boundary_aabb` - The AABB `(min, max)` of the boundary
89pub fn clamp_placement_to_boundary(
90 x: f64,
91 y: f64,
92 geom_aabb: ([f64; 2], [f64; 2]),
93 boundary_aabb: ([f64; 2], [f64; 2]),
94) -> Option<(f64, f64)> {
95 let (g_min, g_max) = geom_aabb;
96 let (b_min, b_max) = boundary_aabb;
97
98 // Calculate valid position bounds
99 // For geometry to stay inside boundary:
100 // - x + g_min[0] >= b_min[0] => x >= b_min[0] - g_min[0]
101 // - x + g_max[0] <= b_max[0] => x <= b_max[0] - g_max[0]
102 let min_valid_x = b_min[0] - g_min[0];
103 let max_valid_x = b_max[0] - g_max[0];
104 let min_valid_y = b_min[1] - g_min[1];
105 let max_valid_y = b_max[1] - g_max[1];
106
107 // Check if geometry can fit
108 if max_valid_x < min_valid_x || max_valid_y < min_valid_y {
109 // Geometry is too large to fit in boundary
110 return None;
111 }
112
113 let clamped_x = x.clamp(min_valid_x, max_valid_x);
114 let clamped_y = y.clamp(min_valid_y, max_valid_y);
115
116 Some((clamped_x, clamped_y))
117}
118
119/// Computes valid placement bounds with margin and clamps a position to keep geometry within boundary.
120///
121/// Returns `Some((clamped_x, clamped_y))` if the geometry can fit in the boundary (with margin),
122/// `None` if the geometry is too large to fit.
123///
124/// # Arguments
125/// * `x`, `y` - The proposed placement position for the geometry's origin
126/// * `geom_aabb` - The AABB `(min, max)` of the geometry at the given rotation
127/// * `boundary_aabb` - The AABB `(min, max)` of the boundary
128/// * `margin` - The margin to apply inside the boundary
129pub fn clamp_placement_to_boundary_with_margin(
130 x: f64,
131 y: f64,
132 geom_aabb: ([f64; 2], [f64; 2]),
133 boundary_aabb: ([f64; 2], [f64; 2]),
134 margin: f64,
135) -> Option<(f64, f64)> {
136 let (g_min, g_max) = geom_aabb;
137 let (b_min, b_max) = boundary_aabb;
138
139 // Calculate valid position bounds (with margin applied to effective boundary)
140 // Effective boundary: [b_min + margin, b_max - margin]
141 // For geometry to stay inside effective boundary:
142 // - x + g_min[0] >= b_min[0] + margin => x >= b_min[0] + margin - g_min[0]
143 // - x + g_max[0] <= b_max[0] - margin => x <= b_max[0] - margin - g_max[0]
144 let min_valid_x = b_min[0] + margin - g_min[0];
145 let max_valid_x = b_max[0] - margin - g_max[0];
146 let min_valid_y = b_min[1] + margin - g_min[1];
147 let max_valid_y = b_max[1] - margin - g_max[1];
148
149 // Check if geometry can fit
150 if max_valid_x < min_valid_x || max_valid_y < min_valid_y {
151 // Geometry is too large to fit in boundary with the given margin
152 return None;
153 }
154
155 let clamped_x = x.clamp(min_valid_x, max_valid_x);
156 let clamped_y = y.clamp(min_valid_y, max_valid_y);
157
158 Some((clamped_x, clamped_y))
159}
160
161/// Checks if a placement is within the boundary.
162///
163/// Returns `true` if the geometry at the given placement is fully within the boundary,
164/// `false` otherwise.
165///
166/// # Arguments
167/// * `placement` - The placement to validate (contains position and rotation)
168/// * `geometry` - The geometry being placed
169/// * `boundary` - The boundary to check against
170/// * `tolerance` - Small tolerance for floating point comparison (e.g., 1e-6)
171pub fn is_placement_within_bounds(
172 placement: &Placement<f64>,
173 geometry: &Geometry2D,
174 boundary: &Boundary2D,
175 tolerance: f64,
176) -> bool {
177 use u_nesting_core::geometry::Boundary;
178
179 // Extract position (Vec<f64> with [x, y] for 2D)
180 let x = placement.position.first().copied().unwrap_or(0.0);
181 let y = placement.position.get(1).copied().unwrap_or(0.0);
182
183 // Extract rotation (Vec<f64> with [θ] for 2D)
184 let rotation = placement.rotation.first().copied().unwrap_or(0.0);
185
186 // Get geometry AABB at the placement rotation
187 let (g_min, g_max) = geometry.aabb_at_rotation(rotation);
188
189 // Get boundary AABB
190 let (b_min, b_max) = boundary.aabb();
191
192 // Calculate the actual bounds of the placed geometry
193 let placed_min_x = x + g_min[0];
194 let placed_max_x = x + g_max[0];
195 let placed_min_y = y + g_min[1];
196 let placed_max_y = y + g_max[1];
197
198 // Check if fully within boundary (with tolerance)
199 placed_min_x >= b_min[0] - tolerance
200 && placed_max_x <= b_max[0] + tolerance
201 && placed_min_y >= b_min[1] - tolerance
202 && placed_max_y <= b_max[1] + tolerance
203}
204
205/// Validates all placements in a SolveResult and removes any that are outside the boundary.
206///
207/// Returns a new SolveResult with only valid placements, updated utilization,
208/// and invalid placements added to the unplaced list.
209///
210/// # Arguments
211/// * `result` - The solve result to validate
212/// * `geometries` - The geometries that were being placed
213/// * `boundary` - The boundary to check against
214pub fn validate_and_filter_placements(
215 mut result: SolveResult<f64>,
216 geometries: &[Geometry2D],
217 boundary: &Boundary2D,
218) -> SolveResult<f64> {
219 use std::collections::HashMap;
220 use u_nesting_core::geometry::{Boundary, Geometry};
221
222 const TOLERANCE: f64 = 1e-6;
223
224 // Build a map from geometry ID to geometry for quick lookup
225 let geom_map: HashMap<_, _> = geometries.iter().map(|g| (g.id().clone(), g)).collect();
226
227 let (b_min, b_max) = boundary.aabb();
228 log::debug!(
229 "Validating placements against boundary: ({:.2}, {:.2}) to ({:.2}, {:.2})",
230 b_min[0],
231 b_min[1],
232 b_max[0],
233 b_max[1]
234 );
235
236 let mut valid_placements = Vec::new();
237 let mut total_valid_area = 0.0;
238 let mut filtered_count = 0;
239
240 for placement in result.placements {
241 if let Some(geom) = geom_map.get(&placement.geometry_id) {
242 let px = placement.position.first().copied().unwrap_or(0.0);
243 let py = placement.position.get(1).copied().unwrap_or(0.0);
244 let rot = placement.rotation.first().copied().unwrap_or(0.0);
245
246 if is_placement_within_bounds(&placement, geom, boundary, TOLERANCE) {
247 total_valid_area += geom.measure();
248 valid_placements.push(placement);
249 } else {
250 // Calculate actual bounds for debugging
251 let (g_min, g_max) = geom.aabb_at_rotation(rot);
252 let placed_min_x = px + g_min[0];
253 let placed_max_x = px + g_max[0];
254 let placed_min_y = py + g_min[1];
255 let placed_max_y = py + g_max[1];
256
257 log::warn!(
258 "FILTERED: {} at ({:.2}, {:.2}) rot={:.2}° - bounds ({:.2}, {:.2}) to ({:.2}, {:.2}) outside boundary",
259 placement.geometry_id,
260 px, py,
261 rot.to_degrees(),
262 placed_min_x, placed_min_y, placed_max_x, placed_max_y
263 );
264 filtered_count += 1;
265 // Add to unplaced list
266 result.unplaced.push(placement.geometry_id.clone());
267 }
268 } else {
269 // Geometry not found - shouldn't happen but handle gracefully
270 log::warn!("Geometry {} not found in lookup map", placement.geometry_id);
271 result.unplaced.push(placement.geometry_id.clone());
272 }
273 }
274
275 if filtered_count > 0 {
276 log::warn!(
277 "Validation filtered out {} placements as out-of-bounds",
278 filtered_count
279 );
280 }
281
282 // Update result with valid placements only
283 result.placements = valid_placements;
284 result.utilization = total_valid_area / boundary.measure();
285
286 result
287}
288
289// Re-exports
290pub use boundary::Boundary2D;
291pub use geometry::Geometry2D;
292pub use nester::Nester2D;
293pub use nfp::{NfpConfig, NfpMethod};
294pub use spatial_index::{SpatialEntry2D, SpatialIndex2D};
295pub use u_nesting_core::{
296 Boundary, Boundary2DExt, Config, Error, Geometry, Geometry2DExt, Placement, Result,
297 RotationConstraint, SolveResult, Solver, Strategy, Transform2D, AABB2D,
298};