xodr 0.8.0

Pure-Rust OpenDRIVE (.xodr) importer: bakes a road network into polylines, a lane graph, and a surface mesh.
Documentation
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
//! Signals, placed once every road is baked.

use std::collections::HashMap;
use std::f64::consts::PI;

use super::{
    attr_f32, attr_f64, child, orientation, validity, BakedRoad, ControllerProvenance,
    JunctionControllerProvenance, ObjectProvenance, Orientation, SignProvenance, SignalProvenance,
    SignalReferenceProvenance, Warning,
};
use crate::coords::{Point, Vector};
use crate::object::orient;
use crate::road::{Road, Validity};
use crate::{
    BoardSign, Control, Controller, ControllerId, Dependency, Direction, DisplayArea, Lane, LaneId,
    MessageBoard, ObjectId, Reference, Referenced, RoadUser, Semantic, SignDependency, SignTarget,
    Signal, SignalBoard, SignalId, Unit, VmsBoardReference, VmsGroup,
};

/// The signals and controllers baked so far, and the provenance of each, in
/// step. Each is at the position its id names.
#[derive(Default)]
pub(super) struct Signals {
    pub baked: Vec<Signal>,
    pub provenance: Vec<SignalProvenance>,
    pub controllers: Vec<Controller>,
    pub controller_provenance: Vec<ControllerProvenance>,
    pub vms_groups: Vec<VmsGroup>,
    pub warnings: Vec<Warning>,
}

/// Bake every `<signal>` of every road, link each to the signals and
/// objects it names, apply each to the roads whose `<signalReference>`s name
/// it, and bake the `<controller>`s over them. `roads` pairs each baked road
/// with its `<road>`, `lanes` are the lanes baked, and `objects` finds the
/// objects an `<object id>` baked to.
///
/// Links, references and controls name a signal by its id. Where two
/// signals share one, they name the first. One naming an id no signal has is
/// skipped, with a [`Warning::SignalLinkDropped`], and so is a signal the
/// crate can't place, with a [`Warning::SignalDropped`].
pub(super) fn place(
    root: roxmltree::Node,
    roads: &[(roxmltree::Node, BakedRoad)],
    lanes: &[Lane],
    objects: &[ObjectProvenance],
) -> Signals {
    let mut road_by_id = HashMap::new();
    for (_, road) in roads {
        road_by_id
            .entry(road.road.od_id.as_str())
            .or_insert(&road.road);
    }
    let entries = || {
        roads.iter().flat_map(|(node, road)| {
            child(*node, "signals")
                .into_iter()
                .flat_map(|n| n.children())
                .map(move |n| (n, &road.road))
        })
    };
    let mut out = Signals::default();
    let mut nodes = Vec::new();
    for (signal, road) in entries().filter(|(n, _)| n.has_tag_name("signal")) {
        if place_signal(signal, road, lanes, &road_by_id, &mut out) {
            nodes.push(signal);
        } else {
            out.warnings.push(Warning::SignalDropped {
                road_id: road.od_id.clone(),
                signal_id: signal.attribute("id").unwrap_or_default().to_string(),
            });
        }
    }
    let mut placed = HashMap::new();
    for p in &out.provenance {
        placed.entry(p.od_id.clone()).or_insert(p.signal);
    }
    let mut baked_from: HashMap<&str, Vec<_>> = HashMap::new();
    for p in objects {
        baked_from
            .entry(p.od_id.as_str())
            .or_default()
            .push(p.object);
    }
    for (i, node) in nodes.into_iter().enumerate() {
        let road_id = out.provenance[i].road_id.clone();
        for (element, id) in link(node, &placed, &baked_from, &mut out.baked[i]) {
            out.warnings
                .push(link_dropped(&road_id, element, id.unwrap_or_default()));
        }
    }
    for (reference, road) in entries().filter(|(n, _)| n.has_tag_name("signalReference")) {
        let id = reference.attribute("id");
        let applied = id
            .and_then(|id| placed.get(id))
            .is_some_and(|&SignalId(i)| {
                apply_reference(
                    reference,
                    road,
                    lanes,
                    &mut out.baked[i],
                    &mut out.provenance[i],
                )
            });
        if !applied {
            let dropped = link_dropped(&road.od_id, "signalReference", id.unwrap_or_default());
            out.warnings.push(dropped);
        }
    }
    place_controllers(root, &placed, &mut out);
    place_vms_groups(root, &placed, &mut out);
    out
}

