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
//! Signals: the traffic signs, traffic lights and road paint that tell
//! traffic what to do. Like objects, they don't depend on the file format. A
//! signal keeps the catalogue codes the map gives it and looks nothing up.

use crate::coords::Point;
use crate::{LaneId, ObjectId};

/// An opaque signal identifier. Not a position in
/// [`RoadNetwork::signals`](crate::RoadNetwork::signals), so look signals up
/// with [`RoadNetwork::signal`](crate::RoadNetwork::signal).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SignalId(pub usize);

/// An opaque controller identifier. Not a position in
/// [`RoadNetwork::controllers`](crate::RoadNetwork::controllers), so look
/// controllers up with
/// [`RoadNetwork::controller`](crate::RoadNetwork::controller).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ControllerId(pub usize);

/// The unit of a signal's [`value`](Signal::value).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Unit {
    /// Metres, `m`.
    Metre,
    /// Kilometres, `km`.
    Kilometre,
    /// Feet, `ft`.
    Foot,
    /// Miles, `mile`.
    Mile,
    /// Metres per second, `m/s`.
    MetrePerSecond,
    /// Miles per hour, `mph`.
    MilePerHour,
    /// Kilometres per hour, `km/h`.
    KilometrePerHour,
    /// Kilograms, `kg`.
    Kilogram,
    /// Metric tons, `t`.
    Tonne,
    /// A percentage, such as a grade, `%`.
    Percent,
    /// A unit this crate does not recognise.
    Unknown,
}

impl Unit {
    /// The unit's usual symbol, such as `km/h`, or `unknown`.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Metre => "m",
            Self::Kilometre => "km",
            Self::Foot => "ft",
            Self::Mile => "mile",
            Self::MetrePerSecond => "m/s",
            Self::MilePerHour => "mph",
            Self::KilometrePerHour => "km/h",
            Self::Kilogram => "kg",
            Self::Tonne => "t",
            Self::Percent => "%",
            Self::Unknown => "unknown",
        }
    }
}

impl std::fmt::Display for Unit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// One signal: what it means, the lanes it applies to, and the board it is.
///
/// Where a signal applies and where it stands can differ. A sign on a gantry
/// over one road can apply to another. `lanes` and `applies_at` say where it
/// applies, and the pose says where the board is.
///
/// The board is a box in its own frame: `length` along its heading, `width`
/// across it, and `height` up from its origin. The origin is the middle of
/// its bottom edge.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Signal {
    /// This signal's identity.
    pub id: SignalId,
    /// The name the map gives it. Empty if it has none.
    pub name: String,
    /// Whether it can change what it shows, such as a traffic light.
    pub dynamic: bool,
    /// The country whose catalogue `kind` and `subtype` come from, such as
    /// `DE`. Free text, since maps also use names such as `OpenDRIVE` for a
    /// generic catalogue. Empty if the map gives none.
    pub country: String,
    /// The year of that country's rules. Empty if the map gives none.
    pub country_revision: String,
    /// The signal's type in the country's catalogue, such as `274` for a
    /// German speed limit. Free text.
    pub kind: String,
    /// The subtype within `kind`. Free text, and often `-1` for none.
    pub subtype: String,
    /// A number the signal shows, such as a speed limit.
    pub value: Option<f64>,
    /// The unit of `value`, if the map gives one.
    pub unit: Option<Unit>,
    /// Text the signal shows, such as a town name. Empty if it has none.
    pub text: String,
    /// Whether the signal has been struck out, such as a crossed-out sign.
    pub invalidated: bool,
    /// Whether the signal is temporary, such as a sign at road works.
    pub temporary: bool,
    /// The controllers the signal belongs to. Usually none, or one.
    pub controllers: Vec<ControllerId>,
    /// The signals this one is linked to by a dependency, such as the plate
    /// that limits a speed sign to lorries.
    pub dependencies: Vec<Dependency>,
    /// The signals and objects this one relates to, such as the stop line of
    /// a traffic light.
    pub references: Vec<Reference>,
    /// The lanes the signal applies to, on every road the map applies it
    /// to. A lane appears once. Without a `<validity>`, these are the lanes
    /// whose traffic runs the way the signal's `orientation` names.
    pub lanes: Vec<LaneId>,
    /// The points on the road surface where the signal takes effect: its
    /// own first, then one for each further place the map applies it. On a
    /// lane raised by its `<height>`s, a point is on the lane.
    pub applies_at: Vec<Point>,
    /// The middle of the board's bottom edge. On a lane raised by its
    /// `<height>`s, the board stands on the lane. The spec does not say
    /// whether it stands there or on the road below.
    pub position: Point,
    /// The direction the board faces, toward the traffic it addresses: yaw
    /// about +Z, counter-clockwise from +X, in radians in `(-π, π]`.
    pub heading: f32,
    /// Pitch about the turned Y, in radians, nose down for a positive angle.
    pub pitch: f32,
    /// Roll about the turned X, in radians, left side up for a positive
    /// angle.
    pub roll: f32,
    /// The board's thickness along its heading, in metres, if the map gives
    /// it.
    pub length: Option<f32>,
    /// Metres across, if the map gives it.
    pub width: Option<f32>,
    /// Metres up from the origin, if the map gives it.
    pub height: Option<f32>,
    /// What the signal means, from its `<semantics>`, in file order. Empty
    /// where the map says only its catalogue codes.
    pub semantics: Vec<Semantic>,
    /// The boards the signal is, from its `<staticBoard>`s and
    /// `<vmsBoard>`s: a gantry of signs, or a variable message display.
    pub boards: Vec<SignalBoard>,
}

