use crate::collision_detection::CDEConfig; use crate::entities::{Container, InferiorQualityZone}; use crate::geometry::primitives::{Rect, SPolygon}; use crate::geometry::shape_modification::{ShapeModifyConfig, ShapeModifyMode}; use crate::geometry::{DTransformation, OriginalShape}; use anyhow::{Result, ensure}; #[derive(Clone, Debug, Copy, PartialEq)] /// Represents a rectangular container with fixed height and variable width. pub struct Strip { pub fixed_height: f32, pub cde_config: CDEConfig, pub shape_modify_config: ShapeModifyConfig, pub width: f32, /// CutNest patch: bars across the strip that no item may overlap, /// dividing it into segments (sheets laid end to end). pub walls: Option, } /// CutNest patch: bars across a [`Strip`], at regular intervals. /// Bar `k` (for `k ≥ 1`) spans `[k·period − thickness, k·period]` along the /// width, so segment `k` spans `[k·period, (k + 1)·period − thickness]`. /// Bars are inflated by the strip's separation offset like the strip's edges /// are deflated by it, so items keep the same distance to both. #[derive(Clone, Debug, Copy, PartialEq)] pub struct Walls { pub period: f32, pub thickness: f32, } impl Walls { /// The bars that lie (partly) within a strip of `width`, as `(start, end)`. pub fn within(&self, width: f32) -> impl Iterator + '_ { (1..) .map(|k| k as f32 * self.period) .map(|end| (end - self.thickness, end)) .take_while(move |(start, _)| *start < width) .map(move |(start, end)| (start, end.min(width))) } /// Where the last segment of a strip of `width` starts. pub fn last_segment_start(&self, width: f32) -> f32 { self.within(width).map(|(_, end)| end).last().unwrap_or(0.0) } } impl Strip { pub fn new( fixed_height: f32, cde_config: CDEConfig, shape_modify_config: ShapeModifyConfig, width: f32, ) -> Result { ensure!(fixed_height > 0.0, "strip height must be positive"); ensure!(width > 0.0, "strip width must be positive"); Ok(Strip { fixed_height, cde_config, shape_modify_config, width, walls: None, }) } pub fn set_width(&mut self, width: f32) { assert!(width > 0.0, "strip width must be positive"); self.width = width; } /// CutNest patch: where shrinking or growing the strip should shift /// items from. With walls, the start of the last segment, so only the /// last segment changes; otherwise the middle. pub fn split_position(&self) -> f32 { match self.walls { Some(w) => w.last_segment_start(self.width), None => self.width / 2.0, } } } impl TryFrom for Container { type Error = anyhow::Error; fn try_from(s: Strip) -> Result { // CutNest patch: walls become a quality-0 zone, which every item // must stay clear of. let mut zones = vec![]; if let Some(walls) = s.walls { let bars = walls .within(s.width) // A sliver of a bar at the strip's end is left out: too thin // for the collision engine, and no item fits in it anyway. .filter(|(start, end)| end - start > walls.thickness * 0.25) .map(|(start, end)| { Ok(OriginalShape { shape: SPolygon::from(Rect::try_new(start, 0.0, end, s.fixed_height)?), pre_transform: DTransformation::empty(), modify_mode: ShapeModifyMode::Inflate, modify_config: s.shape_modify_config, }) }) .collect::>>()?; if !bars.is_empty() { zones.push(InferiorQualityZone::new(0, bars, s.cde_config.item_surrogate_config)?); } } Container::new( OriginalShape { shape: SPolygon::from(Rect::try_new(0.0, 0.0, s.width, s.fixed_height)?), pre_transform: DTransformation::empty(), modify_mode: ShapeModifyMode::Deflate, modify_config: s.shape_modify_config, }, zones, s.cde_config, ) } }