/// Bake every `<vmsGroup>` into `out`, with each board it references by its
/// signal. A reference naming no placed signal is dropped, with a
/// [`Warning::SignalLinkDropped`].
fn place_vms_groups(root: roxmltree::Node, placed: &HashMap<String, SignalId>, out: &mut Signals) {
    let index = |n: roxmltree::Node, name| n.attribute(name)?.trim().parse().ok();
    for node in root.children().filter(|n| n.has_tag_name("vmsGroup")) {
        let mut boards = Vec::new();
        for reference in node
            .children()
            .filter(|n| n.has_tag_name("vmsBoardReference"))
        {
            let named = reference.attribute("signalId");
            match named.and_then(|id| placed.get(id)) {
                Some(&signal) => boards.push(VmsBoardReference {
                    signal,
                    vms_index: index(reference, "vmsIndex"),
                    group_index: index(reference, "groupIndex"),
                }),
                None => out.warnings.push(link_dropped(
                    "",
                    "vmsBoardReference",
                    named.unwrap_or_default(),
                )),
            }
        }
        out.vms_groups.push(VmsGroup {
            od_id: node.attribute("id").unwrap_or_default().to_string(),
            boards,
        });
    }
}

/// Bake every top-level `<controller>` into `out`, and tell each signal it
/// controls. Then add each `<junction>`'s `<controller>` list to the
/// provenance of the controllers it names. `placed` finds a signal by its id.
fn place_controllers(root: roxmltree::Node, placed: &HashMap<String, SignalId>, out: &mut Signals) {
    let sequence = |node: roxmltree::Node| node.attribute("sequence")?.parse().ok();
    let text = |node: roxmltree::Node, name| node.attribute(name).unwrap_or_default().to_string();
    let mut controller_by_id = HashMap::new();
    for node in root.children().filter(|n| n.has_tag_name("controller")) {
        let id = ControllerId(out.controllers.len());
        let mut signals = Vec::new();
        for control in node.children().filter(|n| n.has_tag_name("control")) {
            let named = control.attribute("signalId");
            let Some(&signal) = named.and_then(|s| placed.get(s)) else {
                let dropped = link_dropped("", "control", named.unwrap_or_default());
                out.warnings.push(dropped);
                continue;
            };
            signals.push(Control {
                signal,
                kind: text(control, "type"),
            });
            let controllers = &mut out.baked[signal.0].controllers;
            if !controllers.contains(&id) {
                controllers.push(id);
            }
        }
        out.controllers.push(Controller {
            id,
            name: text(node, "name"),
            sequence: sequence(node),
            signals,
        });
        let od_id = text(node, "id");
        controller_by_id.entry(od_id.clone()).or_insert(id.0);
        out.controller_provenance.push(ControllerProvenance {
            controller: id,
            od_id,
            junctions: Vec::new(),
        });
    }
    for junction in root.children().filter(|n| n.has_tag_name("junction")) {
        for node in junction.children().filter(|n| n.has_tag_name("controller")) {
            let named = node.attribute("id");
            let Some(&i) = named.and_then(|id| controller_by_id.get(id)) else {
                let dropped = link_dropped("", "controller", named.unwrap_or_default());
                out.warnings.push(dropped);
                continue;
            };
            out.controller_provenance[i]
                .junctions
                .push(JunctionControllerProvenance {
                    junction_id: text(junction, "id"),
                    kind: text(node, "type"),
                    sequence: sequence(node),
                });
        }
    }
}

