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