#[derive(Clone, Copy, Debug)]
pub struct FillCost {
pub area_mm2: f64,
pub bytes: u64,
}
const PER_POINT: f64 = 72.0;
const PER_STROKE: f64 = 260.0;
pub fn cost(area_mm2: f64, spec: &crate::profile::FillSpec) -> FillCost {
let (points, strokes) = sample(spec).unwrap_or((40.0, 4.0));
let bytes = area_mm2 * (points * PER_POINT + strokes * PER_STROKE);
FillCost { area_mm2, bytes: bytes as u64 }
}
pub fn sample(spec: &crate::profile::FillSpec) -> Option<(f64, f64)> {
let (name, cs) = crate::fill::catalog_spec(spec).ok()?;
if name == "none" || name == "solid" {
return Some((0.0, 0.0));
}
let widest = penfill::catalog::layers(&name, &cs).ok()?.iter().map(|(_, s)| *s).fold(0.0_f64, f64::max);
let side = (widest * 12.0).clamp(20.0, 150.0);
let (x, y) = (37.3, 41.9);
let square = penfill::Region::from_ring(&[(x, y), (x + side, y), (x + side, y + side), (x, y + side)]);
let out = penfill::catalog::fill(&name, &square, &cs).ok()?;
let points: usize = out.strokes.iter().map(Vec::len).sum();
let area = side * side;
Some((points as f64 / area, out.strokes.len() as f64 / area))
}
#[cfg(test)]
mod tests {
use super::*;
const GB: u64 = 1 << 30;
fn cost(area: f64, pattern: &str, spacing: f64) -> FillCost {
super::cost(area, &crate::profile::FillSpec { generator: Some(pattern.into()), ..crate::profile::FillSpec::spacing(spacing) })
}
#[test]
fn the_fifty_km_strip_fits_a_page_budget() {
let park = cost(742_050.0, "dots", 0.45);
let water = cost(717_000.0, "waves", 0.75);
assert!(park.bytes + water.bytes < 2 * GB, "{} + {}", park.bytes, water.bytes);
assert!(park.bytes > 300_000_000, "{}", park.bytes);
}
#[test]
fn a_heavy_generator_on_the_strip_is_refused() {
assert!(cost(742_050.0, "marble", 0.5).bytes > 2 * GB);
}
#[test]
fn the_postcard_stays_well_under_a_page_budget() {
let park = cost(2_600.0, "dots", 0.45);
let bldg = cost(4_000.0, "serpentine", 0.4);
assert!(park.bytes + bldg.bytes < 100 * 1024 * 1024);
}
#[test]
fn bigger_areas_and_finer_spacing_cost_more() {
let coarse = cost(1000.0, "dots", 1.0);
let fine = cost(1000.0, "dots", 0.1);
let big = cost(10000.0, "dots", 1.0);
assert!(fine.bytes > coarse.bytes);
assert!(big.bytes > coarse.bytes);
}
}