| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::whale::{BITS, Grid}; |
| 4 | |
| 5 | use super::math::smooth; |
| 6 | |
| 7 | pub const FRAMES: usize = 24; |
| 8 | const FRAME_MS: u64 = 60; |
| 9 | pub const DURATION: Duration = Duration::from_millis(FRAME_MS * FRAMES as u64); |
| 10 | pub const RIM_LEVELS: usize = 5; |
| 11 | |
| 12 | pub fn frame_index(elapsed: Duration) -> usize { |
| 13 | (elapsed.as_millis() / u128::from(FRAME_MS)).min(FRAMES as u128) as usize |
| 14 | } |
| 15 | |
| 16 | pub struct Arrival { |
| 17 | pub grid: Grid, |
| 18 | pub rim: Vec<u8>, |
| 19 | } |
| 20 | |
| 21 | pub fn frame(poster: &Grid, index: usize) -> Option<Arrival> { |
| 22 | let cols = usize::from(poster.cols); |
| 23 | let rows = usize::from(poster.rows); |
| 24 | if !(1..=160).contains(&cols) || !(1..=80).contains(&rows) || poster.cells.len() != cols * rows |
| 25 | { |
| 26 | return None; |
| 27 | } |
| 28 | let mut grid = poster.clone(); |
| 29 | let mut rim = vec![0; grid.cells.len()]; |
| 30 | if index >= FRAMES { |
| 31 | return Some(Arrival { grid, rim }); |
| 32 | } |
| 33 | |
| 34 | let t = index as f64 / FRAMES as f64; |
| 35 | let approach = 1.0 - smooth(0.0, 0.84, t); |
| 36 | if cols >= 8 && rows >= 4 && approach > 0.0 { |
| 37 | let w = (2 * cols) as f64; |
| 38 | let h = (4 * rows) as f64; |
| 39 | let scale = 1.0 - 0.23 * approach; |
| 40 | let lift = (std::f64::consts::PI * t).sin(); |
| 41 | let (sin, cos) = (approach * (0.18 * lift - 0.15)).sin_cos(); |
| 42 | let cx = w * (0.5 - 0.068 * approach); |
| 43 | let cy = h * (0.5 + (0.072 - 0.14 * lift) * approach); |
| 44 | grid.cells.fill(0); |
| 45 | for y in 0..4 * rows { |
| 46 | let dy = (y as f64 + 0.5 - cy) / scale; |
| 47 | for x in 0..2 * cols { |
| 48 | let dx = (x as f64 + 0.5 - cx) / scale; |
| 49 | let src_x = (cos * dx + sin * dy + w * 0.5).floor(); |
| 50 | let src_y = (cos * dy - sin * dx + h * 0.5).floor(); |
| 51 | if !(0.0..w).contains(&src_x) || !(0.0..h).contains(&src_y) { |
| 52 | continue; |
| 53 | } |
| 54 | let sx = src_x as usize; |
| 55 | let sy = src_y as usize; |
| 56 | if poster.cells[(sy / 4) * cols + sx / 2] & BITS[sy % 4][sx % 2] != 0 { |
| 57 | grid.cells[(y / 4) * cols + x / 2] |= BITS[y % 4][x % 2]; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | let angle = -1.02 - 3.82 * smooth(0.02, 0.9, t); |
| 64 | let (light_y, light_x) = angle.sin_cos(); |
| 65 | let strength = smooth(0.0, 0.14, t) * (1.0 - smooth(0.68, 1.0, t)); |
| 66 | for (i, level) in rim.iter_mut().enumerate() { |
| 67 | let bits = grid.cells[i]; |
| 68 | if bits == 0 { |
| 69 | continue; |
| 70 | } |
| 71 | let x = i % cols; |
| 72 | let y = i / cols; |
| 73 | let edge = bits != u8::MAX |
| 74 | || x == 0 |
| 75 | || y == 0 |
| 76 | || x + 1 == cols |
| 77 | || y + 1 == rows |
| 78 | || grid.cells[i - 1] == 0 |
| 79 | || grid.cells[i + 1] == 0 |
| 80 | || grid.cells[i - cols] == 0 |
| 81 | || grid.cells[i + cols] == 0; |
| 82 | if edge { |
| 83 | let nx = (x as f64 + 0.5) / cols as f64 - 0.5; |
| 84 | let ny = (y as f64 + 0.5) / rows as f64 - 0.5; |
| 85 | let alignment = (nx * light_x + ny * light_y) / nx.hypot(ny).max(0.001); |
| 86 | let specular = smooth(0.48, 0.97, alignment) * strength; |
| 87 | *level = (specular * (RIM_LEVELS - 1) as f64).round() as u8; |
| 88 | } |
| 89 | } |
| 90 | Some(Arrival { grid, rim }) |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use super::*; |
| 96 | |
| 97 | #[test] |
| 98 | fn gliding_pose_and_rim_resolve_to_the_exact_canonical_poster() { |
| 99 | let poster = crate::whale::frame(crate::whale::WhaleState::Rest, 48, 24).unwrap(); |
| 100 | let opening = frame(&poster, 0).unwrap(); |
| 101 | let upper = frame(&poster, frame_index(Duration::from_millis(360))).unwrap(); |
| 102 | let flank = frame(&poster, frame_index(Duration::from_millis(720))).unwrap(); |
| 103 | let lower = frame(&poster, frame_index(Duration::from_millis(1080))).unwrap(); |
| 104 | assert_ne!(opening.grid, upper.grid); |
| 105 | assert_ne!(upper.grid, flank.grid); |
| 106 | let center = |f: &Arrival| { |
| 107 | let mut weighted = (0, 0, 0); |
| 108 | for (i, &energy) in f.rim.iter().enumerate().filter(|(_, e)| **e > 0) { |
| 109 | assert_ne!(f.grid.cells[i], 0); |
| 110 | let weight = usize::from(energy); |
| 111 | weighted.0 += (i % usize::from(f.grid.cols)) * weight; |
| 112 | weighted.1 += (i / usize::from(f.grid.cols)) * weight; |
| 113 | weighted.2 += weight; |
| 114 | } |
| 115 | (weighted.0 / weighted.2, weighted.1 / weighted.2) |
| 116 | }; |
| 117 | let (x0, y0) = center(&upper); |
| 118 | let (x1, y1) = center(&flank); |
| 119 | let (x2, y2) = center(&lower); |
| 120 | assert!(x1 < x0 && x1 < x2 && y0 < y1 && y1 < y2); |
| 121 | for elapsed in [DURATION, Duration::MAX] { |
| 122 | let settled = frame(&poster, frame_index(elapsed)).unwrap(); |
| 123 | assert_eq!(settled.grid, poster); |
| 124 | assert!(settled.rim.iter().all(|&energy| energy == 0)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |