1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    array::Constraint,
    array_type,
    validate::{Validate, Validator},
    Array, BlockModel, Element, Grid2, Location, Orient2, Vector3,
};

pub(crate) fn zero_origin(v: &Vector3) -> bool {
    *v == [0.0, 0.0, 0.0]
}

/// Selects the type of geometry in an [`Element`](crate::Element) from several options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
pub enum Geometry {
    PointSet(PointSet),
    LineSet(LineSet),
    Surface(Surface),
    GridSurface(GridSurface),
    BlockModel(BlockModel),
    Composite(Composite),
}

impl Geometry {
    /// Returns the valid locations for attributes on this geometry.
    pub fn valid_locations(&self) -> &'static [Location] {
        match self {
            Self::PointSet(_) => &[Location::Vertices],
            Self::LineSet(_) => &[Location::Vertices, Location::Primitives],
            Self::Surface(_) => &[Location::Vertices, Location::Primitives],
            Self::GridSurface(_) => &[Location::Vertices, Location::Primitives],
            Self::Composite(_) => &[Location::Elements],
            Self::BlockModel(b) if b.has_subblocks() => {
                &[Location::Subblocks, Location::Primitives]
            }
            Self::BlockModel(_) => &[Location::Primitives],
        }
    }

    /// Returns the length of the given location, if valid.
    pub fn location_len(&self, location: Location) -> Option<u64> {
        if location == Location::Projected {
            Some(0)
        } else {
            match self {
                Self::PointSet(p) => p.location_len(location),
                Self::LineSet(l) => l.location_len(location),
                Self::Surface(s) => s.location_len(location),
                Self::GridSurface(t) => t.location_len(location),
                Self::BlockModel(b) => b.location_len(location),
                Self::Composite(c) => c.location_len(location),
            }
        }
    }

    pub(crate) fn type_name(&self) -> &'static str {
        match self {
            Self::PointSet(_) => "PointSet",
            Self::LineSet(_) => "LineSet",
            Self::Surface(_) => "Surface",
            Self::GridSurface(_) => "GridSurface",
            Self::Composite(_) => "Composite",
            Self::BlockModel(b) if b.has_subblocks() => "BlockModel(sub-blocked)",
            Self::BlockModel(_) => "BlockModel",
        }
    }
}

impl From<PointSet> for Geometry {
    fn from(value: PointSet) -> Self {
        Self::PointSet(value)
    }
}

impl From<LineSet> for Geometry {
    fn from(value: LineSet) -> Self {
        Self::LineSet(value)
    }
}

impl From<Surface> for Geometry {
    fn from(value: Surface) -> Self {
        Self::Surface(value)
    }
}

impl From<GridSurface> for Geometry {
    fn from(value: GridSurface) -> Self {
        Self::GridSurface(value)
    }
}

impl From<BlockModel> for Geometry {
    fn from(value: BlockModel) -> Self {
        Self::BlockModel(value)
    }
}

impl From<Composite> for Geometry {
    fn from(value: Composite) -> Self {
        Self::Composite(value)
    }
}

/// Point set geometry.
///
/// ### Attribute Locations
///
/// - [`Vertices`](crate::Location::Vertices) puts attribute values on the points.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct PointSet {
    /// Origin of the points relative to the project coordinate reference system.
    #[serde(default, skip_serializing_if = "zero_origin")]
    pub origin: Vector3,
    /// Array with `Vertex` type storing the vertex locations.
    ///
    /// Add `origin` and the [project](crate::Project) origin to get the locations relative
    /// to the project coordinate reference system.
    pub vertices: Array<array_type::Vertex>,
}

impl PointSet {
    pub fn new(vertices: Array<array_type::Vertex>) -> Self {
        Self::with_origin(vertices, Default::default())
    }

    pub fn with_origin(vertices: Array<array_type::Vertex>, origin: Vector3) -> Self {
        Self { origin, vertices }
    }

    pub fn location_len(&self, location: Location) -> Option<u64> {
        match location {
            Location::Vertices => Some(self.vertices.item_count()),
            _ => None,
        }
    }
}

/// A set of line segments.
///
/// ### Attribute Locations
///
/// - [`Vertices`](crate::Location::Vertices) puts attribute values on the vertices.
///
/// - [`Primitives`](crate::Location::Primitives) puts attribute values on the line segments.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct LineSet {
    /// Origin of the lines relative to the project coordinate reference system.
    #[serde(default, skip_serializing_if = "zero_origin")]
    pub origin: Vector3,
    /// Array with `Vertex` type storing the vertex locations.
    ///
    /// Add `origin` and the [project](crate::Project) origin to get the locations relative
    /// to the project coordinate reference system.
    pub vertices: Array<array_type::Vertex>,
    /// Array with `Segment` type storing each segment as a pair of indices into `vertices`.
    pub segments: Array<array_type::Segment>,
}

impl LineSet {
    pub fn new(vertices: Array<array_type::Vertex>, segments: Array<array_type::Segment>) -> Self {
        Self::with_origin(vertices, segments, Default::default())
    }

    pub fn with_origin(
        vertices: Array<array_type::Vertex>,
        segments: Array<array_type::Segment>,
        origin: Vector3,
    ) -> Self {
        Self {
            origin,
            vertices,
            segments,
        }
    }

    pub fn location_len(&self, location: Location) -> Option<u64> {
        match location {
            Location::Vertices => Some(self.vertices.item_count()),
            Location::Primitives => Some(self.segments.item_count()),
            _ => None,
        }
    }
}

