Skip to content
Snippets Groups Projects
Commit 30ab30be authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Improve Debug format for NodeDetector

parent 2abce1c3
No related branches found
No related tags found
No related merge requests found
use std::fmt::Display;
#[derive(Debug, PartialEq, Clone)]
pub enum LightData {
Energy(LightDataEnergy),
......@@ -5,6 +7,14 @@ pub enum LightData {
Fourier,
}
impl Display for LightData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LightData::Energy(e) => write!(f, "Energy: {}", e.energy),
_ => write!(f, "No display defined for this type of LightData"),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct LightDataEnergy {
pub energy: f32,
......
......@@ -3,8 +3,9 @@ use std::collections::HashMap;
use crate::{
analyzer::AnalyzerType,
error::OpossumError,
lightdata::{LightData, LightDataEnergy},
optic_node::{Dottable, LightResult, Optical},
optic_ports::OpticPorts, lightdata::{LightData, LightDataEnergy},
optic_ports::OpticPorts,
};
type Result<T> = std::result::Result<T, OpossumError>;
......@@ -31,31 +32,34 @@ impl NodeBeamSplitter {
self.ratio = ratio;
}
pub fn analyze_energy(&mut self, incoming_data: LightResult) -> Result<LightResult> {
let in1=incoming_data.get("input1");
let in2=incoming_data.get("input2");
let in1 = incoming_data.get("input1");
let in2 = incoming_data.get("input2");
let mut in1_energy=0.0;
let mut in2_energy=0.0;
let mut in1_energy = 0.0;
let mut in2_energy = 0.0;
if let Some(in1)=in1 {
if let Some(in1)=in1 {
match in1 {
LightData::Energy(e) => in1_energy=e.energy,
_ => return Err(OpossumError::Analysis("expected energy value".into()))
}
if let Some(Some(in1)) = in1 {
match in1 {
LightData::Energy(e) => in1_energy = e.energy,
_ => return Err(OpossumError::Analysis("expected energy value".into())),
}
}
if let Some(in2)=in2 {
if let Some(in2)=in2 {
match in2 {
LightData::Energy(e) => in2_energy=e.energy,
_ => return Err(OpossumError::Analysis("expected energy value".into()))
}
if let Some(Some(in2)) = in2 {
match in2 {
LightData::Energy(e) => in2_energy = e.energy,
_ => return Err(OpossumError::Analysis("expected energy value".into())),
}
}
let out1_energy=Some(LightData::Energy(LightDataEnergy{energy: in1_energy*self.ratio+in2_energy*(1.0-self.ratio)}));
let out2_energy =Some(LightData::Energy(LightDataEnergy{energy: in1_energy*(1.0-self.ratio) + in2_energy*self.ratio}));
Ok(HashMap::from([("out1_trans1_refl2".into(),out1_energy),("out2_trans2_refl1".into(),out2_energy)]))
let out1_energy = Some(LightData::Energy(LightDataEnergy {
energy: in1_energy * self.ratio + in2_energy * (1.0 - self.ratio),
}));
let out2_energy = Some(LightData::Energy(LightDataEnergy {
energy: in1_energy * (1.0 - self.ratio) + in2_energy * self.ratio,
}));
Ok(HashMap::from([
("out1_trans1_refl2".into(), out1_energy),
("out2_trans2_refl1".into(), out2_energy),
]))
}
}
......
use crate::{optic_node::{Optical, Dottable, LightResult}, optic_ports::OpticPorts, error::OpossumError};
use crate::lightdata::LightData;
use crate::{
error::OpossumError,
optic_node::{Dottable, LightResult, Optical},
optic_ports::OpticPorts,
};
use std::fmt::Debug;
type Result<T> = std::result::Result<T, OpossumError>;
#[derive(Debug, Default)]
#[derive(Default)]
/// This node rerpresents an universal detector. Any [`LightData`] coming in will be stored internally for later display / export. So far it only has one input (in1).
pub struct NodeDetector {
light_data: Option<LightData>
light_data: Option<LightData>,
}
impl Optical for NodeDetector {
fn node_type(&self) -> &str {
"light sink: detector"
}
fn ports(&self) -> OpticPorts {
let mut ports=OpticPorts::new();
ports.add_input("in1").unwrap();
ports
}
fn analyze(&mut self, incoming_data: LightResult, _analyzer_type: &crate::analyzer::AnalyzerType) -> Result<LightResult> {
let data=incoming_data.into_iter().filter(|data| data.0=="in1").last();
if let Some(data)=data {
self.light_data=data.1;
fn node_type(&self) -> &str {
"light sink: detector"
}
fn ports(&self) -> OpticPorts {
let mut ports = OpticPorts::new();
ports.add_input("in1").unwrap();
ports
}
fn analyze(
&mut self,
incoming_data: LightResult,
_analyzer_type: &crate::analyzer::AnalyzerType,
) -> Result<LightResult> {
let data = incoming_data
.into_iter()
.filter(|data| data.0 == "in1")
.last();
if let Some(data) = data {
self.light_data = data.1;
}
Ok(LightResult::default())
}
Ok(LightResult::default())
}
}
impl Dottable for NodeDetector{
impl Debug for NodeDetector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.light_data {
Some(data) => write!(f,"{}",data),
None => write!(f, "no data"),
}
}
}
impl Dottable for NodeDetector {
fn node_color(&self) -> &str {
"lemonchiffon"
}
}
}
......@@ -47,7 +47,7 @@ impl Optical for NodeSource {
if data.is_some() {
Ok(HashMap::from([("out1".into(), data)]))
} else {
Err(OpossumError::Analysis(format!("no input data available")))
Err(OpossumError::Analysis("no input data available".into()))
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment