diff --git a/engines/vendor/sparrow/src/eval/collision_loss.rs b/engines/vendor/sparrow/src/eval/collision_loss.rs index 63e023a..b40fc6d 100644 --- a/engines/vendor/sparrow/src/eval/collision_loss.rs +++ b/engines/vendor/sparrow/src/eval/collision_loss.rs @@ -1,4 +1,5 @@ -use crate::quantify::quantify_collision_poly_container; +use crate::quantify::{quantify_collision_poly_bar, quantify_collision_poly_container}; +use crate::quantify::tracker::zone_bbox; #[cfg(not(feature = "simd"))] use crate::quantify::quantify_collision_poly_poly; #[cfg(feature = "simd")] @@ -91,6 +92,11 @@ impl<'a> CollisionLossEvaluator<'a> { quantify_collision_poly_container(shape, self.layout.container().outer_cd().bbox()) * self.ct.get_container_weight(self.current_pk), ), + // CutNest patch: bars across the strip count as the container. + BasicHazardEntity::Hole { idx } => Some( + quantify_collision_poly_bar(shape, zone_bbox(self.layout, idx)) + * self.ct.get_container_weight(self.current_pk), + ), _ => unimplemented!("unsupported hazard entity"), } } diff --git a/engines/vendor/sparrow/src/optimizer/compress.rs b/engines/vendor/sparrow/src/optimizer/compress.rs index 65d4894..5053645 100644 --- a/engines/vendor/sparrow/src/optimizer/compress.rs +++ b/engines/vendor/sparrow/src/optimizer/compress.rs @@ -68,7 +68,9 @@ fn attempt_to_compress(sep: &mut Separator, init_sol: &SPSolution, r_shrink: f32 // Shrink the container by the provided amount at a random position let new_width = init_sol.strip_width() * (1.0 - r_shrink); - let split_pos = sep.rng.random_range(0.0..sep.prob.strip_width()); + // (CutNest patch: within the last segment, with walls) + let first = sep.prob.strip().walls.map_or(0.0, |w| w.last_segment_start(sep.prob.strip_width())); + let split_pos = sep.rng.random_range(first..sep.prob.strip_width().max(first + f32::EPSILON)); sep.change_strip_width(new_width, Some(split_pos)).ok()?; // Try to separate layout, if all collisions are eliminated, return the solution diff --git a/engines/vendor/sparrow/src/optimizer/separator.rs b/engines/vendor/sparrow/src/optimizer/separator.rs index 5f1121a..b48c082 100644 --- a/engines/vendor/sparrow/src/optimizer/separator.rs +++ b/engines/vendor/sparrow/src/optimizer/separator.rs @@ -235,7 +235,8 @@ impl Separator { pub fn change_strip_width(&mut self, new_width: f32, split_position: Option) -> anyhow::Result<()> { //if no split position is provided, use the center of the strip - let split_position = split_position.unwrap_or(self.prob.strip_width() / 2.0); + // (CutNest patch: or the start of its last segment, with walls) + let split_position = split_position.unwrap_or(self.prob.strip().split_position()); let delta = new_width - self.prob.strip_width(); //shift all items right of the split position diff --git a/engines/vendor/sparrow/src/quantify/mod.rs b/engines/vendor/sparrow/src/quantify/mod.rs index 492bc88..c5c9580 100644 --- a/engines/vendor/sparrow/src/quantify/mod.rs +++ b/engines/vendor/sparrow/src/quantify/mod.rs @@ -31,6 +31,19 @@ pub fn calc_shape_penalty(s1: &SPolygon, s2: &SPolygon) -> f32 { (p1 * p2).sqrt() } +/// CutNest patch: quantifies a collision between a simple polygon and a bar +/// across the strip (a quality-0 zone): by how far it must move along the +/// strip to clear the bar, over the height they share. +pub fn quantify_collision_poly_bar(s: &SPolygon, bar: Rect) -> f32 { + let b = s.bbox(); + let dx = f32::min(b.x_max - bar.x_min, bar.x_max - b.x_min).max(0.0); + let dy = (f32::min(b.y_max, bar.y_max) - f32::max(b.y_min, bar.y_min)).max(0.0); + let overlap = dx * dy + 0.0001 * b.area(); + debug_assert!(overlap.is_normal()); + + 2.0 * overlap.sqrt() * calc_shape_penalty(s, s) +} + /// Quantifies a collision between a simple polygon and the exterior of the container. #[inline(always)] pub fn quantify_collision_poly_container(s: &SPolygon, c_bbox: Rect) -> f32 { diff --git a/engines/vendor/sparrow/src/quantify/tracker.rs b/engines/vendor/sparrow/src/quantify/tracker.rs index 7bee160..04b8f2f 100644 --- a/engines/vendor/sparrow/src/quantify/tracker.rs +++ b/engines/vendor/sparrow/src/quantify/tracker.rs @@ -1,6 +1,6 @@ use crate::consts::{GLS_WEIGHT_DECAY, GLS_WEIGHT_MAX_INC_RATIO, GLS_WEIGHT_MIN_INC_RATIO}; use crate::quantify::pair_matrix::PairMatrix; -use crate::quantify::{quantify_collision_poly_container, quantify_collision_poly_poly}; +use crate::quantify::{quantify_collision_poly_bar, quantify_collision_poly_container, quantify_collision_poly_poly}; use crate::util::assertions::tracker_matches_layout; use jagua_rs::collision_detection::hazards::collector::{BasicHazardCollector, HazardCollector}; use jagua_rs::collision_detection::hazards::BasicHazardEntity; @@ -76,7 +76,14 @@ impl CollisionTracker { BasicHazardEntity::Exterior => { let loss = quantify_collision_poly_container(shape, l.container().outer_cd().bbox()); assert!(loss > 0.0, "loss for a collision should be > 0.0"); - self.container_collisions[idx].loss = loss; + self.container_collisions[idx].loss += loss; + } + // CutNest patch: bars across the strip (holes: quality-0 + // zones) count as the container. + BasicHazardEntity::Hole { idx: bar } => { + let loss = quantify_collision_poly_bar(shape, zone_bbox(l, *bar)); + assert!(loss > 0.0, "loss for a collision should be > 0.0"); + self.container_collisions[idx].loss += loss; } _ => unimplemented!("unsupported hazard entity"), } @@ -211,3 +218,13 @@ impl CTEntry { self.weight * self.loss } } + +/// CutNest patch: the bounding box of hole `idx` of the layout's container +/// (shape `idx` of its quality-0 zone: a bar across the strip). +pub fn zone_bbox(l: &Layout, idx: usize) -> jagua_rs::geometry::primitives::Rect { + l.container().quality_zones()[0] + .as_ref() + .expect("collision with a hole that doesn't exist") + .shapes_cd()[idx] + .bbox() +}