Skip to content

Commit

Permalink
minor polishing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Aug 26, 2024
1 parent 3cf571f commit b3bf9b1
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 33 deletions.
4 changes: 2 additions & 2 deletions jagua-rs/src/collision_detection/hazard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl HazardEntity {
}

/// Whether the entity is dynamic in nature, i.e. it can be modified in the layout
pub fn dynamic(&self) -> bool {
pub fn is_dynamic(&self) -> bool {
match self {
HazardEntity::PlacedItem(_) => true,
HazardEntity::BinExterior => false,
Expand All @@ -60,7 +60,7 @@ impl HazardEntity {
}

/// Whether the entity universally applicable, i.e. all items need to be checked against it
pub fn universal(&self) -> bool {
pub fn is_universal(&self) -> bool {
match self {
HazardEntity::PlacedItem(_) => true,
HazardEntity::BinExterior => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl HazardProximityGrid {
//all universal hazards are applicable for the grid generator
let uni_hazards = static_hazards
.iter()
.filter(|h| h.entity.universal())
.filter(|h| h.entity.is_universal())
.map(|h| h.clone())
.collect_vec();
grid_generator::generate(bbox, &uni_hazards, n_cells)
Expand Down
4 changes: 2 additions & 2 deletions jagua-rs/src/collision_detection/hpg/hpg_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl HPGCell {

pub fn register_hazard(&mut self, to_register: &Hazard) -> HPGCellUpdate {
debug_assert!(
to_register.entity.universal(),
to_register.entity.is_universal(),
"no support for dynamic non-universal hazards at this time"
);
let current_prox = self.uni_prox.0;
Expand Down Expand Up @@ -155,7 +155,7 @@ impl HPGCell {

pub fn register_hazard_pole(&mut self, to_register: &Hazard, pole: &Circle) -> HPGCellUpdate {
debug_assert!(
to_register.entity.universal(),
to_register.entity.is_universal(),
"no support for dynamic non-universal hazards at this time"
);
let current_prox = self.uni_prox.0;
Expand Down
10 changes: 5 additions & 5 deletions jagua-rs/src/entities/problems/bin_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl BPProblem {
}

impl ProblemGeneric for BPProblem {
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex {
let layout_index = match &i_opt.layout_index {
fn place_item(&mut self, p_opt: &PlacingOption) -> LayoutIndex {
let layout_index = match &p_opt.layout_index {
LayoutIndex::Real(i) => LayoutIndex::Real(*i),
LayoutIndex::Template(i) => {
//Layout is empty, clone it and add it to `layouts`
Expand All @@ -127,11 +127,11 @@ impl ProblemGeneric for BPProblem {
LayoutIndex::Real(i) => &mut self.layouts[i],
LayoutIndex::Template(_) => unreachable!("cannot place item in template layout"),
};
let item = self.instance.item(i_opt.item_id);
layout.place_item(item, &i_opt.d_transform);
let item = self.instance.item(p_opt.item_id);
layout.place_item(item, &p_opt.d_transform);
let layout_id = layout.id();

self.register_included_item(i_opt.item_id);
self.register_included_item(p_opt.item_id);
self.layout_has_changed(layout_id);

layout_index
Expand Down
6 changes: 3 additions & 3 deletions jagua-rs/src/entities/problems/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pub enum Problem {
}

impl ProblemGeneric for Problem {
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex {
fn place_item(&mut self, p_opt: &PlacingOption) -> LayoutIndex {
match self {
Problem::BP(bp) => bp.place_item(i_opt),
Problem::SP(sp) => sp.place_item(i_opt),
Problem::BP(bp) => bp.place_item(p_opt),
Problem::SP(sp) => sp.place_item(p_opt),
}
}

Expand Down
4 changes: 3 additions & 1 deletion jagua-rs/src/entities/problems/problem_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::fsize;
pub trait ProblemGeneric: ProblemGenericPrivate {
/// Places an item into the problem instance according to the given `PlacingOption`.
/// Returns the index of the layout where the item was placed.
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex;
fn place_item(&mut self, p_opt: &PlacingOption) -> LayoutIndex;

/// Removes an item with a specific `PlacedItemUID` from a specific `Layout`
/// For more information about `commit_instantly`, see [`crate::collision_detection::cd_engine::CDEngine::deregister_hazard`].
Expand Down Expand Up @@ -119,6 +119,8 @@ pub(super) mod private {
}
}

pub const STRIP_LAYOUT_IDX: LayoutIndex = LayoutIndex::Real(0);

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
/// Unique index for a `Layout` in a problem instance.
pub enum LayoutIndex {
Expand Down
18 changes: 8 additions & 10 deletions jagua-rs/src/entities/problems/strip_packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::entities::layout::Layout;
use crate::entities::placed_item::PlacedItemUID;
use crate::entities::placing_option::PlacingOption;
use crate::entities::problems::problem_generic::private::ProblemGenericPrivate;
use crate::entities::problems::problem_generic::LayoutIndex;
use crate::entities::problems::problem_generic::ProblemGeneric;
use crate::entities::problems::problem_generic::{LayoutIndex, STRIP_LAYOUT_IDX};
use crate::entities::solution::Solution;
use crate::fsize;
use crate::geometry::geo_traits::{Shape, Transformable};
Expand Down Expand Up @@ -123,7 +123,7 @@ impl SPProblem {
let cde = self.layout.cde();
if !cde.shape_collides(&transformed_shape, entities_to_ignore.as_ref()) {
let insert_opt = PlacingOption {
layout_index: LayoutIndex::Real(0),
layout_index: STRIP_LAYOUT_IDX,
item_id: p_uid.item_id,
transform,
d_transform,
Expand Down Expand Up @@ -167,18 +167,17 @@ impl SPProblem {
}

impl ProblemGeneric for SPProblem {
fn place_item(&mut self, i_opt: &PlacingOption) -> LayoutIndex {
fn place_item(&mut self, p_opt: &PlacingOption) -> LayoutIndex {
assert_eq!(
i_opt.layout_index,
LayoutIndex::Real(0),
p_opt.layout_index, STRIP_LAYOUT_IDX,
"Strip packing problems only have a single layout"
);
let item_id = i_opt.item_id;
let item_id = p_opt.item_id;
let item = self.instance.item(item_id);
self.layout.place_item(item, &i_opt.d_transform);
self.layout.place_item(item, &p_opt.d_transform);

self.register_included_item(item_id);
LayoutIndex::Real(0)
STRIP_LAYOUT_IDX
}

fn remove_item(
Expand All @@ -188,8 +187,7 @@ impl ProblemGeneric for SPProblem {
commit_instantly: bool,
) {
assert_eq!(
layout_index,
LayoutIndex::Real(0),
layout_index, STRIP_LAYOUT_IDX,
"strip packing problems only have a single layout"
);
self.layout.remove_item(pi_uid, commit_instantly);
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 @@ -15,7 +15,7 @@ use crate::entities::instances::strip_packing::SPInstance;
use crate::entities::item::Item;
use crate::entities::placing_option::PlacingOption;
use crate::entities::problems::bin_packing::BPProblem;
use crate::entities::problems::problem_generic::{LayoutIndex, ProblemGeneric};
use crate::entities::problems::problem_generic::{LayoutIndex, ProblemGeneric, STRIP_LAYOUT_IDX};
use crate::entities::problems::strip_packing::SPProblem;
use crate::entities::quality_zone::InferiorQualityZone;
use crate::entities::quality_zone::N_QUALITIES;
Expand Down Expand Up @@ -316,7 +316,7 @@ pub fn build_strip_packing_solution(
let d_transform = transform.decompose();

let placing_opt = PlacingOption {
layout_index: LayoutIndex::Real(0),
layout_index: STRIP_LAYOUT_IDX,
item_id: item.id,
transform,
d_transform,
Expand Down
33 changes: 31 additions & 2 deletions jagua-rs/src/util/assertions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashSet;

use itertools::Itertools;
use log::error;
use std::collections::HashSet;
use std::iter;
use std::ops::Range;

use crate::collision_detection::cd_engine::CDEngine;
use crate::collision_detection::hazard::Hazard;
Expand All @@ -18,6 +19,7 @@ use crate::entities::bin::Bin;
use crate::entities::item::Item;
use crate::entities::layout::Layout;
use crate::entities::layout::LayoutSnapshot;
use crate::entities::placed_item::PlacedItem;
use crate::entities::problems::problem_generic::ProblemGeneric;
use crate::entities::solution::Solution;
use crate::geometry::geo_traits::{Shape, Transformable};
Expand Down Expand Up @@ -155,6 +157,33 @@ pub fn layout_is_collision_free(layout: &Layout) -> bool {
return true;
}

pub fn placed_item_collides(
layout: &Layout,
placed_item: &PlacedItem,
ignored_range_idx: Range<usize>,
) -> bool {
let ehf = EntityHazardFilter(
iter::once(placed_item.into())
.chain(ignored_range_idx.map(|i| (&layout.placed_items()[i]).into()))
.collect_vec(),
);

let combo_filter = match &placed_item.hazard_filter {
None => CombinedHazardFilter {
filters: vec![Box::new(&ehf)],
},
Some(hf) => CombinedHazardFilter {
filters: vec![Box::new(&ehf), Box::new(hf)],
},
};
let entities_to_ignore =
hazard_filter::generate_irrelevant_hazards(&combo_filter, layout.cde().all_hazards());

layout
.cde()
.shape_collides(&placed_item.shape, &entities_to_ignore)
}

pub fn qt_node_contains_no_deactivated_hazards<'a>(
node: &'a QTNode,
mut stacktrace: Vec<&'a QTNode>,
Expand Down
11 changes: 6 additions & 5 deletions lbf/benches/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use jagua_rs::entities::instances::instance::Instance;
use jagua_rs::entities::instances::instance_generic::InstanceGeneric;
use jagua_rs::entities::placed_item::PlacedItemUID;
use jagua_rs::entities::problems::problem::Problem;
use jagua_rs::entities::problems::problem_generic::{LayoutIndex, ProblemGeneric};
use jagua_rs::entities::problems::problem_generic::{
LayoutIndex, ProblemGeneric, STRIP_LAYOUT_IDX,
};
use jagua_rs::entities::problems::strip_packing::SPProblem;
use jagua_rs::fsize;
use jagua_rs::io::json_instance::JsonInstance;
Expand Down Expand Up @@ -54,17 +56,16 @@ pub fn create_blf_problem(
};

let mut rng = SmallRng::seed_from_u64(0);
let layout_index = LayoutIndex::Real(0);
// Remove some items from the layout
let removed_pi_uids = problem
.get_layout(&layout_index)
.get_layout(&STRIP_LAYOUT_IDX)
.placed_items()
.iter()
.map(|p_i| p_i.uid.clone())
.choose_multiple(&mut rng, n_items_removed);

for pi_uid in removed_pi_uids.iter() {
problem.remove_item(layout_index, pi_uid, true);
problem.remove_item(STRIP_LAYOUT_IDX, pi_uid, true);
info!(
"Removed item: {} with {} edges",
pi_uid.item_id,
Expand All @@ -85,7 +86,7 @@ pub fn create_blf_problem(
..SvgDrawOptions::default()
};
let svg = io::layout_to_svg::layout_to_svg(
problem.get_layout(&layout_index),
problem.get_layout(&STRIP_LAYOUT_IDX),
&instance,
draw_options,
);
Expand Down

0 comments on commit b3bf9b1

Please sign in to comment.