Skip to content
Snippets Groups Projects
node_source.rs 1.68 KiB
Newer Older
use std::collections::HashMap;

use crate::{
    lightdata::LightData,
    optic_node::{Dottable, Optical, LightResult},
    optic_ports::OpticPorts, error::OpossumError,
type Result<T> = std::result::Result<T, OpossumError>;

Udo Eisenbarth's avatar
Udo Eisenbarth committed
/// 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 optic scenery.
#[derive(Debug, Default)]
Udo Eisenbarth's avatar
Udo Eisenbarth committed
pub struct Source {
    light_data: Option<LightData>,
}

Udo Eisenbarth's avatar
Udo Eisenbarth committed
impl Source {
    /// Creates a new [`Source`].
    pub fn new(light: LightData) -> Self {
Udo Eisenbarth's avatar
Udo Eisenbarth committed
        Source {
            light_data: Some(light),
        }
    }
Udo Eisenbarth's avatar
Udo Eisenbarth committed
    /// Returns the light data of this [`Source`].
    pub fn light_data(&self) -> Option<&LightData> {
        self.light_data.as_ref()
    }

Udo Eisenbarth's avatar
Udo Eisenbarth committed
    /// 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.light_data = Some(light_data);
    }
}
Udo Eisenbarth's avatar
Udo Eisenbarth committed
impl Optical for Source {
    fn node_type(&self) -> &str {
        "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) -> Result<LightResult> {
        let data=self.light_data.clone();
            Ok(HashMap::from([("out1".into(), data)]))
            Err(OpossumError::Analysis("no input data available".into()))
Udo Eisenbarth's avatar
Udo Eisenbarth committed
impl Dottable for Source {
    fn node_color(&self) -> &str {
        "slateblue"
    }