Scalar3D.java

  1. package org.opentrafficsim.core.math;

  2. import org.djunits.unit.DirectionUnit;
  3. import org.djunits.value.vdouble.scalar.Direction;

  4. /**
  5.  * Calculate between Polar (spherical) and Cartesian (xyz) coordinates.
  6.  * <p>
  7.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  8.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  9.  * </p>
  10.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  11.  * initial version Dec 11, 2015 <br>
  12.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  13.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  14.  */
  15. public final class Scalar3D
  16. {
  17.     /** Utility class, don't instantiate. */
  18.     private Scalar3D()
  19.     {
  20.     }

  21.     /**
  22.      * @param r double; the radius
  23.      * @param theta double; the angle from the z direction
  24.      * @param phi double; the projected angle in the xy-plane from the x direction
  25.      * @return a double array [x, y, z] with cartesian coordinates
  26.      */
  27.     public static double[] polarToCartesian(final double r, final double theta, final double phi)
  28.     {
  29.         // double x = r * Math.sin(theta) * Math.cos(phi);
  30.         // double y = r * Math.sin(theta) * Math.sin(phi);
  31.         // double z = r * Math.cos(theta);

  32.         double mst = Math.sin(theta);
  33.         double msp = Math.sin(phi);
  34.         double mct = Math.sqrt(1.0 - mst * mst);
  35.         double mcp = Math.sqrt(1.0 - msp * msp);
  36.         return new double[] { r * mst * mcp, r * mst * msp, r * mct };
  37.     }

  38.     /**
  39.      * Get the (polar) radius based on Cartesian coordinates.
  40.      * @param x double; the x-coordinate
  41.      * @param y double; the y-coordinate
  42.      * @param z double; the z-coordinate
  43.      * @return the radius, which is the distance from (0,0,0)
  44.      */
  45.     public static double cartesianToRadius(final double x, final double y, final double z)
  46.     {
  47.         return Math.sqrt(x * x + y * y + z * z);
  48.     }

  49.     /**
  50.      * Get the (polar) theta angle, which is the angle from the z-direction, from Cartesian coordinates.
  51.      * @param x double; the x-coordinate
  52.      * @param y double; the y-coordinate
  53.      * @param z double; the z-coordinate
  54.      * @return the radius, which is the distance from (0,0,0)
  55.      */
  56.     public static Direction cartesianToTheta(final double x, final double y, final double z)
  57.     {
  58.         double r = Math.sqrt(x * x + y * y + z * z);
  59.         return new Direction(Math.acos(z / r), DirectionUnit.NORTH_RADIAN);
  60.     }

  61.     /**
  62.      * Get the (polar) phi angle, which is the projected angle in the xy-plane from the x direction.
  63.      * @param x double; the x-coordinate
  64.      * @param y double; the y-coordinate
  65.      * @return the projected angle of direction in the xy-plane
  66.      */
  67.     public static Direction cartesianToPhi(final double x, final double y)
  68.     {
  69.         return new Direction(Math.atan2(y, x), DirectionUnit.NORTH_RADIAN);
  70.     }

  71. }