# SPDX-License-Identifier: Apache-2.0
"""Turn a laid-out set of compartments into the solid that gets cut from a body.
`layout_compartments` decides *where* things go; this module builds *what* is
removed. It handles three kinds of compartment:
* a plain rectangular well (optionally with rounded corners and a finger scoop),
* a silhouette well driven by `shape_file`,
* an element pack — many individual silhouettes at their own offsets, rotations
and depths inside one bounding box (FR-004b).
Everything is returned in the box's own coordinate frame, so a caller only has
to subtract it from the body.
"""
from __future__ import annotations
from collections.abc import Sequence
from typing import TYPE_CHECKING
from pyboxbuilder.box.interior import Interior
from pyboxbuilder.enums import FingerCut, ScoopSide
from pyboxbuilder.rounding import (
max_radius,
round_edges,
rounding_facets,
vertical_edges,
)
if TYPE_CHECKING:
from pybosl2 import Color
from pybosl2.shapes3d import Bosl2Solid
from pyboxbuilder.builders._base import Cut
from pyboxbuilder.compartments.builder import CompartmentBuilder
from pyboxbuilder.compartments.layout import CompartmentPlacement
[docs]
def compartment_floor_z(placement: CompartmentPlacement, interior: Interior) -> float:
"""Z of a compartment's floor.
Wells are cut from the top down: the floor sits `depth` below the interior
ceiling, so shallow compartments sit high in the box and their pieces stay
within reach, matching the original toolkit's `$inner_height - depth` idiom.
"""
depth = min(placement.depth, interior.height)
return interior.origin_z + interior.height - depth
[docs]
def build_compartment_well(
placement: CompartmentPlacement,
interior: Interior,
*,
rounded_corners: float = 0.0,
bottom_rounding: float = 0.0,
) -> Bosl2Solid:
"""Build a compartment's well — the part that stays inside the walls.
Args:
placement: Where and how big the well is.
interior: The box interior it sits in.
rounded_corners: Radius for the well's vertical corners.
bottom_rounding: Radius where the well's walls meet its floor. A tray
wants this — pieces ride up the curve instead of having to be
picked out of a square corner — while a card slot wants ``0``, so
the stack sits flat on the floor.
Returns:
The cutout, positioned in the box frame.
"""
from pybosl2 import Anchor, cuboid
from pyboxbuilder.compartments.element import build_element_pack, svg_solid
width, length = placement.size
depth = min(placement.depth, interior.height)
if placement.elements:
# An element pack positions its own members; it is already corner-based.
well = build_element_pack(placement.elements, depth)
assert well is not None # non-empty by the branch condition
return _place(well, placement, interior)
if placement.shape_file:
well = svg_solid(placement.shape_file, width, length, depth)
else:
# The two radii differ, and a cuboid carries one, so the floor fillet is
# built in and the vertical corners are taken off afterwards. Both make
# the *void* smaller, which is what leaves material as a fillet.
size = [width, length, depth]
if bottom_rounding > 0 and bottom_rounding <= max_radius(size, Anchor.BOTTOM):
well = cuboid(size, rounding=bottom_rounding,
edges=Anchor.BOTTOM, **rounding_facets())
else:
well = cuboid(size)
if rounded_corners > 0:
well = round_edges(
well, [width, length, depth], rounded_corners, vertical_edges(),
at=(-width / 2, -length / 2, -depth / 2),
)
# pybosl2 primitives are centre-anchored; shift so the well's lower-left
# corner and floor land on the placement.
return _place(well.translate([width / 2, length / 2, depth / 2]), placement, interior)
[docs]
def default_scoop_side(placement: CompartmentPlacement) -> ScoopSide:
"""Which wall a scoop goes in when the compartment does not say.
The **shorter** wall. A card stack is lifted out across its narrow
dimension, so the cut belongs in the short face; put it in the long one and
you are reaching along the whole width of the box to get at the cards.
Args:
placement: The compartment the scoop belongs to.
Returns:
`LEFT` when the footprint is wider than it is long, else `FRONT`.
"""
width, length = placement.size
return ScoopSide.LEFT if width > length else ScoopSide.FRONT
[docs]
def build_compartment_scoop(
placement: CompartmentPlacement,
interior: Interior,
scoop_side: ScoopSide = ScoopSide.FRONT,
top_z: float | None = None,
cut: Cut | None = None,
) -> Bosl2Solid:
"""Build a compartment's finger scoop — the part that pierces a wall.
The wall thickness is read off the interior frame (its origin is inset by
exactly one wall), because the scoop's face fillets are produced by flaring
the sweep's ends and therefore need the sweep to match the wall it crosses.
Args:
placement: The compartment the scoop belongs to.
interior: The box interior frame the compartment sits in.
scoop_side: Which wall the scoop pierces.
top_z: The box's top face. The cut reaches it so its roll merges into
the rim; ``None`` falls back to the interior ceiling.
cut: What the compartment asked for (FR-006) — kind, side and any
measurement it wanted to name. Passed in rather than read off the
placement, which does not carry it: reaching for it there returned
a default and looked like it worked.
Returns:
The scoop cutout, positioned in the box frame.
"""
from pyboxbuilder.builders._base import Cut
from pyboxbuilder.compartments.finger_cuts import build_cut
from pyboxbuilder.compartments.finger_outline import CutProfile
from pyboxbuilder.compartments.finger_sweep import FaceTreatment
width, length = placement.size
floor_z = compartment_floor_z(placement, interior)
# The scoop runs from the compartment floor to the **top of the box**, not
# to the interior ceiling. Stopping at the ceiling leaves the lid band
# standing above the cut as a step, and buries the r1 roll inside the wall
# where it can neither be seen nor felt — the roll is meant to merge the
# cut into the box's top edge.
top = top_z if top_z is not None else interior.origin_z + interior.height
depth = max(0.1, top - floor_z)
base_wt = (
interior.wall_thickness
if interior.wall_thickness is not None
else (interior.origin_x if interior.origin_x > 0 else 2.0)
)
if scoop_side == ScoopSide.FRONT:
gap = placement.position[1] - interior.origin_y
elif scoop_side == ScoopSide.BACK:
gap = (interior.origin_y + interior.length) - (placement.position[1] + length)
elif scoop_side == ScoopSide.LEFT:
gap = placement.position[0] - interior.origin_x
elif scoop_side == ScoopSide.RIGHT:
gap = (interior.origin_x + interior.width) - (placement.position[0] + width)
gap = max(0.0, gap)
wall_thickness = base_wt
cut = cut if cut is not None else Cut()
floor_thickness: float | None = (
floor_z if cut.kind is FingerCut.THROUGH_FLOOR
else (interior.origin_z if interior.origin_z > 0 else None)
)
scoop = build_cut(
cut.kind,
width, length, cut.depth if cut.depth is not None else depth, scoop_side,
wall_thickness=wall_thickness,
floor_thickness=floor_thickness,
profile=CutProfile(
width=cut.width,
base_radius=cut.base_radius,
mouth_flare=cut.mouth_flare,
roll_rise=cut.roll_rise,
),
faces=FaceTreatment(fillet=cut.face_fillet),
)
if gap > 0:
from pybosl2 import cuboid
offset_map = {
ScoopSide.FRONT: [0.0, -gap, 0.0],
ScoopSide.BACK: [0.0, gap, 0.0],
ScoopSide.LEFT: [-gap, 0.0, 0.0],
ScoopSide.RIGHT: [gap, 0.0, 0.0],
}
scoop = scoop.translate(offset_map[scoop_side])
cut_radius = (
(cut.width / 2)
if cut.width is not None
else (17.0 if cut.kind is FingerCut.THROUGH_FLOOR else 14.0)
)
span = width if scoop_side in (ScoopSide.FRONT, ScoopSide.BACK) else length
cut_w = min(cut_radius * 2, span)
bridge_h = depth + 4.0
z_mid = bridge_h / 2 - 0.02
fudge = 0.05
if scoop_side == ScoopSide.FRONT:
bridge = cuboid([cut_w, gap + fudge, bridge_h]).translate(
[width / 2, -gap / 2, z_mid]
)
elif scoop_side == ScoopSide.BACK:
bridge = cuboid([cut_w, gap + fudge, bridge_h]).translate(
[width / 2, length + gap / 2, z_mid]
)
elif scoop_side == ScoopSide.LEFT:
bridge = cuboid([gap + fudge, cut_w, bridge_h]).translate(
[-gap / 2, length / 2, z_mid]
)
else: # ScoopSide.RIGHT
bridge = cuboid([gap + fudge, cut_w, bridge_h]).translate(
[width + gap / 2, length / 2, z_mid]
)
scoop = scoop | bridge
return _place(scoop, placement, interior)
def _place(
solid: Bosl2Solid, placement: CompartmentPlacement, interior: Interior
) -> Bosl2Solid:
"""Move a compartment-local solid into the box frame."""
return solid.translate([
placement.position[0],
placement.position[1],
compartment_floor_z(placement, interior),
])
TRAY_ROUNDING_RATIO = 2.0 / 3.0
"""A tray well's default radius, as a fraction of its own depth.
Sized off the *cutout*, not off the box: what makes a piece easy to get out is
the curve your finger follows up the wall, so a deep well wants a big sweep and
a shallow token tray a small one. A box-wide constant gets both wrong.
"""
[docs]
def tray_rounding(placement: CompartmentPlacement, builder: object) -> float:
"""Return the radius for a tray well's vertical corners and its floor.
Rounding is **opt-in**: a well is square unless its builder says it holds
loose pieces. That is the right default because most wells are shaped by
what they hold — a card slot needs a flat floor and square corners to sit
in, a silhouette is reproduced as authored (FR-045), an element pack places
its own members — and softening any of those changes a fit rather than
improving it. A tray full of tokens is the case that benefits, and it says
so.
Args:
placement: The well being carved.
builder: Its :class:`CompartmentBuilder`, if it has one. ``None`` (a
bare placement) is square, like any well that has not opted in.
Returns:
The radius in mm — ``depth × 2/3``, capped so it can neither swallow the
well's footprint nor exceed its depth.
"""
explicit = getattr(builder, "rounded_corners", 0.0) or 0.0
if explicit <= 0:
if not getattr(builder, "holds_pieces", False):
return 0.0 # square unless the well is declared a piece tray
if getattr(builder, "shape_file", None) or getattr(builder, "elements", ()):
return 0.0 # the game dictates this shape; FR-045 keeps it exact
radius = explicit if explicit > 0 else placement.depth * TRAY_ROUNDING_RATIO
width, length = placement.size
# A radius at least half the footprint has no straight side left to blend
# into, and one deeper than the well would cut through its own floor.
return max(0.0, min(radius, min(width, length) / 2 - 0.01, placement.depth))
[docs]
def build_contents(
placements: Sequence[CompartmentPlacement],
interior: Interior,
builders: dict[str, CompartmentBuilder] | None = None,
clip: bool = True,
top_z: float | None = None,
default_side: ScoopSide | None = None,
wall_tops: dict[ScoopSide, float] | None = None,
mask: Bosl2Solid | None = None,
hinge_intrusion: Bosl2Solid | None = None,
suppress_scoops: bool = False,
) -> Bosl2Solid | None:
"""Union the cutouts for every placed compartment. None when there are none.
Wells and finger scoops are treated differently on purpose: a well must stay
inside the walls, while a scoop only works if it cuts through one. So the
wells are clipped to the interior footprint and the scoops are added after.
Args:
placements: Output of `layout_compartments`.
interior: The box interior the placements were laid out in.
builders: Optional {label: CompartmentBuilder} so per-compartment
options (rounded corners, finger scoops) are honoured.
clip: Trim the wells to the interior footprint so none can break through
a side wall (FR-018). Each well's own rounding comes from its depth
and its builder — see :func:`tray_rounding`.
top_z: Fallback top for a scoop's roll when `wall_tops` has no entry
for the side it lands on.
wall_tops: ``{ScoopSide: z}`` giving each wall's top. The four walls do
not have to end level — a sliding box's exit wall stops a lid
thickness below the rest — so the roll aligns per side.
mask: The volume contents may occupy, when the box type has something
of its own standing in the interior. A hinge box's barrel is the
case: it sits inside the outline, so the wells are clipped clear
of it rather than left to collide.
default_side: The box type's preferred scoop wall, used when the
compartment names none. A sliding box insists on the wall its lid
leaves by; most types have no opinion and leave it to the shape.
hinge_intrusion: Volume containing the hinge mechanism that compartments must not intersect.
suppress_scoops: Skip generating finger scoops (e.g. for lids/closures that conflict).
"""
from pyboxbuilder.compartments.element import union_all
builders = builders or {}
wells = []
scoops = []
for placement in placements:
builder = builders.get(placement.label)
radius = tray_rounding(placement, builder)
wells.append(
build_compartment_well(
placement, interior, rounded_corners=radius, bottom_rounding=radius,
)
)
if placement.elements:
from pyboxbuilder.compartments.element import build_element_pack_pull_outs
if len(placements) == 1:
pull_out_canvas = (
max(placement.size[0], interior.width - (placement.position[0] - interior.origin_x)),
max(placement.size[1], interior.length - (placement.position[1] - interior.origin_y)),
)
else:
pull_out_canvas = placement.size
pull_outs = build_element_pack_pull_outs(
placement.elements, placement.depth, pull_out_canvas
)
if pull_outs is not None:
scoops.append(_place(pull_outs, placement, interior))
cut = getattr(builder, "cut", None)
if cut is not None and not suppress_scoops:
if cut.side is not None:
side = cut.side
elif default_side is not None:
# Box types with a fixed exit wall (e.g. SlidingBox) require the
# scoop to share that open wall so it never cuts through a
# dovetail rail.
side = default_side
else:
side = default_scoop_side(placement)
side_top = (wall_tops or {}).get(side, top_z)
scoops.append(
build_compartment_scoop(
placement, interior, side, top_z=side_top, cut=cut,
)
)
if placement.depth > 35.0:
opposing = {
ScoopSide.FRONT: ScoopSide.BACK,
ScoopSide.BACK: ScoopSide.FRONT,
ScoopSide.LEFT: ScoopSide.RIGHT,
ScoopSide.RIGHT: ScoopSide.LEFT,
}
opp_side = opposing[side]
opp_side_top = (wall_tops or {}).get(opp_side, top_z)
scoops.append(
build_compartment_scoop(
placement, interior, opp_side, top_z=opp_side_top, cut=cut,
)
)
contents = union_all(wells)
if contents is not None and clip:
contents = contents & interior_column(interior)
if contents is not None and mask is not None:
# Wells only: a finger scoop's whole job is to breach a wall, so it is
# added after this and is not masked.
contents = contents & mask
if contents is not None:
# Open the box. A compartmented box is carved out of a solid block, so
# without this the material above the interior ceiling — the lid recess
# on a cap box, the groove band on a sliding one — stays put and seals
# every well under a lid of its own.
mouth = interior_mouth(interior)
if hinge_intrusion is not None:
mouth = mouth - hinge_intrusion
contents = contents | mouth
scoop = union_all(scoops)
if scoop is None:
return contents
return scoop if contents is None else contents | scoop
[docs]
def interior_mouth(interior: Interior, headroom: float = 1000.0) -> Bosl2Solid:
"""Return the open volume above the interior ceiling — the box's mouth.
Runs from the ceiling up past the top of any box, so whatever the type puts
between the interior and the rim is cleared away and the wells are reachable.
"""
from pyboxbuilder.box.shell import block
return block(
[interior.width, interior.length, headroom],
at=(
interior.origin_x,
interior.origin_y,
interior.origin_z + interior.height,
),
)
[docs]
def interior_column(interior: Interior) -> Bosl2Solid:
"""Return a tall prism over the interior footprint, used to clip compartments.
Clipping is in X/Y only: a cutout must never break through a side wall, but
it does have to run out through the top of the box, which is where pieces go
in. This is the same guard the original toolkit applies by hand when a
hexagon overhangs the interior — a hex laid out flat-to-flat is wider than
its nominal width across the points, so it can poke through a wall.
"""
from pybosl2 import cuboid
tall = interior.height * 4 + 100.0
return cuboid([interior.width, interior.length, tall]).translate([
interior.origin_x + interior.width / 2,
interior.origin_y + interior.length / 2,
interior.origin_z + interior.height / 2,
])
[docs]
def build_inserts(
placements: Sequence[CompartmentPlacement],
interior: Interior,
) -> list[tuple[Bosl2Solid, Color]]:
"""Build every coloured positive insert across the placed compartments.
These are the *added* counterpart of :func:`build_contents`: a coloured
element is a thin icon pressed into the bottom of its well, not a cutout, so
it is returned as a separate solid (in the box frame) with its colour rather
than subtracted from the body.
"""
from pyboxbuilder.compartments.element import build_element_pack_inserts
inserts: list[tuple[Bosl2Solid, Color]] = []
for placement in placements:
if not placement.elements:
continue
for solid, color in build_element_pack_inserts(placement.elements, placement.depth):
inserts.append((_place(solid, placement, interior), color))
return inserts