Newer
Older
optical::{LightResult, Optical},
properties::{Properties, Property, Proptype},
/// This node represents a source of light.
///
/// Hence it has only one output port (out1) and no input ports. Source nodes usually are the first nodes of an [`OpticScenery`](crate::OpticScenery).
///
/// ## Optical Ports
/// - Inputs
/// - none
/// - Outputs
/// - `out1`
///
/// ## Properties
/// - `name`
/// - `light data`
///
/// **Note**: This node does not have the `inverted` property since it has only one output port.
props: Properties,
}
fn create_default_props() -> Properties {
let mut props = Properties::default();
props.set(
"light data",
Property {
prop: Proptype::LightData(None),
},
);
props
impl Default for Source {
fn default() -> Self {
Self {
props: create_default_props(),
}
}
}
/// The light to be emitted from this source is defined in a [`LightData`] structure.
/// ```rust
/// use opossum::{
/// lightdata::{DataEnergy, LightData},
/// nodes::Source,
/// spectrum::create_he_ne_spectrum};
/// let source=Source::new("My Source", LightData::Energy(DataEnergy {spectrum: create_he_ne_spectrum(1.0)}));
pub fn new(name: &str, light: LightData) -> Self {
let mut props = create_default_props();
props.set(
"light data",
Property {
prop: Proptype::LightData(Some(light.clone())),
},
);
/// Sets the light data of this [`Source`]. The [`LightData`] provided here represents the input data of an `OpticScenery`.
pub fn set_light_data(&mut self, light_data: LightData) {
self.props.set(
"light data",
Property {
prop: Proptype::LightData(Some(light_data.clone())),
},
);
impl Debug for Source {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let light_prop = self.props.get("light data").unwrap();
let data = if let Proptype::LightData(data) = &light_prop.prop {
data
} else {
&None
};
match data {
Some(data) => write!(f, "{}", data),
None => write!(f, "no data"),
}
}
}
fn node_type(&self) -> &str {
"light source"
}
if let Proptype::String(name) = &self.props.get("name").unwrap().prop {
name
} else {
"light source"
}
}
fn ports(&self) -> OpticPorts {
let mut ports = OpticPorts::new();
ports.add_output("out1").unwrap();
ports
}
fn analyze(
&mut self,
_incoming_edges: LightResult,
_analyzer_type: &crate::analyzer::AnalyzerType,
let light_prop = self.props.get("light data").unwrap();
let data = if let Proptype::LightData(data) = &light_prop.prop {
data
} else {
&None
};
if data.is_some() {
Ok(HashMap::from([("out1".into(), data.to_owned())]))
Err(OpossumError::Analysis("no input data available".into()))
fn properties(&self) -> &Properties {
&self.props
}
fn set_property(&mut self, name: &str, prop: Property) -> OpmResult<()> {
if self.props.set(name, prop).is_none() {
Err(OpossumError::Other("property not defined".into()))
} else {
Ok(())
}
}
fn node_color(&self) -> &str {
"slateblue"
}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#[cfg(test)]
mod test {
use super::*;
#[test]
fn default() {
let node = Source::default();
assert_eq!(node.name(), "source");
assert_eq!(node.node_type(), "light source");
assert_eq!(node.is_detector(), false);
assert_eq!(node.inverted(), false);
assert_eq!(node.node_color(), "slateblue");
assert!(node.as_group().is_err());
}
#[test]
fn new() {
let source = Source::new("test", LightData::Fourier);
assert_eq!(source.name(), "test");
}
#[test]
fn not_invertable() {
let mut node = Source::default();
assert!(node.set_property("inverted", true.into()).is_err());
}
#[test]
fn ports() {
let detector = Source::default();
assert!(detector.ports().inputs().is_empty());
assert_eq!(detector.ports().outputs(), vec!["out1"]);
}
}