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
15
16
17
18
19
20
21
22
23
24
25
26 public class Direction3D implements Serializable
27 {
28
29 private static final long serialVersionUID = 20160000L;
30
31
32 private final DirectionVector rotation;
33
34
35
36
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
49
50
51
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
60
61
62
63
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
73
74 public final Direction getRoll()
75 {
76 try
77 {
78 return this.rotation.get(0);
79 }
80 catch (ValueRuntimeException exception)
81 {
82
83 throw new RuntimeException(
84 "getRoll() gave an exception; apparently vector " + this.rotation + " was not constructed right",
85 exception);
86 }
87 }
88
89
90
91
92 public final Direction getPitch()
93 {
94 try
95 {
96 return this.rotation.get(1);
97 }
98 catch (ValueRuntimeException exception)
99 {
100
101 throw new RuntimeException(
102 "getPitch() gave an exception; apparently vector " + this.rotation + " was not constructed right",
103 exception);
104 }
105 }
106
107
108
109
110 public final Direction getYaw()
111 {
112 try
113 {
114 return this.rotation.get(2);
115 }
116 catch (ValueRuntimeException exception)
117 {
118
119 throw new RuntimeException(
120 "getYaw() gave an exception; apparently vector " + this.rotation + " was not constructed right", exception);
121 }
122 }
123
124
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 }