/// Apply `signal` where the `<signalReference>` `node` on `road` says too: add
/// the lanes there and the point it names, and say whether it did. One
/// missing `s` or `t`, or off the ends of the road, is skipped.
fn apply_reference(
    node: roxmltree::Node,
    road: &Road,
    lanes: &[Lane],
    signal: &mut Signal,
    provenance: &mut SignalProvenance,
) -> bool {
    let (Some(s), Some(t)) = (attr_f64(node, "s"), attr_f64(node, "t")) else {
        return false;
    };
    if !road.on_road(s) {
        return false;
    }
    let orientation = orientation(node);
    for lane in signal_lanes(road, lanes, s, orientation, validity(node)) {
        if !signal.lanes.contains(&lane) {
            signal.lanes.push(lane);
        }
    }
    signal.applies_at.push(road.surface(s, t).0);
    provenance.references.push(SignalReferenceProvenance {
        road_id: road.od_id.clone(),
        s,
        t,
        orientation,
    });
    true
}

/// The warning for a link of `element`, on `road_id`, naming `id`, which
/// the crate drops.
fn link_dropped(road_id: &str, element: &str, id: &str) -> Warning {
    Warning::SignalLinkDropped {
        road_id: road_id.to_string(),
        element: element.to_string(),
        id: id.to_string(),
    }
}

/// Where a signal's board stands, and how it is turned.
struct Board {
    position: Point,
    heading: f64,
    pitch: f64,
    roll: f64,
}

/// Bake one `<signal>` on `road` into `out`, and say whether it did.
/// `roads` finds the road a `<positionRoad>` names. One missing `s` or `t`,
/// or off the ends of the road, is skipped.
fn place_signal(
    node: roxmltree::Node,
    road: &Road,
    lanes: &[Lane],
    roads: &HashMap<&str, &Road>,
    out: &mut Signals,
) -> bool {
    let (Some(s), Some(t)) = (attr_f64(node, "s"), attr_f64(node, "t")) else {
        return false;
    };
    let orientation = orientation(node);
    let Some(own) = standing(node, road, orientation) else {
        return false;
    };
    let elsewhere = || {
        let at = child(node, "positionRoad")?;
        standing(at, roads.get(at.attribute("roadId")?)?, orientation)
    };
    let board = child(node, "positionInertial")
        .and_then(inertial)
        .or_else(elsewhere)
        .unwrap_or(own);
    let text = |name| node.attribute(name).unwrap_or_default().to_string();
    let flag = |name| matches!(node.attribute(name), Some("true" | "1"));
    let size = |name| attr_f32(node, name);
    let signal_lanes_here = signal_lanes(road, lanes, s, orientation, validity(node));
    let id = SignalId(out.baked.len());
    out.baked.push(Signal {
        id,
        name: text("name"),
        dynamic: node.attribute("dynamic") == Some("yes"),
        country: text("country"),
        country_revision: text("countryRevision"),
        kind: text("type"),
        subtype: text("subtype"),
        value: attr_f64(node, "value"),
        unit: node.attribute("unit").and_then(unit),
        text: text("text"),
        invalidated: flag("invalidated"),
        temporary: flag("temporary"),
        controllers: Vec::new(),
        dependencies: Vec::new(),
        references: Vec::new(),
        lanes: signal_lanes_here.clone(),
        applies_at: vec![road.surface(s, t).0],
        position: board.position,
        heading: angle(board.heading),
        pitch: angle(board.pitch),
        roll: angle(board.roll),
        length: size("length"),
        width: size("width"),
        height: size("height"),
        semantics: semantics(node),
        boards: boards(
            node,
            &board,
            |n| {
                let own = validity(n);
                let board = n.parent_element().map(validity).unwrap_or_default();
                match (own.is_empty(), board.is_empty()) {
                    (true, true) => signal_lanes_here.clone(),
                    (true, false) => signal_lanes(road, lanes, s, orientation, board),
                    (false, _) => signal_lanes(road, lanes, s, orientation, own),
                }
            },
            |n| {
                let at = child(n, "positionRoad")?;
                standing(at, roads.get(at.attribute("roadId")?)?, orientation)
            },
        ),
    });
    out.provenance.push(SignalProvenance {
        signal: id,
        road_id: road.od_id.clone(),
        od_id: text("id"),
        s,
        t,
        orientation,
        references: Vec::new(),
        signs: signs(node),
    });
    true
}

