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
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
use crate::*;

/// Describes the visual shape and appearance of an object in a scene.
#[derive(Clone, Debug)]
pub struct Geometry {
    /// A text string containing the unique identifier of the element.
    pub id: Option<String>,
    /// The text string name of this element.
    pub name: Option<String>,
    /// Asset management information about this element.
    pub asset: Option<Box<Asset>>,
    /// The element that describes geometric data.
    pub element: GeometryElement,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl Geometry {
    /// Construct a new `Geometry` given an `id` and the underlying element data.
    pub fn new(id: impl Into<String>, element: GeometryElement) -> Self {
        Self {
            id: Some(id.into()),
            name: None,
            asset: None,
            element,
            extra: vec![],
        }
    }

    /// Construct a new `Geometry` containing a [`Mesh`].
    pub fn new_mesh(
        id: impl Into<String>,
        sources: Vec<Source>,
        vertices: Vertices,
        elements: Vec<Primitive>,
    ) -> Self {
        Self::new(id, Mesh::new(sources, vertices, elements).into())
    }
}

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)?,
        })
    }
}

impl XNodeWrite for Geometry {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.opt_attr("id", &self.id);
        e.opt_attr("name", &self.name);
        let e = e.start(w)?;
        self.asset.write_to(w)?;
        self.element.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

impl CollectLocalMaps for Geometry {
    fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
        maps.insert(self);
        self.element.collect_local_maps(maps);
    }
}

/// Extra data associated to [`Instance`]<[`Geometry`]>.
#[derive(Clone, Debug, Default)]
pub struct InstanceGeometryData {
    /// Binds material symbols to material instances. This allows a
    /// single geometry to be instantiated into a scene multiple times
    /// each with a different appearance.
    pub bind_material: Option<BindMaterial>,
}

impl XNodeWrite for InstanceGeometryData {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        self.bind_material.write_to(w)
    }
}

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)?,
        })
    }
    fn is_empty(data: &Self::Data) -> bool {
        data.bind_material.is_none()
    }
}

impl Instance<Geometry> {
    /// Get the list of [`InstanceMaterial`]s bound in this [`Instance<Geometry>`].
    pub fn instance_materials(&self) -> &[InstanceMaterial] {
        match self.data.bind_material {
            Some(ref m) => &m.instance_material,
            None => &[],
        }
    }

    /// Look up an [`InstanceMaterial`] by symbol name.
    pub fn get_instance_material(&self, symbol: &str) -> Option<&InstanceMaterial> {
        let bm = self.data.bind_material.as_ref()?;
        bm.instance_material.iter().find(|mat| mat.symbol == symbol)
    }
}

/// An element that describes geometric data.
#[derive(Clone, Debug)]
pub enum GeometryElement {
    /// The parameter is a URI string of a `Geometry`, and this element
    /// describes the convex hull of the specified mesh.
    ConvexHullOf(Url),
    /// A mesh or convex mesh.
    Mesh(Mesh),
    /// A multisegment spline.
    Spline(Spline),
}

impl From<Spline> for GeometryElement {
    fn from(v: Spline) -> Self {
        Self::Spline(v)
    }
}

impl From<Mesh> for GeometryElement {
    fn from(v: Mesh) -> Self {
        Self::Mesh(v)
    }
}

impl CollectLocalMaps for GeometryElement {
    fn collect_local_maps<'a>(&'a self, maps: &mut LocalMaps<'a>) {
        match self {
            GeometryElement::ConvexHullOf(_) => {}
            GeometryElement::Mesh(mesh) => mesh.sources.collect_local_maps(maps),
            GeometryElement::Spline(spline) => spline.sources.collect_local_maps(maps),
        }
    }
}

impl GeometryElement {
    /// Parse a [`GeometryElement`] from an XML element.
    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),
        }))
    }

    /// The `sources` field, which gives the list of [`Source`] elements on this element.
    pub fn sources(&self) -> &[Source] {
        match self {
            GeometryElement::ConvexHullOf(_) => &[],
            GeometryElement::Mesh(mesh) => &mesh.sources,
            GeometryElement::Spline(spline) => &spline.sources,
        }
    }

    /// Returns this element if it is a `<mesh>`.
    pub fn as_mesh(&self) -> Option<&Mesh> {
        match self {
            GeometryElement::Mesh(mesh) if !mesh.convex => Some(mesh),
            _ => None,
        }
    }
}

