From: Ian Jackson Date: Sun, 21 Mar 2021 23:36:39 +0000 (+0000) Subject: shapelib: Provide sensible default for centre X-Git-Tag: otter-0.5.0~538 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8a1e49c4239679f13253bc8fd7178b54a6aca625;p=otter.git shapelib: Provide sensible default for centre Signed-off-by: Ian Jackson --- diff --git a/src/shapelib-toml.rs b/src/shapelib-toml.rs index 8ae0f8d3..796b91c7 100644 --- a/src/shapelib-toml.rs +++ b/src/shapelib-toml.rs @@ -110,7 +110,9 @@ pub struct GroupDetails { /// scale by different amounts in x and y. pub orig_size: Vec, - pub centre: [f64; 2], + #[serde(default)] + /// Default if not supplied is the centre according to the size. + pub centre: Option<[f64; 2]>, #[serde(default)] /// Default is `false` diff --git a/src/shapelib.rs b/src/shapelib.rs index 02a55645..13090a46 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -196,9 +196,23 @@ impl OutlineTrait for Item { delegate! { to self.outline { impl FaceTransform { #[throws(LLE)] fn from_group(d: &GroupDetails) -> Self { - let centre = d.centre; + // by this point d.size has already been scaled by scale + let centre = d.centre.map(Ok).unwrap_or_else(|| Ok::<_,LLE>({ + match d.size.as_slice() { + [a] => [a,a], + [a,b] => [a,b], + x => throw!(LLE::WrongNumberOfSizeDimensions { + got: x.len(), + expected: [1,2], + }), + }.iter().cloned().map(|size| { + size * 0.5 / d.scale + }) + .collect::>() + .into_inner() + .unwrap() + }))?; let scale = if ! d.orig_size.is_empty() && ! d.size.is_empty() { - // by this point d.size has already been scaled by scale izip!(&d.orig_size, &d.size) .map(|(&orig_size, &target_size)| { target_size / orig_size