Something went wrong. Try again.
iterative image reconstruction using random cubic bézier strokes, accelerated on metal
metal graphics rust
Something went wrong. Try again.
12345678910111213141516171819202122232425262728use crate::metal::Candidate;use crate::rng::Rng;
pub fn sample_candidates(rng: &mut Rng, w: u32, h: u32, batch: u32) -> Vec<Candidate> { let max_x = (w.saturating_sub(1)) as f32; let max_y = (h.saturating_sub(1)) as f32; let mut sample_pt = || [ (rng.next_f32() * (max_x + 1.0)).clamp(0.0, max_x), (rng.next_f32() * (max_y + 1.0)).clamp(0.0, max_y), ];
(0..batch).map(|_| { let [p0, p1, p2, p3] = std::array::from_fn(|_| sample_pt()); let (bx, by, bw, bh) = bbox_from_points(p0, p1, p2, p3, w, h); Candidate { p0, p1, p2, p3, bx, by, bw, bh } }).collect() }
pub fn bbox_from_points( p0: [f32; 2], p1: [f32; 2], p2: [f32; 2], p3: [f32; 2], w: u32, h: u32 ) -> (u32, u32, u32, u32) { let min_x = p0[0].min(p1[0]).min(p2[0]).min(p3[0]); let max_x = p0[0].max(p1[0]).max(p2[0]).max(p3[0]); let min_y = p0[1].min(p1[1]).min(p2[1]).min(p3[1]); let max_y = p0[1].max(p1[1]).max(p2[1]).max(p3[1]);
let bx = (min_x.floor() as i64 - 1).clamp(0, (w.saturating_sub(1)) as i64) as u32; let by = (min_y.floor() as i64 - 1).clamp(0, (h.saturating_sub(1)) as i64) as u32; let ex = (max_x.ceil() as i64 + 1).clamp(0, (w.saturating_sub(1)) as i64) as u32; let ey = (max_y.ceil() as i64 + 1).clamp(0, (h.saturating_sub(1)) as i64) as u32;
let bw = (ex - bx + 1).max(1); let bh = (ey - by + 1).max(1); (bx, by, bw, bh) }