jagua_rs/geometry/
original_shape.rs

1use crate::geometry::DTransformation;
2use crate::geometry::geo_traits::Transformable;
3use crate::geometry::primitives::{Point, Rect, SPolygon};
4use crate::geometry::shape_modification::{
5    ShapeModifyConfig, ShapeModifyMode, offset_shape, simplify_shape,
6};
7use anyhow::Result;
8
9#[derive(Clone, Debug)]
10/// A [`SPolygon`] exactly as is defined in the input file
11///
12/// Also contains all required operation to convert it to a shape that can be used internally.
13/// Currently, these are centering and simplification operations, but could be extended in the future.
14pub struct OriginalShape {
15    pub shape: SPolygon,
16    pub pre_transform: DTransformation,
17    pub modify_mode: ShapeModifyMode,
18    pub modify_config: ShapeModifyConfig,
19}
20
21impl OriginalShape {
22    pub fn convert_to_internal(&self) -> Result<SPolygon> {
23        // Apply the transformation
24        let mut internal = self.shape.transform_clone(&self.pre_transform.compose());
25
26        if let Some(offset) = self.modify_config.offset {
27            // Offset the shape
28            internal = offset_shape(&internal, self.modify_mode, offset)?;
29        }
30        if let Some(tolerance) = self.modify_config.simplify_tolerance {
31            // Simplify the shape
32            internal = simplify_shape(&internal, self.modify_mode, tolerance);
33        };
34        Ok(internal)
35    }
36
37    pub fn centroid(&self) -> Point {
38        self.shape.centroid()
39    }
40
41    pub fn area(&self) -> f32 {
42        self.shape.area
43    }
44
45    pub fn bbox(&self) -> Rect {
46        self.shape.bbox
47    }
48
49    pub fn diameter(&self) -> f32 {
50        self.shape.diameter
51    }
52}