/// What each `<sign>` of `node`'s static boards says about itself that the
/// crate builds nothing from, in file order. `board` counts the baked boards,
/// which are the `<staticBoard>`s and `<vmsBoard>`s.
fn signs(node: roxmltree::Node) -> Vec<SignProvenance> {
    let flag = |n: roxmltree::Node, name| matches!(n.attribute(name), Some("true" | "1"));
    let turn = |n: roxmltree::Node, name| angle(attr_f64(n, name).unwrap_or(0.0));
    let boards = node
        .children()
        .filter(|b| b.has_tag_name("staticBoard") || b.has_tag_name("vmsBoard"));
    let mut out = Vec::new();
    for (board, b) in boards.enumerate() {
        let signs = b.children().filter(|n| n.has_tag_name("sign"));
        for (sign, node) in signs.enumerate() {
            out.push(SignProvenance {
                board,
                sign,
                id: node.attribute("id").unwrap_or_default().to_string(),
                country_revision: node
                    .attribute("countryRevision")
                    .unwrap_or_default()
                    .to_string(),
                dynamic: node.attribute("dynamic") == Some("yes"),
                invalidated: flag(node, "invalidated"),
                temporary: flag(node, "temporary"),
                orientation: orientation(node),
                h_offset: turn(node, "hOffset"),
                pitch: turn(node, "pitch"),
                roll: turn(node, "roll"),
                length: attr_f32(node, "length"),
            });
        }
    }
    out
}

/// Give `signal` the `<dependency>`s and `<reference>`s of its `<signal>`,
/// `node`, and each sign and display area on its boards theirs. `placed`
/// finds a signal by its id, and `baked_from` the objects an `<object id>`
/// baked to. A reference to an object names each of them.
///
/// A sign's or a display area's own `<dependency>`s and a sign's own
/// `<reference>`s override its board's, and a board's own override the
/// signal's, as the spec says. One with none takes its board's, and a board
/// with none the signal's. A sign's dependency may name a signal, or another
/// sign on its board. Returns each link that names nothing the load baked.
fn link<'a>(
    node: roxmltree::Node<'a, 'a>,
    placed: &HashMap<String, SignalId>,
    baked_from: &HashMap<&str, Vec<ObjectId>>,
    signal: &mut Signal,
) -> Vec<(&'static str, Option<&'a str>)> {
    let text = |node: roxmltree::Node, name| node.attribute(name).unwrap_or_default().to_string();
    let named = |id: Option<&str>| id.and_then(|id| placed.get(id)).copied();
    let children = |n: roxmltree::Node<'a, 'a>, tag: &'static str| {
        n.children().filter(move |c| c.has_tag_name(tag))
    };
    let mut dropped = Vec::new();
    let references = |n: roxmltree::Node<'a, 'a>, dropped: &mut Vec<_>| {
        let mut out = Vec::new();
        for reference in children(n, "reference") {
            let id = reference.attribute("elementId");
            let to: Vec<Referenced> = match reference.attribute("elementType") {
                Some("signal") => named(id).map(Referenced::Signal).into_iter().collect(),
                Some("object") => id
                    .and_then(|id| baked_from.get(id))
                    .into_iter()
                    .flatten()
                    .map(|&o| Referenced::Object(o))
                    .collect(),
                _ => Vec::new(),
            };
            if to.is_empty() {
                dropped.push(("reference", id));
            }
            let kind = text(reference, "type");
            out.extend(to.into_iter().map(|to| Reference {
                to,
                kind: kind.clone(),
            }));
        }
        out
    };
    for dependency in children(node, "dependency") {
        match named(dependency.attribute("id")) {
            Some(named) => signal.dependencies.push(Dependency {
                signal: named,
                kind: text(dependency, "type"),
            }),
            None => dropped.push(("dependency", dependency.attribute("id"))),
        }
    }
    signal.references = references(node, &mut dropped);
    let board_nodes = node
        .children()
        .filter(|b| b.has_tag_name("staticBoard") || b.has_tag_name("vmsBoard"));
    for (b, board) in board_nodes.zip(&mut signal.boards) {
        let board_dependencies: Vec<Dependency> = match children(b, "dependency").next() {
            None => signal.dependencies.clone(),
            Some(_) => children(b, "dependency")
                .filter_map(|d| match named(d.attribute("id")) {
                    Some(named) => Some(Dependency {
                        signal: named,
                        kind: text(d, "type"),
                    }),
                    None => {
                        dropped.push(("dependency", d.attribute("id")));
                        None
                    }
                })
                .collect(),
        };
        let board_references = match children(b, "reference").next() {
            None => signal.references.clone(),
            Some(_) => references(b, &mut dropped),
        };
        match board {
            SignalBoard::Static(signs) => {
                let sign_nodes: Vec<_> = children(b, "sign").collect();
                let ids: Vec<Option<&str>> = sign_nodes.iter().map(|n| n.attribute("id")).collect();
                for (n, sign) in sign_nodes.iter().zip(signs.iter_mut()) {
                    let own: Vec<_> = children(*n, "dependency").collect();
                    sign.dependencies = if own.is_empty() {
                        (board_dependencies.iter())
                            .map(|d| SignDependency {
                                to: SignTarget::Signal(d.signal),
                                kind: d.kind.clone(),
                            })
                            .collect()
                    } else {
                        own.iter()
                            .filter_map(|d| {
                                let id = d.attribute("id");
                                let on_board = ids.iter().position(|&i| i.is_some() && i == id);
                                let to = match (named(id), on_board) {
                                    (Some(s), _) => SignTarget::Signal(s),
                                    (None, Some(k)) => SignTarget::Sign(k),
                                    (None, None) => {
                                        dropped.push(("dependency", id));
                                        return None;
                                    }
                                };
                                Some(SignDependency {
                                    to,
                                    kind: text(*d, "type"),
                                })
                            })
                            .collect()
                    };
                    sign.references = match children(*n, "reference").next() {
                        Some(_) => references(*n, &mut dropped),
                        None => board_references.clone(),
                    };
                }
            }
            SignalBoard::Message(message) => {
                let area_nodes = children(b, "displayArea");
                for (n, area) in area_nodes.zip(message.areas.iter_mut()) {
                    let own: Vec<_> = children(n, "dependency").collect();
                    area.dependencies = if own.is_empty() {
                        board_dependencies.clone()
                    } else {
                        own.iter()
                            .filter_map(|d| match named(d.attribute("id")) {
                                Some(named) => Some(Dependency {
                                    signal: named,
                                    kind: text(*d, "type"),
                                }),
                                None => {
                                    dropped.push(("dependency", d.attribute("id")));
                                    None
                                }
                            })
                            .collect()
                    };
                }
            }
        }
    }
    dropped
}

