From de26c1c79359dfd9bfaf74f71a6ef3ea6afd426a Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sun, 4 Jun 2023 21:42:55 +0200 Subject: [PATCH] first draft for transformers --- src/main.rs | 25 ++++++++----------------- src/modifier.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 src/modifier.rs diff --git a/src/main.rs b/src/main.rs index 3e81384..9a33a20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod redis_ctrl; mod conf; mod errors; mod point; +mod modifier; use helios_dac::{ self, @@ -20,8 +21,9 @@ use redis_ctrl::{RedisCtrl,Order}; use conf::Conf; use errors::{LJError,LJResult}; use point::Point; +use modifier::{Transformers,Translate}; -const CENTER : (u16,u16) = (2000, 2000); +const CENTER : (f32,f32) = (2000.0, 2000.0); pub fn main() { match run_all() { @@ -84,8 +86,9 @@ fn get_next_frame( ) -> LJResult { let line = rs.get(&format!("/pl/{}/0", config.laser_id))?; - let line: Vec = line.iter().map(tuple_to_point).collect(); - + let line: Vec = line.into_iter().map(| tpl | tpl.into()).collect(); + let line = Translate::new(CENTER.0, CENTER.1).apply(&line); + let mut line2 = vec![]; while line2.len() < 48 { for p in &line { @@ -94,19 +97,7 @@ fn get_next_frame( } println!("{:?}", line2); - + + let line2 : Vec = line2.into_iter().map(| p | p.into()).collect(); Ok(Frame::new(speed, line2)) } - -fn tuple_to_point(tpl: &(f32, f32, u32)) -> helios_dac::Point { - let mut point : Point = (*tpl).into(); - - point.x = (CENTER.0 + point.x as u16 * 2).into(); - point.y = (CENTER.1 + point.y as u16 * 2).into(); - - if point.x >= 4096.0 || point.y >= 4096.0 { - println!("WARN: coordinate out of range: {:?}", point); - } - - point.into() -} diff --git a/src/modifier.rs b/src/modifier.rs new file mode 100644 index 0000000..56395ed --- /dev/null +++ b/src/modifier.rs @@ -0,0 +1,29 @@ +use crate::point::Point; + +pub trait Transformers { + fn apply(&self, point_list: &Vec) -> Vec; +} + +#[derive(Debug,Clone,Copy)] +pub struct Translate { + x: f32, + y: f32 +} + +impl Translate { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +impl Transformers for Translate { + fn apply(&self, point_list: &Vec) -> Vec { + point_list.iter() + .map(| pt | { + Point { x: pt.x + self.x, + y: pt.y + self.y, + ..*pt + } + }).collect() + } +}