From 9a5db444a01ad2a002b4d244a00957cb2d3850fc Mon Sep 17 00:00:00 2001 From: Marc Planard Date: Sat, 10 Jun 2023 18:47:04 +0200 Subject: [PATCH] cleanup and new grid --- src/conf.rs | 19 ++++++++-- src/main.rs | 9 +++-- src/transformer.rs | 2 ++ src/transformer/flip_horizontal.rs | 6 ---- src/transformer/flip_vertical.rs | 6 ---- src/transformer/grid.rs | 56 ++++++++++++++++++++++++++++++ src/transformer/rotate.rs | 6 ---- src/transformer/translate.rs | 12 ++----- 8 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 src/transformer/grid.rs diff --git a/src/conf.rs b/src/conf.rs index fe5e762..01827c3 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -36,7 +36,16 @@ pub enum TransformConf { #[serde(rename = "translate")] Translate(transformer::Translate), #[serde(rename = "replicate")] - Replicate(transformer::Replicate) + Replicate(transformer::Replicate), + #[serde(rename = "rotate")] + Rotate(transformer::Rotate), + #[serde(rename = "flip_horizontal")] + FlipH(transformer::FlipHorizontal), + #[serde(rename = "flip_vertical")] + FlipV(transformer::FlipVertical), + #[serde(rename = "grid")] + Grid(transformer::Grid) + } @@ -55,7 +64,11 @@ impl Conf { for t in &self.transformers { let t : Box = match t { TransformConf::Translate(t) => Box::new(*t), - TransformConf::Replicate(r) => Box::new(*r) + TransformConf::Replicate(r) => Box::new(*r), + TransformConf::Rotate(r) => Box::new(*r), + TransformConf::FlipH(r) => Box::new(*r), + TransformConf::FlipV(r) => Box::new(*r), + TransformConf::Grid(r) => Box::new(*r) }; v.push(t); } @@ -69,7 +82,7 @@ impl Conf { redis_url: "redis://127.0.0.1:6379/".to_string(), dac: DacFamily::Helios(HeliosConf { id: 0 }), transformers: vec![ - TransformConf::Translate(transformer::Translate::new(2000.0,2000.0)), + TransformConf::Translate(transformer::Translate { x: 2000.0, y: 2000.0 } ), TransformConf::Replicate(transformer::Replicate::Until(48)) ] }; diff --git a/src/main.rs b/src/main.rs index e37a2b8..95c0dfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,7 @@ fn run_all() -> LJResult<()> { let frame = get_next_frame(&config, &transformers, &mut rs, order == Order::Black)?; // For now, draw all the time - tracer.draw(frame, 40_000)?; + tracer.draw(frame, 2_000)?; } info!("Exiting, stoping device."); @@ -102,8 +102,11 @@ fn get_next_frame( rs: &mut RedisCtrl, _black: bool, ) -> LJResult> { - let line = rs.get(&format!("/pl/{}/0", config.laser_id))?; - let mut line: Vec = line.into_iter().map(|tpl| tpl.into()).collect(); + //let line = rs.get(&format!("/pl/{}/0", config.laser_id))?; + let line = vec![(300.0,200.0,0),(500.0,200.0,65280),(500.0,400.0,65280),(300.0,400.0,65280),(300.0,200.0,65280),(300.0,200.0,0),(200.0,100.0,0),(600.0,100.0,65280),(600.0,500.0,65280),(200.0,500.0,65280),(200.0,100.0,65280)]; + + let mut line: Vec = line.into_iter().map(|tpl| tpl.into()).collect(); + for transformer in transformers { line = transformer.apply(&line); } diff --git a/src/transformer.rs b/src/transformer.rs index c31b947..19d4445 100644 --- a/src/transformer.rs +++ b/src/transformer.rs @@ -4,6 +4,7 @@ mod replicate; mod rotate; mod flip_horizontal; mod flip_vertical; +mod grid; use crate::point::Point; @@ -13,6 +14,7 @@ pub use replicate::Replicate; pub use rotate::Rotate; pub use flip_horizontal::FlipHorizontal; pub use flip_vertical::FlipVertical; +pub use grid::Grid; pub trait Transformers { fn apply(&self, point_list: &[Point]) -> Vec; diff --git a/src/transformer/flip_horizontal.rs b/src/transformer/flip_horizontal.rs index 2157ddd..d156739 100644 --- a/src/transformer/flip_horizontal.rs +++ b/src/transformer/flip_horizontal.rs @@ -9,12 +9,6 @@ pub struct FlipHorizontal { x: f32, } -impl FlipHorizontal { - pub fn new(x: f32) -> Self { - Self {x} - } -} - impl Transformers for FlipHorizontal { fn apply(&self, point_list: &[Point]) -> Vec { point_list.iter() diff --git a/src/transformer/flip_vertical.rs b/src/transformer/flip_vertical.rs index b4f6d59..fc741c7 100644 --- a/src/transformer/flip_vertical.rs +++ b/src/transformer/flip_vertical.rs @@ -9,12 +9,6 @@ pub struct FlipVertical { y: f32, } -impl FlipVertical { - pub fn new(y: f32) -> Self { - Self {y} - } -} - impl Transformers for FlipVertical { fn apply(&self, point_list: &[Point]) -> Vec { point_list.iter() diff --git a/src/transformer/grid.rs b/src/transformer/grid.rs new file mode 100644 index 0000000..25258ff --- /dev/null +++ b/src/transformer/grid.rs @@ -0,0 +1,56 @@ +use crate::transformer::Transformers; +use crate::point::Point; +use serde::{Serialize,Deserialize}; + +/// Translate + +#[derive(Serialize,Deserialize,Debug,Clone,Copy)] +pub struct Grid { + width: f32, + height: f32 +} + +fn square_box(size: f32, color: u32) -> Vec<(f32, f32, u32)> { + vec![ + (-size, -size, 0), + (-size, -size, color), + + (0.0, -size, color), + (0.0, -size, color), + + (size, -size, color), + (size, -size, color), + + (size, 0.0, color), + (size, 0.0, color), + + (size, size, color), + (size, size, color), + + (0.0, size, color), + (0.0, size, color), + + (-size, size, color), + (-size, size, color), + + (-size, 0.0, color), + (-size, 0.0, color), + + (-size, -size, color), + (-size, -size, color), + + (-size, -size, 0) + ] +} + +impl Transformers for Grid { + fn apply(&self, _point_list: &[Point]) -> Vec { + let mut sq1 = square_box(1000.0, 255 << 8); + let mut line = square_box(2000.0, 255); + line.append(&mut sq1); + + let line: Vec = line.into_iter() + .map(|tpl| tpl.into()).collect(); + line + } +} diff --git a/src/transformer/rotate.rs b/src/transformer/rotate.rs index 177a0d3..2cb9cac 100644 --- a/src/transformer/rotate.rs +++ b/src/transformer/rotate.rs @@ -13,12 +13,6 @@ pub struct Rotate { } -impl Rotate { - pub fn new(cx: f32, cy: f32, angle: f32) -> Self { - Self { cx, cy, angle: angle / 180. * PI} - } -} - impl Transformers for Rotate { fn apply(&self, point_list: &[Point]) -> Vec { point_list.iter() diff --git a/src/transformer/translate.rs b/src/transformer/translate.rs index a36f272..c3f4fec 100644 --- a/src/transformer/translate.rs +++ b/src/transformer/translate.rs @@ -6,18 +6,12 @@ use serde::{Serialize,Deserialize}; #[derive(Serialize,Deserialize,Debug,Clone,Copy)] pub struct Translate { - x: f32, - y: f32 -} - -impl Translate { - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } + pub x: f32, + pub y: f32 } impl Transformers for Translate { - fn apply(&self, point_list: &[Point]) -> Vec { + fn apply(&self, point_list: &[Point]) -> Vec { point_list.iter() .map(| pt | { Point { x: pt.x + self.x,