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

IdealFIlter: Add filter_type to properties.

parent bfa75023
No related branches found
No related tags found
No related merge requests found
#![warn(missing_docs)]
use serde_derive::{Serialize, Deserialize};
use crate::analyzer::AnalyzerType;
use crate::dottable::Dottable;
use crate::error::OpossumError;
use crate::lightdata::{DataEnergy, LightData};
use crate::optic_ports::OpticPorts;
use crate::optical::{LightResult, Optical};
use crate::properties::{Properties, Property};
use crate::properties::{Properties, Property, Proptype};
use crate::spectrum::Spectrum;
use std::collections::HashMap;
type Result<T> = std::result::Result<T, OpossumError>;
/// Config data for an [`IdealFilter`].
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FilterType {
/// a fixed (wavelength-independant) transmission value. Must be between 0.0 and 1.0
Constant(f64),
......@@ -28,7 +30,6 @@ pub enum FilterType {
/// - Outputs
/// - `rear`
pub struct IdealFilter {
filter_type: FilterType,
props: Properties,
}
......@@ -36,13 +37,13 @@ fn create_default_props() -> Properties {
let mut props = Properties::default();
props.set("name", "ideal filter".into());
props.set("inverted", false.into());
props.set("filter type", FilterType::Constant(1.0).into());
props
}
impl Default for IdealFilter {
fn default() -> Self {
Self {
filter_type: FilterType::Constant(1.0),
props: create_default_props(),
}
}
......@@ -62,14 +63,20 @@ impl IdealFilter {
));
}
}
let mut props=create_default_props();
props.set("filter type", filter_type.into());
Ok(Self {
filter_type,
props: create_default_props(),
props: props,
})
}
/// Returns the filter type of this [`IdealFilter`].
pub fn filter_type(&self) -> FilterType {
self.filter_type.clone()
let filter_type=self.props.get("filter type").unwrap().prop.clone();
if let Proptype::FilterType(filter_type)=filter_type {
filter_type
} else {
panic!("wrong data type")
}
}
/// Sets a constant transmission value for this [`IdealFilter`].
///
......@@ -79,7 +86,7 @@ impl IdealFilter {
/// This function will return an error if a transmission factor > 1.0 is given (This would be an amplifiying filter :-) ).
pub fn set_transmission(&mut self, transmission: f64) -> Result<()> {
if (0.0..=1.0).contains(&transmission) {
self.filter_type = FilterType::Constant(transmission);
self.props.set("filter type", FilterType::Constant(transmission).into());
Ok(())
} else {
Err(OpossumError::Other(
......@@ -95,7 +102,7 @@ impl IdealFilter {
/// This function will return an error if an optical density < 0.0 was given.
pub fn set_optical_density(&mut self, density: f64) -> Result<()> {
if density >= 0.0 {
self.filter_type = FilterType::Constant(f64::powf(10.0, -1.0 * density));
self.props.set("filter type", FilterType::Constant(f64::powf(10.0, -1.0 * density)).into());
Ok(())
} else {
Err(OpossumError::Other("optical densitiy must be >=0".into()))
......@@ -105,7 +112,7 @@ impl IdealFilter {
///
/// This functions `None` if the filter type is not [`FilterType::Constant`].
pub fn optical_density(&self) -> Option<f64> {
match self.filter_type {
match self.filter_type() {
FilterType::Constant(t) => Some(-1.0 * f64::log10(t)),
_ => None,
}
......@@ -116,7 +123,7 @@ impl IdealFilter {
match input {
LightData::Energy(e) => {
let mut out_spec = e.spectrum.clone();
match &self.filter_type {
match &self.filter_type() {
FilterType::Constant(t) => {
if out_spec.scale_vertical(*t).is_ok() {
let light_data =
......
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::{error::OpossumError, lightdata::LightData, optical::OpticGraph};
use crate::{error::OpossumError, lightdata::LightData, optical::OpticGraph, nodes::FilterType};
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
pub struct Properties {
......@@ -77,6 +77,13 @@ impl From<i32> for Property {
}
}
}
impl From<FilterType> for Property {
fn from(value: FilterType) -> Self {
Property {
prop: Proptype::FilterType(value),
}
}
}
#[non_exhaustive]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Proptype {
......@@ -86,4 +93,5 @@ pub enum Proptype {
Bool(bool),
LightData(Option<LightData>),
OpticGraph(OpticGraph),
FilterType(FilterType)
}
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