Coordinates.java

  1. package org.opentrafficsim.core.network.factory.xml.units;

  2. import org.opentrafficsim.core.geometry.OTSPoint3D;

  3. /**
  4.  * <p>
  5.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  6.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  7.  * <p>
  8.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  9.  * initial version Jul 23, 2015 <br>
  10.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  11.  */
  12. public final class Coordinates
  13. {
  14.     /** Utility class. */
  15.     private Coordinates()
  16.     {
  17.         // do not instantiate
  18.     }

  19.     /**
  20.      * Parse a group of coordinates with (x,y) or (x,y,z).
  21.      * @param cs String; the string containing the coordinates
  22.      * @return OTSPoint3D[] containing the x,y or x,y,z values.
  23.      */
  24.     public static OTSPoint3D[] parseCoordinates(final String cs)
  25.     {
  26.         String cs1 = cs.replaceAll("\\s+", "");
  27.         String c = cs1.replace(")(", ")split(");
  28.         String[] cc = c.split("split");
  29.         OTSPoint3D[] coords = new OTSPoint3D[cc.length];
  30.         int i = 0;
  31.         for (String coord : cc)
  32.         {
  33.             coords[i] = parseCoordinate(coord);
  34.             i++;
  35.         }
  36.         return coords;

  37.     }

  38.     /**
  39.      * Parse a coordinate with (x,y) or (x,y,z).
  40.      * @param cs String; the string containing the coordinate
  41.      * @return OTSPoint3D containing the x,y or x,y,z values
  42.      */
  43.     public static OTSPoint3D parseCoordinate(final String cs)
  44.     {
  45.         String c = cs.replace("(", "");
  46.         c = c.replace(")", "");
  47.         String[] cc = c.split(",");
  48.         double x = Double.parseDouble(cc[0]);
  49.         double y = Double.parseDouble(cc[1]);
  50.         double z = cc.length > 2 ? Double.parseDouble(cc[2]) : 0.0;
  51.         return new OTSPoint3D(x, y, z);
  52.     }

  53. }