#!/usr/bin/env python3
"""
source.py — 2026-10-28--grok-ai
Volumetric section (realism as construction method).

A density field is accumulated by successive deposition of material under
gravity (downward bias) and adhesion (preference for already-dense sites).
One planar cut through the volume is shown; density is thresholded into two
solid Flexoki inks on a removable black ground. Clear margin on all sides.
No perspective, no viewpoint, no illustrated objects — only the section.
"""
import numpy as np
from PIL import Image

BLACK  = np.array([16, 15, 15], dtype=np.uint8)
ORANGE = np.array([188, 82, 21], dtype=np.uint8)   # orange-600
CYAN   = np.array([36, 131, 123], dtype=np.uint8)  # cyan-600

SIZE   = 2500
MARGIN = 200
SEED   = 20261028

def make_density(grid=640, seed=SEED):
    rng = np.random.default_rng(seed)
    dens = np.zeros((grid, grid), dtype=np.float32)

    # --- 1. Bottom sedimentary layer (settled mass) ---
    for _ in range(6):
        n = 12000
        xs = rng.integers(15, grid-15, size=n)
        ys = rng.integers(grid - 120, grid - 8, size=n)
        dens[ys, xs] += rng.uniform(0.8, 1.8, size=n)
        for ddy in range(-3, 1):
            dens[np.clip(ys+ddy,0,grid-1), xs] += 0.25

    # --- 2. Vertical columns / piles that grow upward from the base ---
    n_piles = 14
    pile_x = np.linspace(50, grid-50, n_piles).astype(int)
    pile_x += rng.integers(-20, 21, size=n_piles)
    for i, px in enumerate(pile_x):
        h = int(grid * rng.uniform(0.25, 0.55))
        width = rng.integers(4, 11)
        for yy in range(grid - h, grid):
            w = width + int(3 * (yy - (grid-h)) / h)  # taper or flare
            xs = px + rng.integers(-w, w+1, size=w*3)
            xs = np.clip(xs, 0, grid-1)
            dens[yy, xs] += rng.uniform(0.6, 1.4, size=len(xs))
            # adhesion sideways
            dens[yy, np.clip(xs-1,0,grid-1)] += 0.3
            dens[yy, np.clip(xs+1,0,grid-1)] += 0.3

    # --- 3. Falling material that sticks to the piles (adhesion) ---
    for wave in range(12):
        n = 7000
        xs = rng.integers(20, grid-20, size=n)
        ys = rng.integers(5, 80, size=n)
        for step in range(grid):
            ys = np.clip(ys + 1, 0, grid-1)
            xs = np.clip(xs + rng.integers(-1, 2, size=n), 0, grid-1)
            local = dens[ys, xs]
            stick = (local > 0.5) | ((ys > grid*0.6) & (local > 0.15))
            dens[ys[stick], xs[stick]] += 1.0
            for ddy,ddx in [(-1,0),(0,-1),(0,1),(1,0)]:
                dens[np.clip(ys[stick]+ddy,0,grid-1), np.clip(xs[stick]+ddx,0,grid-1)] += 0.25
            mask = ~stick
            xs, ys = xs[mask], ys[mask]
            n = len(xs)
            if n < 10:
                break

    # --- 4. A few overhanging / bridged masses (adhesion against gravity) ---
    for _ in range(8):
        cx = rng.integers(80, grid-80)
        cy = rng.integers(grid//5, grid//2)
        for r in range(25, 5, -2):
            for a in np.linspace(0, 2*np.pi, int(2*np.pi*r)):
                x = int(cx + r * np.cos(a) + rng.normal(0, 1.5))
                y = int(cy + r * 0.6 * np.sin(a) + rng.normal(0, 1))
                if 0 <= x < grid and 0 <= y < grid:
                    dens[y, x] += 0.7

    dens /= (dens.max() + 1e-8)
    # mild smooth
    dens[1:-1,1:-1] = 0.5*dens[1:-1,1:-1] + 0.125*(
        dens[:-2,1:-1] + dens[2:,1:-1] + dens[1:-1,:-2] + dens[1:-1,2:])
    dens /= (dens.max() + 1e-8)
    return dens


def render(dens, size=SIZE, margin=MARGIN, high_t=0.30, mid_t=0.09):
    d_img = Image.fromarray((np.clip(dens,0,1)*255).astype(np.uint8), mode='L')
    d_img = d_img.resize((size-2*margin, size-2*margin), Image.Resampling.LANCZOS)
    dens_f = np.asarray(d_img, dtype=np.float32) / 255.0

    high = dens_f > high_t
    mid  = (dens_f > mid_t) & ~high

    img = np.zeros((size, size, 3), dtype=np.uint8)
    img[:] = BLACK
    y0 = x0 = margin
    img[y0:y0+high.shape[0], x0:x0+high.shape[1]][high] = ORANGE
    img[y0:y0+mid.shape[0], x0:x0+mid.shape[1]][mid] = CYAN
    return Image.fromarray(img)


if __name__ == "__main__":
    dens = make_density()
    im = render(dens)
    im.save("render-1.png")
    a = np.array(im)
    black = np.all(a == BLACK, axis=2)
    print("render-1.png", im.size)
    print("black", round(float(black.mean()),4), "ink", round(float(1-black.mean()),4))
