Skip to content

Commit

Permalink
HazardEntity::PlacedItem() reverted to {id,d_transf}
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Nov 21, 2024
1 parent e369c1b commit 75c0b6d
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 50 deletions.
14 changes: 14 additions & 0 deletions jagua-rs/src/collision_detection/hazard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::entities::placed_item::PlacedItem;
use crate::geometry::d_transformation::DTransformation;
use crate::geometry::geo_enums::GeoPosition;
use crate::geometry::primitives::simple_polygon::SimplePolygon;
use std::borrow::Borrow;
use std::sync::Arc;

/// Defines a certain spatial constraint that affects the feasibility of a placement.
Expand Down Expand Up @@ -68,3 +70,15 @@ impl HazardEntity {
}
}
}

impl<T> From<T> for HazardEntity
where
T: Borrow<PlacedItem>,
{
fn from(pi: T) -> Self {
HazardEntity::PlacedItem {
id: pi.borrow().item_id,
dt: pi.borrow().d_transf,
}
}
}
2 changes: 1 addition & 1 deletion jagua-rs/src/collision_detection/hazard_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct CombinedHazardFilter<'a> {
impl HazardFilter for BinHazardFilter {
fn is_irrelevant(&self, entity: &HazardEntity) -> bool {
match entity {
HazardEntity::PlacedItem(_) => false,
HazardEntity::PlacedItem { .. } => false,
HazardEntity::BinExterior => true,
HazardEntity::BinHole { .. } => true,
HazardEntity::InferiorQualityZone { .. } => true,
Expand Down
4 changes: 2 additions & 2 deletions jagua-rs/src/collision_detection/quadtree/qt_hazard_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl QTHazardVec {
self.hazards
.iter()
.filter(|other| other.entity == haz.entity
&& matches!(haz.entity, HazardEntity::PlacedItem(_)))
&& matches!(haz.entity, HazardEntity::PlacedItem { .. }))
.count()
== 0,
"More than one hazard from same item entity in the vector! (This should never happen!)"
Expand All @@ -48,7 +48,7 @@ impl QTHazardVec {
self.hazards
.iter()
.filter(|ch| ch.entity == haz_entity
&& matches!(ch.entity, HazardEntity::PlacedItem(_)))
&& matches!(ch.entity, HazardEntity::PlacedItem { .. }))
.count()
<= 1,
"More than one hazard from same item entity in the vector! (This should never happen!)"
Expand Down
32 changes: 18 additions & 14 deletions jagua-rs/src/entities/layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::collision_detection::cd_engine::{CDESnapshot, CDEngine};
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::{Hazard, HazardEntity};
use crate::entities::bin::Bin;
use crate::entities::item::Item;
use crate::entities::placed_item::{PItemKey, PlacedItem};
Expand Down Expand Up @@ -48,8 +48,8 @@ impl Layout {
self.bin = bin;
// update the CDE
self.cde = self.bin.base_cde.as_ref().clone();
for (pik, pi) in self.placed_items.iter() {
let hazard = Hazard::new(pik.into(), pi.shape.clone());
for (_, pi) in self.placed_items.iter() {
let hazard = Hazard::new(pi.into(), pi.shape.clone());
self.cde.register_hazard(hazard);
}
}
Expand Down Expand Up @@ -79,10 +79,10 @@ impl Layout {
}

pub fn place_item(&mut self, item: &Item, d_transformation: DTransformation) -> PItemKey {
let placed_item = PlacedItem::new(item, d_transformation);
let pik = self.placed_items.insert(placed_item);
let pi = PlacedItem::new(item, d_transformation);
let hazard = Hazard::new(HazardEntity::from(&pi), pi.shape.clone());

let hazard = Hazard::new(pik.into(), self.placed_items[pik].shape.clone());
let pik = self.placed_items.insert(pi);
self.cde.register_hazard(hazard);

debug_assert!(assertions::layout_qt_matches_fresh_qt(self));
Expand All @@ -91,36 +91,40 @@ impl Layout {
}

pub fn remove_item(&mut self, key: PItemKey, commit_instant: bool) -> PlacedItem {
let p_item = self
let pi = self
.placed_items
.remove(key)
.expect("key is not valid anymore");

// update the collision detection engine
self.cde.deregister_hazard(key.into(), commit_instant);
self.cde
.deregister_hazard(HazardEntity::from(&pi), commit_instant);

debug_assert!(assertions::layout_qt_matches_fresh_qt(self));

p_item
pi
}

/// True if no items are placed
pub fn is_empty(&self) -> bool {
self.placed_items.is_empty()
}

pub fn bin(&self) -> &Bin {
&self.bin
}

pub fn placed_items(&self) -> &SlotMap<PItemKey, PlacedItem> {
&self.placed_items
}

pub fn hazard_to_p_item_key(&self, hz: &HazardEntity) -> Option<PItemKey> {
self.placed_items
.iter()
.find(|(_, pi)| HazardEntity::from(*pi) == *hz)
.map(|(k, _)| k)
}

/// Returns the usage of the bin with the items placed.
/// It is the ratio of the area of the items placed to the area of the bin.
pub fn usage(&self) -> fsize {
let bin_area = self.bin().area;
let bin_area = self.bin.area;
let item_area = self
.placed_items
.iter()
Expand Down
6 changes: 6 additions & 0 deletions jagua-rs/src/entities/placed_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ use crate::entities::item::Item;
use crate::geometry::d_transformation::DTransformation;
use crate::geometry::geo_traits::Transformable;
use crate::geometry::primitives::simple_polygon::SimplePolygon;
use slotmap::new_key_type;
use std::sync::Arc;

new_key_type! {
/// Unique key for each `PlacedItem` in a layout.
pub struct PItemKey;
}

/// Represents an `Item` that has been placed in a `Layout`
#[derive(Clone, Debug)]
pub struct PlacedItem {
Expand Down
4 changes: 2 additions & 2 deletions jagua-rs/src/entities/problems/bin_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl BPProblem {
}

pub fn register_layout(&mut self, layout: Layout) -> LayoutIndex {
self.register_bin(layout.bin().id);
self.register_bin(layout.bin.id);
layout
.placed_items()
.values()
Expand All @@ -78,7 +78,7 @@ impl BPProblem {
LayoutIndex::Real(i) => {
let layout = self.layouts.remove(i);
self.layout_has_changed(layout.id());
self.deregister_bin(layout.bin().id);
self.deregister_bin(layout.bin.id);
layout
.placed_items()
.values()
Expand Down
6 changes: 3 additions & 3 deletions jagua-rs/src/entities/problems/problem_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ pub trait ProblemGeneric: ProblemGenericPrivate {
fn usage(&mut self) -> fsize {
let (total_bin_area, total_used_area) =
self.layouts_mut().iter_mut().fold((0.0, 0.0), |acc, l| {
let bin_area = l.bin().area;
let bin_area = l.bin.area;
let used_area = bin_area * l.usage();
(acc.0 + bin_area, acc.1 + used_area)
});
total_used_area / total_bin_area
}

fn used_bin_cost(&self) -> u64 {
self.layouts().iter().map(|l| l.bin().value).sum()
self.layouts().iter().map(|l| l.bin.value).sum()
}

/// Returns the `LayoutIndex` of all layouts.
Expand All @@ -74,7 +74,7 @@ pub trait ProblemGeneric: ProblemGenericPrivate {
self.template_layouts()
.iter()
.enumerate()
.filter_map(|(i, l)| match self.bin_qtys()[l.bin().id] {
.filter_map(|(i, l)| match self.bin_qtys()[l.bin.id] {
0 => None,
_ => Some(LayoutIndex::Template(i)),
})
Expand Down
14 changes: 7 additions & 7 deletions jagua-rs/src/entities/problems/strip_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ impl SPProblem {

/// Adds or removes width in the back of the strip.
pub fn modify_strip_in_back(&mut self, new_width: fsize) {
let bbox = self.layout.bin().outer.bbox();
let bbox = self.layout.bin.outer.bbox();
let new_strip_shape =
AARectangle::new(bbox.x_min, bbox.y_min, bbox.x_min + new_width, bbox.y_max);
self.modify_strip(new_strip_shape);
}

/// Adds or removes width at the front of the strip.
pub fn modify_strip_at_front(&mut self, new_width: fsize) {
let bbox = self.layout.bin().outer.bbox();
let bbox = self.layout.bin.outer.bbox();
let new_strip_shape =
AARectangle::new(bbox.x_max - new_width, bbox.y_min, bbox.x_max, bbox.y_max);
self.modify_strip(new_strip_shape);
Expand All @@ -80,9 +80,9 @@ impl SPProblem {

let new_strip_shape = AARectangle::new(
new_x_min,
self.layout.bin().outer.bbox().y_min,
self.layout.bin.outer.bbox().y_min,
new_x_max,
self.layout.bin().outer.bbox().y_max,
self.layout.bin.outer.bbox().y_max,
);

self.modify_strip(new_strip_shape);
Expand All @@ -107,7 +107,7 @@ impl SPProblem {
//Modifying the width causes the bin to change, so the layout must be replaced
self.layout = Layout::new(
self.next_layout_id(),
Bin::from_strip(rect, self.layout.bin().base_cde.config()),
Bin::from_strip(rect, self.layout.bin.base_cde.config()),
);

//place the items back in the new layout
Expand Down Expand Up @@ -168,11 +168,11 @@ impl SPProblem {
}

pub fn strip_width(&self) -> fsize {
self.layout.bin().outer.bbox().width()
self.layout.bin.outer.bbox().width()
}

pub fn strip_height(&self) -> fsize {
self.layout.bin().outer.bbox().height()
self.layout.bin.outer.bbox().height()
}
}

Expand Down
4 changes: 2 additions & 2 deletions jagua-rs/src/io/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub fn build_strip_packing_solution(
let transform = absolute_to_internal_transform(
&abs_transform,
&item.pretransform,
&problem.layout.bin().pretransform,
&problem.layout.bin.pretransform,
);

let d_transf = transform.decompose();
Expand Down Expand Up @@ -332,7 +332,7 @@ pub fn build_bin_packing_solution(instance: &BPInstance, json_layouts: &[JsonLay
let template_index = problem
.template_layouts()
.iter()
.position(|tl| tl.bin().id == bin.id)
.position(|tl| tl.bin.id == bin.id)
.expect("no template layout found for bin");

let json_first_item = json_layout
Expand Down
12 changes: 6 additions & 6 deletions jagua-rs/src/util/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn problem_matches_solution<P: ProblemGeneric>(problem: &P, solution: &Solut
}

pub fn layouts_match(layout: &Layout, layout_snapshot: &LayoutSnapshot) -> bool {
if layout.bin().id != layout_snapshot.bin.id {
if layout.bin.id != layout_snapshot.bin.id {
return false;
}
for placed_item in layout_snapshot.placed_items.values() {
Expand Down Expand Up @@ -120,8 +120,8 @@ pub fn item_to_place_does_not_collide(
}

pub fn layout_is_collision_free(layout: &Layout) -> bool {
for (key, pi) in layout.placed_items().iter() {
let ehf = EntityHazardFilter(vec![key.into()]);
for (_, pi) in layout.placed_items().iter() {
let ehf = EntityHazardFilter(vec![pi.into()]);

let combo_filter = match &pi.hazard_filter {
None => CombinedHazardFilter {
Expand Down Expand Up @@ -254,10 +254,10 @@ pub fn layout_qt_matches_fresh_qt(layout: &Layout) -> bool {
//check if every placed item is correctly represented in the quadtree

//rebuild the quadtree
let bin = layout.bin();
let bin = &layout.bin;
let mut fresh_cde = bin.base_cde.as_ref().clone();
for (pik, pi) in layout.placed_items().iter() {
let hazard = Hazard::new(pik.into(), pi.shape.clone());
for (_, pi) in layout.placed_items().iter() {
let hazard = Hazard::new(pi.into(), pi.shape.clone());
fresh_cde.register_hazard(hazard);
}

Expand Down
2 changes: 1 addition & 1 deletion jagua-rs/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod polygon_simplification;
pub fn print_layout(layout: &Layout) {
println!(
"let mut layout = Layout::new(0, instance.bin({}).clone());",
layout.bin().id
layout.bin.id
);
println!();

Expand Down
2 changes: 1 addition & 1 deletion lbf/benches/edge_sensitivity_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn edge_sensitivity_bench(config: LBFConfig, mut g: BenchmarkGroup<WallTime>) {

let layout = problem.get_layout(LayoutIndex::Real(0));
/*let samples = {
let sampler = UniformAARectSampler::new(layout.bin().bbox(), instance.item(0));
let sampler = UniformAARectSampler::new(layout.bin.bbox(), instance.item(0));
(0..N_SAMPLES).map(
|_| sampler.sample(&mut rng).compose()
).collect_vec()
Expand Down
4 changes: 2 additions & 2 deletions lbf/benches/hpg_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn hpg_query_bench(c: &mut Criterion) {
println!(
"[{}] sampler coverage: {:.3}% with {} samplers",
n_hpg_cells,
sampler.coverage_area / layout.bin().bbox().area() * 100.0,
sampler.coverage_area / layout.bin.bbox().area() * 100.0,
sampler.cell_samplers.len()
);

Expand Down Expand Up @@ -182,7 +182,7 @@ fn hpg_update_bench(c: &mut Criterion) {
println!(
"[{}] sampler coverage: {:.3}% with {} samplers",
n_hpg_cells,
sampler.coverage_area / layout.bin().bbox().area() * 100.0,
sampler.coverage_area / layout.bin.bbox().area() * 100.0,
sampler.cell_samplers.len()
);

Expand Down
12 changes: 6 additions & 6 deletions lbf/benches/quadtree_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::util::{create_base_config, N_ITEMS_REMOVED, SWIM_PATH};
criterion_main!(benches);
criterion_group!(
benches,
//quadtree_query_update_1000_1,
//quadtree_query_bench,
//quadtree_update_bench,
quadtree_query_update_1000_1,
quadtree_query_bench,
quadtree_update_bench,
quadtree_collect_query_bench
);

Expand Down Expand Up @@ -110,7 +110,7 @@ fn quadtree_query_bench(c: &mut Criterion) {
util::create_blf_problem(instance.clone(), config, N_ITEMS_REMOVED);

let layout = problem.get_layout(LayoutIndex::Real(0));
let sampler = UniformAARectSampler::new(layout.bin().bbox(), instance.item(0));
let sampler = UniformAARectSampler::new(layout.bin.bbox(), instance.item(0));
let mut rng = SmallRng::seed_from_u64(0);

let samples = (0..N_TOTAL_SAMPLES)
Expand Down Expand Up @@ -170,7 +170,7 @@ fn quadtree_query_update_1000_1(c: &mut Criterion) {
let (mut problem, _) = util::create_blf_problem(instance.clone(), config, N_ITEMS_REMOVED);

let layout = problem.get_layout(LayoutIndex::Real(0));
let sampler = UniformAARectSampler::new(layout.bin().bbox(), instance.item(0));
let sampler = UniformAARectSampler::new(layout.bin.bbox(), instance.item(0));
let mut rng = SmallRng::seed_from_u64(0);

let samples = (0..N_TOTAL_SAMPLES)
Expand Down Expand Up @@ -241,7 +241,7 @@ fn quadtree_collect_query_bench(c: &mut Criterion) {
util::create_blf_problem(instance.clone(), config, N_ITEMS_REMOVED);

let layout = problem.get_layout(LayoutIndex::Real(0));
let sampler = UniformAARectSampler::new(layout.bin().bbox(), instance.item(0));
let sampler = UniformAARectSampler::new(layout.bin.bbox(), instance.item(0));
let mut rng = SmallRng::seed_from_u64(0);

let samples = (0..N_TOTAL_SAMPLES)
Expand Down
2 changes: 1 addition & 1 deletion lbf/src/io/layout_to_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn s_layout_to_svg(
}

pub fn layout_to_svg(layout: &Layout, instance: &Instance, options: SvgDrawOptions) -> Document {
let internal_bin = layout.bin();
let internal_bin = &layout.bin;
let inv_bin_transf = internal_bin.pretransform.clone().inverse();
let bin = parser::pretransform_bin(internal_bin, &inv_bin_transf);

Expand Down
2 changes: 1 addition & 1 deletion lbf/src/lbf_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub fn sample_layout(
And the standard deviation tightens, to focus the search around the best sample.
*/

let mut ls_sampler = LSSampler::from_defaults(item, &best_opt.d_transf, &layout.bin().bbox());
let mut ls_sampler = LSSampler::from_defaults(item, &best_opt.d_transf, &layout.bin.bbox());

for i in 0..ls_sample_budget {
let d_transf = ls_sampler.sample(rng);
Expand Down
Loading

0 comments on commit 75c0b6d

Please sign in to comment.