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            if offset != 0.0 {
29                internal = offset_shape(&internal, self.modify_mode, offset)?;
30            }
31        }
32        if let Some(tolerance) = self.modify_config.simplify_tolerance {
33            // Simplify the shape
34            internal = simplify_shape(&internal, self.modify_mode, tolerance);
35        };
36        Ok(internal)
37    }
38
39    pub fn centroid(&self) -> Point {
40        self.shape.centroid()
41    }
42
43    pub fn area(&self) -> f32 {
44        self.shape.area
45    }
46
47    pub fn bbox(&self) -> Rect {
48        self.shape.bbox
49    }
50
51    pub fn diameter(&self) -> f32 {
52        self.shape.diameter
53    }
54}