rectangle_pack

Struct TargetBin

Source
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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.