impl XNodeWrite for GeometryElement {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        match self {
            GeometryElement::ConvexHullOf(s) => {
                let mut e = ElemBuilder::new(Mesh::CONVEX);
                e.print_attr("convex_hull_of", s);
                e.end(w)
            }
            GeometryElement::Mesh(e) => e.write_to(w),
            GeometryElement::Spline(e) => e.write_to(w),
        }
    }
}

/// Describes basic geometric meshes using vertex and primitive information.
#[derive(Clone, Debug)]
pub struct Mesh {
    /// If true, this is a `<convex_mesh>` element.
    /// Both elements have the same structure otherwise.
    pub convex: bool,
    /// Provides the bulk of the mesh’s vertex data.
    pub sources: Vec<Source>,
    /// Describes the mesh-vertex attributes and establishes their topological identity.
    pub vertices: Option<Vertices>,
    /// Geometric primitives, which assemble values from the
    /// inputs into vertex attribute data.
    pub elements: Vec<Primitive>,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl Mesh {
    /// Construct a new `Mesh` from vertices and elements.
    pub fn new(sources: Vec<Source>, vertices: Vertices, elements: Vec<Primitive>) -> Self {
        assert!(!sources.is_empty());
        Self {
            convex: false,
            sources,
            vertices: Some(vertices),
            elements,
            extra: vec![],
        }
    }

    /// Construct a new convex `Mesh` from vertices and elements.
    pub fn new_convex(sources: Vec<Source>, vertices: Vertices, elements: Vec<Primitive>) -> Self {
        assert!(!sources.is_empty());
        Self {
            convex: true,
            sources,
            vertices: Some(vertices),
            elements,
            extra: vec![],
        }
    }

    /// The name of the XML node: `convex_mesh`
    pub const CONVEX: &'static str = "convex_mesh";

    /// Parse a `<convex_mesh>` XML element.
    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)?))
    }

    /// Parse a [`Mesh`] from an XML element of type `<convex_mesh>` or `<mesh>`.
    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)?,
        })
    }

    fn write_inner<W: Write>(&self, e: ElemBuilder, w: &mut XWriter<W>) -> Result<()> {
        let e = e.start(w)?;
        self.sources.write_to(w)?;
        self.vertices.write_to(w)?;
        self.elements.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

impl XNode for Mesh {
    const NAME: &'static str = "mesh";
    fn parse(element: &Element) -> Result<Self> {
        Self::parse(false, element)
    }
}

impl XNodeWrite for Mesh {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        self.write_inner(Self::elem(), w)
    }
}

