233 lines
6.3 KiB
Rust
233 lines
6.3 KiB
Rust
use std::{hash::Hash, hash::Hasher, io};
|
|
|
|
use anyhow::Result;
|
|
use clap::{app_from_crate, crate_authors, crate_description, crate_name, crate_version, Arg};
|
|
use io::Write;
|
|
use lasy::{
|
|
blank_segment_points, euler_graph_to_euler_circuit, interpolate_euler_circuit,
|
|
point_graph_to_euler_graph, points_to_segments, segments_to_point_graph, Blanked,
|
|
InterpolationConfig, IsBlank, Lerp, Position, Weight,
|
|
};
|
|
|
|
type InputArrayEntry = (f32, f32, u32);
|
|
|
|
#[derive(Clone, Copy)]
|
|
struct Point {
|
|
x: f32,
|
|
y: f32,
|
|
color: u32,
|
|
weight: u32,
|
|
}
|
|
|
|
impl Hash for Point {
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
#[derive(Hash, Eq, PartialEq)]
|
|
struct HashPoint {
|
|
x: u32,
|
|
y: u32,
|
|
color: u32,
|
|
}
|
|
|
|
impl From<Point> for HashPoint {
|
|
fn from(p: Point) -> Self {
|
|
HashPoint {
|
|
x: (p.x * i16::MAX as f32) as u32,
|
|
y: (p.y * i16::MAX as f32) as u32,
|
|
color: p.color,
|
|
}
|
|
}
|
|
}
|
|
|
|
HashPoint::from(*self).hash(hasher);
|
|
}
|
|
}
|
|
|
|
impl Position for Point {
|
|
fn position(&self) -> [f32; 2] {
|
|
[self.x, self.y]
|
|
}
|
|
}
|
|
|
|
impl IsBlank for Point {
|
|
fn is_blank(&self) -> bool {
|
|
self.color == 0
|
|
}
|
|
}
|
|
|
|
impl Blanked for Point {
|
|
fn blanked(&self) -> Self {
|
|
Self {
|
|
color: 0,
|
|
..*self
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Weight for Point {
|
|
fn weight(&self) -> u32 {
|
|
self.weight
|
|
}
|
|
}
|
|
|
|
impl Lerp for Point {
|
|
type Scalar = f32;
|
|
fn lerp(&self, other: &Self, amt: f32) -> Self {
|
|
let v = [self.x, self.y].lerp(&[other.x, other.y], amt);
|
|
|
|
let (b1, g1, r1) = (
|
|
((self.color) & 0xff) as f32,
|
|
((self.color >> 8) & 0xff) as f32,
|
|
((self.color >> 16) & 0xff) as f32,
|
|
);
|
|
|
|
let (b2, g2, r2) = (
|
|
((other.color) & 0xff) as f32,
|
|
((other.color >> 8) & 0xff) as f32,
|
|
((other.color >> 16) & 0xff) as f32,
|
|
);
|
|
|
|
let [r3, g3, b3] = [r1, g1, b1].lerp(&[r2, g2, b2], amt);
|
|
|
|
let color: u32 = ((r3 as u32) << 16) | ((g3 as u32) << 8) | b3 as u32;
|
|
|
|
Point {
|
|
x: v[0],
|
|
y: v[1],
|
|
color,
|
|
weight: self.weight,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn optimize_line(
|
|
default_weight: u32,
|
|
line: &str,
|
|
config: &InterpolationConfig,
|
|
) -> Result<Vec<InputArrayEntry>> {
|
|
let frame: Vec<InputArrayEntry> = serde_json::from_str(line)?;
|
|
|
|
let input_points: Vec<Point> = frame
|
|
.into_iter()
|
|
.map(|(x, y, color)| Point {
|
|
x,
|
|
y,
|
|
color,
|
|
weight: default_weight,
|
|
})
|
|
.collect();
|
|
|
|
let segs = points_to_segments(&input_points);
|
|
let pg = segments_to_point_graph(&input_points, segs);
|
|
let eg = point_graph_to_euler_graph(&pg);
|
|
let ec = euler_graph_to_euler_circuit(&input_points, &eg);
|
|
let output_points: Vec<Point> =
|
|
interpolate_euler_circuit(&input_points, &ec, &eg, input_points.len() as u32, config);
|
|
|
|
let output_frame: Vec<_> = output_points.iter().map(|p| (p.x, p.y, p.color)).collect();
|
|
|
|
Ok(output_frame)
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let matches = app_from_crate!()
|
|
.args(&[
|
|
Arg::with_name("default-weight")
|
|
.short("w")
|
|
.takes_value(true)
|
|
.multiple(false)
|
|
.default_value("0"),
|
|
Arg::with_name("distance-per-point")
|
|
.short("d")
|
|
.takes_value(true)
|
|
.multiple(false),
|
|
Arg::with_name("blank-delay-points")
|
|
.short("b")
|
|
.takes_value(true)
|
|
.multiple(false),
|
|
Arg::with_name("radians-per-point")
|
|
.short("r")
|
|
.takes_value(true)
|
|
.multiple(false),
|
|
])
|
|
.get_matches();
|
|
|
|
let default_weight = matches.value_of("default-weight").unwrap().parse().unwrap();
|
|
let mut config = InterpolationConfig::default();
|
|
|
|
if let Some(distance_per_point) = matches.value_of("distance-per-point") {
|
|
config.distance_per_point = distance_per_point
|
|
.parse()
|
|
.expect("distance-per-point must be a float")
|
|
}
|
|
|
|
if let Some(blank_delay_points) = matches.value_of("blank-delay-points") {
|
|
config.blank_delay_points = blank_delay_points
|
|
.parse()
|
|
.expect("blank-delay-points must be an unsigned integer")
|
|
}
|
|
|
|
if let Some(radians_per_point) = matches.value_of("radians-per-point") {
|
|
config.radians_per_point = radians_per_point
|
|
.parse()
|
|
.expect("radians-per-point must be a float")
|
|
}
|
|
|
|
let mut last_frames = vec![];
|
|
|
|
loop {
|
|
let mut line = String::new();
|
|
io::stdin().read_line(&mut line)?;
|
|
|
|
last_frames.push(optimize_line(default_weight, &line, &config)?);
|
|
|
|
if last_frames.len() == 2 {
|
|
let a: Vec<Point> = last_frames
|
|
.first()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|(x, y, color)| Point {
|
|
x: *x,
|
|
y: *y,
|
|
color: *color,
|
|
weight: default_weight,
|
|
})
|
|
.collect();
|
|
|
|
let b: Vec<Point> = last_frames
|
|
.last()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|(x, y, color)| Point {
|
|
x: *x,
|
|
y: *y,
|
|
color: *color,
|
|
weight: default_weight,
|
|
})
|
|
.collect();
|
|
|
|
let c: Vec<InputArrayEntry> = a
|
|
.into_iter()
|
|
.zip(b.into_iter())
|
|
.map(|(a, b)| blank_segment_points(a, b, config.blank_delay_points))
|
|
.flatten()
|
|
.map(|p| (p.x, p.y, p.color))
|
|
.collect();
|
|
|
|
serde_json::to_writer(io::stdout(), &c)?;
|
|
io::stdout().write(b"\n")?;
|
|
io::stdout().flush()?;
|
|
|
|
last_frames.remove(0);
|
|
}
|
|
|
|
serde_json::to_writer(io::stdout(), &last_frames.last())?;
|
|
io::stdout().write(b"\n")?;
|
|
io::stdout().flush()?;
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn optimize_line_test() {
|
|
optimize_line(0, "[[100.0, 100.0, 65280], [100.0, 500.0, 65280], [500.0, 500.0, 65280], [500.0, 100.0, 65280], [100.0, 100.0, 65280]]", &InterpolationConfig::default()).unwrap();
|
|
}
|