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