From 0f2631fece831c56825db9b7c6aa2697a9f21f94 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 18 Mar 2022 13:56:05 +0100 Subject: [PATCH] Validate tolerance on `ShapeProcessor` creation --- fj-app/src/main.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/fj-app/src/main.rs b/fj-app/src/main.rs index c352d565b..fd1114769 100644 --- a/fj-app/src/main.rs +++ b/fj-app/src/main.rs @@ -94,9 +94,7 @@ fn main() -> anyhow::Result<()> { // https://github.com/hannobraun/fornjot/issues/32 let shape = model.load(¶meters)?; - let shape_processor = ShapeProcessor { - tolerance: args.tolerance, - }; + let shape_processor = ShapeProcessor::new(args.tolerance)?; let mut processed_shape = shape_processor.process(&shape)?; if let Some(path) = args.export { @@ -344,10 +342,26 @@ fn main() -> anyhow::Result<()> { } struct ShapeProcessor { - tolerance: Option, + tolerance: Option, } impl ShapeProcessor { + fn new(tolerance: Option) -> anyhow::Result { + if let Some(tolerance) = tolerance { + if tolerance <= 0. { + anyhow::bail!( + "Invalid user defined model deviation tolerance: {}.\n\ + Tolerance must be larger than zero", + tolerance + ); + } + } + + let tolerance = tolerance.map(Scalar::from_f64); + + Ok(Self { tolerance }) + } + fn process(&self, shape: &fj::Shape) -> anyhow::Result { let aabb = shape.bounding_volume(); @@ -369,17 +383,7 @@ impl ShapeProcessor { tolerance } - Some(user_defined_tolerance) => { - if user_defined_tolerance > 0.0 { - Scalar::from_f64(user_defined_tolerance) - } else { - anyhow::bail!( - "Invalid user defined model deviation tolerance: {}.\n\ - Tolerance must be larger than zero", - user_defined_tolerance - ) - } - } + Some(user_defined_tolerance) => user_defined_tolerance, }; let mut debug_info = DebugInfo::new();