/// Declares the attributes and identity of mesh-vertices.
#[derive(Clone, Debug)]
pub struct Vertices {
    /// A text string containing the unique identifier of the element.
    /// This value must be unique within the document.
    pub id: String,
    /// The text string name of this element.
    pub name: Option<String>,
    /// The list of inputs.
    pub inputs: Vec<Input>,
    /// The index into `inputs` for the [`Semantic::Position`] input (which must exist).
    pub position: usize,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

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 XNodeWrite for Vertices {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.attr("id", &self.id);
        e.opt_attr("name", &self.name);
        let e = e.start(w)?;
        self.inputs.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

impl Traversable for Vertices {
    fn traverse<'a, E>(
        doc: &'a Document,
        mut f: impl FnMut(&'a Self) -> Result<(), E>,
    ) -> Result<(), E>
    where
        Self: 'a,
    {
        for lib in doc.iter::<Geometry>() {
            if let GeometryElement::Mesh(Mesh {
                vertices: Some(v), ..
            }) = &lib.element
            {
                f(v)?
            }
        }
        Ok(())
    }
}

impl Vertices {
    /// Construct a new `Vertices` object with the given inputs.
    /// * One of the inputs must have [`Semantic::Position`].
    pub fn new(id: impl Into<String>, inputs: Vec<Input>) -> Self {
        Self {
            id: id.into(),
            name: None,
            position: inputs
                .iter()
                .position(|i| i.semantic == Semantic::Position)
                .expect("vertices: missing POSITION input"),
            inputs,
            extra: vec![],
        }
    }

    /// The input with [`Semantic::Position`].
    pub fn position_input(&self) -> &Input {
        &self.inputs[self.position]
    }
}

/// The common data for the geometry types:
///
/// * [`Lines`]` = Geom<`[`LineGeom`]`>`
/// * [`LineStrips`]` = Geom<`[`LineStripGeom`]`>`
/// * [`Polygons`]` = Geom<`[`PolygonGeom`]`>`
/// * [`PolyList`]` = Geom<`[`PolyListGeom`]`>`
/// * [`Triangles`]` = Geom<`[`TriangleGeom`]`>`
/// * [`TriFans`]` = Geom<`[`TriFanGeom`]`>`
/// * [`TriStrips`]` = Geom<`[`TriStripGeom`]`>`
#[derive(Clone, Default, Debug)]
pub struct Geom<T> {
    /// The text string name of this element.
    pub name: Option<String>,
    /// Declares a symbol for a material.
    /// This symbol is bound to a material at the time of instantiation;
    /// see [`Instance<Geometry>`] and [`BindMaterial`].
    /// If not specified then the lighting and shading results are application defined.
    pub material: Option<String>,
    /// The number of line/triangle/polygon primitives.
    pub count: usize,
    /// The vertex attribute access information.
    pub inputs: InputList,
    /// The specific data for the geometry element.
    pub data: T,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl<T: ParseGeom> Geom<T> {
    fn new_geom(material: Option<String>, inputs: InputList, count: usize, data: T) -> Self {
        Self {
            name: None,
            material,
            count,
            inputs,
            data,
            extra: vec![],
        }
    }
}

pub(crate) use private::ParseGeom;
pub(crate) mod private {
    use super::*;
    /// The trait for types that can appear in a [`Geom<T>`].
    pub trait ParseGeom: XNodeWrite + Default {
        /// The name of the element for the enclosing `Geom`, for example
        /// `"lines"` for [`LineGeom`].
        const NAME: &'static str;

        /// Parse the data from an element iterator.
        fn parse(it: &mut ElementIter<'_>) -> Result<Self>;

        /// Perform custom validation on the resulting [`Geom<T>`] before yielding it.
        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)
    }
}

impl<T: ParseGeom> XNodeWrite for Geom<T> {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.opt_attr("name", &self.name);
        e.opt_attr("material", &self.material);
        e.print_attr("count", &self.count);
        let e = e.start(w)?;
        self.inputs.write_to(w)?;
        self.data.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

macro_rules! mk_primitive {
    ($($(#[$doc:meta])* $name:ident($as:ident),)*) => {
        /// A collection of primitive elements.
        #[derive(Clone, Debug)]
        pub enum Primitive {
            $($(#[$doc])* $name($name),)*
        }

        $(
            impl From<$name> for Primitive {
                fn from(val: $name) -> Self {
                    Self::$name(val)
                }
            }
        )*

        impl Primitive {
            /// Parse a [`Primitive`] from an XML element.
            pub fn parse(e: &Element) -> Result<Option<Self>> {
                Ok(Some(match e.name() {
                    $($name::NAME => Self::$name(Geom::parse(e)?),)*
                    _ => return Ok(None),
                }))
            }

            $(
                /// An accessor for the variant.
                pub fn $as(&self) -> Option<&$name> {
                    if let Self::$name(x) = self {
                        Some(x)
                    } else {
                        None
                    }
                }
            )*
        }

        impl XNodeWrite for Primitive {
            fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
                match self {
                    $(Self::$name(e) => e.write_to(w),)*
                }
            }
        }
    }
}

mk_primitive! {
    /// Line primitives.
    Lines(as_lines),
    /// Line-strip primitives.
    LineStrips(as_line_strips),
    /// Polygon primitives which may contain holes.
    Polygons(as_polygons),
    /// Polygon primitives that cannot contain holes.
    PolyList(as_polylist),
    /// Triangle primitives.
    Triangles(as_triangles),
    /// Triangle-fan primitives.
    TriFans(as_trifans),
    /// Triangle-strip primitives.
    TriStrips(as_tristrips),
}

/// The data for a [`Lines`] element.
///
/// Each line described by the mesh has two vertices.
/// The first line is formed from the first and second vertices.
/// The second line is formed from the third and fourth vertices, and so on.
#[derive(Clone, Default, Debug)]
pub struct LineGeom {
    /// Contains indices that describe the vertex attributes for an
    /// arbitrary number of individual lines.
    pub prim: Option<Box<[u32]>>,
}

/// Provides the information needed for a mesh to bind vertex attributes
/// together and then organize those vertices into individual lines.
pub type Lines = Geom<LineGeom>;

impl Lines {
    /// Construct a new `Lines` object from a data buffer.
    /// The data buffer `prim` contains exactly `count` lines, consisting of 2 vertices,
    /// each consisting of `inputs.len()` indices,
    /// for a total of `inputs.len() * 2 * count` values.
    pub fn new(
        material: Option<String>,
        inputs: Vec<InputS>,
        count: usize,
        prim: Box<[u32]>,
    ) -> Self {
        let inputs = InputList::new(inputs);
        assert!(inputs.len() * 2 * count == prim.len());
        Self::new_geom(material, inputs, count, LineGeom { prim: Some(prim) })
    }
}

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.stride * 2 * res.count != data.len() {
                return Err("line count does not match <p> field".into());
            }
        }
        Ok(())
    }
}

impl XNodeWrite for LineGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        opt(&self.prim, |e| ElemBuilder::print_arr("p", e, w))
    }
}

/// The data for a [`LineStrips`] element.
///
/// Each line-strip described by the mesh has an arbitrary number of vertices.
/// Each line segment within the line-strip is formed from the current vertex and the preceding vertex.
#[derive(Clone, Default, Debug)]
pub struct LineStripGeom {
    /// Contains indices that describe the vertex attributes for an
    /// arbitrary number of connected line segments.
    pub prim: Vec<Box<[u32]>>,
}

/// Provides the information needed to bind vertex attributes together and then organize those vertices into
/// connected line-strips.
pub type LineStrips = Geom<LineStripGeom>;

impl LineStrips {
    /// Construct a new `LineStrips` object from a data buffer.
    /// Each buffer in `data` contains 2 or more vertices,
    /// each consisting of `inputs.len()` indices,
    /// for a total of `inputs.len() * verts` values.
    pub fn new(material: Option<String>, inputs: Vec<InputS>, prim: Vec<Box<[u32]>>) -> Self {
        let inputs = InputList::new(inputs);
        debug_assert!(prim.iter().all(|p| inputs.check_prim::<2>(p)));
        Self::new_geom(material, inputs, prim.len(), LineStripGeom { prim })
    }
}

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(())
    }
}

