Skip to main content

mlt_core/decoder/geometry/
model.rs

1use derive_debug::Dbg;
2use num_enum::TryFromPrimitive;
3use serde::{Deserialize, Serialize};
4
5use crate::decoder::RawStream;
6use crate::utils::formatter::{opt_vec_seq, vec_seq};
7use crate::{DecodeState, Lazy};
8
9/// Geometry column representation, parameterized by decode state.
10///
11/// - `Geometry<'a>` / `Geometry<'a, Lazy>` - either raw bytes or decoded, in an [`crate::LazyParsed`] enum.
12/// - `Geometry<'a, Parsed>` - decoded [`GeometryValues`] directly (no enum wrapper).
13pub type Geometry<'a, S = Lazy> = <S as DecodeState>::LazyOrParsed<RawGeometry<'a>, GeometryValues>;
14
15/// Raw geometry data as read directly from the tile (borrows from input bytes)
16#[derive(Debug, PartialEq, Clone)]
17pub struct RawGeometry<'a> {
18    pub(crate) types: GeoTypes<'a>,
19    pub(crate) index_base: IndexBase,
20    pub(crate) items: Vec<RawStream<'a>>,
21}
22
23/// Which vertex a section's triangle index `0` names.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub(crate) enum IndexBase {
26    /// The first vertex of the index's own feature, as v1 stores it.
27    Feature,
28    /// The first vertex of the layer, as v2 stores it.
29    #[cfg(feature = "unstable-v2")]
30    Layer,
31}
32
33/// Where a geometry section's per-feature [`GeometryType`]s come from.
34///
35/// A v1 section always spells them out. A v2 section may instead name one type in
36/// its [layer header byte](crate::decoder::LayerHeader02), which is all a layer of
37/// a single geometry type has to say.
38#[derive(Debug, PartialEq, Clone)]
39pub enum GeoTypes<'a> {
40    /// The leading stream of the section, one value per feature.
41    Stream(RawStream<'a>),
42    /// No stream: every one of `feature_count` features has this type.
43    #[cfg(feature = "unstable-v2")]
44    Uniform {
45        geometry_type: GeometryType,
46        feature_count: u32,
47    },
48}
49
50/// Parsed (decoded) geometry data
51#[derive(Clone, Dbg, Default, PartialEq, Eq)]
52pub struct GeometryValues {
53    #[dbg(formatter = "vec_seq")]
54    pub(crate) vector_types: Vec<GeometryType>,
55    #[dbg(formatter = "opt_vec_seq")]
56    pub(crate) geometry_offsets: Option<Vec<u32>>,
57    #[dbg(formatter = "opt_vec_seq")]
58    pub(crate) part_offsets: Option<Vec<u32>>,
59    #[dbg(formatter = "opt_vec_seq")]
60    pub(crate) ring_offsets: Option<Vec<u32>>,
61    #[dbg(formatter = "opt_vec_seq")]
62    pub(crate) index_buffer: Option<Vec<u32>>,
63    #[dbg(formatter = "opt_vec_seq")]
64    pub(crate) triangle_offsets: Option<Vec<u32>>,
65    #[dbg(formatter = "opt_vec_seq")]
66    pub(crate) vertices: Option<Vec<i32>>,
67}
68
69/// Types of geometries supported in MLT
70#[derive(
71    Debug,
72    Clone,
73    Copy,
74    PartialEq,
75    PartialOrd,
76    Eq,
77    Hash,
78    Ord,
79    TryFromPrimitive,
80    strum::Display,
81    strum::IntoStaticStr,
82    Serialize,
83    Deserialize,
84)]
85#[repr(u8)]
86#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
87pub enum GeometryType {
88    /*
89        ATTENTION: Do not modify the order of this enum - it is being used in geometry decoding
90    */
91    Point,
92    LineString,
93    Polygon,
94    MultiPoint,
95    MultiLineString,
96    MultiPolygon,
97}