lab.rs

2.3 kB · rust · 72 lines

1use crate::{checked, code_of, Fault, Pixels};2use mrlyrs::core::Colorizer;3use mrlyrs::math::bang::Code;4use mrlyrs::math::moire::{pairs, presets, render};5use mrlyrs::math::press;6use wasm_bindgen::prelude::*;78/// Lists the first members of a design in the sequence press, each as a decimal string.9#[wasm_bindgen]10pub fn press_members(11    code: &str,12    dimension: usize,13    base: usize,14    count: usize,15) -> Result<Vec<String>, Fault> {16    let code = checked(code, dimension, base)?;17    Ok(press::members(Code::from(code), dimension, base, count)?18        .iter()19        .map(|m| m.to_string())20        .collect())21}2223/// Counts the members of a design below the limit, as a decimal string.24#[wasm_bindgen]25pub fn press_count_below(26    code: &str,27    dimension: usize,28    base: usize,29    limit: &str,30) -> Result<String, Fault> {31    let code = checked(code, dimension, base)?;32    Ok(press::count_below(Code::from(code), dimension, base, code_of(limit)?)?.to_string())33}3435/// Names the moire presets.36#[wasm_bindgen]37pub fn moire_names() -> Vec<String> {38    presets::all(1).iter().map(|p| p.name.to_string()).collect()39}4041/// Samples a named moire preset up to the scale limit into a square of the size and renders it through the fire, heat or diverge ramp into pixels.42#[wasm_bindgen]43pub fn moire(44    name: &str,45    limit: usize,46    size: usize,47    ramp: &str,48    levels: usize,49    invert: bool,50) -> Result<Pixels, Fault> {51    let field = presets::named(name, limit)?.field(size)?;52    let colorizer = match ramp {53        "heat" => Colorizer::heat(),54        "diverge" => Colorizer::diverge(),55        _ => Colorizer::fire(),56    };57    let png = render(&field, &colorizer, levels, false, invert, 1)?;58    let (width, height, colors) = mrlyrs::core::unpng(&png)?;59    Ok(Pixels::of(width, height, colors))60}6162/// Returns the exact Pearson correlation of the flat carpet layers at two scales, area-weighted on their lcm grid, which is zero exactly when the two odd scales are coprime.63#[wasm_bindgen]64pub fn moire_correlation(m: usize, n: usize) -> f64 {65    pairs::correlation(m, n)66}6768/// Lists the odd scales a moire stack samples up to the limit.69#[wasm_bindgen]70pub fn odd_scales(limit: usize) -> Vec<u32> {71    (1..=limit.max(1)).step_by(2).map(|n| n as u32).collect()72}