impl XNodeWrite for LineStripGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        self.prim
            .iter()
            .try_for_each(|e| ElemBuilder::print_arr("p", e, w))
    }
}

/// The data for an individual polygon-with-hole.
#[derive(Clone, Debug)]
pub struct PolygonHole {
    /// The vertex data for the polygon.
    pub verts: Box<[u32]>,
    /// A list of 0 or more holes, each of which describes a polygonal hole
    /// in the main polygon.
    pub hole: Vec<Box<[u32]>>,
}

impl PolygonHole {
    /// Construct a new `PolygonHole` from a list of vertices and 0 or more holes.
    pub fn new(verts: Box<[u32]>, hole: Vec<Box<[u32]>>) -> Self {
        Self { verts, hole }
    }
}

impl XNodeWrite for PolygonHole {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let e = ElemBuilder::new("ph").start(w)?;
        ElemBuilder::print_arr("p", &self.verts, w)?;
        many(&self.hole, |h| ElemBuilder::print_arr("h", h, w))?;
        e.end(w)
    }
}

/// The data for a [`Polygons`] element.
#[derive(Clone, Default, Debug)]
pub struct PolygonGeom(
    /// The list of polygons, each of which may contain 0 or more holes.
    pub Vec<PolygonHole>,
);

/// Provides the information needed for a mesh to bind vertex attributes
/// together and then organize those vertices into individual polygons.
pub type Polygons = Geom<PolygonGeom>;

impl Polygons {
    /// Construct a new `Polygons` object from raw data.
    pub fn new(material: Option<String>, inputs: Vec<InputS>, prim: Vec<PolygonHole>) -> Self {
        let inputs = InputList::new(inputs);
        debug_assert!(prim.iter().all(|ph| {
            inputs.check_prim::<3>(&ph.verts) && ph.hole.iter().all(|h| inputs.check_prim::<3>(h))
        }));
        Self::new_geom(material, inputs, prim.len(), PolygonGeom(prim))
    }
}

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(())
    }
}

impl XNodeWrite for PolygonGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        self.0.write_to(w)
    }
}

