Skip to main content

egml_core/model/geometry/primitives/
surface.rs

1use crate::Error;
2use crate::impl_has_geometry_type;
3use crate::model::common::{
4    ApplyTransform, ComputeEnvelope, IterGeometries, Triangulate, Triangulation,
5};
6use crate::model::geometry::primitives::{
7    AbstractSurface, AbstractSurfacePatchArrayProperty, AsAbstractSurface, AsAbstractSurfaceMut,
8    TriangulatedSurface,
9};
10use crate::model::geometry::refs::AbstractGeometryKindRef;
11use crate::model::geometry::{DirectPosition, Envelope};
12use nalgebra::{Isometry3, Rotation3, Scale3, Transform3, Vector3};
13
14/// A 2-D geometry composed of one or more surface patches.
15///
16/// Corresponds to `gml:Surface` in [OGC 07-036 ยง10.5.10](https://docs.ogc.org/is/07-036/07-036.pdf).  Patches are stored as
17/// a [`AbstractSurfacePatchArrayProperty`] and may be of mixed kinds (polygons, triangles).
18#[derive(Debug, Clone, PartialEq)]
19pub struct Surface {
20    pub abstract_surface: AbstractSurface,
21    patches: AbstractSurfacePatchArrayProperty,
22}
23
24impl Surface {
25    /// Creates a new `Surface` from a patch array.
26    pub fn new(patches: AbstractSurfacePatchArrayProperty) -> Self {
27        Surface {
28            abstract_surface: AbstractSurface::default(),
29            patches,
30        }
31    }
32
33    pub fn from_abstract_surface(
34        abstract_surface: AbstractSurface,
35        patches: AbstractSurfacePatchArrayProperty,
36    ) -> Self {
37        Self {
38            abstract_surface,
39            patches,
40        }
41    }
42
43    pub fn patches(&self) -> &AbstractSurfacePatchArrayProperty {
44        &self.patches
45    }
46}
47
48/// Object-safe read accessor for [`Surface`] data.
49pub trait AsSurface: AsAbstractSurface {
50    /// Returns a reference to the underlying [`Surface`].
51    fn surface(&self) -> &Surface;
52
53    /// Returns the patch array of this surface.
54    fn patches(&self) -> &AbstractSurfacePatchArrayProperty {
55        &self.surface().patches
56    }
57
58    fn patches_len(&self) -> usize {
59        self.patches().objects_len()
60    }
61}
62
63/// Mutable companion to [`AsSurface`].
64pub trait AsSurfaceMut: AsSurface + AsAbstractSurfaceMut {
65    /// Returns a mutable reference to the underlying [`Surface`].
66    fn surface_mut(&mut self) -> &mut Surface;
67
68    fn patches_mut(&mut self) -> &mut AbstractSurfacePatchArrayProperty {
69        &mut self.surface_mut().patches
70    }
71}
72
73impl AsSurface for Surface {
74    fn surface(&self) -> &Surface {
75        self
76    }
77}
78
79impl AsSurfaceMut for Surface {
80    fn surface_mut(&mut self) -> &mut Surface {
81        self
82    }
83}
84
85#[macro_export]
86macro_rules! impl_surface_traits {
87    ($type:ty) => {
88        $crate::impl_abstract_surface_traits!($type);
89
90        impl $crate::model::geometry::primitives::AsAbstractSurface for $type {
91            fn abstract_surface(&self) -> &$crate::model::geometry::primitives::AbstractSurface {
92                &<$type as $crate::model::geometry::primitives::AsSurface>::surface(self)
93                    .abstract_surface
94            }
95        }
96    };
97}
98
99#[macro_export]
100macro_rules! impl_surface_mut_traits {
101    ($type:ty) => {
102        $crate::impl_abstract_surface_mut_traits!($type);
103
104        impl $crate::model::geometry::primitives::AsAbstractSurfaceMut for $type {
105            fn abstract_surface_mut(
106                &mut self,
107            ) -> &mut $crate::model::geometry::primitives::AbstractSurface {
108                &mut <$type as $crate::model::geometry::primitives::AsSurfaceMut>::surface_mut(self)
109                    .abstract_surface
110            }
111        }
112    };
113}
114
115impl_surface_traits!(Surface);
116impl_surface_mut_traits!(Surface);
117impl_has_geometry_type!(Surface, Surface);
118
119impl Surface {
120    pub(crate) fn into_patches(self) -> AbstractSurfacePatchArrayProperty {
121        self.patches
122    }
123
124    pub fn area_3d(&self) -> Result<f64, Error> {
125        self.patches.area_3d()
126    }
127
128    pub fn points(&self) -> Vec<&DirectPosition> {
129        todo!("needs to be implemented")
130    }
131}
132
133impl IterGeometries for Surface {
134    fn iter_geometries(&self) -> Box<dyn Iterator<Item = AbstractGeometryKindRef<'_>> + '_> {
135        Box::new(std::iter::once(self.into()))
136    }
137}
138
139impl ApplyTransform for Surface {
140    fn apply_transform(&mut self, transform: Transform3<f64>) {
141        self.patches.apply_transform(transform)
142    }
143
144    fn apply_isometry(&mut self, isometry: Isometry3<f64>) {
145        self.patches.apply_isometry(isometry)
146    }
147
148    fn apply_translation(&mut self, vector: Vector3<f64>) {
149        self.patches.apply_translation(vector)
150    }
151
152    fn apply_rotation(&mut self, rotation: Rotation3<f64>) {
153        self.patches.apply_rotation(rotation)
154    }
155
156    fn apply_scale(&mut self, scale: Scale3<f64>) {
157        self.patches.apply_scale(scale)
158    }
159}
160
161impl ComputeEnvelope for Surface {
162    /// Returns the union of the bounding boxes of all patches.
163    fn compute_envelope(&self) -> Option<Envelope> {
164        self.patches.compute_envelope()
165    }
166}
167
168impl Triangulate for Surface {
169    /// Patches that fail to triangulate individually (e.g. a degenerate ring) are
170    /// skipped rather than failing the whole surface; see their errors via
171    /// [`Triangulation::skipped`].
172    ///
173    /// # Errors
174    ///
175    /// Returns [`Error::TooFewElements`] if no patch could be triangulated.
176    fn triangulate(&self) -> Result<Triangulation, Error> {
177        let mut surfaces = Vec::new();
178        let mut skipped = Vec::new();
179
180        for patch in self.patches.objects() {
181            match patch.triangulate() {
182                Ok(triangulation) => {
183                    let (surface, nested_skipped) = triangulation.into_parts();
184                    surfaces.push(surface);
185                    skipped.extend(nested_skipped);
186                }
187                Err(error) => {
188                    skipped.push(error);
189                }
190            }
191        }
192
193        let combined = TriangulatedSurface::from_triangulated_surfaces(surfaces)?;
194        Ok(Triangulation::new(combined, skipped))
195    }
196}