u-nesting-d3 0.3.5

3D bin packing algorithms for U-Nesting spatial optimization engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Shared utilities for 3D bin packing solvers.
//!
//! This module consolidates common code across GA, SA, and BRKGA packing solvers,
//! following the same deduplication pattern as d2's `placement_utils`.
//!
//! # Extracted Components
//!
//! - [`InstanceInfo`]: Maps expanded instances back to their source geometry
//! - [`packing_fitness`]: Unified fitness formula for all solvers
//! - [`build_unplaced_list`]: Identifies geometries not placed in the solution
//! - [`layer_place_items`]: Core layer-based placement algorithm shared by all solvers

use crate::boundary::Boundary3D;
use crate::geometry::Geometry3D;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use u_nesting_core::geometry::{Boundary, Geometry};
use u_nesting_core::solver::Config;
use u_nesting_core::Placement;

/// Instance information mapping expanded instances to source geometries.
///
/// When a geometry has quantity > 1, it expands into multiple instances.
/// This struct tracks which geometry each instance belongs to and its
/// ordinal within that geometry's quantity.
#[derive(Debug, Clone)]
pub struct InstanceInfo {
    /// Index into the geometries array.
    pub geometry_idx: usize,
    /// Instance number within this geometry's quantity.
    pub instance_num: usize,
    /// Number of allowed orientations.
    pub orientation_count: usize,
}

/// Builds the instance mapping from geometries.
///
/// Expands each geometry by its quantity, recording the geometry index,
/// instance number, and orientation count for each expanded instance.
pub fn build_instances(geometries: &[Geometry3D]) -> Vec<InstanceInfo> {
    let mut instances = Vec::new();
    for (geom_idx, geom) in geometries.iter().enumerate() {
        let orient_count = geom.allowed_orientations().len();
        for instance_num in 0..geom.quantity() {
            instances.push(InstanceInfo {
                geometry_idx: geom_idx,
                instance_num,
                orientation_count: orient_count,
            });
        }
    }
    instances
}

/// Represents a single item to be placed, with its resolved orientation.
pub struct PlacementItem {
    /// Index into the instances array.
    pub instance_idx: usize,
    /// Resolved orientation index for this item.
    pub orientation_idx: usize,
}

/// Computes packing fitness using the unified formula.
///
/// The fitness combines placement ratio and volume utilization:
/// `fitness = (placed / total) * 100 + utilization * 10`
///
/// This prioritizes placing more items (100x weight) while also
/// rewarding tighter packing (10x weight).
///
/// # Arguments
/// * `placed_count` - Number of items successfully placed
/// * `total_count` - Total number of items to place
/// * `utilization` - Volume utilization ratio (0.0 to 1.0)
pub fn packing_fitness(placed_count: usize, total_count: usize, utilization: f64) -> f64 {
    let placement_ratio = placed_count as f64 / total_count.max(1) as f64;
    placement_ratio * 100.0 + utilization * 10.0
}

/// Builds a list of geometry IDs that were not placed.
///
/// Compares placed geometry IDs against the full input set to identify
/// which geometries have no placements.
pub fn build_unplaced_list(
    placements: &[Placement<f64>],
    geometries: &[Geometry3D],
) -> Vec<String> {
    let mut placed_ids: std::collections::HashSet<String> = std::collections::HashSet::new();
    for p in placements {
        placed_ids.insert(p.geometry_id.clone());
    }
    let mut unplaced = Vec::new();
    for geom in geometries {
        if !placed_ids.contains(geom.id()) {
            unplaced.push(geom.id().clone());
        }
    }
    unplaced
}

/// Result of the layer-based placement algorithm.
pub struct LayerPlacementResult {
    /// Successfully placed items.
    pub placements: Vec<Placement<f64>>,
    /// Volume utilization ratio.
    pub utilization: f64,
    /// Number of items placed.
    pub placed_count: usize,
}

