Direction3D.java

  1. package org.opentrafficsim.core.math;

  2. import java.io.Serializable;
  3. import java.util.Locale;

  4. import org.djunits.unit.AngleUnit;
  5. import org.djunits.value.StorageType;
  6. import org.djunits.value.ValueException;
  7. import org.djunits.value.vdouble.scalar.Direction;
  8. import org.djunits.value.vdouble.vector.DirectionVector;

  9. /**
  10.  * 3D-rotation, RPY coded (longitudinal roll along the x-axis, lateral pitch along the y-axis and vertical yaw along the
  11.  * z-axis), also called Tait–Bryan angles or Cardan angles. Angles are absolute and relate to the absolute XYZ-frame of the
  12.  * world.
  13.  * <p>
  14.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  16.  * </p>
  17.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  18.  * initial version Dec 10, 2015 <br>
  19.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  20.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  21.  */
  22. public class Direction3D implements Serializable
  23. {
  24.     /** */
  25.     private static final long serialVersionUID = 20160000L;

  26.     /** The angles of the rotation in 3D (RPY coded). */
  27.     private final DirectionVector rotation;

  28.     /**
  29.      * @param rotation the angles in 3D (RPY coded)
  30.      * @throws ValueException in case the vector does not have exactly three elements
  31.      */
  32.     public Direction3D(final DirectionVector rotation) throws ValueException
  33.     {
  34.         if (rotation.size() != 3)
  35.         {
  36.             throw new ValueException("Size of an RPY-rotation vector should be exactly 3. Got: " + rotation);
  37.         }
  38.         this.rotation = rotation;
  39.     }

  40.     /**
  41.      * @param roll (phi) the rotation around the x-axis
  42.      * @param pitch (theta) the rotation around the y-axis
  43.      * @param yaw (psi) the rotation around the z-axis
  44.      * @throws ValueException in case the units are incorrect
  45.      */
  46.     public Direction3D(final Direction roll, final Direction pitch, final Direction yaw) throws ValueException
  47.     {
  48.         this.rotation = new DirectionVector(new Direction[] { roll, pitch, yaw }, StorageType.DENSE);
  49.     }

  50.     /**
  51.      * @param roll (phi) the rotation around the x-axis
  52.      * @param pitch (theta) the rotation around the y-axis
  53.      * @param yaw (psi) the rotation around the z-axis
  54.      * @param unit the unit of the RPY parameters
  55.      * @throws ValueException in case the units are incorrect
  56.      */
  57.     public Direction3D(final double roll, final double pitch, final double yaw, final AngleUnit unit) throws ValueException
  58.     {
  59.         this.rotation = new DirectionVector(new double[] { roll, pitch, yaw }, unit, StorageType.DENSE);
  60.     }

  61.     /**
  62.      * @return the roll.
  63.      */
  64.     public final Direction getRoll()
  65.     {
  66.         try
  67.         {
  68.             return this.rotation.get(0);
  69.         }
  70.         catch (ValueException exception)
  71.         {
  72.             // should be impossible as we constructed the vector always with three elements
  73.             throw new RuntimeException("getRoll() gave an exception; apparently vector " + this.rotation
  74.                     + " was not constructed right", exception);
  75.         }
  76.     }

  77.     /**
  78.      * @return the pitch.
  79.      */
  80.     public final Direction getPitch()
  81.     {
  82.         try
  83.         {
  84.             return this.rotation.get(1);
  85.         }
  86.         catch (ValueException exception)
  87.         {
  88.             // should be impossible as we constructed the vector always with three elements
  89.             throw new RuntimeException("getPitch() gave an exception; apparently vector " + this.rotation
  90.                     + " was not constructed right", exception);
  91.         }
  92.     }

  93.     /**
  94.      * @return the yaw.
  95.      */
  96.     public final Direction getYaw()
  97.     {
  98.         try
  99.         {
  100.             return this.rotation.get(2);
  101.         }
  102.         catch (ValueException exception)
  103.         {
  104.             // should be impossible as we constructed the vector always with three elements
  105.             throw new RuntimeException("getYaw() gave an exception; apparently vector " + this.rotation
  106.                     + " was not constructed right", exception);
  107.         }
  108.     }

  109.     /** {@inheritDoc} */
  110.     public final String toString()
  111.     {
  112.         return String.format(Locale.US, "Rotation3D.Abs roll %s, pitch %s, yaw %s", getRoll(), getPitch(), getYaw());
  113.     }
  114. }