/// A board standing `zOffset` straight above `road`'s surface at the `s` and
/// `t` of `node`, a `<signal>` or a `<positionRoad>`, or `None` if it is
/// missing either or off the ends of the road.
///
/// The board faces the traffic `orientation` names, against `+s` for `+`
/// and along it otherwise, turned `hOffset` counter-clockwise. `pitch` and
/// `roll` are against the horizontal, not the road, so a board on a banked
/// road stays upright.
fn standing(node: roxmltree::Node, road: &Road, orientation: Orientation) -> Option<Board> {
    let (s, t) = (attr_f64(node, "s")?, attr_f64(node, "t")?);
    if !road.on_road(s) {
        return None;
    }
    let number = |name| attr_f64(node, name).unwrap_or(0.0);
    let (ground, road_heading) = road.surface(s, t);
    let facing = match orientation {
        Orientation::Positive => PI,
        Orientation::Negative | Orientation::Both => 0.0,
    };
    Some(Board {
        position: ground + Vector::new(0.0, 0.0, attr_f32(node, "zOffset").unwrap_or(0.0)),
        heading: road_heading + facing + number("hOffset"),
        pitch: number("pitch"),
        roll: number("roll"),
    })
}

/// What `node`'s `<semantics>` say, in file order. A child the spec does not
/// name is skipped.
pub(super) fn semantics(node: roxmltree::Node) -> Vec<Semantic> {
    let text = |n: roxmltree::Node, name| n.attribute(name).unwrap_or_default().to_string();
    let users = |n: roxmltree::Node| -> Vec<RoadUser> {
        n.children()
            .filter_map(|u| {
                let category = || {
                    u.children()
                        .find(|c| c.has_tag_name("type"))
                        .and_then(|c| c.text())
                        .unwrap_or_default()
                        .trim()
                        .to_string()
                };
                match u.tag_name().name() {
                    "animal" => Some(RoadUser::Animal),
                    "person" => Some(RoadUser::Person(category())),
                    "vehicle" => Some(RoadUser::Vehicle(category())),
                    _ => None,
                }
            })
            .collect()
    };
    child(node, "semantics")
        .into_iter()
        .flat_map(|s| s.children())
        .filter_map(|n| {
            Some(match n.tag_name().name() {
                "speed" => Semantic::Speed {
                    kind: text(n, "type"),
                    value: attr_f64(n, "value"),
                    unit: n.attribute("unit").and_then(unit),
                },
                "lane" => Semantic::Lane {
                    kind: text(n, "type"),
                },
                "priority" => Semantic::Priority {
                    kind: text(n, "type"),
                },
                "prohibited" => Semantic::Prohibited(users(n)),
                "warning" => Semantic::Warning,
                "routing" => Semantic::Routing,
                "streetname" => Semantic::StreetName,
                "parking" => Semantic::Parking,
                "tourist" => Semantic::Tourist,
                "supplementaryTime" => Semantic::SupplementaryTime {
                    kind: text(n, "type"),
                    value: attr_f64(n, "value"),
                },
                "supplementaryAllows" => Semantic::SupplementaryAllows(users(n)),
                "supplementaryProhibits" => Semantic::SupplementaryProhibits(users(n)),
                "supplementaryDistance" => Semantic::SupplementaryDistance {
                    kind: text(n, "type"),
                    value: attr_f64(n, "value"),
                    unit: n.attribute("unit").and_then(unit),
                },
                "supplementaryEnvironment" => Semantic::SupplementaryEnvironment {
                    kind: text(n, "type"),
                },
                "supplementaryExplanatory" => Semantic::SupplementaryExplanatory,
                _ => return None,
            })
        })
        .collect()
}

