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

BeamSplitter: Add range check for split ratio

parent 550d04b1
No related branches found
No related tags found
1 merge request!6Resolve "Beam splitter: Check splitting ratio"
Pipeline #7380 failed
......@@ -28,8 +28,14 @@ pub struct BeamSplitter {
impl BeamSplitter {
/// Creates a new [`BeamSplitter`] with a given splitting ratio.
pub fn new(ratio: f64) -> Self {
Self { ratio }
pub fn new(ratio: f64) -> Result<Self> {
if (0.0..1.0).contains(&ratio) {
Ok(Self { ratio })
} else {
Err(OpossumError::Other(
"splitting ration must be within (0.0..1.0)".into(),
))
}
}
/// Returns the splitting ratio of this [`BeamSplitter`].
......@@ -38,8 +44,15 @@ impl BeamSplitter {
}
/// Sets the splitting ratio of this [`BeamSplitter`].
pub fn set_ratio(&mut self, ratio: f64) {
self.ratio = ratio;
pub fn set_ratio(&mut self, ratio: f64) -> Result<()> {
if (0.0..1.0).contains(&ratio) {
self.ratio = ratio;
Ok(())
} else {
Err(OpossumError::Other(
"splitting ration must be within (0.0..1.0)".into(),
))
}
}
fn analyze_energy(&mut self, incoming_data: LightResult) -> Result<LightResult> {
let in1 = incoming_data.get("input1");
......@@ -135,4 +148,3 @@ impl Dottable for BeamSplitter {
"lightpink"
}
}
......@@ -707,7 +707,7 @@ mod test {
let sn1_i = og.add_node(sub_node1);
let sub_node1 = OpticNode::new("test2", Dummy::default());
let sn2_i = og.add_node(sub_node1);
let sub_node3 = OpticNode::new("test3", BeamSplitter::new(0.5));
let sub_node3 = OpticNode::new("test3", BeamSplitter::new(0.5).unwrap());
let sn3_i = og.add_node(sub_node3);
og.connect_nodes(sn1_i, "rear", sn2_i, "front").unwrap();
og.connect_nodes(sn2_i, "rear", sn3_i, "input1").unwrap();
......@@ -718,7 +718,7 @@ mod test {
let mut og = NodeGroup::new();
let sub_node1 = OpticNode::new("test1", Dummy::default());
let sn1_i = og.add_node(sub_node1);
let sub_node1 = OpticNode::new("test2", BeamSplitter::new(0.5));
let sub_node1 = OpticNode::new("test2", BeamSplitter::new(0.5).unwrap());
let sn2_i = og.add_node(sub_node1);
let sub_node3 = OpticNode::new("test3", Dummy::default());
let sn3_i = og.add_node(sub_node3);
......
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