pub struct TargetBin { /* private fields */ }
Expand description

A bin that we’d like to play our incoming rectangles into

Implementations§

source§

impl TargetBin

source

pub fn coalesce_available_sections( _bin_section_index: usize, _compare_to_indices: Range<usize> )

Over time as you use TargetBin.push_available_bin_section to return remove packed rectangles from the TargetBin, you may end up with neighboring bin sections that can be combined into a larger bin section.

Combining bin sections in this was is desirable because a larger bin section allows you to place larger rectangles that might not fit into the smaller bin sections.

In order to coalesce, or combine a bin section with other bin sections, we need to check every other available bin section to see if they are neighbors.

This means that fully coalescing the entire list of available bin sections is O(n^2) time complexity, where n is the number of available empty sections.

Basic Usage
let target_bin = my_target_bin();

for idx in 0..target_bin.available_bin_sections().len() {
    let len = target_bin.available_bin_sections().len();
    target_bin.coalesce_available_sections(idx, 0..len);
}
Distributing the Workload

It is possible that you are developing an application that can in some cases have a lot of heavily fragmented bins that need to be coalesced. If your application has a tight performance budget, such as a real time simulation, you may not want to do all of your coalescing at once.

This method allows you to split the work over many frames by giving you fine grained control over which bin sections is getting coalesced and which other bin sections it gets tested against.

So, for example, say you have an application where you want to fully coalesce the entire bin every ten seconds, and you are running at 60 frames per second. You would then distribute the coalescing work such that it would take 600 calls to compare every bin section.

Here’s a basic eample of splitting the work.

let target_bin = my_target_bin();

let current_frame: usize = get_current_frame() % 600;

for idx in 0..target_bin.available_bin_sections().len() {
    let len = target_bin.available_bin_sections().len();

    let start = len / 600 * current_frame;
    let end = start + len / 600;

    target_bin.coalesce_available_sections(idx, start..end);
}
source§

impl TargetBin

source

pub fn push_available_bin_section( &mut self, bin_section: BinSection ) -> Result<(), PushBinSectionError>

Push a BinSection to the list of remaining BinSection’s that rectangles can be placed in.

Performance

This checks that your BinSection does not overlap any other bin sections. In many cases this will be negligible, however it is important to note that this has a worst case time complexity of O(Width * Height * Depth), where the worst case is tht you have a bin full of 1x1x1 rectangles.

To skip the validity checks use TargetBin.push_available_bin_section_unchecked.

source

pub fn push_available_bin_section_unchecked(&mut self, bin_section: BinSection)

Push a BinSection to the list of remaining BinSection’s that rectangles can be placed in, without checking whether or not it is valid.

Use TargetBin.push_available_bin_section if you want to check that the new bin section does not overlap any existing bin sections nad that it is within the TargetBin’s bounds.

source§

impl TargetBin

source

pub fn new(max_width: u32, max_height: u32, max_depth: u32) -> Self

source

pub fn available_bin_sections(&self) -> &Vec<BinSection>

The free BinSections within the TargetBin that rectangles can still be placed into.

source

pub fn remove_filled_section(&mut self, idx: usize)

Remove the section that was just split by a placed rectangle.

source

pub fn add_new_sections(&mut self, new_sections: [BinSection; 3])

When a section is filled it gets split into three new sections. Here we add those.

TODO: Ignore sections with a volume of 0

Trait Implementations§

source§

impl Clone for TargetBin

source§

fn clone(&self) -> TargetBin

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TargetBin

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.