rectpacker

R package for packing rectangles into a box using skyline algorithm implmented by STB

https://github.com/coolbutuseless/rectpacker

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.9%) to scientific vocabulary

Keywords

r
Last synced: 10 months ago · JSON representation

Repository

R package for packing rectangles into a box using skyline algorithm implmented by STB

Basic Info
  • Host: GitHub
  • Owner: coolbutuseless
  • License: other
  • Language: C
  • Default Branch: main
  • Homepage:
  • Size: 65.4 KB
Statistics
  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
r
Created over 1 year ago · Last pushed over 1 year ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = FALSE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(rectpacker)
```

# rectpacker


![](https://img.shields.io/badge/cool-useless-green.svg)
[![CRAN](https://www.r-pkg.org/badges/version/rectpacker)](https://CRAN.R-project.org/package=rectpacker)
[![R-CMD-check](https://github.com/coolbutuseless/rectpacker/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/rectpacker/actions/workflows/R-CMD-check.yaml)


'rectpacker' implements the skyline algorithm for packing rectangles into a box
using `stb_rect_pack.h` from [stb](https://github.com/nothings/stb).

I wrote this package as I needed to assemble graphical assets
into a single image - hopefully minimising the size of the final image.


## Installation

This package can be installed from CRAN

``` r
install.packages('rectpacker')
```

You can install the latest development version from
[GitHub](https://github.com/coolbutuseless/rectpacker) with:

``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/rectpacker')
```

Pre-built source/binary versions can also be installed from
[R-universe](https://r-universe.dev)

``` r
install.packages('rectpacker', repos = c('https://coolbutuseless.r-universe.dev', 'https://cloud.r-project.org'))
```

## Packing rectangles into a box

Pack 100 randomly sized rectangles into a 50x25 box.

Note that not all rectangles will fit into this box, but the algorithm does 
the best it can. If a rectangle cannot fit, then `packed = FALSE`

```{r example}
library(ggplot2)
library(rectpacker)

set.seed(1)
N       <- 100
widths  <- sample(7, N, replace = TRUE)
heights <- sample(6, N, replace = TRUE)
rects   <- pack_rects(50, 25, widths, heights)

# How many of the 100 rectangles actually got packed?
table(rects$packed)

# Keep only the rects which were able to be packed into the box
rects <- subset(rects, packed == TRUE)
head(rects)

# Plot the rectangles
ggplot(rects) +
  geom_rect(
    aes(
      xmin = x,
      ymin = y,
      xmax = x + w,
      ymax = y + h,
      fill = as.factor(idx)
    ),
    col  = 'black'
  ) + 
  annotate('rect', xmin = 0, ymin = 0, xmax = 50, ymax = 25, fill = NA, col = 'red') + 
  coord_equal() + 
  theme_minimal() + 
  theme(legend.position = 'none') + 
  scale_fill_viridis_d()
```


## Finding a good box size to pack rectangles into

Often it is necessary to find the size of a box which will fit all rectangles
without too much wasted space.

`calc_small_box()` performs a brute force search for a suitable box within the 
set of boxes of a given aspect ratio.

Note: This search is not guaranteed to find the absolute minimal box size.

```{r}
set.seed(1)
N <- 100
rect_widths  <- sample(N, N, T)
rect_heights <- sample(N, N, T)
box <- calc_small_box(rect_widths, rect_heights)
box
rects <- pack_rects(box$width, box$height, rect_widths, rect_heights)

ggplot(rects) +
  geom_rect(
    aes(
      xmin = x,
      ymin = y,
      xmax = x + w,
      ymax = y + h,
      fill = as.factor(idx)
    ),
    col  = 'black'
  ) + 
  annotate('rect', xmin = 0, ymin = 0, xmax = box$width, ymax = box$height, fill = NA, col = 'red') + 
  coord_equal() + 
  theme_minimal() + 
  theme(legend.position = 'none') + 
  scale_fill_viridis_d(option = 'D')
```



Owner

  • Name: mikefc
  • Login: coolbutuseless
  • Kind: user
  • Location: Australia

Cool, but useless.

GitHub Events

Total
  • Watch event: 3
  • Push event: 6
  • Public event: 1
Last Year
  • Watch event: 3
  • Push event: 6
  • Public event: 1

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 134 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
cran.r-project.org: rectpacker

Rectangle Packing

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 134 Last month
Rankings
Dependent packages count: 27.6%
Dependent repos count: 34.0%
Average: 49.5%
Downloads: 86.9%
Maintainers (1)
Last synced: 10 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/rhub.yaml actions
  • r-hub/actions/checkout v1 composite
  • r-hub/actions/platform-info v1 composite
  • r-hub/actions/run-check v1 composite
  • r-hub/actions/setup v1 composite
  • r-hub/actions/setup-deps v1 composite
  • r-hub/actions/setup-r v1 composite
DESCRIPTION cran
  • testthat >= 3.0.0 suggests
.devcontainer/Dockerfile docker
  • ghcr.io/rocker-org/devcontainer/tidyverse ${VARIANT} build
.devcontainer/requirements.txt pypi
  • ydiff * development