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.vdouble.scalar.Direction;
9 import org.djunits.value.vdouble.vector.DirectionVector;
10
11
12
13
14
15
16
17
18
19
20
21
22 public class Direction3d implements Serializable
23 {
24
25 private static final long serialVersionUID = 20160000L;
26
27
28 private final DirectionVector rotation;
29
30
31
32
33
34 public Direction3d(final DirectionVector 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
45
46
47
48
49 public Direction3d(final Direction roll, final Direction pitch, final Direction yaw) throws ValueRuntimeException
50 {
51 this.rotation = new DirectionVector(new Direction[] {roll, pitch, yaw}, roll.getDisplayUnit());
52 }
53
54
55
56
57
58
59
60
61 public Direction3d(final double roll, final double pitch, final double yaw, final DirectionUnit unit)
62 throws ValueRuntimeException
63 {
64 this.rotation = new DirectionVector(new double[] {roll, pitch, yaw}, unit);
65 }
66
67
68
69
70 public final Direction getRoll()
71 {
72 try
73 {
74 return this.rotation.get(0);
75 }
76 catch (ValueRuntimeException exception)
77 {
78
79 throw new RuntimeException(
80 "getRoll() gave an exception; apparently vector " + this.rotation + " was not constructed right",
81 exception);
82 }
83 }
84
85
86
87
88 public final Direction getPitch()
89 {
90 try
91 {
92 return this.rotation.get(1);
93 }
94 catch (ValueRuntimeException exception)
95 {
96
97 throw new RuntimeException(
98 "getPitch() gave an exception; apparently vector " + this.rotation + " was not constructed right",
99 exception);
100 }
101 }
102
103
104
105
106 public final Direction getYaw()
107 {
108 try
109 {
110 return this.rotation.get(2);
111 }
112 catch (ValueRuntimeException exception)
113 {
114
115 throw new RuntimeException(
116 "getYaw() gave an exception; apparently vector " + this.rotation + " was not constructed right", exception);
117 }
118 }
119
120
121 @Override
122 public final String toString()
123 {
124 return String.format(Locale.US, "Rotation3d.Abs roll %s, pitch %s, yaw %s", getRoll(), getPitch(), getYaw());
125 }
126 }