/// `node`'s `<staticBoard>`s and `<vmsBoard>`s, in file order, placed on
/// `board`: `v` across it, to the left of its heading, and `z` up it,
/// turned by its heading, pitch and roll. A sign with its own
/// `<positionInertial>` stands there instead, and one with its own
/// `<positionRoad>` where `elsewhere` puts it. `lanes` gives the lanes a
/// sign or a display area applies to, from its own `<validity>`, or its
/// board's, if it has any. Their dependencies and references are linked
/// later, by [`link`].
fn boards(
    node: roxmltree::Node,
    board: &Board,
    lanes: impl Fn(roxmltree::Node) -> Vec<LaneId>,
    elsewhere: impl Fn(roxmltree::Node) -> Option<Board>,
) -> Vec<SignalBoard> {
    let own = |n: roxmltree::Node| {
        child(n, "positionInertial")
            .and_then(inertial)
            .or_else(|| elsewhere(n))
            .map(|b| b.position)
    };
    let at = |n: roxmltree::Node| {
        let local = [
            0.0,
            attr_f32(n, "v").map_or(0.0, f64::from),
            attr_f32(n, "z").map_or(0.0, f64::from),
        ];
        let [x, y, z] = orient(board.heading, board.pitch, board.roll, local);
        let placed = board.position + Vector::new(x as f32, y as f32, z as f32);
        if placed.to_array().iter().all(|c| c.is_finite()) {
            placed
        } else {
            board.position
        }
    };
    let size = |n: roxmltree::Node, name| {
        n.attribute(name)
            .and_then(|v| v.trim().parse::<f64>().ok())
            .map(|v| v as f32)
            .filter(|v| v.is_finite())
    };
    let text = |n: roxmltree::Node, name| n.attribute(name).unwrap_or_default().to_string();
    node.children()
        .filter_map(|b| {
            if b.has_tag_name("staticBoard") {
                let signs = b
                    .children()
                    .filter(|n| n.has_tag_name("sign"))
                    .map(|sign| BoardSign {
                        name: text(sign, "name"),
                        country: text(sign, "country"),
                        kind: text(sign, "type"),
                        subtype: text(sign, "subtype"),
                        value: attr_f64(sign, "value"),
                        unit: sign.attribute("unit").and_then(unit),
                        text: text(sign, "text"),
                        semantics: semantics(sign),
                        position: own(sign).unwrap_or_else(|| at(sign)),
                        width: size(sign, "width"),
                        height: size(sign, "height"),
                        lanes: lanes(sign),
                        dependencies: Vec::new(),
                        references: Vec::new(),
                    })
                    .collect();
                Some(SignalBoard::Static(signs))
            } else if b.has_tag_name("vmsBoard") {
                Some(SignalBoard::Message(MessageBoard {
                    display: text(b, "displayType"),
                    position: at(b),
                    width: size(b, "displayWidth"),
                    height: size(b, "displayHeight"),
                    areas: b
                        .children()
                        .filter(|n| n.has_tag_name("displayArea"))
                        .map(|area| DisplayArea {
                            index: area.attribute("index").and_then(|v| v.trim().parse().ok()),
                            position: at(area),
                            width: size(area, "width"),
                            height: size(area, "height"),
                            lanes: lanes(area),
                            dependencies: Vec::new(),
                        })
                        .collect(),
                }))
            } else {
                None
            }
        })
        .collect()
}

