feat: add Intensity Transformer

This commit is contained in:
alban 2023-07-24 18:53:19 +02:00
parent 22d7d3c718
commit 2c0d5cd13f
4 changed files with 123 additions and 77 deletions

View File

@ -1,101 +1,106 @@
use config::Config; use config::Config;
use serde::{Serialize,Deserialize}; use serde::{Serialize, Deserialize};
use crate::errors::{LJError,LJResult}; use crate::errors::{LJError, LJResult};
use crate::transformer; use crate::transformer;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Conf { pub struct Conf {
pub laser_id: u8, pub laser_id: u8,
pub debug: bool, pub debug: bool,
pub redis_url: String, pub redis_url: String,
pub dac: DacFamily, pub dac: DacFamily,
#[serde(default)] #[serde(default)]
pub transformers: Vec<TransformConf> pub transformers: Vec<TransformConf>,
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub enum DacFamily { pub enum DacFamily {
#[serde(rename = "helios")] #[serde(rename = "helios")]
Helios(HeliosConf), Helios(HeliosConf),
#[serde(rename = "etherdream")] #[serde(rename = "etherdream")]
Etherdream(EtherDreamConf), Etherdream(EtherDreamConf),
#[serde(rename = "dummy")] #[serde(rename = "dummy")]
Dummy, Dummy,
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HeliosConf { pub struct HeliosConf {
pub id: u8 pub id: u8,
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EtherDreamConf { pub struct EtherDreamConf {
pub ip: String pub ip: String,
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub enum TransformConf { pub enum TransformConf {
#[serde(rename = "translate")] #[serde(rename = "translate")]
Translate(transformer::Translate), Translate(transformer::Translate),
#[serde(rename = "replicate")] #[serde(rename = "replicate")]
Replicate(transformer::Replicate), Replicate(transformer::Replicate),
#[serde(rename = "rotate")] #[serde(rename = "rotate")]
Rotate(transformer::Rotate), Rotate(transformer::Rotate),
#[serde(rename = "flip_horizontal")] #[serde(rename = "flip_horizontal")]
FlipH(transformer::FlipHorizontal), FlipH(transformer::FlipHorizontal),
#[serde(rename = "flip_vertical")] #[serde(rename = "flip_vertical")]
FlipV(transformer::FlipVertical), FlipV(transformer::FlipVertical),
#[serde(rename = "grid")] #[serde(rename = "grid")]
Grid(transformer::Grid), Grid(transformer::Grid),
#[serde(rename = "homography")] #[serde(rename = "homography")]
Homography(transformer::Homography), Homography(transformer::Homography),
#[serde(rename = "helios_to_etherdream")] #[serde(rename = "helios_to_etherdream")]
HeliosToEtherdream(transformer::HeliosToEtherdream), HeliosToEtherdream(transformer::HeliosToEtherdream),
#[serde(rename = "intensity")]
Intensity(transformer::Intensity),
} }
impl Conf { impl Conf {
pub fn new(path: &str) -> LJResult<Conf> { pub fn new(path: &str) -> LJResult<Conf> {
let settings = Config::builder() let settings = Config::builder()
.add_source(config::File::with_name(path)) .add_source(config::File::with_name(path))
.build()?; .build()?;
let conf : Conf = settings.try_deserialize().map_err(LJError::Config)?; let conf: Conf = settings.try_deserialize().map_err(LJError::Config)?;
Ok(conf) Ok(conf)
}
pub fn get_transformers(&self) -> Vec<Box<dyn transformer::Transformers>> {
let mut v = vec![];
for t in &self.transformers {
let t : Box<dyn transformer::Transformers> = match t {
TransformConf::Translate(t) => Box::new(*t),
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),
TransformConf::Homography(r) => Box::new(*r),
TransformConf::HeliosToEtherdream(r) => Box::new(*r),
};
v.push(t);
} }
v
}
#[allow(dead_code)] pub fn get_transformers(&self) -> Vec<Box<dyn transformer::Transformers>> {
pub fn dump() { let mut v = vec![];
let conf = Conf { for t in &self.transformers {
laser_id: 0, let t: Box<dyn transformer::Transformers> = match t {
debug: true, TransformConf::FlipH(r) => Box::new(*r),
redis_url: "redis://127.0.0.1:6379/".to_string(), TransformConf::FlipV(r) => Box::new(*r),
dac: DacFamily::Helios(HeliosConf { id: 0 }), TransformConf::Grid(r) => Box::new(*r),
transformers: vec![ TransformConf::HeliosToEtherdream(r) => Box::new(*r),
TransformConf::Translate(transformer::Translate { x: 2000.0, TransformConf::Homography(r) => Box::new(*r),
y: 2000.0 } ), TransformConf::Intensity(r) => Box::new(*r),
TransformConf::Replicate(transformer::Replicate::Until(48)) TransformConf::Replicate(r) => Box::new(*r),
] TransformConf::Rotate(r) => Box::new(*r),
}; TransformConf::Translate(t) => Box::new(*t),
let s = toml::to_string(&conf).unwrap(); };
println!("{}", s); v.push(t);
} }
v
}
#[allow(dead_code)]
pub fn dump() {
let conf = Conf {
laser_id: 0,
debug: true,
redis_url: "redis://127.0.0.1:6379/".to_string(),
dac: DacFamily::Helios(HeliosConf { id: 0 }),
transformers: vec![
TransformConf::Translate(transformer::Translate {
x: 2000.0,
y: 2000.0,
}),
TransformConf::Replicate(transformer::Replicate::Until(48)),
],
};
let s = toml::to_string(&conf).unwrap();
println!("{}", s);
}
} }

View File

@ -1,4 +1,5 @@
use ether_dream::protocol::DacPoint; use ether_dream::protocol::DacPoint;
use std::ops::Mul;
#[derive(Debug, Clone, Copy, Default, PartialEq)] #[derive(Debug, Clone, Copy, Default, PartialEq)]
@ -14,6 +15,17 @@ pub struct Color {
pub g: u8, pub g: u8,
pub b: u8, pub b: u8,
} }
impl Mul<u8> for Color {
type Output = Self;
fn mul(self, rhs: u8) -> Self {
Self{
r: (255 * self.r as u16 / rhs as u16) as u8,
g: (255 * self.g as u16 / rhs as u16) as u8,
b: (255 * self.b as u16 / rhs as u16) as u8,
}
}
}
impl From<Color> for u32 { impl From<Color> for u32 {
fn from(value: Color) -> Self { fn from(value: Color) -> Self {

View File

@ -1,12 +1,12 @@
//pub mod common;
mod translate;
mod replicate;
mod rotate;
mod flip_horizontal; mod flip_horizontal;
mod flip_vertical; mod flip_vertical;
mod grid; mod grid;
mod homography;
mod helios_to_etherdream; mod helios_to_etherdream;
mod homography;
mod intensity;
mod replicate;
mod rotate;
mod translate;
use crate::point::Point; use crate::point::Point;
use crate::worldstate::WorldState; use crate::worldstate::WorldState;
@ -20,6 +20,7 @@ pub use flip_vertical::FlipVertical;
pub use grid::Grid; pub use grid::Grid;
pub use self::homography::Homography; pub use self::homography::Homography;
pub use helios_to_etherdream::HeliosToEtherdream; pub use helios_to_etherdream::HeliosToEtherdream;
pub use intensity::Intensity;
pub trait Transformers { pub trait Transformers {
fn apply( fn apply(

View File

@ -0,0 +1,28 @@
use log::debug;
use crate::transformer::Transformers;
use crate::point::Point;
use crate::worldstate::WorldState;
use serde::{Serialize, Deserialize};
/// Converts helios Geometry to Helios
#[allow(dead_code)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Intensity {
}
impl Transformers for Intensity {
fn apply(&self, point_list: &[Point], ws: &WorldState) -> Vec<Point> {
// debug!("list helios {:?}", point_list);
let out = point_list.iter().map(|pt| {
Point {
x: pt.x,
y: pt.y,
color: pt.color * ws.intensity
}
}).collect();
debug!("list intensity {:?}", out);
out
}
}