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