Skip to content

Skeleton

Wolren edited this page Apr 30, 2026 · 3 revisions

Skeleton Algorithm

The Skeleton family is a BCRS-free solver using medial-axis skeleton decomposition for seed generation, combined with SDF-guided boundary expansion. It offers an alternative approach to the largest inscribed rectangle problem with different computational characteristics.

Overview

Unlike BCRS which uses vertex-coordinate raster solve, Skeleton uses:

  • Medial-axis skeleton decomposition for initial seed generation
  • SDF (Signed Distance Field) for boundary expansion and containment verification

The approach captures the topological structure of the polygon to generate promising seed locations and orientations, then uses continuous optimization via SDF for boundary expansion.

Algorithm Flow

flowchart TD
    A[Stage 1: Geometry preparation] --> B[Stage 2: Skeleton extraction]
    B --> C[Stage 3: Hybrid seeds - skeleton PCA + edge angles]
    C --> D[Stage 4: Grid solve per seed]
    D --> E[Stage 5: SDF binary expansion]
    E --> F[Stage 6: Delta test (±2°)]
    F --> G[Stage 7: SDF containment certification]
    G --> H[Stage 8: Selection + output]
Loading

Technical Analysis

Medial-Axis Skeleton Decomposition

The medial axis (skeleton) of a polygon is the set of points equidistant from the polygon boundary. It encodes the topological structure as a set of connected branches.

Algorithm:

  1. Rasterize polygon at resolution 150 (long axis)
  2. Compute Euclidean distance transform
  3. Detect ridge points (local maxima of distance field)
  4. Cluster ridge points into regions using connected components
  5. Filter regions by clearance percentile (top 40%)
  6. Apply PCA to each region to extract principal orientation

Why it works:

  • Points with high clearance correspond to interior "corridors" where large rectangles can fit
  • The principal axis of each skeleton region indicates the optimal orientation for a rectangle in that region
  • Skeleton branches naturally follow the medial structure of the polygon

Signed Distance Field (SDF)

The SDF provides continuous distance to the polygon boundary at any point:

SDF(p) =  +distance(p, boundary)  if p outside polygon
          0                        if p on boundary  
          -distance(p, boundary)   if p inside polygon

Properties:

  • Negative inside, positive outside, zero on boundary
  • Continuous and differentiable (almost everywhere)
  • Enables binary search for boundary contact without vertex discretization

Implementation:

def _polygon_sdf(poly, x, y):
    pt = Point(x, y)
    d_poly = poly.distance(pt)
    if d_poly > 0.0:
        return d_poly  # outside
    d_ext = poly.exterior.distance(pt)
    if poly.contains(pt):
        # inside: return negative distance to nearest boundary
        min_d = d_ext
        for ring in poly.interiors:
            d_h = ring.distance(pt)
            min_d = min(min_d, d_h)
        return -min_d
    # on boundary or in hole
    return 0.0

SDF-Guided Binary Expansion

After initial grid solve, expand each rectangle side using SDF:

  1. For each side, compute SDF at side midpoint
  2. If SDF < 0 (inside), the maximum expansion is bounded by |SDF|
  3. Binary search from current position to maximum expansion
  4. Iterate 3 times for all four sides (coordinate ascent)
  5. 10 binary search steps per side per iteration

Advantages over CABF:

  • SDF provides continuous distance estimation
  • No dependency on vertex-coordinate grid
  • More precise expansion bounds

SDF Containment Certification

SDF also enables containment verification without explicit geometric tests:

  1. Compute SDF at rectangle corners and midpoints
  2. If any point has SDF > 0 (outside), rectangle is not contained
  3. If contained, apply symmetric shrink (up to 20%) if needed to guarantee containment

Stage-by-Stage

Stage 1: Geometry Preparation

  • Validate and normalize polygon geometry
  • Handle multipolygons (use largest part)
  • Optional precision snapping

Stage 2: Skeleton Extraction

  • Compute distance transform on rasterized polygon
  • Detect ridges (local maxima of distance field)
  • Cluster into regions, filter by clearance threshold

Stage 3: Hybrid Seeds

  • PCA on skeleton regions → candidate angles
  • Augment with nearest edge angles within ±10°
  • Fall back to pure edge angles if <2 skeleton seeds

Stage 4: Grid Solve

  • 40×40 grid at each seed angle
  • Largest-rectangle-in-histogram (LRH) algorithm
  • Returns axis-aligned rectangle in rotated frame

Stage 5: SDF Binary Expansion

  • 10 binary search steps per side
  • 3 outer iteration cycles (coordinate ascent)
  • Clamp to boundary contact

Stage 6: Delta Test

  • Test ±2° around each candidate angle
  • Catches edge-aligned angular optima missed by skeleton PCA

Stage 7: Containment Certification

  • Verify full containment via SDF evaluation
  • Symmetric shrink (up to 20%) if needed for certification
  • Best-effort fallback if strict certification fails

Stage 8: Output

  • Select best certified rectangle
  • Include diagnostics: rank, s2_gain, best_effort

Parameters

Parameter Default Purpose
GRID_COARSE 40 Initial grid resolution
GRID_FINE 40 Fine grid resolution (lower than BCRS)
TOP_K 5 Candidates for refinement
MAX_RATIO 0 (unlimited) Aspect ratio constraint
ALWAYS_RETURN True Allow fallback on failure
ANGLE_STEP 5.0 Fallback sweep step

Performance

Benchmark on 290 real-world polygons (i5-12400F, 32GB DDR4):

Metric Value
Mean fill rate 54.96%
Median fill rate 52.58%
Min fill rate 8.12%
Max fill rate 97.52%
Std deviation 21.21%
Time @290 34.64s

Comparison with other families:

Algorithm Mean Fill% Median Fill% Time @290 (s)
Axis-Aligned 35.87 35.43 12.98
Skeleton 54.96 52.58 34.64
BCRS Fast 55.27 53.24 15.28
BCRS 55.74 53.81 26.14

Best mode: 12 workers for most datasets.


Output Fields

  • area: Rectangle area in CRS map units
  • angle_deg: Rotation angle in degrees (0-90)
  • ratio: Aspect ratio (long:short)
  • cand_rank: Rank of selected candidate (0 = best)
  • s2_gain: Area gain from skeleton seed generation
  • best_effort: 1 if fallback used, 0 otherwise

Comparison with BCRS

Aspect Skeleton BCRS
Seed generation Medial-axis skeleton (distance transform) Edge-angle histogram
Expansion method SDF binary search CABF (vertex-coordinate clamping)
Containment SDF-based evaluation BCRS boundary-grid verification
Grid usage Uniform 40×40 Vertex-coordinate (variable pitch)
Mean fill rate 54.96% 55.74%
Time @290 34.64s 26.14s

Trade-offs:

  • Skeleton: Different seed generation approach, slower but captures interior structure
  • BCRS: Faster, slightly higher fill rate, uses vertex-coordinate grid

References

  • SDF: Signed distance fields — Osher & Fedkiw (2003). Level Set Methods and Dynamic Implicit Surfaces.
  • Medial Axis: Blum, H. (1967). "A transformation for extracting new descriptors of shape". Models for the Perception of Speech and Visual Form.
  • Distance Transform: Rosenfeld, A. & Pfaltz, J.L. (1966). "Sequential operations in digital picture processing". JACM.

Navigation

Clone this wiki locally