OTSGeometryUtil.java

  1. package org.opentrafficsim.core.geometry;

  2. import java.util.Locale;

  3. /**
  4.  * Utility class for OTS geometry.
  5.  * <p>
  6.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  7.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  8.  * <p>
  9.  * $LastChangedDate: 2015-07-16 10:20:53 +0200 (Thu, 16 Jul 2015) $, @version $Revision: 1124 $, by $Author: pknoppers $,
  10.  * initial version Jul 22, 2015 <br>
  11.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  12.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  13.  * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
  14.  */
  15. public final class OTSGeometryUtil
  16. {
  17.     /** */
  18.     private OTSGeometryUtil()
  19.     {
  20.         // do not instantiate this class.
  21.     }

  22.     /**
  23.      * Print one OTSPoint3D on the console.
  24.      * @param prefix String; text to put before the output
  25.      * @param point OTSPoint3D; the coordinate to print
  26.      * @return String
  27.      */
  28.     public static String printCoordinate(final String prefix, final OTSPoint3D point)
  29.     {
  30.         return String.format(Locale.US, "%s %8.3f,%8.3f   ", prefix, point.x, point.y);
  31.     }

  32.     /**
  33.      * Build a string description from an array of coordinates.
  34.      * @param prefix String; text to put before the coordinates
  35.      * @param coordinates OTSPoint3D[]; the points to print
  36.      * @param separator String; prepended to each coordinate
  37.      * @return String; description of the array of coordinates
  38.      */
  39.     public static String printCoordinates(final String prefix, final OTSPoint3D[] coordinates, final String separator)
  40.     {
  41.         return printCoordinates(prefix + "(" + coordinates.length + " pts)", coordinates, 0, coordinates.length, separator);
  42.     }

  43.     /**
  44.      * Build a string description from an OTSLine3D.
  45.      * @param prefix String; text to put before the coordinates
  46.      * @param line OTSLine3D; the line for which to print the points
  47.      * @param separator String; prepended to each coordinate
  48.      * @return String; description of the OTSLine3D
  49.      */
  50.     public static String printCoordinates(final String prefix, final OTSLine3D line, final String separator)
  51.     {
  52.         return printCoordinates(prefix + "(" + line.size() + " pts)", line.getPoints(), 0, line.size(), separator);
  53.     }

  54.     /**
  55.      * Built a string description from part of an array of coordinates.
  56.      * @param prefix String; text to put before the output
  57.      * @param points OTSPoint3D[]; the coordinates to print
  58.      * @param fromIndex int; index of the first coordinate to print
  59.      * @param toIndex int; one higher than the index of the last coordinate to print
  60.      * @param separator String; prepended to each coordinate
  61.      * @return String; description of the selected part of the array of coordinates
  62.      */
  63.     public static String printCoordinates(final String prefix, final OTSPoint3D[] points, final int fromIndex,
  64.             final int toIndex, final String separator)
  65.     {
  66.         StringBuilder result = new StringBuilder();
  67.         result.append(prefix);
  68.         String operator = "M"; // Move absolute
  69.         for (int i = fromIndex; i < toIndex; i++)
  70.         {
  71.             result.append(separator);
  72.             result.append(printCoordinate(operator, points[i]));
  73.             operator = "L"; // LineTo Absolute
  74.         }
  75.         return result.toString();
  76.     }
  77.    
  78. }