/// One thing a signal means, from an OpenDRIVE `<semantics>` child. Each
/// `kind` is the file's `type`, such as `maximum` for a speed, empty if it
/// gives none.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Semantic {
    /// A speed rule, such as a limit.
    Speed {
        /// Its `type`, such as `maximum` or `zone`.
        kind: String,
        /// The speed.
        value: Option<f64>,
        /// Its unit.
        unit: Option<Unit>,
    },
    /// A lane rule, such as no overtaking.
    Lane {
        /// Its `type`.
        kind: String,
    },
    /// A priority rule, such as a stop line or right of way.
    Priority {
        /// Its `type`.
        kind: String,
    },
    /// The road users who may not enter.
    Prohibited(Vec<RoadUser>),
    /// A warning.
    Warning,
    /// Routing information.
    Routing,
    /// A street's name.
    StreetName,
    /// A parking rule.
    Parking,
    /// Tourist information.
    Tourist,
    /// When another sign applies. Means nothing on its own.
    SupplementaryTime {
        /// Its `type`.
        kind: String,
        /// Its `value`.
        value: Option<f64>,
    },
    /// Who another sign does not apply to. Means nothing on its own.
    SupplementaryAllows(Vec<RoadUser>),
    /// Who another sign applies to only. Means nothing on its own.
    SupplementaryProhibits(Vec<RoadUser>),
    /// How far off or along another sign applies. Means nothing on its own.
    SupplementaryDistance {
        /// Its `type`.
        kind: String,
        /// The distance.
        value: Option<f64>,
        /// Its unit.
        unit: Option<Unit>,
    },
    /// In what weather another sign applies. Means nothing on its own.
    SupplementaryEnvironment {
        /// Its `type`.
        kind: String,
    },
    /// An explanation of another sign. Means nothing on its own.
    SupplementaryExplanatory,
}

/// A road user a [`Semantic`] names.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum RoadUser {
    /// An animal.
    Animal,
    /// A person, of the category the file names, such as `pedestrian`.
    Person(String),
    /// A vehicle, of the category the file names, such as `car`.
    Vehicle(String),
}

/// A board a signal is, from a `<staticBoard>` or a `<vmsBoard>`.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum SignalBoard {
    /// A board of fixed signs, such as a gantry, with each of its signs.
    Static(Vec<BoardSign>),
    /// A variable message board, which shows what a scenario tells it to.
    Message(MessageBoard),
}

/// One `<sign>` on a static board: its catalogue codes and meaning, as on a
/// [`Signal`], and where it is.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BoardSign {
    /// The name the map gives it. Empty if it has none.
    pub name: String,
    /// As [`Signal::country`].
    pub country: String,
    /// As [`Signal::kind`].
    pub kind: String,
    /// As [`Signal::subtype`].
    pub subtype: String,
    /// As [`Signal::value`].
    pub value: Option<f64>,
    /// As [`Signal::unit`].
    pub unit: Option<Unit>,
    /// As [`Signal::text`].
    pub text: String,
    /// What it means.
    pub semantics: Vec<Semantic>,
    /// Where on the board it is, `v` across the board and `z` up it, turned
    /// with the board: the middle of its bottom edge.
    pub position: Point,
    /// Metres across, if the map gives it.
    pub width: Option<f32>,
    /// Metres up, if the map gives it.
    pub height: Option<f32>,
    /// The lanes it applies to: those of its own `<validity>`, which
    /// overrides the signal's, as the spec says, or the signal's
    /// [`Signal::lanes`] if it has none. Its own validity applies where the
    /// signal stands, not where a `<signalReference>` applies the signal too.
    pub lanes: Vec<LaneId>,
    /// What it depends on: its own `<dependency>`s, or else its board's,
    /// or else the signal's [`Signal::dependencies`].
    pub dependencies: Vec<SignDependency>,
    /// What it relates to: its own `<reference>`s, or else its board's, or
    /// else the signal's [`Signal::references`].
    pub references: Vec<Reference>,
}

