View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.opentrafficsim.core.geometry.OTSPoint3D;
7   import org.opentrafficsim.core.network.NetworkException;
8   import org.opentrafficsim.core.network.factory.xml.units.Coordinates;
9   import org.xml.sax.SAXException;
10  
11  /**
12   * <p>
13   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * <p>
16   * $LastChangedDate: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, by $Author: averbraeck $,
17   * initial version Jul 24, 2015 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  class PolyLineTag implements Serializable
21  {
22      /** */
23      private static final long serialVersionUID = 20150724L;
24  
25      /** Length. */
26      @SuppressWarnings("checkstyle:visibilitymodifier")
27      Length length = null;
28  
29      /** Points of the line. */
30      OTSPoint3D[] vertices = null;
31  
32      /**
33       * @param polyLineTag PolyLineTag; the parser with the lists of information
34       */
35      public PolyLineTag(PolyLineTag polyLineTag)
36      {
37          if (polyLineTag != null)
38          {
39              this.length = polyLineTag.length;
40              this.vertices = polyLineTag.vertices;
41          }
42      }
43  
44      /**
45       * @param length Length; length of the line
46       * @param vertices OTSPoint3D[]; the points of the line
47       */
48      public PolyLineTag(Length length, OTSPoint3D[] vertices)
49      {
50          super();
51          this.length = length;
52          this.vertices = vertices;
53      }
54  
55      /**
56       *
57       */
58      public PolyLineTag()
59      {
60          // TODO Auto-generated constructor stub
61      }
62  
63      /**
64       * Parse the LINK.POLYLINE tag.
65       * @param coords String; the XML-node to parse
66       * @param parser VissimNetworkLaneParser; the parser with the lists of information
67       * @param linkTag LinkTag; the parent link tag
68       * @throws SAXException when parsing of the tag fails
69       * @throws NetworkException when parsing of the tag fails
70       */
71      @SuppressWarnings("checkstyle:needbraces")
72      static void parsePolyLine(final String coords, final VissimNetworkLaneParser parser, final LinkTag linkTag)
73              throws SAXException, NetworkException
74      {
75          linkTag.polyLineTag = new PolyLineTag();
76          linkTag.polyLineTag.vertices = parseVertices(coords);
77      }
78  
79      /**
80       * Parse a coordinate with (x,y) or (x,y,z).
81       * @param cs String; the string containing the coordinate.
82       * @return a Point3d containing the x,y or x,y,z values.
83       */
84      public static OTSPoint3D[] parseVertices(final String cs)
85      {
86          String cs1 = cs.replaceAll("\\s+", "");
87          String c = cs1.replace(")(", ")split(");
88          String[] cc = c.split("split");
89          OTSPoint3D[] coords = new OTSPoint3D[cc.length - 2];
90          // only intermediate points: therefore the first and last are not included!!
91          int i = 0;
92          int vertexCount = 0;
93          for (String coord : cc)
94          {
95              if (i > 0 && i < cc.length - 1)
96              {
97                  coords[vertexCount] = Coordinates.parseCoordinate(coord);
98                  vertexCount++;
99              }
100             i++;
101         }
102         return coords;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public final String toString()
108     {
109         return "PolyLineTag [length=" + this.length + "]";
110     }
111 
112 }