/// Core layer-based placement algorithm shared by all 3D packing solvers.
///
/// Places items in a layer-by-layer, row-by-row fashion within the boundary.
/// Items are placed left-to-right in rows, rows stack front-to-back in layers,
/// and layers stack bottom-to-top.
///
/// # Algorithm
///
/// For each item in the given order:
/// 1. Get oriented dimensions
/// 2. Check mass constraint
/// 3. Try to fit in current row (x direction)
/// 4. If not, move to next row (y direction)
/// 5. If row doesn't fit, move to next layer (z direction)
/// 6. If layer doesn't fit, skip item
///
/// # Arguments
///
/// * `items` - Ordered list of items with resolved orientations
/// * `instances` - Instance mapping (geometry index, instance number)
/// * `geometries` - Source geometry definitions
/// * `boundary` - Container dimensions and constraints
/// * `config` - Margin and spacing configuration
/// * `cancelled` - Cancellation flag for early termination
pub fn layer_place_items(
    items: &[PlacementItem],
    instances: &[InstanceInfo],
    geometries: &[Geometry3D],
    boundary: &Boundary3D,
    config: &Config,
    cancelled: &Arc<AtomicBool>,
) -> LayerPlacementResult {
    let mut placements = Vec::new();

    let margin = config.margin;
    let spacing = config.spacing;

    let bound_max_x = boundary.width() - margin;
    let bound_max_y = boundary.depth() - margin;
    let bound_max_z = boundary.height() - margin;

    let mut current_x = margin;
    let mut current_y = margin;
    let mut current_z = margin;
    let mut row_depth = 0.0_f64;
    let mut layer_height = 0.0_f64;

    let mut total_placed_volume = 0.0;
    let mut total_placed_mass = 0.0;
    let mut placed_count = 0;

    for item in items {
        if cancelled.load(Ordering::Relaxed) {
            break;
        }

        if item.instance_idx >= instances.len() {
            continue;
        }

        let info = &instances[item.instance_idx];
        let geom = &geometries[info.geometry_idx];

        let orientation_idx = item.orientation_idx % info.orientation_count.max(1);

        // Get dimensions for this orientation
        let dims = geom.dimensions_for_orientation(orientation_idx);
        let g_width = dims.x;
        let g_depth = dims.y;
        let g_height = dims.z;

        // Check mass constraint
        if let (Some(max_mass), Some(item_mass)) = (boundary.max_mass(), geom.mass()) {
            if total_placed_mass + item_mass > max_mass {
                continue;
            }
        }

        // Try to fit in current row
        if current_x + g_width > bound_max_x {
            current_x = margin;
            current_y += row_depth + spacing;
            row_depth = 0.0;
        }

        // Check if fits in current layer (y direction)
        if current_y + g_depth > bound_max_y {
            current_x = margin;
            current_y = margin;
            current_z += layer_height + spacing;
            row_depth = 0.0;
            layer_height = 0.0;
        }

        // Check if fits in container height
        if current_z + g_height > bound_max_z {
            continue;
        }

        // Place the item. Preserve the resolved orientation via `rotation_index` so the
        // response reports the actual box footprint — without it, every rotated GA/BRKGA/SA
        // placement reported the identity orientation and read as out-of-bounds downstream.
        let placement = Placement::new_3d(
            geom.id().clone(),
            info.instance_num,
            current_x,
            current_y,
            current_z,
            0.0,
            0.0,
            0.0,
        )
        .with_rotation_index(orientation_idx);

        placements.push(placement);
        total_placed_volume += geom.measure();
        if let Some(mass) = geom.mass() {
            total_placed_mass += mass;
        }
        placed_count += 1;

        // Update position for next item
        current_x += g_width + spacing;
        row_depth = row_depth.max(g_depth);
        layer_height = layer_height.max(g_height);
    }

    let utilization = total_placed_volume / boundary.measure();
    LayerPlacementResult {
        placements,
        utilization,
        placed_count,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_instances_single() {
        let geometries = vec![Geometry3D::new("B1", 10.0, 10.0, 10.0).with_quantity(3)];
        let instances = build_instances(&geometries);
        assert_eq!(instances.len(), 3);
        assert_eq!(instances[0].geometry_idx, 0);
        assert_eq!(instances[0].instance_num, 0);
        assert_eq!(instances[2].instance_num, 2);
    }

    #[test]
    fn test_build_instances_multiple() {
        let geometries = vec![
            Geometry3D::new("A", 10.0, 10.0, 10.0).with_quantity(2),
            Geometry3D::new("B", 20.0, 20.0, 20.0).with_quantity(3),
        ];
        let instances = build_instances(&geometries);
        assert_eq!(instances.len(), 5);
        assert_eq!(instances[0].geometry_idx, 0);
        assert_eq!(instances[1].geometry_idx, 0);
        assert_eq!(instances[2].geometry_idx, 1);
        assert_eq!(instances[4].geometry_idx, 1);
    }

    #[test]
    fn test_packing_fitness_all_placed() {
        let fitness = packing_fitness(10, 10, 0.8);
        // 1.0 * 100 + 0.8 * 10 = 108.0
        assert!((fitness - 108.0).abs() < 1e-10);
    }

    #[test]
    fn test_packing_fitness_partial() {
        let fitness = packing_fitness(5, 10, 0.5);
        // 0.5 * 100 + 0.5 * 10 = 55.0
        assert!((fitness - 55.0).abs() < 1e-10);
    }

    #[test]
    fn test_packing_fitness_none_placed() {
        let fitness = packing_fitness(0, 10, 0.0);
        assert!((fitness - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_packing_fitness_zero_total() {
        let fitness = packing_fitness(0, 0, 0.0);
        assert!((fitness - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_build_unplaced_list_all_placed() {
        let geometries = vec![
            Geometry3D::new("A", 10.0, 10.0, 10.0),
            Geometry3D::new("B", 20.0, 20.0, 20.0),
        ];
        let placements = vec![
            Placement::new_3d("A".to_string(), 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
            Placement::new_3d("B".to_string(), 0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0),
        ];
        let unplaced = build_unplaced_list(&placements, &geometries);
        assert!(unplaced.is_empty());
    }

    #[test]
    fn test_build_unplaced_list_partial() {
        let geometries = vec![
            Geometry3D::new("A", 10.0, 10.0, 10.0),
            Geometry3D::new("B", 20.0, 20.0, 20.0),
        ];
        let placements = vec![Placement::new_3d(
            "A".to_string(),
            0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
        )];
        let unplaced = build_unplaced_list(&placements, &geometries);
        assert_eq!(unplaced, vec!["B"]);
    }

    #[test]
    fn test_layer_place_items_respects_bounds() {
        // Invariant: every placed box lies fully inside the boundary for its actual
        // resolved orientation, across mixed sizes and Fixed/Any constraints.
        use crate::geometry::OrientationConstraint::{Any, Fixed};
        let dims_pool = [
            (30.0, 20.0, 25.0),
            (40.0, 10.0, 40.0),
            (60.0, 70.0, 15.0),
            (15.0, 80.0, 35.0),
            (50.0, 30.0, 45.0),
            (90.0, 90.0, 10.0),
        ];
        let boundary = Boundary3D::new(100.0, 100.0, 100.0);
        let config = Config::default();
        let cancelled = Arc::new(AtomicBool::new(false));
        let mut seed: u64 = 0xABCD_1234;
        let mut next = || {
            seed = seed
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            (seed >> 33) as usize
        };
        for constraint in [Fixed, Any] {
            for _ in 0..200 {
                // Each geometry quantity 1 with a unique id → deterministic orientation map.
                let n = 3 + next() % 8;
                let orients: Vec<usize> = (0..n).map(|_| next()).collect();
                let geometries: Vec<_> = (0..n)
                    .map(|g| {
                        let (w, d, h) = dims_pool[next() % dims_pool.len()];
                        Geometry3D::new(format!("g{g}"), w, d, h).with_orientation(constraint)
                    })
                    .collect();
                let instances = build_instances(&geometries);
                let items: Vec<PlacementItem> = (0..instances.len())
                    .map(|i| PlacementItem {
                        instance_idx: i,
                        orientation_idx: orients[i],
                    })
                    .collect();
                let result = layer_place_items(
                    &items,
                    &instances,
                    &geometries,
                    &boundary,
                    &config,
                    &cancelled,
                );
                for p in &result.placements {
                    let gi: usize = p.geometry_id[1..].parse().unwrap();
                    let oc = geometries[gi].allowed_orientations().len().max(1);
                    let dm = geometries[gi].dimensions_for_orientation(orients[gi] % oc);
                    let (px, py, pz) = (p.x(), p.y(), p.z().unwrap_or(0.0));
                    let e = 1e-6;
                    assert!(
                        px + dm.x <= 100.0 + e && py + dm.y <= 100.0 + e && pz + dm.z <= 100.0 + e,
                        "{} out of bounds: pos({px:.1},{py:.1},{pz:.1}) dims({:.1},{:.1},{:.1})",
                        p.geometry_id,
                        dm.x,
                        dm.y,
                        dm.z,
                    );
                }
            }
        }
    }

    #[test]
    fn test_layer_place_items_basic() {
        let geometries = vec![Geometry3D::new("B1", 20.0, 20.0, 20.0).with_quantity(2)];
        let boundary = Boundary3D::new(100.0, 100.0, 100.0);
        let config = Config::default();
        let instances = build_instances(&geometries);
        let cancelled = Arc::new(AtomicBool::new(false));

        let items: Vec<PlacementItem> = (0..instances.len())
            .map(|i| PlacementItem {
                instance_idx: i,
                orientation_idx: 0,
            })
            .collect();

        let result = layer_place_items(
            &items,
            &instances,
            &geometries,
            &boundary,
            &config,
            &cancelled,
        );

        assert_eq!(result.placed_count, 2);
        assert_eq!(result.placements.len(), 2);
        assert!(result.utilization > 0.0);
    }

    #[test]
    fn test_layer_place_items_cancellation() {
        let geometries = vec![Geometry3D::new("B1", 20.0, 20.0, 20.0).with_quantity(5)];
        let boundary = Boundary3D::new(100.0, 100.0, 100.0);
        let config = Config::default();
        let instances = build_instances(&geometries);
        let cancelled = Arc::new(AtomicBool::new(true)); // Already cancelled

        let items: Vec<PlacementItem> = (0..instances.len())
            .map(|i| PlacementItem {
                instance_idx: i,
                orientation_idx: 0,
            })
            .collect();

        let result = layer_place_items(
            &items,
            &instances,
            &geometries,
            &boundary,
            &config,
            &cancelled,
        );

        assert_eq!(result.placed_count, 0);
    }

    #[test]
    fn test_layer_place_items_mass_constraint() {
        let geometries = vec![Geometry3D::new("B1", 20.0, 20.0, 20.0)
            .with_quantity(10)
            .with_mass(100.0)];
        let boundary = Boundary3D::new(100.0, 100.0, 100.0).with_max_mass(250.0);
        let config = Config::default();
        let instances = build_instances(&geometries);
        let cancelled = Arc::new(AtomicBool::new(false));

        let items: Vec<PlacementItem> = (0..instances.len())
            .map(|i| PlacementItem {
                instance_idx: i,
                orientation_idx: 0,
            })
            .collect();

        let result = layer_place_items(
            &items,
            &instances,
            &geometries,
            &boundary,
            &config,
            &cancelled,
        );

        // Max 2 boxes at 100kg each within 250kg limit
        assert!(result.placed_count <= 2);
    }
}