/// The data for a [`PolyList`] element.
#[derive(Clone, Default, Debug)]
pub struct PolyListGeom {
    /// Contains a list of integers, each specifying the number of
    /// vertices for one polygon described by the [`PolyList`] element.
    pub vcount: Box<[u32]>,
    /// Contains a list of integers that specify the vertex attributes
    /// (indices) for an individual polylist.
    /// The winding order of vertices produced is counter-clockwise
    /// and describes the front side of each polygon.
    pub prim: Box<[u32]>,
}

/// Provides the information needed for a mesh to bind vertex attributes
/// together and then organize those vertices into individual polygons.
pub type PolyList = Geom<PolyListGeom>;

impl PolyList {
    /// Construct a new `PolyList` object from a data buffer.
    /// Each value `n` in `vcount` corresponds to a polygon with `n` vertices,
    /// to which `inputs.len() * n` index values are associated in `prim`.
    pub fn new(
        material: Option<String>,
        inputs: Vec<InputS>,
        vcount: Box<[u32]>,
        prim: Box<[u32]>,
    ) -> Self {
        let inputs = InputList::new(inputs);
        debug_assert!(inputs.len() * vcount.iter().sum::<u32>() as usize == prim.len());
        Self::new_geom(
            material,
            inputs,
            vcount.len(),
            PolyListGeom { vcount, prim },
        )
    }
}

pub(crate) fn validate_vcount<T>(
    count: usize,
    stride: usize,
    vcount: &[u32],
    prim: &[T],
) -> Result<()> {
    if count != vcount.len() {
        return Err("count does not match <vcount> field".into());
    }
    if stride * vcount.iter().sum::<u32>() as usize != prim.len() {
        return Err("vcount does not match <p>/<v> field".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)?.unwrap_or_default(),
            prim: parse_opt("p", it, parse_array)?.unwrap_or_default(),
        })
    }

    fn validate(res: &Geom<Self>) -> Result<()> {
        validate_vcount(
            res.count,
            res.inputs.stride,
            &res.data.vcount,
            &res.data.prim,
        )
    }
}

impl XNodeWrite for PolyListGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        if !self.vcount.is_empty() {
            ElemBuilder::print_arr("vcount", &self.vcount, w)?;
        }
        if !self.prim.is_empty() {
            ElemBuilder::print_arr("p", &self.prim, w)?;
        }
        Ok(())
    }
}

/// The data for a [`Triangles`] element.
///
/// Each triangle described by the mesh has three vertices.
/// The first triangle is formed from the first, second, and third vertices.
/// The second triangle is formed from the fourth, fifth, and sixth vertices, and so on.
#[derive(Clone, Default, Debug)]
pub struct TriangleGeom {
    /// Contains indices that describe the vertex attributes for a number of triangles.
    /// The indices reference into the parent’s [`Source`] elements that are
    /// referenced by the [`InputS`] elements.
    pub prim: Option<Box<[u32]>>,
}

/// Provides the information needed for a mesh to bind vertex attributes
/// together and then organize those vertices into individual triangles.
pub type Triangles = Geom<TriangleGeom>;

impl Triangles {
    /// Construct a new `Triangles` object from a data buffer.
    /// The data buffer `prim` contains exactly `count` triangles, consisting of 3 vertices,
    /// each consisting of `inputs.len()` indices,
    /// for a total of `inputs.len() * 3 * count` values.
    pub fn new(
        material: Option<String>,
        inputs: Vec<InputS>,
        count: usize,
        prim: Box<[u32]>,
    ) -> Self {
        let inputs = InputList::new(inputs);
        assert!(inputs.len() * 3 * count == prim.len());
        Self::new_geom(material, inputs, count, TriangleGeom { prim: Some(prim) })
    }
}

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.stride * 3 * res.count != data.len() {
                return Err("triangle count does not match <p> field".into());
            }
        }
        Ok(())
    }
}

impl XNodeWrite for TriangleGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        opt(&self.prim, |e| ElemBuilder::print_arr("p", e, w))
    }
}

/// The data for a [`TriFans`] element.
///
/// Each triangle described by the mesh has three vertices.
/// The first triangle is formed from the first, second, and third vertices.
/// Each subsequent triangle is formed from the current vertex,
/// reusing the first and the previous vertices.
#[derive(Clone, Default, Debug)]
pub struct TriFanGeom {
    /// Contains indices that describe the vertex attributes for an
    /// arbitrary number of connected triangles.
    /// The indices reference into the parent’s [`Source`] elements that are
    /// referenced by the [`InputS`] elements.
    pub prim: Vec<Box<[u32]>>,
}