/// A board at a `<positionInertial>`, facing its `hdg`, or `None` if it is
/// missing `x`, `y` or `z`, or one is too large for an `f32`.
fn inertial(node: roxmltree::Node) -> Option<Board> {
    let number = |name| attr_f64(node, name).unwrap_or(0.0);
    let (x, y, z) = (
        attr_f32(node, "x")?,
        attr_f32(node, "y")?,
        attr_f32(node, "z")?,
    );
    Some(Board {
        position: Point::new(x, y, z),
        heading: number("hdg"),
        pitch: number("pitch"),
        roll: number("roll"),
    })
}

/// The lanes at `s` on `road` a signal applies to: those in its `<validity>`
/// ranges if it has any, else those whose traffic runs the way its
/// `orientation` names, `+s` for `+` and against it for `-`. A two-way lane
/// carries traffic both ways. `none` names every lane. `lanes` are the lanes
/// baked, which say which way each runs.
fn signal_lanes(
    road: &Road,
    lanes: &[Lane],
    s: f64,
    orientation: Orientation,
    validity: Vec<Validity>,
) -> Vec<LaneId> {
    let found = road.lanes((s, s), &validity);
    let wanted = match (validity.is_empty(), orientation) {
        (false, _) | (_, Orientation::Both) => return found,
        (_, Orientation::Positive) => Direction::Forward,
        (_, Orientation::Negative) => Direction::Backward,
    };
    found
        .into_iter()
        .filter(|id| {
            debug_assert_eq!(lanes[id.0].id, *id, "a lane's id is its place in `lanes`");
            let direction = lanes[id.0].direction;
            direction == wanted || direction == Direction::Both
        })
        .collect()
}

/// `angle` as an `f32` in `(-π, π]`. The `f32` nearest π is a little over
/// it, so an angle just over -π rounds onto -π, and is turned to π.
fn angle(angle: f64) -> f32 {
    let wrapped = wrap(angle) as f32;
    if wrapped <= -std::f32::consts::PI {
        std::f32::consts::PI
    } else {
        wrapped
    }
}

/// `angle` in `(-π, π]`.
fn wrap(angle: f64) -> f64 {
    let wrapped = angle.rem_euclid(2.0 * PI);
    if wrapped > PI {
        wrapped - 2.0 * PI
    } else {
        wrapped
    }
}

