Skip to content
Edit this page

ictdesigntools.geometry.packing

Gravity-style circle packing for cable cross-section diagrams.

Cables are dropped one at a time (largest first) to the lowest free spot, touching the container wall or other cables, so the picture looks like cables resting in a run. Containers decide what "inside" means: a round conduit bore or an open-top tray.

Coordinates are in inches with y pointing down, matching SVG.

CircleContainer dataclass

CircleContainer(radius: float)

A round bore centred on the origin.

RectContainer dataclass

RectContainer(half_width: float, top: float, bottom: float)

An open-top box: floor at bottom, side walls at +-half_width, usable from top.

intersections

intersections(x0, y0, r0, x1, y1, r1)

Intersection points of two circles (0, 1 or 2 points).

Source code in src/ictdesigntools/geometry/packing.py
def intersections(x0, y0, r0, x1, y1, r1):
    """Intersection points of two circles (0, 1 or 2 points)."""
    dx, dy = x1 - x0, y1 - y0
    d = math.hypot(dx, dy)
    if d < 1e-12 or d > r0 + r1 + EPS or d < abs(r0 - r1) - EPS:
        return []
    a = (r0 * r0 - r1 * r1 + d * d) / (2 * d)
    h = math.sqrt(max(r0 * r0 - a * a, 0.0))
    xm, ym = x0 + a * dx / d, y0 + a * dy / d
    return [(xm + h * dy / d, ym - h * dx / d), (xm - h * dy / d, ym + h * dx / d)]

pack cached

pack(
    container: CircleContainer | RectContainer,
    radii: tuple[tuple[float, int], ...],
)

Pack (radius, line_index) items, largest first. Returns (placed, unplaced_count).

Source code in src/ictdesigntools/geometry/packing.py
@lru_cache(maxsize=256)
def pack(container: CircleContainer | RectContainer, radii: tuple[tuple[float, int], ...]):
    """Pack (radius, line_index) items, largest first. Returns (placed, unplaced_count)."""
    return _pack(container, radii)