dymokomi/luce-develop
Source of release 0.1.0
0.1.0 / src / film_grains.lucb
## The emulsion as grains, for the stochastic render (docs/PHYSICAL-FILM.md,
## "Rendering"). Each layer's three populations (fast, mid, slow; film_model.lucb's
## `fit_populations`) are real grains on the film plane, in µm: a Poisson
## process of each population's density, made cell by cell from a hash, so
## nothing is stored and the same grains come back every pass. Each grain has
## its place, a size (0.6–1.4 times its population's), and a rank (0..1, its
## sensitivity among its population's). It develops where its rank is under
## its population's developed share at the exposure its place had (after
## inhibition and edges), so more exposure develops more of the same grains.
## A developed grain forms a cloud of dye (silver on black and white): density
## q·(1 − r²/ρ²)² within its radius ρ. Clouds add as density, as dyes do.
##
## The scanner averages the light a pixel passes, not its density: a pixel is
## −log10 of the mean transmittance over its area. The Poisson field's Laplace
## functional (Campbell's theorem applied to the transmittance) gives that mean
## exactly for a field of clouds (−log10 E[T] = λ·∫(1 − 10^(−q·c)) dA / ln 10)
## and the variance of its mean over an aperture (an integral of
## exp(λ·K(r)) − 1, K the clouds' overlap of their 1 − 10^(−q·c)); the
## density's variance through the aperture is first order in it (a delta
## method). Both hold for grains developing independently of each other. `fit`
## uses both: each population's grains are placed so the developed field's
## density over a large area is the stock's curve and its rms through a 48 µm
## aperture the stock's granularity, at every exposure. Summing densities and
## taking a mean (the thin-grain, first-order picture) would be wrong by a
## factor of two or more at real densities: where clouds overlap, light comes
## through the gaps between them.
##
## Where a pixel holds many developed clouds (`gaussian_from` and more), the
## light it passes is the sum of many independent grains: Gaussian to a few
## percent, with Campbell's mean and covariances. The pixel's variance is the
## granularity scaled to its area (Selwyn's law) times how much of a cloud's
## spread a pixel of its size averages (`pixel_covariance`), and clouds across
## a pixel's border tie it to its neighbours: its deviate is its own plus
## those of its four edges and four corners, shared with the pixels there,
## weighted so the variance and the neighbours' and diagonals' covariances are
## Campbell's. There the stochastic render takes the pixel from that
## distribution; grains are traced one by one where few develop (a toe, a
## slide's highlights, a view finer than the grain), where their shape and
## sparseness show. Grains are made in bins of
## rank (each its own Poisson stream), and only the bins that can develop are
## made, so tracing costs what develops, not what is coated.
##
## The clouds' peak density is an estimate: 0.5 for a color dye cloud, 2.0 for
## a developed silver grain (nearly opaque). Their sizes follow from it and
## the granularity, as Selwyn's law ties the two: grains of a given density
## make more grain the larger they are. Populations' radii are 1, 0.75 and
## 0.55 of the fast one's (an estimate: faster emulsions have larger grains).
import memory
import sync
from luce_std import math
import film_model
pub let populations: usize = 3
## Every population's sizes, uniform over this range of its radius.
pub let size_low: f64 = 0.6
pub let size_high: f64 = 1.4
## A cell is this many radii wide, so a 3 × 3 block of cells holds every grain
## that can reach a point in the middle one.
pub let cell_radii: f64 = 1.4
## The 48 µm circular aperture granularity is read through (µm²).
pub let aperture_area: f64 = 1809.5573684677208
## The clouds' peak density (estimates), and the populations' radii against the fast one's.
pub let color_peak: f64 = 0.5
pub let silver_peak: f64 = 2.0
let radius_ratio: f64[3] = [1.0, 0.75, 0.55]
let ln10: f64 = 2.302585092994046
## A pixel's layer is taken from its distribution from this many developed
## clouds in it.
pub let gaussian_from: f64 = 16.0
## The rank bins: the first [0, 1/128), then each twice as wide.
pub let rank_bins: u32 = 8
pub func bin_low(b: u32) -> f64:
return 0.0 if b == 0 else math.pow(2.0, (f64)b - 8.0)
pub func bin_high(b: u32) -> f64:
return math.pow(2.0, (f64)b - 7.0)
## Poisson counts beyond this mean come from the normal approximation.
let normal_beyond: f64 = 24.0
pub let count_most: u32 = 96
## The share tables over log H (film_model.lucb's grid).
pub let table_samples: usize = 1024
pub let log_low: f64 = -4.5
pub let log_high: f64 = 2.5
## A population's grains: how many per µm², their clouds' peak density, their radius (µm).
pub struct Geometry:
pub var density: f64
pub var peak: f64
pub var radius: f64
## The grains of a stock developed some way: each layer's populations, and per
## layer over log H (`table_samples`, effective log H) each population's share
## developed (four per sample: three shares and the clouds' strength, the
## factor on their peak density).
pub struct Fit:
pub var geometry: Geometry[9]
pub var table: f64[]
## The fitted grains' own granularity (rms through 48 µm, Campbell) over
## the same log H, per layer: what pixels drawn from their distribution
## take, so they stand in for these grains exactly.
pub var granularity: f64[]
pub func release():
if self.table.length > 0:
free(self.table) in memory.heap
if self.granularity.length > 0:
free(self.granularity) in memory.heap
var empty: f64[]
self.table = empty
var none_left: f64[]
self.granularity = none_left
# mark: Hashing ===============================================================
## A hash of two numbers (film.lucb's `hashed`, without the fraction).
pub func mixed(px: u32, py: u32) -> u32:
var x = px *% 1664525 +% py *% 1013904223
x = x ^ (x >> 16)
x = x *% 0x7feb352d
x = x ^ (x >> 15)
x = x *% 0x846ca68b
x = x ^ (x >> 16)
return x
## A hash as a number in [0, 1), 24 bits (exact in single floats).
pub func fraction(x: u32) -> f64:
return (f64)(x >> 8) / 16777216.0
## Cell (`cx`, `cy`)'s seed for stream `stream` (a layer's population).
pub func cell_seed(cx: u32, cy: u32, stream: u32) -> u32:
return mixed(cx +% stream *% 0x9e3779b9, cy ^ (stream *% 0x85ebca6b))
## Grain `j` of a cell with `seed`: its place in the cell (0..1 each way, from
## one hash: 16 bits each), and its size and rank (from another, so a grain out
## of reach costs one hash).
pub func grain_place(seed: u32, j: u32) -> (f64, f64):
let h = mixed(seed, j *% 2)
return ((f64)(h & 0xffff) / 65536.0 + 1.0 / 131072.0, (f64)(h >> 16) / 65536.0 + 1.0 / 131072.0)
pub func grain_size_rank(seed: u32, j: u32) -> (f64, f64):
let h = mixed(seed, j *% 2 +% 1)
return (size_low + (size_high - size_low) * (f64)(h & 0xffff) / 65536.0, (f64)(h >> 16) / 65536.0)
## How many grains a cell with `seed` holds, Poisson with mean `mean`: one
## uniform through the cumulative distribution (the normal approximation
## beyond `normal_beyond`).
pub func grain_count(seed: u32, mean: f64) -> u32:
if mean <= 0.0:
return 0
if mean > normal_beyond:
let first = math.max(fraction(mixed(seed, 0xfffffff1)), 1.0e-7)
let second = fraction(mixed(seed, 0xfffffff2))
let z = math.sqrt(-2.0 * math.log(first)) * math.cos(2.0 * math.pi * second)
return (u32)math.clamp(math.floor(mean + math.sqrt(mean) * z + 0.5), 0.0, (f64)count_most)
let u = fraction(mixed(seed, 0xffffff00))
var p = math.exp(-mean)
var cumulative = p
var count: u32 = 0
while u > cumulative and count < count_most:
count += 1
p *= mean / (f64)count
cumulative += p
return count
# mark: Campbell's integrals ==================================================
## A unit cloud's light taken, 1 − 10^(−q·(1 − r²)²), integrated over its disc
## and over ln 10 (`light_taken`), and two clouds' overlap of it at `r` apart
## (`overlap`), tabulated over r (0..2) and over q on a log grid.
pub let peak_count: usize = 48
pub let peak_low: f64 = 0.002
pub let peak_high: f64 = 120.0
let distance_count: usize = 41
var overlap_table: f64[1968]
var taken_table: f64[48]
var tables_once: sync.Once
pub func peak_at(index: usize) -> f64:
return peak_low * math.pow(peak_high / peak_low, (f64)index / (f64)(peak_count - 1))
func taken(q: f64, rr: f64) -> f64:
if rr >= 1.0:
return 0.0
let c = (1.0 - rr) * (1.0 - rr)
return 1.0 - math.pow(10.0, -q * c)
func build_tables():
let grid = 48
let step = 2.0 / (f64)grid
for qi in 0..<peak_count:
let q = peak_at(qi)
var sum = 0.0
for at in 0..<(usize)256:
let v = ((f64)at + 0.5) / 256.0
sum += taken(q, v) / 256.0
taken_table[qi] = math.pi * sum / ln10
for di in 0..<distance_count:
let d = 2.0 * (f64)di / (f64)(distance_count - 1)
# Over the unit disc on a grid, the cloud at the origin against one
# `d` along x.
var shared = 0.0
for yi in 0..<grid:
let y = -1.0 + ((f64)yi + 0.5) * step
for xi in 0..<grid:
let x = -1.0 + ((f64)xi + 0.5) * step
let here = x * x + y * y
if here >= 1.0:
continue
let there = (x - d) * (x - d) + y * y
if there >= 1.0:
continue
shared += taken(q, here) * taken(q, there)
overlap_table[qi * distance_count + di] = shared * step * step
## Where `q` falls on the peak grid: its cell and the share of the way.
func peak_place(q: f64) -> (usize, f64):
let place = math.clamp(math.log(math.max(q, peak_low) / peak_low) / math.log(peak_high / peak_low), 0.0, 1.0) * (f64)(peak_count - 1)
let below = (usize)math.min(math.floor(place), (f64)(peak_count - 2))
return (below, place - (f64)below)
## A unit cloud of peak `q`: the density it takes from a large area, per cloud
## per unit area (∫(1 − 10^(−q·c)) dA / ln 10).
pub func light_taken(q: f64) -> f64:
tables_once.run(build_tables)
let (below, t) = peak_place(q)
return taken_table[below] * (1.0 - t) + taken_table[below + 1] * t
## Two unit clouds of peak `q` `d` apart: ∫(1 − 10^(−q·c(x)))(1 − 10^(−q·c(x − d))) dx.
pub func overlap(q: f64, d: f64) -> f64:
tables_once.run(build_tables)
if d >= 2.0:
return 0.0
let (below, t) = peak_place(q)
let place = d / 2.0 * (f64)(distance_count - 1)
let near = (usize)math.min(math.floor(place), (f64)(distance_count - 2))
let u = place - (f64)near
let low = overlap_table[below * distance_count + near] * (1.0 - u) + overlap_table[below * distance_count + near + 1] * u
let high = overlap_table[(below + 1) * distance_count + near] * (1.0 - u) + overlap_table[(below + 1) * distance_count + near + 1] * u
return low * (1.0 - t) + high * t
## The size factors (uniform over 0.6–1.4), at five midpoints, and the mean of
## the square of the size.
let size_points: usize = 5
func size_at(index: usize) -> f64:
return size_low + (size_high - size_low) * ((f64)index + 0.5) / (f64)size_points
pub let size_square_mean: f64 = 1.0533333333333332
## The developed field of populations whose developed grains number `lambda`
## per µm² (a share of their density), of `geometry`, their clouds `strength`
## times their peak: its density over a large area (−log10 of the mean
## transmittance), and the variance of its density through the 48 µm aperture.
func field_density(geometry: const Geometry[], lambda: const f64[3], strength: f64) -> f64:
var d = 0.0
for p in 0..<populations:
let g = geometry[p]
d += lambda[p] * g.radius * g.radius * size_square_mean * light_taken(g.peak * strength)
return d
func field_variance(geometry: const Geometry[], lambda: const f64[3], strength: f64) -> f64:
var reach = 0.0
for p in 0..<populations:
if lambda[p] > 0.0:
reach = math.max(reach, 2.0 * geometry[p].radius * size_high)
if reach <= 0.0:
return 0.0
let steps = 64
var sum = 0.0
for at in 0..<steps:
let r = reach * ((f64)at + 0.5) / (f64)steps
var exponent = 0.0
for p in 0..<populations:
let g = geometry[p]
if lambda[p] <= 0.0:
continue
var mean = 0.0
for s in 0..<size_points:
let radius = g.radius * size_at(s)
mean += radius * radius * overlap(g.peak * strength, r / radius) / (f64)size_points
exponent += lambda[p] * mean
sum += (math.exp(exponent) - 1.0) * 2.0 * math.pi * r * (reach / (f64)steps)
# The transmittance's relative variance through the aperture, as density's.
return sum / aperture_area / (ln10 * ln10)
# mark: Fitting ===============================================================
## A population's share developed at `log_h`: a logistic (film_model.lucb's
## `developed_share`); on a slide, the share that forms dye, what the first
## developer left.
func share(populations_at: const f64[18], layer: usize, p: usize, log_h: f64, reversal: bool) -> f64:
let centre = populations_at[layer * 6 + p * 2]
let width = math.max(populations_at[layer * 6 + p * 2 + 1], 1.0e-6)
let developed = 1.0 / (1.0 + math.exp(-(log_h - centre) / width))
return 1.0 - developed if reversal else developed
func log_h_at(at: usize) -> f64:
return log_low + (log_high - log_low) * (f64)at / (f64)(table_samples - 1)
## Fit grains to `layers` layers (three, one for black and white): `curves`
## their dye over its floor and `granularity` the rms through 48 µm wanted,
## each `table_samples` × 3 over effective log H; `reversal` for a slide (dye
## where the first developer left halide); `peak` the clouds' peak density.
## The caller releases it.
pub func fit(layers: usize, curves: const f64[], granularity: const f64[], reversal: bool, peak: f64) -> Fit!:
var out = Fit()
out.table = try new f64[table_samples * 3 * 4] --- in memory.heap
for at in 0..<out.table.length:
out.table[at] = 0.0
out.granularity = try new f64[table_samples * 3] --- in memory.heap
for at in 0..<out.granularity.length:
out.granularity[at] = 0.0
for layer in 0..<layers:
let populations_at = placed(curves, layer, reversal)
try fit_layer(&out, layer, curves, granularity, populations_at, reversal, peak)
return out
## The populations of layer `layer` placed on its developed curve (as
## film_model.lucb's `fit_populations` places them on the published one): the
## fast, mid and slow ones half developed where the share the first developer
## or the dye has reached is a sixth, a half and five sixths, each as wide as
## its third of the curve (its ends at 2 % and 98 %) over 4.4. Laid out as film_model.lucb's (centre,
## width per population, six per layer).
func placed(curves: const f64[], layer: usize, reversal: bool) -> f64[18]:
var top = 0.0
for at in 0..<table_samples:
top = math.max(top, curves[at * 3 + layer])
var crossings: f64[7]
let levels: f64[7] = [0.02, 1.0 / 6.0, 1.0 / 3.0, 0.5, 2.0 / 3.0, 5.0 / 6.0, 0.98]
for c in 0..<(usize)7:
let level = levels[c]
var found = log_high if not reversal else log_low
for at in 1..<table_samples:
let a = developed_at(curves, layer, at - 1, top, reversal)
let b = developed_at(curves, layer, at, top, reversal)
if a < level and b >= level:
found = log_h_at(at - 1) + (level - a) / math.max(b - a, 1.0e-12) * (log_h_at(at) - log_h_at(at - 1))
break
crossings[c] = found
var out: f64[18]
for k in 0..<(usize)18:
out[k] = 0.0
for p in 0..<populations:
out[layer * 6 + p * 2] = crossings[2 * p + 1]
out[layer * 6 + p * 2 + 1] = math.max((crossings[2 * p + 2] - crossings[2 * p]) / 4.4, 0.03)
return out
## How far the development has come at sample `at`: the dye's share of its most
## on a negative, the first developer's (what the dye lost) on a slide.
func developed_at(curves: const f64[], layer: usize, at: usize, top: f64, reversal: bool) -> f64:
let reached = curves[at * 3 + layer] / math.max(top, 1.0e-12)
return 1.0 - reached if reversal else reached
## How many more grains each population has than its share of the curve's
## most needs at clouds of their peak density (the strengths solved vary).
let headroom: f64 = 1.6
## A cloud is at most this dense at its middle (a dye cloud is never opaque;
## a developed silver grain nearly is): where a density needs more, the
## populations get more grains instead. Its strength stays above 0.01.
let densest: f64 = 2.5
## Every eighth sample is solved; between them, interpolated.
let solved_every: usize = 8
func fit_layer(out: Fit*, layer: usize, curves: const f64[], granularity: const f64[], populations_at: const f64[18], reversal: bool, peak: f64) -> !:
# Each population's most density, by least squares (none negative) over
# the curve: dye over its floor ≈ Σ M·share.
var normal: f64[9]
var right: f64[3]
for k in 0..<(usize)9:
normal[k] = 0.0
for k in 0..<(usize)3:
right[k] = 0.0
var at: usize = 0
while at < table_samples:
var basis: f64[3]
for p in 0..<populations:
basis[p] = share(populations_at, layer, p, log_h_at(at), reversal)
for a in 0..<(usize)3:
for b in 0..<(usize)3:
normal[a * 3 + b] += basis[a] * basis[b]
right[a] += basis[a] * curves[at * 3 + layer]
at += 4
let most = nonnegative3(normal, right)
# The fast population's radius from the granularity where the curve is
# halfway up (first order: σ²·S = Σ D·ρ²·F(q)·E[s⁴]/E[s²]); the others'
# in proportion. Then no smaller than each population needs where it makes
# most of the dye: there, at the clouds' densest, first order (which never
# overstates Campbell's variance) still has to reach the granularity (in a
# toe the fast grains alone make it: faster emulsions, larger grains).
var top = 0.0
for at2 in 0..<table_samples:
top = math.max(top, curves[at2 * 3 + layer])
var middle: usize = 0
for at2 in 0..<table_samples:
if curves[at2 * 3 + layer] >= 0.5 * top:
middle = at2
if not reversal:
break
let wanted = granularity[middle * 3 + layer] * granularity[middle * 3 + layer] * aperture_area
var weighed = 0.0
for p in 0..<populations:
weighed += most[p] * share(populations_at, layer, p, log_h_at(middle), reversal) * radius_ratio[p] * radius_ratio[p] * light_taken(peak)
let fourth_over_second = 1.3251199999999998 / size_square_mean
let fast = math.sqrt(math.max(wanted, 1.0e-12) / math.max(weighed * fourth_over_second, 1.0e-12))
var radii: f64[3]
for p in 0..<populations:
radii[p] = fast * radius_ratio[p]
let densest_taken = light_taken(densest)
var at3: usize = 0
while at3 < table_samples:
let dye = curves[at3 * 3 + layer]
if dye >= 0.1 * top and dye <= 0.9 * top:
var dominant: usize = 0
var largest_dye = -1.0
for p in 0..<populations:
let part = most[p] * share(populations_at, layer, p, log_h_at(at3), reversal)
if part > largest_dye:
largest_dye = part
dominant = p
let variance = granularity[at3 * 3 + layer] * granularity[at3 * 3 + layer] * aperture_area
let needed = math.sqrt(variance / math.max(dye * densest_taken * fourth_over_second, 1.0e-12))
radii[dominant] = math.max(radii[dominant], needed)
at3 += 1
# At each exposure, how far the populations have developed and how strong
# their clouds are, so the field's density is the curve's and its rms the
# granularity's. The populations develop along their own logistics at a
# warped exposure, the same for the three (so no share ever turns back as
# exposure grows), found by bisection for the density; the clouds'
# strength by bisection outside it (at a given density, variance grows
# with the strength). Where even every grain developed cannot make the
# curve's density without grain coarser than the granularity (a slide's
# D-max is high and its grain fine), the populations get more grains and
# all is solved again.
var geometry: Geometry[3]
var solved_warp: f64[129]
var solved_strength: f64[129]
let count = table_samples // solved_every + 1
var more = headroom
for _ in 0..<10:
for p in 0..<populations:
let radius = math.clamp(radii[p], 0.2, 12.0)
let density = more * most[p] / (radius * radius * size_square_mean * light_taken(peak))
geometry[p] = Geometry(density = density, peak = peak, radius = radius)
out.geometry[layer * 3 + p] = geometry[p]
var short = false
for index in 0..<count:
let sample = index * solved_every if index * solved_every < table_samples else table_samples - 1
let target = curves[sample * 3 + layer]
let variance = granularity[sample * 3 + layer] * granularity[sample * 3 + layer]
var low = math.log(0.01)
var high = math.log(math.max(densest / peak, 0.06))
var strength = 1.0
var warp = log_low - 3.0 if not reversal else log_high + 3.0
var reached_last = true
if target > 1.0e-6:
for _ in 0..<40:
let middle_log = 0.5 * (low + high)
strength = math.exp(middle_log)
let (found, reached) = warp_for(geometry, populations_at, layer, reversal, strength, target)
warp = found
reached_last = reached
let lambda = developed_grains(geometry, populations_at, layer, reversal, warp)
if reached and field_variance(geometry, lambda, strength) > variance:
high = middle_log
else:
low = middle_log
let lambda = developed_grains(geometry, populations_at, layer, reversal, warp)
if not reached_last or field_variance(geometry, lambda, strength) > 1.3 * variance:
short = true
solved_warp[index] = warp
solved_strength[index] = strength
if not short:
break
more *= 1.6
# At every sample, the clouds' strength between the solved ones and the
# warp solved for the curve's density exactly (cheap without the
# variance). Where the curve rises with exposure the warp does, so more
# exposure develops more of the same grains; a curve that turns back (a
# slide's near its D-max, as interimage leaves it) is followed.
for sample in 0..<table_samples:
let place = (f64)sample / (f64)solved_every
let below = (usize)math.min(math.floor(place), (f64)(count - 2))
let t = place - (f64)below
let strength = solved_strength[below] * (1.0 - t) + solved_strength[below + 1] * t
let target = curves[sample * 3 + layer]
var warp = log_low - 3.0 if not reversal else log_high + 3.0
if target > 1.0e-6:
warp = warp_for(geometry, populations_at, layer, reversal, strength, target).0
let base = (sample * 3 + layer) * 4
for p in 0..<populations:
out.table[base + p] = share(populations_at, layer, p, warp, reversal)
out.table[base + 3] = strength
# The grains' own granularity, every few samples, between them interpolated.
var index: usize = 0
while index < count:
let sample = index * solved_every if index * solved_every < table_samples else table_samples - 1
let base = (sample * 3 + layer) * 4
var lambda: f64[3]
for p in 0..<populations:
lambda[p] = geometry[p].density * out.table[base + p]
out.granularity[sample * 3 + layer] = math.sqrt(field_variance(geometry, lambda, out.table[base + 3]))
index += 1
for sample in 0..<table_samples:
if sample % solved_every == 0 or sample == table_samples - 1:
continue
let below = sample - sample % solved_every
let above = below + solved_every if below + solved_every < table_samples else table_samples - 1
let t = (f64)(sample - below) / (f64)(above - below)
out.granularity[sample * 3 + layer] = out.granularity[below * 3 + layer] * (1.0 - t) + out.granularity[above * 3 + layer] * t
## The developed grains per µm² of each population with all developed as at
## log H `warp`.
func developed_grains(geometry: const Geometry[], populations_at: const f64[18], layer: usize, reversal: bool, warp: f64) -> f64[3]:
var lambda: f64[3]
for p in 0..<populations:
lambda[p] = geometry[p].density * share(populations_at, layer, p, warp, reversal)
return lambda
## The warped exposure at which the field of clouds of `strength` reaches
## density `target`, and whether it can (every grain developed may be too few).
func warp_for(geometry: const Geometry[], populations_at: const f64[18], layer: usize, reversal: bool, strength: f64, target: f64) -> (f64, bool):
# Density grows toward high log H on a negative, toward low on a slide.
var thin = log_low - 3.0 if not reversal else log_high + 3.0
var dense = log_high + 3.0 if not reversal else log_low - 3.0
if field_density(geometry, developed_grains(geometry, populations_at, layer, reversal, dense), strength) < target:
return (dense, false)
for _ in 0..<50:
let middle = 0.5 * (thin + dense)
if field_density(geometry, developed_grains(geometry, populations_at, layer, reversal, middle), strength) >= target:
dense = middle
else:
thin = middle
return (dense, true)
## The least-squares solution of 3 × 3 normal equations with none negative:
## of every subset of the three left free (the rest zero), the best.
pub func nonnegative3(normal: f64[9], right: f64[3]) -> f64[3]:
var best: f64[3] = [0.0, 0.0, 0.0]
var best_cost = 1.0e300
for mask in 1..<8:
var m: f64[9]
var v: f64[3]
for a in 0..<(usize)3:
let free_a = ((mask >> (i64)a) & 1) == 1
v[a] = right[a] if free_a else 0.0
for b in 0..<(usize)3:
let free_b = ((mask >> (i64)b) & 1) == 1
m[a * 3 + b] = normal[a * 3 + b] if free_a and free_b else (1.0 if a == b else 0.0)
let solved = film_model.invert3(m) else continue
var x: f64[3]
var fine = true
for a in 0..<(usize)3:
x[a] = solved[a * 3] * v[0] + solved[a * 3 + 1] * v[1] + solved[a * 3 + 2] * v[2]
fine = fine and x[a] >= 0.0
if not fine:
continue
var cost = 0.0
for a in 0..<(usize)3:
for b in 0..<(usize)3:
cost += x[a] * normal[a * 3 + b] * x[b]
cost -= 2.0 * x[a] * right[a]
if cost < best_cost:
best_cost = cost
best = x
return best
# mark: Sampling ==============================================================
## A developed grain's dye cloud near a traced pixel: its centre (µm), its
## radius squared, its peak density (its population's times the strength
## there), its layer.
pub struct Cloud:
pub var x: f64
pub var y: f64
pub var radius2: f64
pub var peak: f64
pub var layer: usize
## The most clouds a traced pixel gathers: beyond, it is drawn instead.
pub let cloud_most: usize = 128
## The cells a bin's grains are made in: no smaller than the grains' reach
## (`cell_radii` radii), and wide enough to hold about `cell_grains` of them,
## so finding a sparse bin's grains costs what is there.
let cell_grains: f64 = 8.0
pub func bin_cell(g: Geometry, b: u32) -> f64:
let width = bin_high(b) - bin_low(b)
return math.max(cell_radii * g.radius, math.sqrt(cell_grains / math.max(g.density * width, 1.0e-9)))
## The developed grains whose clouds reach the rectangle `left`..`right`,
## `top`..`bottom` (µm), of the `traced` layers (of `layers`), from grains of
## `geometry`: only the rank bins under `bounds` (each population's most share
## developed around it) are made, each in cells of its own size, each bin its
## own Poisson stream. Each grain asks `lookup` (with `context`) what the film
## says at its own centre for its layer: each population's share developed
## there (as far as the development has come) and the clouds' strength; it
## develops where its rank is under its share. Into `out`; how many, or more
## than `cloud_most` when there are too many.
pub func gather(geometry: const Geometry[], layers: usize, left: f64, top: f64, right: f64, bottom: f64, traced: bool[3], bounds: f64[9],
lookup: func(void*, usize, f64, f64) -> f64[4], context: void*, out: Cloud[]) -> usize:
var count: usize = 0
for layer in 0..<layers:
if not traced[layer]:
continue
for p in 0..<populations:
let g = geometry[layer * 3 + p]
if g.density <= 0.0:
continue
let largest = g.radius * size_high
for b in 0..<rank_bins:
let low = bin_low(b)
if low >= bounds[layer * 3 + p]:
break
let width = bin_high(b) - low
let cell = bin_cell(g, b)
let mean = g.density * width * cell * cell
let stream = (u32)(layer * 3 + p + 1) *% rank_bins +% b
# Four cells in, so no cell index is negative.
let first_x = (i64)math.floor((left - largest) / cell) + 4
let last_x = (i64)math.floor((right + largest) / cell) + 4
let first_y = (i64)math.floor((top - largest) / cell) + 4
let last_y = (i64)math.floor((bottom + largest) / cell) + 4
for ky in first_y..=last_y:
for kx in first_x..=last_x:
let seed = cell_seed((u32)kx, (u32)ky, stream)
let grains = grain_count(seed, mean)
for j in 0..<grains:
let (px, py) = grain_place(seed, j)
let gx = ((f64)kx + px - 4.0) * cell
let gy = ((f64)ky + py - 4.0) * cell
# Out of reach of the rectangle?
let dx = math.max(math.max(left - gx, gx - right), 0.0)
let dy = math.max(math.max(top - gy, gy - bottom), 0.0)
if dx * dx + dy * dy >= largest * largest:
continue
let (size, fraction_of) = grain_size_rank(seed, j)
let radius = g.radius * size
if dx * dx + dy * dy >= radius * radius:
continue
let place = lookup(context, layer, gx, gy)
if low + width * fraction_of >= place[p]:
continue
if count < cloud_most:
out[count] = Cloud(x = gx, y = gy, radius2 = radius * radius, peak = g.peak * place[3], layer = layer)
count += 1
return count
## The dye of `count` gathered `clouds` at (`x`, `y`) µm, per layer.
pub func dye_at(clouds: const Cloud[], count: usize, x: f64, y: f64) -> f64[3]:
var out: f64[3] = [0.0, 0.0, 0.0]
for at in 0..<count:
let c = clouds[at]
let far = ((x - c.x) * (x - c.x) + (y - c.y) * (y - c.y)) / c.radius2
if far < 1.0:
out[c.layer] += c.peak * (1.0 - far) * (1.0 - far)
return out
## How a pixel `u` cloud radii wide averages a field of clouds, against
## Selwyn's large-aperture law (variance · area constant): its variance, and
## its covariance with the pixel beside it and the one diagonal to it, each
## as a share of Selwyn's variance for its area. From the clouds' own
## autocorrelation over two pixels' areas; tabulated over u (0.25–20, log).
pub let covariance_count: usize = 32
var covariance_table: f64[96]
var covariance_once: sync.Once
pub func covariance_width(index: usize) -> f64:
return 0.25 * math.pow(80.0, (f64)index / (f64)(covariance_count - 1))
func build_covariance():
# The clouds' autocorrelation (the thinnest clouds' overlap: linear) on a grid.
let grid = 64
let step = 4.0 / (f64)grid
var whole = 0.0
for yi in 0..<grid:
for xi in 0..<grid:
let x = -2.0 + ((f64)xi + 0.5) * step
let y = -2.0 + ((f64)yi + 0.5) * step
whole += overlap(peak_low, math.sqrt(x * x + y * y)) * step * step
for index in 0..<covariance_count:
let u = covariance_width(index)
let offsets: f64[6] = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0]
for which in 0..<(usize)3:
var sum = 0.0
for yi in 0..<grid:
for xi in 0..<grid:
let x = -2.0 + ((f64)xi + 0.5) * step
let y = -2.0 + ((f64)yi + 0.5) * step
# Two pixels' overlap at this lag: the box autocorrelation.
let wx = math.max(u - math.abs(x + offsets[which * 2] * u), 0.0)
let wy = math.max(u - math.abs(y + offsets[which * 2 + 1] * u), 0.0)
sum += overlap(peak_low, math.sqrt(x * x + y * y)) * wx * wy * step * step
covariance_table[index * 3 + which] = sum / (u * u * whole)
## `pixel_covariance` at a pixel `u` cloud radii wide: variance, side, diagonal.
pub func pixel_covariance(u: f64) -> f64[3]:
covariance_once.run(build_covariance)
let place = math.clamp(math.log(math.max(u, 0.25) / 0.25) / math.log(80.0), 0.0, 1.0) * (f64)(covariance_count - 1)
let below = (usize)math.min(math.floor(place), (f64)(covariance_count - 2))
let t = place - (f64)below
var out: f64[3]
for k in 0..<(usize)3:
out[k] = covariance_table[below * 3 + k] * (1.0 - t) + covariance_table[(below + 1) * 3 + k] * t
return out
## A standard normal deviate from a hash of (`x`, `y`, `tag`).
func normal_at(x: u32, y: u32, tag: u32) -> f64:
let first = math.max(fraction(mixed(x *% 0x27d4eb2d +% tag, y ^ 0x165667b1)), 1.0e-7)
let second = fraction(mixed(y *% 0x9e3779b1 +% tag, x ^ 0x85ebca77))
return math.sqrt(-2.0 * math.log(first)) * math.cos(2.0 * math.pi * second)
## Pixel (`px`, `py`)'s deviate for layer `layer` with variance 1 and, with its
## neighbours', the correlations `shares` (variance, side, diagonal, as
## `pixel_covariance` gives them, normalized by the variance): its own, its
## edges' and its corners'.
pub func pixel_deviate(px: u32, py: u32, layer: u32, shares: f64[3]) -> f64:
let side = math.max(shares[1] / math.max(shares[0], 1.0e-12), 0.0)
let diagonal = math.max(shares[2] / math.max(shares[0], 1.0e-12), 0.0)
let corner = math.sqrt(math.min(diagonal, 0.0625))
let edge = math.sqrt(math.clamp(side - 2.0 * corner * corner, 0.0, 0.25))
let own = math.sqrt(math.max(1.0 - 4.0 * edge * edge - 4.0 * corner * corner, 0.0))
let tag = layer *% 8
# Coordinates one in, so the pixels at the image's edge have edges too.
let x = px +% 1
let y = py +% 1
var out = own * normal_at(x, y, tag)
out += edge * (normal_at(x, y, tag +% 1) + normal_at(x -% 1, y, tag +% 1) + normal_at(x, y, tag +% 2) + normal_at(x, y -% 1, tag +% 2))
out += corner * (normal_at(x, y, tag +% 3) + normal_at(x -% 1, y, tag +% 3) + normal_at(x, y -% 1, tag +% 3) + normal_at(x -% 1, y -% 1, tag +% 3))
return out
## Where pixel (`px`, `py`)'s sample `k` falls in it (0..1 each way): a
## low-discrepancy sequence (R2) turned by an offset of the pixel's own, so
## its samples spread evenly over it and neighbours' do not line up.
pub func sample_place(px: u32, py: u32, k: u32) -> (f64, f64):
let rx = fraction(mixed(px, py ^ 0x51ed27))
let ry = fraction(mixed(py +% 0x2545f491, px))
let ax = rx + 0.7548776662466927 * (f64)k
let ay = ry + 0.5698402909980532 * (f64)k
return (ax - math.floor(ax), ay - math.floor(ay))
# mark: Tests =================================================================
## A place developed evenly (`context` holds what it says).
func even_place(context: void*, layer: usize, x: f64, y: f64) -> f64[4]:
_ = layer
_ = x
_ = y
return *(f64[4]*)context
test "an even field's sampled transmittance is Campbell's density":
var geometry: Geometry[9]
for at in 0..<(usize)9:
geometry[at] = Geometry(density = 0.0, peak = 0.5, radius = 1.0)
geometry[0] = Geometry(density = 1.2, peak = 0.5, radius = 1.5)
geometry[1] = Geometry(density = 3.0, peak = 0.5, radius = 0.9)
var place: f64[4] = [0.7, 0.4, 0.0, 1.3]
var gathered: Cloud[128]
var sum = 0.0
let n = 300
for yi in 0..<n:
for xi in 0..<n:
let x = ((f64)xi + fraction(mixed((u32)xi, (u32)yi))) * 0.37
let y = ((f64)yi + fraction(mixed((u32)yi +% 77, (u32)xi))) * 0.37
let count = gather(geometry, 1, x, y, x, y, [true, false, false], [1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], even_place,
(void*)&place, gathered)
let d = dye_at(gathered, count, x, y)
sum += math.pow(10.0, -d[0])
let measured = -math.log10(sum / (f64)(n * n))
let lambda: f64[3] = [1.2 * 0.7, 3.0 * 0.4, 0.0]
let expected = field_density(geometry[0..<3], lambda, 1.3)
assert(math.abs(measured / expected - 1.0) < 0.03)
test "the fit makes a curve's density and granularity":
# A curve rising over three stops to 2.0, its granularity 0.01·√D: the
# fitted field reads both (Campbell), the shares never turn back.
var curves: f64[3072]
var grain: f64[3072]
for at in 0..<table_samples:
let d = 2.0 / (1.0 + math.exp(-(log_h_at(at) + 1.0) / 0.35))
for k in 0..<(usize)3:
curves[at * 3 + k] = d
grain[at * 3 + k] = 0.01 * math.sqrt(d)
var fitted = fit(1, curves, grain, false, color_peak) catch failure:
assert(false)
return
defer fitted.release()
let checked: usize[4] = [450, 512, 600, 700]
for at in checked:
let base = (at * 3) * 4
var lambda: f64[3]
for p in 0..<populations:
lambda[p] = fitted.geometry[p].density * fitted.table[base + p]
let strength = fitted.table[base + 3]
let density = field_density(fitted.geometry[0..<3], lambda, strength)
let rms = math.sqrt(field_variance(fitted.geometry[0..<3], lambda, strength))
assert(math.abs(density / curves[at * 3] - 1.0) < 0.03)
assert(math.abs(rms / grain[at * 3] - 1.0) < 0.05)
for at in 1..<table_samples:
for p in 0..<populations:
assert(fitted.table[(at * 3) * 4 + p] >= fitted.table[((at - 1) * 3) * 4 + p])