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 serde::{Serialize,Deserialize};
use crate::errors::{LJError,LJResult};
use serde::{Serialize, Deserialize};
use crate::errors::{LJError, LJResult};
use crate::transformer;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Conf {
pub laser_id: u8,
pub debug: bool,
pub redis_url: String,
pub dac: DacFamily,
#[serde(default)]
pub transformers: Vec<TransformConf>
pub laser_id: u8,
pub debug: bool,
pub redis_url: String,
pub dac: DacFamily,
#[serde(default)]
pub transformers: Vec<TransformConf>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum DacFamily {
#[serde(rename = "helios")]
Helios(HeliosConf),
#[serde(rename = "etherdream")]
Etherdream(EtherDreamConf),
#[serde(rename = "dummy")]
Dummy,
#[serde(rename = "helios")]
Helios(HeliosConf),
#[serde(rename = "etherdream")]
Etherdream(EtherDreamConf),
#[serde(rename = "dummy")]
Dummy,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HeliosConf {
pub id: u8
pub id: u8,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EtherDreamConf {
pub ip: String
pub ip: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum TransformConf {
#[serde(rename = "translate")]
Translate(transformer::Translate),
#[serde(rename = "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),
#[serde(rename = "homography")]
Homography(transformer::Homography),
#[serde(rename = "helios_to_etherdream")]
HeliosToEtherdream(transformer::HeliosToEtherdream),
#[serde(rename = "translate")]
Translate(transformer::Translate),
#[serde(rename = "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),
#[serde(rename = "homography")]
Homography(transformer::Homography),
#[serde(rename = "helios_to_etherdream")]
HeliosToEtherdream(transformer::HeliosToEtherdream),
#[serde(rename = "intensity")]
Intensity(transformer::Intensity),
}
impl Conf {
pub fn new(path: &str) -> LJResult<Conf> {
let settings = Config::builder()
.add_source(config::File::with_name(path))
.build()?;
let conf : Conf = settings.try_deserialize().map_err(LJError::Config)?;
Ok(conf)
}
pub fn new(path: &str) -> LJResult<Conf> {
let settings = Config::builder()
.add_source(config::File::with_name(path))
.build()?;
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);
let conf: Conf = settings.try_deserialize().map_err(LJError::Config)?;
Ok(conf)
}
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);
}
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::FlipH(r) => Box::new(*r),
TransformConf::FlipV(r) => Box::new(*r),
TransformConf::Grid(r) => Box::new(*r),
TransformConf::HeliosToEtherdream(r) => Box::new(*r),
TransformConf::Homography(r) => Box::new(*r),
TransformConf::Intensity(r) => Box::new(*r),
TransformConf::Replicate(r) => Box::new(*r),
TransformConf::Rotate(r) => Box::new(*r),
TransformConf::Translate(t) => Box::new(*t),
};
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 std::ops::Mul;
#[derive(Debug, Clone, Copy, Default, PartialEq)]
@ -14,6 +15,17 @@ pub struct Color {
pub g: 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 {
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_vertical;
mod grid;
mod homography;
mod helios_to_etherdream;
mod homography;
mod intensity;
mod replicate;
mod rotate;
mod translate;
use crate::point::Point;
use crate::worldstate::WorldState;
@ -20,6 +20,7 @@ pub use flip_vertical::FlipVertical;
pub use grid::Grid;
pub use self::homography::Homography;
pub use helios_to_etherdream::HeliosToEtherdream;
pub use intensity::Intensity;
pub trait Transformers {
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
}
}