use geo::MapCoords as _;
use geo_types::{Coord, LineString};
#[derive(Clone, Debug, PartialEq)]
pub struct ContourFeature {
pub geometry: LineString<f64>,
pub elevation: i16,
pub is_major: bool,
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct GeometryFeatures(Vec<ContourFeature>);
impl GeometryFeatures {
#[must_use]
pub fn new(features: Vec<ContourFeature>) -> Self {
Self(features)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
pub fn iter(&self) -> std::slice::Iter<'_, ContourFeature> {
self.0.iter()
}
}
impl IntoIterator for GeometryFeatures {
type Item = ContourFeature;
type IntoIter = std::vec::IntoIter<ContourFeature>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a GeometryFeatures {
type Item = &'a ContourFeature;
type IntoIter = std::slice::Iter<'a, ContourFeature>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GeometryTransform {
pub scaling: f64,
pub margin: f64,
}
impl Default for GeometryTransform {
fn default() -> Self {
Self {
scaling: 16.0,
margin: 0.0,
}
}
}
impl GeometryTransform {
#[must_use]
pub fn apply(&self, features: GeometryFeatures) -> GeometryFeatures {
GeometryFeatures(
features
.into_iter()
.map(|feature| ContourFeature {
geometry: feature.geometry.map_coords(|coord| Coord {
x: (coord.x - self.margin) * self.scaling,
y: (coord.y - self.margin) * self.scaling,
}),
..feature
})
.collect(),
)
}
}
#[cfg(test)]
mod tests {
use approx::assert_relative_eq;
use super::*;
fn feature(points: &[(f64, f64)]) -> ContourFeature {
ContourFeature {
geometry: LineString::from(points.to_vec()),
elevation: 100,
is_major: false,
}
}
#[test]
fn margin_is_shifted_off_before_scaling() {
let transform = GeometryTransform {
scaling: 2.0,
margin: 10.0,
};
let shifted = transform.apply(GeometryFeatures::new(vec![feature(&[
(10.0, 10.0),
(20.0, 30.0),
])]));
let coords = &shifted.iter().next().expect("one feature").geometry.0;
assert_relative_eq!(coords[0].x, 0.0);
assert_relative_eq!(coords[0].y, 0.0);
assert_relative_eq!(coords[1].x, 20.0);
assert_relative_eq!(coords[1].y, 40.0);
}
#[test]
fn tags_survive_the_transform() {
let transform = GeometryTransform::default();
let original = ContourFeature {
geometry: LineString::from(vec![(0.0, 0.0), (1.0, 1.0)]),
elevation: 1234,
is_major: true,
};
let transformed = transform.apply(GeometryFeatures::new(vec![original]));
let feature = transformed.iter().next().expect("one feature");
assert_eq!(feature.elevation, 1234);
assert!(feature.is_major);
}
}