Deterministic rectangle packing and layout verification toolkit for MoonBit
YM-ai-nb/rectloommoon add YM-ai-nb/rectloomlet options = @rectloom.PackOptions::default(64, 32)
let rects = [
@rectloom.Rect::make("hero", 18, 12),
@rectloom.Rect::make("icon", 8, 8),
@rectloom.Rect::make("badge", 9, 5),
]
match @rectloom.pack(rects, options) {
Ok(layout) => println(@rectloom.to_report(layout))
Err(err) => println(@rectloom.error_message(err))
}let layout = @rectloom.pack_spec(
"bin 64 32\npadding 1 1\nalgorithm guillotine\nsort area\nrotate true\nrect hero 18 12\n",
)moon fmt --check
moon check --deny-warn
moon build
moon test --deny-warn
moon run cmd/mainbin 64 32
padding 1 1
algorithm guillotine
sort area
rotate true
rect hero 18 12
rect icon 8 8let pages = @rectloom.pack_pages(rects, options, 3)
let preview = @rectloom.to_ascii_preview(layout, cell_width=4, cell_height=4)moon fmt --check
moon check --deny-warn
moon build
moon test --deny-warn
moon run cmd/main
moon publish --dry-runpub(all) struct ConstraintViolation {
name : String
expected : Int
actual : Int
severity : IssueSeverity
} derive(Eq, Debug)pub(all) struct GridReport {
cell_width : Int
cell_height : Int
cols : Int
rows : Int
cells : Array[DensityCell]
occupied_cells : Int
empty_cells : Int
densest_cell : DensityCell
sparsest_cell : DensityCell
} derive(Eq, Debug)pub(all) struct LayoutDiff {
left_algorithm : Algorithm
right_algorithm : Algorithm
left_occupancy_permille : Int
right_occupancy_permille : Int
occupancy_delta : Int
added_count : Int
removed_count : Int
moved_count : Int
resized_count : Int
rotation_changed_count : Int
unchanged_count : Int
changes : Array[PlacementChange]
} derive(Eq, Debug)pub(all) struct LayoutIssue {
severity : IssueSeverity
kind : IssueKind
id : String
message : String
value : Int
} derive(Eq, Debug)pub(all) struct LayoutStats {
placed_count : Int
unpacked_count : Int
rotated_count : Int
used_area : Int
container_area : Int
padded_capacity : Int
occupancy_permille : Int
padded_occupancy_permille : Int
waste_area : Int
bounding_x : Int
bounding_y : Int
bounding_width : Int
bounding_height : Int
bounding_area : Int
bounding_occupancy_permille : Int
row_count : Int
column_count : Int
horizontal_span : Int
vertical_span : Int
fragmentation_score : Int
compactness_score : Int
} derive(Eq, Debug)pub(all) struct PackCandidate {
options : PackOptions
layout : Layout
score : Int
rank : Int
} derive(Eq, Debug)pub(all) struct PackComparison {
base_options : PackOptions
candidates : Array[PackCandidate]
best : PackCandidate
} derive(Eq, Debug)pub(all) enum PackError {
InvalidBin(width~ : Int, height~ : Int)
InvalidPadding(outer~ : Int, inner~ : Int)
InvalidRect(id~ : String, width~ : Int, height~ : Int)
InvalidPageLimit(limit~ : Int)
InvalidCellSize(width~ : Int, height~ : Int)
DuplicateId(id~ : String)
OutOfBounds(id~ : String)
Overlap(left~ : String, right~ : String)
ParseError(line~ : Int, message~ : String)
} derive(Eq, Debug)pub(all) struct PlacementChange {
id : String
kind : PlacementChangeKind
old_x : Int
old_y : Int
old_width : Int
old_height : Int
new_x : Int
new_y : Int
new_width : Int
new_height : Int
area_delta : Int
} derive(Eq, Debug)pub(all) struct RectSetStats {
count : Int
total_area : Int
min_width : Int
max_width : Int
min_height : Int
max_height : Int
min_area : Int
max_area : Int
average_width_permille : Int
average_height_permille : Int
average_area_permille : Int
square_count : Int
wide_count : Int
tall_count : Int
oversized_count : Int
} derive(Eq, Debug)fn analyze_layout_with_options(layout : Layout, options : PackOptions) -> Result[LayoutStats, PackError]fn check_constraints(layout : Layout, constraints : LayoutConstraints) -> Result[Array[ConstraintViolation], PackError]fn compare_packers(rects : ArrayView[Rect], base : PackOptions) -> Result[PackComparison, PackError]fn density_grid(layout : Layout, cell_width? : Int, cell_height? : Int) -> Result[GridReport, PackError]fn diff_algorithms(rects : ArrayView[Rect], base : PackOptions, left_algorithm : Algorithm, right_algorithm : Algorithm) -> Result[LayoutDiff, PackError]fn pack_pages(rects : ArrayView[Rect], options : PackOptions, max_pages : Int) -> Result[MultiLayout, PackError]fn recommend_candidate(rects : ArrayView[Rect], base : PackOptions) -> Result[PackCandidate, PackError]Install
Download zipDeterministic rectangle packing and layout verification toolkit for MoonBit