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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use crate::*;
#[derive(Clone, Debug)]
pub struct Geometry {
pub id: Option<String>,
pub name: Option<String>,
pub asset: Option<Box<Asset>>,
pub element: GeometryElement,
pub extra: Vec<Extra>,
}
impl HasId for Geometry {
fn id(&self) -> Option<&str> {
self.id.as_deref()
}
}
impl XNode for Geometry {
const NAME: &'static str = "geometry";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(Geometry {
id: element.attr("id").map(Into::into),
name: element.attr("name").map(Into::into),
asset: Asset::parse_opt_box(&mut it)?,
element: parse_one_many(&mut it, GeometryElement::parse)?,
extra: Extra::parse_many(it)?,
})
}
}
#[derive(Clone, Debug)]
pub struct InstanceGeometryData {
pub bind_material: Option<BindMaterial>,
}
impl Instantiate for Geometry {
const INSTANCE: &'static str = "instance_geometry";
type Data = InstanceGeometryData;
fn parse_data(_: &Element, it: &mut ElementIter<'_>) -> Result<Self::Data> {
Ok(InstanceGeometryData {
bind_material: BindMaterial::parse_opt(it)?,
})
}
}
#[derive(Clone, Debug)]
pub enum GeometryElement {
ConvexHullOf(Url),
Mesh(Mesh),
Spline(Spline),
}
impl GeometryElement {
pub fn parse(element: &Element) -> Result<Option<Self>> {
Ok(Some(match element.name() {
Mesh::CONVEX => Mesh::parse_convex(element)?,
Mesh::NAME => GeometryElement::Mesh(Mesh::parse(false, element)?),
Spline::NAME => GeometryElement::Spline(Spline::parse(element)?),
_ => return Ok(None),
}))
}
pub fn sources(&self) -> &[Source] {
match self {
GeometryElement::ConvexHullOf(_) => &[],
GeometryElement::Mesh(mesh) => &mesh.sources,
GeometryElement::Spline(spline) => &spline.sources,
}
}
}
#[derive(Clone, Debug)]
pub struct Mesh {
pub convex: bool,
pub sources: Vec<Source>,
pub vertices: Option<Vertices>,
pub elements: Vec<Primitive>,
pub extra: Vec<Extra>,
}
impl Mesh {
pub const CONVEX: &'static str = "convex_mesh";
pub fn parse_convex(element: &Element) -> Result<GeometryElement> {
debug_assert_eq!(element.name(), Self::CONVEX);
if let Some(s) = parse_attr(element.attr("convex_hull_of"))? {
return Ok(GeometryElement::ConvexHullOf(s));
}
Ok(GeometryElement::Mesh(Mesh::parse(true, element)?))
}
pub fn parse(convex: bool, element: &Element) -> Result<Self> {
debug_assert_eq!(
element.name(),
if convex { Self::CONVEX } else { Self::NAME }
);
let mut it = element.children().peekable();
Ok(Mesh {
convex,
sources: Source::parse_list_n::<1>(&mut it)?,
vertices: Vertices::parse_opt(&mut it)?,
elements: parse_list_many(&mut it, Primitive::parse)?,
extra: Extra::parse_many(it)?,
})
}
}
impl XNode for Mesh {
const NAME: &'static str = "mesh";
fn parse(element: &Element) -> Result<Self> {
Self::parse(false, element)
}
}
#[derive(Clone, Debug)]
pub struct Vertices {
pub id: String,
pub name: Option<String>,
pub inputs: Vec<Input>,
pub position: usize,
pub extra: Vec<Extra>,
}
impl HasId for Vertices {
fn id(&self) -> Option<&str> {
Some(&self.id)
}
}
impl XNode for Vertices {
const NAME: &'static str = "vertices";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let inputs = Input::parse_list(&mut it)?;
Ok(Vertices {
id: element.attr("id").ok_or("missing 'id' attr")?.into(),
name: element.attr("name").map(Into::into),
position: inputs
.iter()
.position(|i| i.semantic == Semantic::Position)
.ok_or("vertices: missing POSITION input")?,
inputs,
extra: Extra::parse_many(it)?,
})
}
}
impl Vertices {
pub fn position_input(&self) -> &Input {
&self.inputs[self.position]
}
}
#[derive(Clone, Default, Debug)]
pub struct Geom<T> {
pub name: Option<String>,
pub material: Option<String>,
pub count: usize,
pub inputs: InputList,
pub data: T,
pub extra: Vec<Extra>,
}
pub trait ParseGeom: Default {
const NAME: &'static str;
fn parse(it: &mut ElementIter<'_>) -> Result<Self>;
fn validate(_: &Geom<Self>) -> Result<()>;
}
impl<T: ParseGeom> XNode for Geom<T> {
const NAME: &'static str = T::NAME;
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let res = Geom {
name: element.attr("name").map(Into::into),
material: element.attr("material").map(Into::into),
count: parse_attr(element.attr("count"))?.ok_or("expected 'count' attr")?,
inputs: InputList::parse::<0>(&mut it)?,
data: T::parse(&mut it)?,
extra: Extra::parse_many(it)?,
};
T::validate(&res)?;
Ok(res)
}
}
#[derive(Clone, Debug)]
pub enum Primitive {
Lines(Lines),
LineStrips(LineStrips),
Polygons(Polygons),
PolyList(PolyList),
Triangles(Triangles),
TriFans(TriFans),
TriStrips(TriStrips),
}
impl Primitive {
pub fn parse(e: &Element) -> Result<Option<Self>> {
Ok(Some(match e.name() {
LineGeom::NAME => Primitive::Lines(Geom::parse(e)?),
LineStripGeom::NAME => Primitive::LineStrips(Geom::parse(e)?),
PolygonGeom::NAME => Primitive::Polygons(Geom::parse(e)?),
PolyListGeom::NAME => Primitive::PolyList(Geom::parse(e)?),
TriangleGeom::NAME => Primitive::Triangles(Geom::parse(e)?),
TriFanGeom::NAME => Primitive::TriFans(Geom::parse(e)?),
TriStripGeom::NAME => Primitive::TriStrips(Geom::parse(e)?),
_ => return Ok(None),
}))
}
}
#[derive(Clone, Default, Debug)]
pub struct LineGeom {
pub prim: Option<Box<[u32]>>,
}
pub type Lines = Geom<LineGeom>;
impl Deref for LineGeom {
type Target = Option<Box<[u32]>>;
fn deref(&self) -> &Self::Target {
&self.prim
}
}
impl ParseGeom for LineGeom {
const NAME: &'static str = "lines";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
Ok(LineGeom {
prim: parse_opt("p", it, parse_array)?,
})
}
fn validate(res: &Geom<Self>) -> Result<()> {
if let Some(ref data) = *res.data {
if res.inputs.depth * 2 * res.count != data.len() {
return Err("line count does not match <p> field".into());
}
}
Ok(())
}
}
#[derive(Clone, Default, Debug)]
pub struct LineStripGeom {
pub prim: Vec<Box<[u32]>>,
}
pub type LineStrips = Geom<LineStripGeom>;
impl Deref for LineStripGeom {
type Target = Vec<Box<[u32]>>;
fn deref(&self) -> &Self::Target {
&self.prim
}
}
impl ParseGeom for LineStripGeom {
const NAME: &'static str = "line_strips";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
Ok(LineStripGeom {
prim: parse_list("p", it, parse_array)?,
})
}
fn validate(res: &Geom<Self>) -> Result<()> {
if res.count != res.data.len() {
return Err("line strip count does not match <p> fields".into());
}
if !res.data.iter().all(|p| res.inputs.check_prim::<2>(p)) {
return Err("incorrect <p> field in line strips".into());
}
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct PolygonHole {
pub verts: Box<[u32]>,
pub hole: Vec<Box<[u32]>>,
}
#[derive(Clone, Default, Debug)]
pub struct PolygonGeom(
pub Vec<PolygonHole>,
);
pub type Polygons = Geom<PolygonGeom>;
impl Deref for PolygonGeom {
type Target = Vec<PolygonHole>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ParseGeom for PolygonGeom {
const NAME: &'static str = "polygon";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
let mut polys = parse_list("p", it, |e| {
Ok(PolygonHole {
verts: parse_array(e)?,
hole: vec![],
})
})?;
let more_polys = parse_list("ph", it, |e| {
let mut it = e.children().peekable();
let verts = parse_one("p", &mut it, parse_array)?;
let hole = parse_list("h", &mut it, parse_array)?;
if hole.is_empty() {
return Err(
"<ph> element can only be used when at least one hole is present".into(),
);
}
finish(PolygonHole { verts, hole }, it)
})?;
polys.extend(more_polys);
Ok(PolygonGeom(polys))
}
fn validate(res: &Geom<Self>) -> Result<()> {
if res.count != res.data.len() {
return Err("polygon count does not match <p> fields".into());
}
if !res.data.iter().all(|ph| {
res.inputs.check_prim::<3>(&ph.verts)
&& ph.hole.iter().all(|h| res.inputs.check_prim::<3>(h))
}) {
return Err("incorrect <p> field in polygon".into());
}
Ok(())
}
}
#[derive(Clone, Default, Debug)]
pub struct PolyListGeom {
pub vcount: Option<Box<[u32]>>,
pub prim: Option<Box<[u32]>>,
}
pub type PolyList = Geom<PolyListGeom>;
pub(crate) fn validate_vcount<T>(
count: usize,
depth: usize,
vcount: Option<&[u32]>,
prim: Option<&[T]>,
) -> Result<()> {
match (vcount, prim) {
(None, None) => {}
(Some(vcount), Some(data)) => {
if count != vcount.len() {
return Err("count does not match <vcount> field".into());
}
if depth * vcount.iter().sum::<u32>() as usize != data.len() {
return Err("vcount does not match <p>/<v> field".into());
}
}
_ => return Err("<vcount> and <p>/<v> should be provided together".into()),
}
Ok(())
}
impl ParseGeom for PolyListGeom {
const NAME: &'static str = "polylist";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
Ok(PolyListGeom {
vcount: parse_opt("vcount", it, parse_array)?,
prim: parse_opt("p", it, parse_array)?,
})
}
fn validate(res: &Geom<Self>) -> Result<()> {
validate_vcount(
res.count,
res.inputs.depth,
res.data.vcount.as_deref(),
res.data.prim.as_deref(),
)
}
}
#[derive(Clone, Default, Debug)]
pub struct TriangleGeom {
pub prim: Option<Box<[u32]>>,
}
pub type Triangles = Geom<TriangleGeom>;
impl Deref for TriangleGeom {
type Target = Option<Box<[u32]>>;
fn deref(&self) -> &Self::Target {
&self.prim
}
}
impl ParseGeom for TriangleGeom {
const NAME: &'static str = "triangles";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
Ok(TriangleGeom {
prim: parse_opt("p", it, parse_array)?,
})
}
fn validate(res: &Geom<Self>) -> Result<()> {
if let Some(ref data) = *res.data {
if res.inputs.depth * 3 * res.count != data.len() {
return Err("triangle count does not match <p> field".into());
}
}
Ok(())
}
}
#[derive(Clone, Default, Debug)]
pub struct TriFanGeom {
pub prim: Vec<Box<[u32]>>,
}
pub type TriFans = Geom<TriFanGeom>;
impl Deref for TriFanGeom {
type Target = Vec<Box<[u32]>>;
fn deref(&self) -> &Self::Target {
&self.prim
}
}
impl ParseGeom for TriFanGeom {
const NAME: &'static str = "trifans";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
Ok(TriFanGeom {
prim: parse_list("p", it, parse_array)?,
})
}
fn validate(res: &Geom<Self>) -> Result<()> {
if res.count != res.data.len() {
return Err("triangle fan count does not match <p> fields".into());
}
if !res.data.iter().all(|p| res.inputs.check_prim::<3>(p)) {
return Err("incorrect <p> field in triangle fans".into());
}
Ok(())
}
}
#[derive(Clone, Default, Debug)]
pub struct TriStripGeom {
pub prim: Vec<Box<[u32]>>,
}
pub type TriStrips = Geom<TriStripGeom>;
impl Deref for TriStripGeom {
type Target = Vec<Box<[u32]>>;
fn deref(&self) -> &Self::Target {
&self.prim
}
}
impl ParseGeom for TriStripGeom {
const NAME: &'static str = "tristrips";
fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
Ok(TriStripGeom {
prim: parse_list("p", it, parse_array)?,
})
}
fn validate(res: &Geom<Self>) -> Result<()> {
if res.count != res.data.len() {
return Err("triangle strip count does not match <p> fields".into());
}
if !res.data.iter().all(|p| res.inputs.check_prim::<3>(p)) {
return Err("incorrect <p> field in triangle strips".into());
}
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct Spline {
pub closed: bool,
pub sources: Vec<Source>,
pub controls: ControlVertices,
pub extra: Vec<Extra>,
}
impl XNode for Spline {
const NAME: &'static str = "spline";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
Ok(Spline {
closed: parse_attr(element.attr("closed"))?.unwrap_or(false),
sources: Source::parse_list_n::<1>(&mut it)?,
controls: ControlVertices::parse_one(&mut it)?,
extra: Extra::parse_many(it)?,
})
}
}
#[derive(Clone, Debug)]
pub struct ControlVertices {
pub inputs: Vec<Input>,
pub position: usize,
pub extra: Vec<Extra>,
}
impl XNode for ControlVertices {
const NAME: &'static str = "control_vertices";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let inputs = Input::parse_list(&mut it)?;
Ok(ControlVertices {
position: inputs
.iter()
.position(|i| i.semantic == Semantic::Position)
.ok_or("control_vertices: missing POSITION input")?,
inputs,
extra: Extra::parse_many(it)?,
})
}
}
impl ControlVertices {
pub fn position_input(&self) -> &Input {
&self.inputs[self.position]
}
}