first draft for transformers

This commit is contained in:
Marc Planard 2023-06-04 21:42:55 +02:00
parent d9450e903f
commit de26c1c793
2 changed files with 37 additions and 17 deletions

View File

@ -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<Frame> {
let line = rs.get(&format!("/pl/{}/0", config.laser_id))?;
let line: Vec<helios_dac::Point> = line.iter().map(tuple_to_point).collect();
let line: Vec<Point> = 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<helios_dac::Point> = 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()
}

29
src/modifier.rs Normal file
View File

@ -0,0 +1,29 @@
use crate::point::Point;
pub trait Transformers {
fn apply(&self, point_list: &Vec<Point>) -> Vec<Point>;
}
#[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<Point>) -> Vec<Point> {
point_list.iter()
.map(| pt | {
Point { x: pt.x + self.x,
y: pt.y + self.y,
..*pt
}
}).collect()
}
}