/// The unit a `unit` attribute names. `None` for an empty one.
fn unit(name: &str) -> Option<Unit> {
    Some(match name {
        "" => return None,
        "m" => Unit::Metre,
        "km" => Unit::Kilometre,
        "ft" => Unit::Foot,
        "mile" => Unit::Mile,
        "m/s" => Unit::MetrePerSecond,
        "mph" => Unit::MilePerHour,
        "km/h" => Unit::KilometrePerHour,
        "kg" => Unit::Kilogram,
        "t" => Unit::Tonne,
        "%" => Unit::Percent,
        _ => Unit::Unknown,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::load_str;

    /// A flat 20 m road heading +X with lane -1, carrying `signals`.
    fn road_with(signals: &str) -> String {
        format!(
            r#"<OpenDRIVE>
  <road name="r" length="20.0" id="1" junction="-1">
    <planView><geometry s="0.0" x="0.0" y="0.0" hdg="0.0" length="20.0"><line/></geometry></planView>
    <lanes><laneSection s="0.0"><right><lane id="-1" type="driving">
      <width sOffset="0.0" a="3.5"/>
    </lane></right></laneSection></lanes>
    <signals>{signals}</signals>
  </road>
</OpenDRIVE>"#
        )
    }

    fn signals_of(signals: &str) -> Vec<Signal> {
        load_str(&road_with(signals))
            .expect("import")
            .signals()
            .to_vec()
    }

    #[test]
    fn a_signal_without_a_station_or_off_the_road_is_skipped() {
        let signals = signals_of(
            r#"<signal id="a" t="0" orientation="+"/>
               <signal id="b" s="5" orientation="+"/>
               <signal id="c" s="25" t="0" orientation="+"/>
               <signal id="d" s="20" t="0" orientation="+"/>"#,
        );
        assert_eq!(signals.len(), 1);
        assert_eq!(signals[0].position, crate::Point::new(20.0, 0.0, 0.0));
    }

    #[test]
    fn a_position_that_cannot_be_placed_leaves_the_board_at_its_station() {
        let signals = signals_of(
            r#"<signal s="5" t="-2" zOffset="1" orientation="-">
                 <positionRoad roadId="9" s="1" t="0" zOffset="0" hOffset="0"/>
               </signal>
               <signal s="5" t="-2" zOffset="1" orientation="-">
                 <positionInertial x="1" y="2" hdg="0"/>
               </signal>
               <signal s="5" t="-2" zOffset="1" orientation="-">
                 <positionRoad roadId="1" s="30" t="0" zOffset="0" hOffset="0"/>
               </signal>"#,
        );
        for signal in &signals {
            assert_eq!(signal.position, crate::Point::new(5.0, -2.0, 1.0));
        }
        assert_eq!(signals.len(), 3);
    }

    #[test]
    fn a_link_to_nothing_is_skipped() {
        let signals = signals_of(
            r#"<signal id="a" s="1" t="0">
                 <dependency id="zz"/>
                 <reference elementType="signal" elementId="zz"/>
                 <reference elementType="object" elementId="zz"/>
                 <reference elementType="junction" elementId="a"/>
                 <reference elementId="a"/>
                 <dependency id="a"/>
               </signal>"#,
        );
        let signal = &signals[0];
        assert_eq!(signal.references, []);
        assert_eq!(
            signal.dependencies,
            [Dependency {
                signal: signal.id,
                kind: String::new()
            }]
        );
    }

    #[test]
    fn an_unrecognised_unit_is_unknown_and_an_empty_one_is_none() {
        let signals = signals_of(
            r#"<signal s="1" t="0" value="3" unit="furlong"/>
               <signal s="1" t="0" value="3" unit=""/>
               <signal s="1" t="0" value="x" unit="mph"/>"#,
        );
        let units: Vec<_> = signals.iter().map(|s| (s.value, s.unit)).collect();
        assert_eq!(
            units,
            [
                (Some(3.0), Some(Unit::Unknown)),
                (Some(3.0), None),
                (None, Some(Unit::MilePerHour)),
            ]
        );
    }

    #[test]
    fn an_angle_wraps_into_the_half_open_turn() {
        for (angle, want) in [
            (0.0, 0.0),
            (PI, PI),
            (-PI, PI),
            (3.0 * PI / 2.0, -PI / 2.0),
            (14.0 * PI + 0.1, 0.1),
            (-14.0 * PI - 0.1, -0.1),
        ] {
            assert!(
                (wrap(angle) - want).abs() < 1e-9,
                "{angle} wrapped to {}",
                wrap(angle)
            );
        }
    }
}