View Javadoc
1   package org.opentrafficsim.core.math;
2   
3   import java.io.Serializable;
4   import java.util.Locale;
5   
6   import org.djunits.unit.DirectionUnit;
7   import org.djunits.value.ValueRuntimeException;
8   import org.djunits.value.storage.StorageType;
9   import org.djunits.value.vdouble.scalar.Direction;
10  import org.djunits.value.vdouble.vector.DirectionVector;
11  import org.djunits.value.vdouble.vector.base.DoubleVector;
12  
13  /**
14   * 3D-rotation, RPY coded (longitudinal roll along the x-axis, lateral pitch along the y-axis and vertical yaw along the
15   * z-axis), also called Tait–Bryan angles or Cardan angles. Angles are absolute and relate to the absolute XYZ-frame of the
16   * world.
17   * <p>
18   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20   * </p>
21   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
23   */
24  public class Direction3d implements Serializable
25  {
26      /** */
27      private static final long serialVersionUID = 20160000L;
28  
29      /** The angles of the rotation in 3D (RPY coded). */
30      private final DirectionVector rotation;
31  
32      /**
33       * @param rotation DirectionVector; the angles in 3D (RPY coded)
34       * @throws ValueRuntimeException in case the vector does not have exactly three elements
35       */
36      public Direction3d(final DirectionVector rotation) throws ValueRuntimeException
37      {
38          if (rotation.size() != 3)
39          {
40              throw new ValueRuntimeException("Size of an RPY-rotation vector should be exactly 3. Got: " + rotation);
41          }
42          this.rotation = rotation;
43      }
44  
45      /**
46       * @param roll Direction; (phi) the rotation around the x-axis
47       * @param pitch Direction; (theta) the rotation around the y-axis
48       * @param yaw Direction; (psi) the rotation around the z-axis
49       * @throws ValueRuntimeException in case the units are incorrect
50       */
51      public Direction3d(final Direction roll, final Direction pitch, final Direction yaw) throws ValueRuntimeException
52      {
53          this.rotation = DoubleVector.instantiate(new Direction[] {roll, pitch, yaw}, roll.getDisplayUnit(), StorageType.DENSE);
54      }
55  
56      /**
57       * @param roll double; (phi) the rotation around the x-axis
58       * @param pitch double; (theta) the rotation around the y-axis
59       * @param yaw double; (psi) the rotation around the z-axis
60       * @param unit DirectionUnit; the unit of the RPY parameters
61       * @throws ValueRuntimeException in case the units are incorrect
62       */
63      public Direction3d(final double roll, final double pitch, final double yaw, final DirectionUnit unit)
64              throws ValueRuntimeException
65      {
66          this.rotation = DoubleVector.instantiate(new double[] {roll, pitch, yaw}, unit, StorageType.DENSE);
67      }
68  
69      /**
70       * @return the roll.
71       */
72      public final Direction getRoll()
73      {
74          try
75          {
76              return this.rotation.get(0);
77          }
78          catch (ValueRuntimeException exception)
79          {
80              // should be impossible as we constructed the vector always with three elements
81              throw new RuntimeException(
82                      "getRoll() gave an exception; apparently vector " + this.rotation + " was not constructed right",
83                      exception);
84          }
85      }
86  
87      /**
88       * @return the pitch.
89       */
90      public final Direction getPitch()
91      {
92          try
93          {
94              return this.rotation.get(1);
95          }
96          catch (ValueRuntimeException exception)
97          {
98              // should be impossible as we constructed the vector always with three elements
99              throw new RuntimeException(
100                     "getPitch() gave an exception; apparently vector " + this.rotation + " was not constructed right",
101                     exception);
102         }
103     }
104 
105     /**
106      * @return the yaw.
107      */
108     public final Direction getYaw()
109     {
110         try
111         {
112             return this.rotation.get(2);
113         }
114         catch (ValueRuntimeException exception)
115         {
116             // should be impossible as we constructed the vector always with three elements
117             throw new RuntimeException(
118                     "getYaw() gave an exception; apparently vector " + this.rotation + " was not constructed right", exception);
119         }
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final String toString()
125     {
126         return String.format(Locale.US, "Rotation3d.Abs roll %s, pitch %s, yaw %s", getRoll(), getPitch(), getYaw());
127     }
128 }