Skip to main content

opendrive/junction/
mod.rs

1use crate::core::additional_data::AdditionalData;
2use crate::junction::connection::Connection;
3use crate::junction::controller::Controller;
4use crate::junction::priority::Priority;
5use crate::junction::surface::Surface;
6use crate::object::orientation::Orientation;
7use junction_type::JunctionType;
8use std::borrow::Cow;
9use uom::si::f64::Length;
10use uom::si::length::meter;
11use vec1::Vec1;
12
13pub mod connection;
14pub mod connection_type;
15pub mod contact_point;
16pub mod controller;
17pub mod crg;
18pub mod crg_mode;
19pub mod crg_purpose;
20pub mod element_dir;
21pub mod junction_group;
22pub mod junction_group_type;
23pub mod junction_reference;
24pub mod junction_type;
25pub mod lane_link;
26pub mod predecessor_successor;
27pub mod priority;
28pub mod surface;
29
30#[derive(Debug, Clone, PartialEq)]
31pub struct Junction {
32    pub connection: Vec1<Connection>,
33    pub priority: Vec<Priority>,
34    pub controller: Vec<Controller>,
35    pub surface: Option<Surface>,
36    /// Unique ID within database
37    pub id: String,
38    /// The main road from which the connecting roads of the virtual junction branch off. This
39    /// attribute is mandatory for virtual junctions and shall not be specified for other junction
40    /// types.
41    pub main_road: Option<String>,
42    /// Name of the junction. May be chosen freely.
43    pub name: Option<String>,
44    /// Defines the relevance of the virtual junction according to the driving direction. This
45    /// attribute is mandatory for virtual junctions and shall not be specified for other junction
46    /// types. The enumerator "none" specifies that the virtual junction is valid in both
47    /// directions.
48    pub orientation: Option<Orientation>,
49    /// End position of the virtual junction in the reference line coordinate system. This attribute
50    /// is mandatory for virtual junctions and shall not be specified for other junction types.
51    pub s_end: Option<Length>,
52    /// Start position of the virtual junction in the reference line coordinate system. This
53    /// attribute is mandatory for virtual junctions and shall not be specified for other junction
54    /// types.
55    pub s_start: Option<Length>,
56    /// Type of the junction. Common junctions are of type "default". This attribute is mandatory
57    /// for virtual junctions and direct junctions. If the attribute is not specified, the junction
58    /// type is "default".
59    pub r#type: Option<JunctionType>,
60    pub additional_data: AdditionalData,
61}
62
63impl Junction {
64    pub fn visit_attributes(
65        &self,
66        visitor: impl for<'b> FnOnce(
67            Cow<'b, [xml::attribute::Attribute<'b>]>,
68        ) -> xml::writer::Result<()>,
69    ) -> xml::writer::Result<()> {
70        visit_attributes_flatten!(
71            visitor,
72            "id" => Some(self.id.as_str()),
73            "mainRoad" => self.main_road.as_deref(),
74            "name" => self.name.as_deref(),
75            "orientation" => self.orientation.as_ref().map(Orientation::as_str),
76            "sEnd" => self.s_end.map(|v| v.value.to_scientific_string()).as_deref(),
77            "sStart" => self.s_start.map(|v| v.value.to_scientific_string()).as_deref(),
78            "type" => self.r#type.as_ref().map(JunctionType::as_str),
79        )
80    }
81
82    pub fn visit_children(
83        &self,
84        mut visitor: impl FnMut(xml::writer::XmlEvent) -> xml::writer::Result<()>,
85    ) -> xml::writer::Result<()> {
86        for connection in &self.connection {
87            visit_children!(visitor, "connection" => connection);
88        }
89
90        for priority in &self.priority {
91            visit_children!(visitor, "priority" => priority);
92        }
93
94        for controller in &self.controller {
95            visit_children!(visitor, "controller" => controller);
96        }
97
98        if let Some(surface) = &self.surface {
99            visit_children!(visitor, "surface" => surface);
100        }
101
102        self.additional_data.append_children(visitor)
103    }
104}
105
106impl<'a, I> TryFrom<crate::parser::ReadContext<'a, I>> for Junction
107where
108    I: Iterator<Item = xml::reader::Result<xml::reader::XmlEvent>>,
109{
110    type Error = Box<crate::parser::Error>;
111
112    fn try_from(mut read: crate::parser::ReadContext<'a, I>) -> Result<Self, Self::Error> {
113        let mut connection = Vec::new();
114        let mut priority = Vec::new();
115        let mut controller = Vec::new();
116        let mut surface = None;
117        let mut additional_data = AdditionalData::default();
118
119        match_child_eq_ignore_ascii_case!(
120            read,
121            "connection" true => Connection => |v| connection.push(v),
122            "priority" => Priority => |v| priority.push(v),
123            "controller" => Controller => |v| controller.push(v),
124            "surface" => Surface => |v| surface = Some(v),
125            _ => |_name, context| additional_data.fill(context),
126        );
127
128        Ok(Self {
129            connection: Vec1::try_from(connection).unwrap(),
130            priority,
131            controller,
132            surface,
133            id: read.attribute("id")?,
134            main_road: read.attribute_opt("mainRoad")?,
135            name: read.attribute_opt("name")?,
136            orientation: read.attribute_opt("orientation")?,
137            s_end: read.attribute_opt("sEnd")?.map(Length::new::<meter>),
138            s_start: read.attribute_opt("sStart")?.map(Length::new::<meter>),
139            r#type: read.attribute_opt("type")?,
140            additional_data,
141        })
142    }
143}
144
145#[cfg(feature = "fuzzing")]
146impl arbitrary::Arbitrary<'_> for Junction {
147    fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
148        use crate::fuzzing::NotNan;
149        Ok(Self {
150            connection: {
151                let mut vec1 = Vec1::new(u.arbitrary()?);
152                vec1.extend(u.arbitrary::<Vec<_>>()?);
153                vec1
154            },
155            priority: u.arbitrary()?,
156            controller: u.arbitrary()?,
157            surface: u.arbitrary()?,
158            id: u.arbitrary()?,
159            main_road: u.arbitrary()?,
160            name: u.arbitrary()?,
161            orientation: u.arbitrary()?,
162            s_start: u
163                .arbitrary::<Option<()>>()?
164                .map(|_| u.not_nan_f64())
165                .transpose()?
166                .map(Length::new::<meter>),
167            s_end: u
168                .arbitrary::<Option<()>>()?
169                .map(|_| u.not_nan_f64())
170                .transpose()?
171                .map(Length::new::<meter>),
172            r#type: u.arbitrary()?,
173            additional_data: u.arbitrary()?,
174        })
175    }
176}