Skip to content

Commit

Permalink
Take TargetBin's mutably
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Mar 23, 2021
1 parent 3dc48f5 commit c9ecd58
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rectangle-pack"
version = "0.2.0"
version = "0.2.1"
authors = ["Chinedu Francis Nwafili <[email protected]>"]
edition = "2018"
keywords = ["texture", "atlas", "bin", "box", "packer"]
Expand Down
82 changes: 63 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod box_size_heuristics;
/// // Information about where each `MyCustomRectId` was placed
/// let rectangle_placements = pack_rects(
/// &rects_to_place,
/// target_bins,
/// &mut target_bins,
/// &volume_heuristic,
/// &contains_smallest_box
/// ).unwrap();
Expand All @@ -113,13 +113,13 @@ pub fn pack_rects<
GroupId: Debug + Hash + PartialEq + Eq + Clone + Ord + PartialOrd,
>(
rects_to_place: &GroupedRectsToPlace<RectToPlaceId, GroupId>,
target_bins: BTreeMap<BinId, TargetBin>,
target_bins: &mut BTreeMap<BinId, TargetBin>,
box_size_heuristic: &BoxSizeHeuristicFn,
more_suitable_containers_fn: &ComparePotentialContainersFn,
) -> Result<RectanglePackOk<RectToPlaceId, BinId>, RectanglePackError> {
let mut packed_locations = HashMap::new();

let mut target_bins: Vec<(BinId, TargetBin)> = target_bins.into_iter().collect();
let mut target_bins: Vec<(&BinId, &mut TargetBin)> = target_bins.iter_mut().collect();
sort_bins_smallest_to_largest(&mut target_bins, box_size_heuristic);

let mut group_id_to_inbound_ids: Vec<(&Group<GroupId, RectToPlaceId>, &Vec<RectToPlaceId>)> =
Expand Down Expand Up @@ -277,7 +277,7 @@ impl Display for RectanglePackError {
impl std::error::Error for RectanglePackError {}

fn sort_bins_smallest_to_largest<BinId>(
bins: &mut Vec<(BinId, TargetBin)>,
bins: &mut Vec<(&BinId, &mut TargetBin)>,
box_size_heuristic: &BoxSizeHeuristicFn,
) where
BinId: Debug + Hash + PartialEq + Eq + Clone,
Expand Down Expand Up @@ -349,7 +349,14 @@ mod tests {
let mut groups: GroupedRectsToPlace<_, ()> = GroupedRectsToPlace::new();
groups.push_rect(RectToPlaceId::One, None, RectToInsert::new(3, 1, 1));

match pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap_err() {
match pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap_err()
{
RectanglePackError::NotEnoughBinSpace => {}
};
}
Expand Down Expand Up @@ -378,7 +385,14 @@ mod tests {
RectToInsert::new(100, 100, 1),
);

match pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap_err() {
match pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap_err()
{
RectanglePackError::NotEnoughBinSpace => {}
};
}
Expand All @@ -393,8 +407,13 @@ mod tests {
let mut targets = BTreeMap::new();
targets.insert(BinId::Three, TargetBin::new(5, 5, 1));

let packed =
pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap();
let packed = pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap();
let locations = packed.packed_locations;

assert_eq!(locations.len(), 1);
Expand Down Expand Up @@ -428,8 +447,13 @@ mod tests {
targets.insert(BinId::Three, TargetBin::new(5, 5, 1));
targets.insert(BinId::Four, TargetBin::new(5, 5, 2));

let packed =
pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap();
let packed = pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap();
let locations = packed.packed_locations;

assert_eq!(locations[&RectToPlaceId::One].0, BinId::Three,);
Expand Down Expand Up @@ -463,8 +487,13 @@ mod tests {
let mut targets = BTreeMap::new();
targets.insert(BinId::Three, TargetBin::new(20, 20, 2));

let packed =
pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap();
let packed = pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap();
let locations = packed.packed_locations;

assert_eq!(locations.len(), 2);
Expand Down Expand Up @@ -521,8 +550,13 @@ mod tests {
targets.insert(BinId::Three, TargetBin::new(20, 20, 1));
targets.insert(BinId::Four, TargetBin::new(50, 50, 1));

let packed =
pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap();
let packed = pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap();
let locations = packed.packed_locations;

assert_eq!(locations.len(), 2);
Expand Down Expand Up @@ -592,8 +626,13 @@ mod tests {
groups.push_rect(RectToPlaceId::One, None, RectToInsert::new(50, 90, 1));
groups.push_rect(RectToPlaceId::Two, None, RectToInsert::new(1, 1, 1));

let packed =
pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap();
let packed = pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap();
let locations = packed.packed_locations;

assert_eq!(locations.len(), 2);
Expand Down Expand Up @@ -670,8 +709,13 @@ mod tests {
groups.push_rect(RectToPlaceId::Two, None, RectToInsert::new(40, 10, 1));
groups.push_rect(RectToPlaceId::Three, None, RectToInsert::new(60, 3, 1));

let packed =
pack_rects(&groups, targets, &volume_heuristic, &contains_smallest_box).unwrap();
let packed = pack_rects(
&groups,
&mut targets,
&volume_heuristic,
&contains_smallest_box,
)
.unwrap();
let locations = packed.packed_locations;

assert_eq!(
Expand Down Expand Up @@ -753,7 +797,7 @@ mod tests {

let packed = pack_rects(
&rects_to_place,
target_bins.clone(),
&mut target_bins.clone(),
&volume_heuristic,
&contains_smallest_box,
)
Expand Down

0 comments on commit c9ecd58

Please sign in to comment.