Cotton code
67 lines · 2.9 KB · RustRaw
//! Picks: which shots of a set to keep. "Best first, then only what looks//! different", at most [`CAP`] (DECISIONS.md question 3).//!//! - **Best:** sharpness against the set's sharpest shot, less a penalty for//!   being far darker or brighter than the set's well-lit shots (its upper//!   quartile: the scene sets the light), plus Vision's aesthetics and face quality when the phone gave//!   them.//! - **Then only what looks different:** a further keeper must be at least//!   [`APART`] from every keeper already chosen (core print), and never much//!   worse than the best ([`NEVER_BELOW`] of its quality).//! - **Sure or not:** when no shot was seen at [`Level::Full`] and the phone//!   gave no aesthetics, the core cannot tell sharp from blurred: the picks are//!   offered as a guess (`sure: false`, and the set says "Albums will look//!   closer on Wi-Fi") and nothing is pre-binned without his tap anyway.use super::look::{print_distance, Level, Look};pub const CAP: usize = 6;pub const APART: f32 = 0.30;pub const NEVER_BELOW: f32 = 0.6;#[derive(Debug, Clone, PartialEq)]pub struct Picks {    /// Indices into the members handed in, best first.    pub keep: Vec<usize>,    pub sure: bool,}fn quality(l: &Look, max_sharp: f32, mid: f32) -> f32 {    let sharp = l.sharp.map_or(0.5, |s| s / max_sharp.max(1e-6));    let light = 3.0 * ((l.brightness - mid).abs() - 0.10).max(0.0);    let taste = l.platform.aesthetics.map_or(0.0, |a| 0.25 * a);    let face = l.platform.face_quality.map_or(0.0, |q| 0.2 * q);    sharp - light + taste + face}/// Choose the keepers among a set's members.pub fn picks(members: &[&Look]) -> Picks {    if members.is_empty() {        return Picks { keep: Vec::new(), sure: false };    }    let max_sharp = members.iter().filter_map(|l| l.sharp).fold(0.0f32, f32::max);    // The light reference is the set's upper quartile, not its middle: when    // most shots of a moment came out dark, the one well-lit shot is the good    // one, not the odd one out.    let mut lum: Vec<f32> = members.iter().map(|l| l.brightness).collect();    lum.sort_by(f32::total_cmp);    let mid = lum[(lum.len() * 3 / 4).min(lum.len() - 1)];    let q: Vec<f32> = members.iter().map(|l| quality(l, max_sharp, mid)).collect();    let mut order: Vec<usize> = (0..members.len()).collect();    order.sort_by(|&a, &b| q[b].total_cmp(&q[a]).then(a.cmp(&b)));    let best = q[order[0]];    let mut keep: Vec<usize> = Vec::new();    for &i in &order {        if keep.len() >= CAP {            break;        }        if !keep.is_empty() && q[i] < best * NEVER_BELOW {            continue;        }        if keep.iter().all(|&k| print_distance(&members[k].print, &members[i].print) > APART) {            keep.push(i);        }    }    let sure = members.iter().any(|l| l.level == Level::Full) || members.iter().any(|l| l.platform.aesthetics.is_some());    Picks { keep, sure }}