View Javadoc
1   package org.opentrafficsim.road.network.factory.opendrive;
2   
3   import java.io.Serializable;
4   import java.util.UUID;
5   
6   import org.djunits.unit.AngleUnit;
7   import org.djunits.unit.LengthUnit;
8   import org.djunits.value.vdouble.scalar.Angle;
9   import org.djunits.value.vdouble.scalar.Length;
10  import org.opentrafficsim.core.geometry.OTSLine3D;
11  import org.opentrafficsim.core.geometry.OTSPoint3D;
12  import org.opentrafficsim.core.network.Network;
13  import org.opentrafficsim.core.network.NetworkException;
14  import org.opentrafficsim.core.network.OTSNode;
15  import org.w3c.dom.NamedNodeMap;
16  import org.w3c.dom.Node;
17  import org.xml.sax.SAXException;
18  
19  /**
20   * Parser for geometry tag.
21   * <p>
22   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * <p>
25   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
26   * initial version Jul 23, 2015 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   */
29  class GeometryTag implements Serializable
30  {
31  
32      /** */
33      private static final long serialVersionUID = 20150723L;
34  
35      /** Sequence of the geometry. */
36      @SuppressWarnings("checkstyle:visibilitymodifier")
37      String id = null;
38  
39      /** Start position (s-coordinate). */
40      @SuppressWarnings("checkstyle:visibilitymodifier")
41      Length s = null;
42  
43      /** The x position (s-coordinate). */
44      @SuppressWarnings("checkstyle:visibilitymodifier")
45      Length x = null;
46  
47      /** The y position (s-coordinate). */
48      @SuppressWarnings("checkstyle:visibilitymodifier")
49      Length y = null;
50  
51      /** The z position (s-coordinate). */
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      Length z = null;
54  
55      /** The hdg position (s-coordinate). */
56      @SuppressWarnings("checkstyle:visibilitymodifier")
57      Angle hdg = null;
58  
59      /** Total length of the reference line in the xy-plane, as indicated in the XML document. */
60      @SuppressWarnings("checkstyle:visibilitymodifier")
61      Length length = null;
62  
63      /** SpiralTag */
64      @SuppressWarnings("checkstyle:visibilitymodifier")
65      SpiralTag spiralTag = null;
66  
67      /** ArcTag */
68      @SuppressWarnings("checkstyle:visibilitymodifier")
69      ArcTag arcTag = null;
70  
71      /** The calculated Node, either through a coordinate or after calculation. */
72      @SuppressWarnings("checkstyle:visibilitymodifier")
73      OTSNode node = null;
74  
75      OTSLine3D interLine = null;
76  
77      /**
78       * Parse the attributes of the road tag. The sub-elements are parsed in separate classes.
79       * @param node Node; the top-level road node
80       * @param parser OpenDriveNetworkLaneParser; the parser with the lists of information
81       * @return the generated RoadTag for further reference
82       * @throws SAXException when parsing of the tag fails
83       */
84      @SuppressWarnings("checkstyle:needbraces")
85      static GeometryTag parseGeometry(final Node node, final OpenDriveNetworkLaneParser parser) throws SAXException
86      {
87          NamedNodeMap attributes = node.getAttributes();
88          GeometryTag geometryTag = new GeometryTag();
89  
90          Node s = attributes.getNamedItem("s");
91          if (s == null)
92              throw new SAXException("Geometry: missing attribute s");
93          geometryTag.s = new Length(Double.parseDouble(s.getNodeValue().trim()), LengthUnit.METER);
94  
95          Node x = attributes.getNamedItem("x");
96          if (x == null)
97              throw new SAXException("Geometry: missing attribute x");
98          geometryTag.x = new Length(Double.parseDouble(x.getNodeValue().trim()), LengthUnit.METER);
99  
100         Node y = attributes.getNamedItem("y");
101         if (y == null)
102             throw new SAXException("Geometry: missing attribute y");
103         geometryTag.y = new Length(Double.parseDouble(y.getNodeValue().trim()), LengthUnit.METER);
104 
105         Node hdg = attributes.getNamedItem("hdg");
106         if (hdg == null)
107             throw new SAXException("Geometry: missing attribute hdg");
108         geometryTag.hdg = new Angle(Double.parseDouble(hdg.getNodeValue().trim()), AngleUnit.RADIAN);
109 
110         Node length = attributes.getNamedItem("length");
111         if (length == null)
112             throw new SAXException("Geometry: missing attribute length");
113         geometryTag.length = new Length(Double.parseDouble(length.getNodeValue().trim()), LengthUnit.METER);
114 
115         SpiralTag.parseSpiral(node.getChildNodes(), parser, geometryTag);
116 
117         ArcTag.parseArc(node.getChildNodes(), parser, geometryTag);
118 
119         return geometryTag;
120     }
121 
122     /**
123      * @param network Network; the network
124      * @param geometryTag GeometryTag; the tag with the info for the node.
125      * @return a constructed node
126      * @throws NetworkException if node already exists in the network, or if name of the node is not unique.
127      */
128     static OTSNode makeOTSNode(final Network network, final GeometryTag geometryTag) throws NetworkException
129     {
130         OTSPoint3D coordinate =
131                 new OTSPoint3D(geometryTag.x.doubleValue(), geometryTag.y.doubleValue(), geometryTag.z.doubleValue());
132 
133         if (geometryTag.id == null)
134         {
135             geometryTag.id = UUID.randomUUID().toString();
136         }
137 
138         OTSNode node = new OTSNode(network, geometryTag.id, coordinate);
139         geometryTag.node = node;
140         return node;
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final String toString()
146     {
147         return "GeometryTag [id=" + this.id + ", s=" + this.s + ", x=" + this.x + ", y=" + this.y + ", z=" + this.z + ", hdg="
148                 + this.hdg + ", length=" + this.length + ", spiralTag=" + this.spiralTag + ", arcTag=" + this.arcTag + ", node="
149                 + this.node + ", interLine=" + this.interLine + "]";
150     }
151 }