/// A surface made up of triangles.
///
/// ### Attribute Locations
///
/// - [`Vertices`](crate::Location::Vertices) puts attribute values on the vertices.
///
/// - [`Primitives`](crate::Location::Primitives) puts attribute values on the triangles.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Surface {
    /// Origin of the surface relative to the project coordinate reference system.
    #[serde(default, skip_serializing_if = "zero_origin")]
    pub origin: Vector3,
    /// Array with `Vertex` type storing the vertex locations.
    ///
    /// Add `origin` and the [project](crate::Project) origin to get the locations relative
    /// to the project coordinate reference system.
    pub vertices: Array<array_type::Vertex>,
    /// Array with `Triangle` type storing each triangle as a triple of indices into `vertices`.
    /// Triangle winding should be counter-clockwise around an outward-pointing normal.
    pub triangles: Array<array_type::Triangle>,
}

impl Surface {
    pub fn new(
        vertices: Array<array_type::Vertex>,
        triangles: Array<array_type::Triangle>,
    ) -> Self {
        Self::with_origin(vertices, triangles, Default::default())
    }

    pub fn with_origin(
        vertices: Array<array_type::Vertex>,
        triangles: Array<array_type::Triangle>,
        origin: Vector3,
    ) -> Self {
        Self {
            origin,
            vertices,
            triangles,
        }
    }

    pub fn location_len(&self, location: Location) -> Option<u64> {
        match location {
            Location::Vertices => Some(self.vertices.item_count()),
            Location::Primitives => Some(self.triangles.item_count()),
            _ => None,
        }
    }
}

/// A surface defined by a 2D grid a height on each grid vertex.
///
/// ### Attribute Locations
///
/// - [`Vertices`](crate::Location::Vertices) puts attribute values on the grid vertices.
///
/// - [`Primitives`](crate::Location::Primitives) puts attribute values on the grid cells.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct GridSurface {
    /// Position and orientation of the surface.
    pub orient: Orient2,
    /// 2D grid definition, which can be regular or tensor.
    pub grid: Grid2,
    /// Array with `Scalar` type storing the offset of each grid vertex from the place.
    /// Heights may be positive or negative. Will be absent from flat 2D grids.
    pub heights: Option<Array<array_type::Scalar>>,
}

impl GridSurface {
    pub fn new(orient: Orient2, grid: Grid2, heights: Option<Array<array_type::Scalar>>) -> Self {
        Self {
            orient,
            grid,
            heights,
        }
    }

    pub fn location_len(&self, location: Location) -> Option<u64> {
        match location {
            Location::Vertices => Some(self.grid.flat_corner_count()),
            Location::Primitives => Some(self.grid.flat_count()),
            _ => None,
        }
    }
}

/// A container for sub-elements.
///
/// ### Attribute Locations
///
/// - [`Elements`](crate::Location::Elements) puts attribute values on elements.
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Composite {
    #[serde(default)]
    pub elements: Vec<Element>,
}

impl Composite {
    pub fn new(elements: Vec<Element>) -> Self {
        Self { elements }
    }

    pub fn location_len(&self, location: Location) -> Option<u64> {
        match location {
            Location::Elements => Some(self.elements.len().try_into().expect("usize fits in u64")),
            _ => None,
        }
    }
}

impl Validate for Geometry {
    fn validate_inner(&mut self, val: &mut Validator) {
        let v = val.enter("Geometry");
        match self {
            Self::PointSet(x) => v.obj(x),
            Self::LineSet(x) => v.obj(x),
            Self::Surface(x) => v.obj(x),
            Self::GridSurface(x) => v.obj(x),
            Self::BlockModel(x) => v.obj(x),
            Self::Composite(x) => v.obj(x),
        };
    }
}

impl Validate for PointSet {
    fn validate_inner(&mut self, val: &mut Validator) {
        val.enter("PointSet")
            .finite_seq(self.origin, "origin")
            .array(&mut self.vertices, Constraint::Vertex, "vertices");
    }
}

impl Validate for LineSet {
    fn validate_inner(&mut self, val: &mut Validator) {
        val.enter("LineSet")
            .finite_seq(self.origin, "origin")
            .array(&mut self.vertices, Constraint::Vertex, "vertices")
            .array(
                &mut self.segments,
                Constraint::Segment(self.vertices.item_count()),
                "segments",
            );
    }
}

impl Validate for Surface {
    fn validate_inner(&mut self, val: &mut Validator) {
        val.enter("Surface")
            .finite_seq(self.origin, "origin")
            .array(&mut self.vertices, Constraint::Vertex, "vertices")
            .array(
                &mut self.triangles,
                Constraint::Triangle(self.vertices.item_count()),
                "triangles",
            );
    }
}

impl Validate for GridSurface {
    fn validate_inner(&mut self, val: &mut Validator) {
        let mut val = val
            .enter("GridSurface")
            .obj(&mut self.orient)
            .obj(&mut self.grid)
            .array_opt(self.heights.as_mut(), Constraint::Scalar, "heights")
            .array_size_opt(
                self.heights.as_ref().map(|h| h.item_count()),
                self.grid.flat_corner_count(),
                "heights",
            );
        self.orient.validate_ortho(&mut val);
    }
}

impl Validate for Composite {
    fn validate_inner(&mut self, val: &mut Validator) {
        val.enter("Composite").objs(&mut self.elements).unique(
            self.elements.iter().map(|e| &e.name),
            "elements[..]::name",
            false,
        );
    }
}