/// Provides the information needed for a mesh to bind vertex attributes
/// together and then organize those vertices into connected triangles.
pub type TriFans = Geom<TriFanGeom>;

impl TriFans {
    /// Construct a new `TriFans` object from a data buffer.
    /// Each buffer in `data` contains 3 or more vertices,
    /// each consisting of `inputs.len()` indices,
    /// for a total of `inputs.len() * verts` values.
    pub fn new(material: Option<String>, inputs: Vec<InputS>, prim: Vec<Box<[u32]>>) -> Self {
        let inputs = InputList::new(inputs);
        debug_assert!(prim.iter().all(|p| inputs.check_prim::<3>(p)));
        Self::new_geom(material, inputs, prim.len(), TriFanGeom { prim })
    }
}

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(())
    }
}

impl XNodeWrite for TriFanGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        many(&self.prim, |e| ElemBuilder::print_arr("p", e, w))
    }
}

/// The data for a [`TriStrips`] element.
///
/// Each triangle described by the mesh has three vertices.
/// The first triangle is formed from the first, second, and third vertices.
/// Each subsequent triangle is formed from the current vertex,
/// reusing the previous two vertices.
#[derive(Clone, Default, Debug)]
pub struct TriStripGeom {
    /// Contains indices that describe the vertex attributes for an
    /// arbitrary number of connected triangles.
    /// The indices reference into the parent’s [`Source`] elements that are
    /// referenced by the [`InputS`] elements.
    pub prim: Vec<Box<[u32]>>,
}

/// Provides the information needed for a mesh to bind vertex attributes
/// together and then organize those vertices into connected triangles.
pub type TriStrips = Geom<TriStripGeom>;

impl TriStrips {
    /// Construct a new `TriStrips` object from a data buffer.
    /// Each buffer in `data` contains 3 or more vertices,
    /// each consisting of `inputs.len()` indices,
    /// for a total of `inputs.len() * verts` values.
    pub fn new(material: Option<String>, inputs: Vec<InputS>, prim: Vec<Box<[u32]>>) -> Self {
        let inputs = InputList::new(inputs);
        debug_assert!(prim.iter().all(|p| inputs.check_prim::<3>(p)));
        Self::new_geom(material, inputs, prim.len(), TriStripGeom { prim })
    }
}

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(())
    }
}

impl XNodeWrite for TriStripGeom {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        many(&self.prim, |e| ElemBuilder::print_arr("p", e, w))
    }
}

/// Describes a multisegment spline with control vertex (CV) and segment information.
#[derive(Clone, Debug)]
pub struct Spline {
    /// Whether there is a segment connecting the first and last control vertices.
    /// The default is "false", indicating that the spline is open.
    pub closed: bool,
    /// Provides the values for the CVs and segments of the spline.
    pub sources: Vec<Source>,
    /// Describes the CVs of the spline.
    pub controls: ControlVertices,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl Spline {
    /// Construct a new `Spline` object with the given inputs.
    pub fn new(sources: Vec<Source>, controls: Vec<Input>) -> Self {
        Self {
            closed: false,
            sources,
            controls: ControlVertices::new(controls),
            extra: vec![],
        }
    }
}

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)?,
        })
    }
}

impl XNodeWrite for Spline {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let mut e = Self::elem();
        e.def_print_attr("closed", self.closed, false);
        let e = e.start(w)?;
        self.sources.write_to(w)?;
        self.controls.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

/// Describes the control vertices (CVs) of a spline.
#[derive(Clone, Debug)]
pub struct ControlVertices {
    /// The list of inputs.
    pub inputs: Vec<Input>,
    /// The index into `inputs` for the [`Semantic::Position`] input (which must exist).
    pub position: usize,
    /// Provides arbitrary additional information about this element.
    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 XNodeWrite for ControlVertices {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let e = Self::elem().start(w)?;
        self.inputs.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

impl ControlVertices {
    /// Construct a new `Vertices` object with the given inputs.
    /// * One of the inputs must have [`Semantic::Position`].
    pub fn new(inputs: Vec<Input>) -> Self {
        Self {
            position: inputs
                .iter()
                .position(|i| i.semantic == Semantic::Position)
                .expect("control_vertices: missing POSITION input"),
            inputs,
            extra: vec![],
        }
    }

    /// The input with [`Semantic::Position`].
    pub fn position_input(&self) -> &Input {
        &self.inputs[self.position]
    }
}