/// A dependency of a [`BoardSign`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SignDependency {
    /// What it names.
    pub to: SignTarget,
    /// What the dependency is. Free text, and empty if the map gives none.
    pub kind: String,
}

/// What a [`SignDependency`] names: a signal, or a sign on the same board,
/// as the spec's example of a speed sign and the plates under it does.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SignTarget {
    /// A signal.
    Signal(SignalId),
    /// The sign at this position among the same board's signs.
    Sign(usize),
}

/// A `<vmsBoard>`: a display that shows what a scenario tells it to.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MessageBoard {
    /// Its `displayType`, such as `LED`. Empty if it has none.
    pub display: String,
    /// Where on the signal it is, as a [`BoardSign`]'s position.
    pub position: Point,
    /// Metres across, if the map gives it.
    pub width: Option<f32>,
    /// Metres up, if the map gives it.
    pub height: Option<f32>,
    /// The places on it a signal can be shown, in file order.
    pub areas: Vec<DisplayArea>,
}

/// A `<displayArea>` of a [`MessageBoard`].
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DisplayArea {
    /// Its `index`, if the map gives one.
    pub index: Option<i32>,
    /// Where on the board it is, as a [`BoardSign`]'s position.
    pub position: Point,
    /// Metres across, if the map gives it.
    pub width: Option<f32>,
    /// Metres up, if the map gives it.
    pub height: Option<f32>,
    /// The lanes it applies to, as a [`BoardSign`]'s, from its own
    /// `<validity>`. The spec's text gives a display area one, though its
    /// schema does not, and the crate reads it.
    pub lanes: Vec<LaneId>,
    /// What it depends on: its own `<dependency>`s, or else its board's, or
    /// else the signal's [`Signal::dependencies`]. Like its validity, from
    /// the spec's text.
    pub dependencies: Vec<Dependency>,
}

/// The variable message boards on one gantry, from a `<vmsGroup>`, which a
/// scenario treats as one display.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VmsGroup {
    /// Its `id`, empty if it has none.
    pub od_id: String,
    /// Each board in it, from its `<vmsBoardReference>`s, in file order.
    pub boards: Vec<VmsBoardReference>,
}

/// One board of a [`VmsGroup`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VmsBoardReference {
    /// The signal the board is on.
    pub signal: SignalId,
    /// Which of the signal's boards it is, its `vmsIndex`, as the file
    /// numbers them, if the file gives a whole number.
    pub vms_index: Option<i32>,
    /// Its place in the group, its `groupIndex`, if the file gives a whole
    /// number.
    pub group_index: Option<i32>,
}

/// A group of signals that always show the same state, such as the lights of
/// one approach to a junction. The map does not say what they show, or when.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Controller {
    /// This controller's identity.
    pub id: ControllerId,
    /// The name the map gives it. Empty if it has none.
    pub name: String,
    /// Its priority among the other controllers, if the map gives one.
    pub sequence: Option<u32>,
    /// The signals it controls, in the order the map gives them.
    pub signals: Vec<Control>,
}

/// One signal a [`Controller`] controls.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Control {
    /// The signal.
    pub signal: SignalId,
    /// How the controller controls it. Free text, and empty if the map gives
    /// none.
    pub kind: String,
}

/// A signal another depends on.
///
/// The map's `<dependency>` names it. OpenDRIVE 1.7 calls it the signal
/// controlled, and 1.9 the one controlling, so this keeps the link as the
/// map gives it and reads nothing into its direction.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Dependency {
    /// The signal named.
    pub signal: SignalId,
    /// What the dependency is. Free text, and empty if the map gives none.
    pub kind: String,
}

/// Something a signal relates to.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Reference {
    /// The signal or object.
    pub to: Referenced,
    /// What the relation is, such as `stopline`. Free text, and empty if the
    /// map gives none.
    pub kind: String,
}

/// What a [`Reference`] names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Referenced {
    /// A signal.
    Signal(SignalId),
    /// An object.
    Object(ObjectId),
}