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