From 4c81324e9e1b0bac603ba7cc04bd4bd5f2e65802 Mon Sep 17 00:00:00 2001 From: mxdamien Date: Tue, 15 Mar 2022 11:05:30 +0100 Subject: [PATCH] Add command line argument for user defined model tolerance --- .vscode/launch.json | 4 +++- src/args.rs | 4 ++++ src/main.rs | 36 +++++++++++++++++++++++------------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 73d64e0ef7..3847a403ac 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -19,7 +19,9 @@ "kind": "bin" } }, - "args": [], + "args": [ + "-t=-2.3" + ], "cwd": "${workspaceFolder}" }, { diff --git a/src/args.rs b/src/args.rs index 42084065cf..4c540877c8 100644 --- a/src/args.rs +++ b/src/args.rs @@ -14,6 +14,10 @@ pub struct Args { /// Parameters for the model, each in the form `key=value` #[clap(short, long)] pub parameters: Vec, + + // Model deviation tolerance + #[clap[short, long]] + pub tolerance: Option, } impl Args { diff --git a/src/main.rs b/src/main.rs index da88179ea6..be7d6ea107 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,22 +81,32 @@ fn main() -> anyhow::Result<()> { let mut aabb = shape.bounding_volume(); - // Compute a reasonable default for the tolerance value. To do this, we just - // look at the smallest non-zero extent of the bounding box and divide that - // by some value. - let tolerance = { - let mut min_extent = Scalar::MAX; - for extent in aabb.size().components() { - if extent > Scalar::ZERO && extent < min_extent { - min_extent = extent; + let tolerance = match args.tolerance { + None => { + // Compute a reasonable default for the tolerance value. To do this, we just + // look at the smallest non-zero extent of the bounding box and divide that + // by some value. + let mut min_extent = Scalar::MAX; + for extent in aabb.size().components() { + if extent > Scalar::ZERO && extent < min_extent { + min_extent = extent; + } } - } - // `tolerance` must not be zero, or we'll run into trouble. - let tolerance = min_extent / Scalar::from_f64(1000.); - assert!(tolerance > Scalar::ZERO); + // `tolerance` must not be zero, or we'll run into trouble. + let tolerance = min_extent / Scalar::from_f64(1000.); + assert!(tolerance > Scalar::ZERO); - tolerance + 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: {}. Tolerance must be larger than zero", + user_defined_tolerance) + } + } }; let mut debug_info = DebugInfo::new();