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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
20 * </p>
21 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
22 * initial version Dec 10, 2015 <br>
23 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25 */
26 public class Direction3D implements Serializable
27 {
28 /** */
29 private static final long serialVersionUID = 20160000L;
30
31 /** The angles of the rotation in 3D (RPY coded). */
32 private final DirectionVector rotation;
33
34 /**
35 * @param rotation DirectionVector; the angles in 3D (RPY coded)
36 * @throws ValueRuntimeException in case the vector does not have exactly three elements
37 */
38 public Direction3D(final DirectionVector rotation) throws ValueRuntimeException
39 {
40 if (rotation.size() != 3)
41 {
42 throw new ValueRuntimeException("Size of an RPY-rotation vector should be exactly 3. Got: " + rotation);
43 }
44 this.rotation = rotation;
45 }
46
47 /**
48 * @param roll Direction; (phi) the rotation around the x-axis
49 * @param pitch Direction; (theta) the rotation around the y-axis
50 * @param yaw Direction; (psi) the rotation around the z-axis
51 * @throws ValueRuntimeException in case the units are incorrect
52 */
53 public Direction3D(final Direction roll, final Direction pitch, final Direction yaw) throws ValueRuntimeException
54 {
55 this.rotation = DoubleVector.instantiate(new Direction[] {roll, pitch, yaw}, roll.getDisplayUnit(), StorageType.DENSE);
56 }
57
58 /**
59 * @param roll double; (phi) the rotation around the x-axis
60 * @param pitch double; (theta) the rotation around the y-axis
61 * @param yaw double; (psi) the rotation around the z-axis
62 * @param unit DirectionUnit; the unit of the RPY parameters
63 * @throws ValueRuntimeException in case the units are incorrect
64 */
65 public Direction3D(final double roll, final double pitch, final double yaw, final DirectionUnit unit)
66 throws ValueRuntimeException
67 {
68 this.rotation = DoubleVector.instantiate(new double[] {roll, pitch, yaw}, unit, StorageType.DENSE);
69 }
70
71 /**
72 * @return the roll.
73 */
74 public final Direction getRoll()
75 {
76 try
77 {
78 return this.rotation.get(0);
79 }
80 catch (ValueRuntimeException exception)
81 {
82 // should be impossible as we constructed the vector always with three elements
83 throw new RuntimeException(
84 "getRoll() gave an exception; apparently vector " + this.rotation + " was not constructed right",
85 exception);
86 }
87 }
88
89 /**
90 * @return the pitch.
91 */
92 public final Direction getPitch()
93 {
94 try
95 {
96 return this.rotation.get(1);
97 }
98 catch (ValueRuntimeException exception)
99 {
100 // should be impossible as we constructed the vector always with three elements
101 throw new RuntimeException(
102 "getPitch() gave an exception; apparently vector " + this.rotation + " was not constructed right",
103 exception);
104 }
105 }
106
107 /**
108 * @return the yaw.
109 */
110 public final Direction getYaw()
111 {
112 try
113 {
114 return this.rotation.get(2);
115 }
116 catch (ValueRuntimeException exception)
117 {
118 // should be impossible as we constructed the vector always with three elements
119 throw new RuntimeException(
120 "getYaw() gave an exception; apparently vector " + this.rotation + " was not constructed right", exception);
121 }
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public final String toString()
127 {
128 return String.format(Locale.US, "Rotation3D.Abs roll %s, pitch %s, yaw %s", getRoll(), getPitch(), getYaw());
129 }
130 }