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 sa_nesting;
77pub mod spatial_index;
78
79/// Computes valid placement bounds and clamps a position to keep geometry within boundary.
80///
81/// Returns `Some((clamped_x, clamped_y))` if the geometry can fit in the boundary,
82/// `None` if the geometry is too large to fit.
83///
84/// # Arguments
85/// * `x`, `y` - The proposed placement position for the geometry's origin
86/// * `geom_aabb` - The AABB `(min, max)` of the geometry at the given rotation
87/// * `boundary_aabb` - The AABB `(min, max)` of the boundary
88pub fn clamp_placement_to_boundary(
89    x: f64,
90    y: f64,
91    geom_aabb: ([f64; 2], [f64; 2]),
92    boundary_aabb: ([f64; 2], [f64; 2]),
93) -> Option<(f64, f64)> {
94    let (g_min, g_max) = geom_aabb;
95    let (b_min, b_max) = boundary_aabb;
96
97    // Calculate valid position bounds
98    // For geometry to stay inside boundary:
99    // - x + g_min[0] >= b_min[0]  => x >= b_min[0] - g_min[0]
100    // - x + g_max[0] <= b_max[0]  => x <= b_max[0] - g_max[0]
101    let min_valid_x = b_min[0] - g_min[0];
102    let max_valid_x = b_max[0] - g_max[0];
103    let min_valid_y = b_min[1] - g_min[1];
104    let max_valid_y = b_max[1] - g_max[1];
105
106    // Check if geometry can fit
107    if max_valid_x < min_valid_x || max_valid_y < min_valid_y {
108        // Geometry is too large to fit in boundary
109        return None;
110    }
111
112    let clamped_x = x.clamp(min_valid_x, max_valid_x);
113    let clamped_y = y.clamp(min_valid_y, max_valid_y);
114
115    Some((clamped_x, clamped_y))
116}
117
118/// Computes valid placement bounds with margin and clamps a position to keep geometry within boundary.
119///
120/// Returns `Some((clamped_x, clamped_y))` if the geometry can fit in the boundary (with margin),
121/// `None` if the geometry is too large to fit.
122///
123/// # Arguments
124/// * `x`, `y` - The proposed placement position for the geometry's origin
125/// * `geom_aabb` - The AABB `(min, max)` of the geometry at the given rotation
126/// * `boundary_aabb` - The AABB `(min, max)` of the boundary
127/// * `margin` - The margin to apply inside the boundary
128pub fn clamp_placement_to_boundary_with_margin(
129    x: f64,
130    y: f64,
131    geom_aabb: ([f64; 2], [f64; 2]),
132    boundary_aabb: ([f64; 2], [f64; 2]),
133    margin: f64,
134) -> Option<(f64, f64)> {
135    let (g_min, g_max) = geom_aabb;
136    let (b_min, b_max) = boundary_aabb;
137
138    // Calculate valid position bounds (with margin applied to effective boundary)
139    // Effective boundary: [b_min + margin, b_max - margin]
140    // For geometry to stay inside effective boundary:
141    // - x + g_min[0] >= b_min[0] + margin  => x >= b_min[0] + margin - g_min[0]
142    // - x + g_max[0] <= b_max[0] - margin  => x <= b_max[0] - margin - g_max[0]
143    let min_valid_x = b_min[0] + margin - g_min[0];
144    let max_valid_x = b_max[0] - margin - g_max[0];
145    let min_valid_y = b_min[1] + margin - g_min[1];
146    let max_valid_y = b_max[1] - margin - g_max[1];
147
148    // Check if geometry can fit
149    if max_valid_x < min_valid_x || max_valid_y < min_valid_y {
150        // Geometry is too large to fit in boundary with the given margin
151        return None;
152    }
153
154    let clamped_x = x.clamp(min_valid_x, max_valid_x);
155    let clamped_y = y.clamp(min_valid_y, max_valid_y);
156
157    Some((clamped_x, clamped_y))
158}
159
160/// Checks if a placement is within the boundary.
161///
162/// Returns `true` if the geometry at the given placement is fully within the boundary,
163/// `false` otherwise.
164///
165/// # Arguments
166/// * `placement` - The placement to validate (contains position and rotation)
167/// * `geometry` - The geometry being placed
168/// * `boundary` - The boundary to check against
169/// * `tolerance` - Small tolerance for floating point comparison (e.g., 1e-6)
170pub fn is_placement_within_bounds(
171    placement: &Placement<f64>,
172    geometry: &Geometry2D,
173    boundary: &Boundary2D,
174    tolerance: f64,
175) -> bool {
176    use u_nesting_core::geometry::Boundary;
177
178    // Extract position (Vec<f64> with [x, y] for 2D)
179    let x = placement.position.first().copied().unwrap_or(0.0);
180    let y = placement.position.get(1).copied().unwrap_or(0.0);
181
182    // Extract rotation (Vec<f64> with [θ] for 2D)
183    let rotation = placement.rotation.first().copied().unwrap_or(0.0);
184
185    // Get geometry AABB at the placement rotation
186    let (g_min, g_max) = geometry.aabb_at_rotation(rotation);
187
188    // Get boundary AABB
189    let (b_min, b_max) = boundary.aabb();
190
191    // Calculate the actual bounds of the placed geometry
192    let placed_min_x = x + g_min[0];
193    let placed_max_x = x + g_max[0];
194    let placed_min_y = y + g_min[1];
195    let placed_max_y = y + g_max[1];
196
197    // Check if fully within boundary (with tolerance)
198    placed_min_x >= b_min[0] - tolerance
199        && placed_max_x <= b_max[0] + tolerance
200        && placed_min_y >= b_min[1] - tolerance
201        && placed_max_y <= b_max[1] + tolerance
202}
203
204/// Validates all placements in a SolveResult and removes any that are outside the boundary.
205///
206/// Returns a new SolveResult with only valid placements, updated utilization,
207/// and invalid placements added to the unplaced list.
208///
209/// # Arguments
210/// * `result` - The solve result to validate
211/// * `geometries` - The geometries that were being placed
212/// * `boundary` - The boundary to check against
213pub fn validate_and_filter_placements(
214    mut result: SolveResult<f64>,
215    geometries: &[Geometry2D],
216    boundary: &Boundary2D,
217) -> SolveResult<f64> {
218    use std::collections::HashMap;
219    use u_nesting_core::geometry::{Boundary, Geometry};
220
221    const TOLERANCE: f64 = 1e-6;
222
223    // Build a map from geometry ID to geometry for quick lookup
224    let geom_map: HashMap<_, _> = geometries.iter().map(|g| (g.id().clone(), g)).collect();
225
226    let (b_min, b_max) = boundary.aabb();
227    log::debug!(
228        "Validating placements against boundary: ({:.2}, {:.2}) to ({:.2}, {:.2})",
229        b_min[0],
230        b_min[1],
231        b_max[0],
232        b_max[1]
233    );
234
235    let mut valid_placements = Vec::new();
236    let mut total_valid_area = 0.0;
237    let mut filtered_count = 0;
238
239    for placement in result.placements {
240        if let Some(geom) = geom_map.get(&placement.geometry_id) {
241            let px = placement.position.first().copied().unwrap_or(0.0);
242            let py = placement.position.get(1).copied().unwrap_or(0.0);
243            let rot = placement.rotation.first().copied().unwrap_or(0.0);
244
245            if is_placement_within_bounds(&placement, geom, boundary, TOLERANCE) {
246                total_valid_area += geom.measure();
247                valid_placements.push(placement);
248            } else {
249                // Calculate actual bounds for debugging
250                let (g_min, g_max) = geom.aabb_at_rotation(rot);
251                let placed_min_x = px + g_min[0];
252                let placed_max_x = px + g_max[0];
253                let placed_min_y = py + g_min[1];
254                let placed_max_y = py + g_max[1];
255
256                log::warn!(
257                    "FILTERED: {} at ({:.2}, {:.2}) rot={:.2}° - bounds ({:.2}, {:.2}) to ({:.2}, {:.2}) outside boundary",
258                    placement.geometry_id,
259                    px, py,
260                    rot.to_degrees(),
261                    placed_min_x, placed_min_y, placed_max_x, placed_max_y
262                );
263                filtered_count += 1;
264                // Add to unplaced list
265                result.unplaced.push(placement.geometry_id.clone());
266            }
267        } else {
268            // Geometry not found - shouldn't happen but handle gracefully
269            log::warn!("Geometry {} not found in lookup map", placement.geometry_id);
270            result.unplaced.push(placement.geometry_id.clone());
271        }
272    }
273
274    if filtered_count > 0 {
275        log::warn!(
276            "Validation filtered out {} placements as out-of-bounds",
277            filtered_count
278        );
279    }
280
281    // Update result with valid placements only
282    result.placements = valid_placements;
283    result.utilization = total_valid_area / boundary.measure();
284
285    result
286}
287
288// Re-exports
289pub use boundary::Boundary2D;
290pub use geometry::Geometry2D;
291pub use nester::Nester2D;
292pub use nfp::{NfpConfig, NfpMethod};
293pub use spatial_index::{SpatialEntry2D, SpatialIndex2D};
294pub use u_nesting_core::{
295    Boundary, Boundary2DExt, Config, Error, Geometry, Geometry2DExt, Placement, Result,
296    RotationConstraint, SolveResult, Solver, Strategy, Transform2D, AABB2D,
297};