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.unit.SpeedUnit;
8 import org.djunits.value.ValueRuntimeException;
9 import org.djunits.value.storage.StorageType;
10 import org.djunits.value.vdouble.scalar.Direction;
11 import org.djunits.value.vdouble.scalar.Speed;
12 import org.djunits.value.vdouble.vector.SpeedVector;
13 import org.djunits.value.vdouble.vector.base.DoubleVector;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class Speed3d implements Serializable
32 {
33
34 private static final long serialVersionUID = 20160000L;
35
36
37 private final SpeedVector speed;
38
39
40
41
42
43
44 public Speed3d(final SpeedVector speed) throws ValueRuntimeException
45 {
46 if (speed.size() != 3)
47 {
48 throw new ValueRuntimeException("Size of an RPY-speed vector should be exactly 3. Got: " + speed);
49 }
50 this.speed = speed;
51 }
52
53
54
55
56
57
58
59
60 public Speed3d(final Speed x, final Speed y, final Speed z) throws ValueRuntimeException
61 {
62 this.speed = DoubleVector.instantiate(new Speed[] {x, y, z}, x.getDisplayUnit(), StorageType.DENSE);
63 }
64
65
66
67
68
69
70
71
72
73 public Speed3d(final double x, final double y, final double z, final SpeedUnit unit) throws ValueRuntimeException
74 {
75 this.speed = DoubleVector.instantiate(new double[] {x, y, z}, unit, StorageType.DENSE);
76 }
77
78
79
80
81
82
83
84
85 public Speed3d(final Speed speed, final Direction theta, final Direction phi) throws ValueRuntimeException
86 {
87 double[] xyz = Scalar3d.polarToCartesian(speed.getInUnit(), theta.si, phi.si);
88 this.speed = DoubleVector.instantiate(xyz, speed.getDisplayUnit(), StorageType.DENSE);
89 }
90
91
92
93
94
95 public final Speed getX()
96 {
97 try
98 {
99 return this.speed.get(0);
100 }
101 catch (ValueRuntimeException exception)
102 {
103
104 throw new RuntimeException(
105 "getX() gave an exception; apparently vector " + this.speed + " was not constructed right", exception);
106 }
107 }
108
109
110
111
112
113 public final Speed getY()
114 {
115 try
116 {
117 return this.speed.get(1);
118 }
119 catch (ValueRuntimeException exception)
120 {
121
122 throw new RuntimeException(
123 "getY() gave an exception; apparently vector " + this.speed + " was not constructed right", exception);
124 }
125 }
126
127
128
129
130
131 public final Speed getZ()
132 {
133 try
134 {
135 return this.speed.get(2);
136 }
137 catch (ValueRuntimeException exception)
138 {
139
140 throw new RuntimeException(
141 "getZ() gave an exception; apparently vector " + this.speed + " was not constructed right", exception);
142 }
143 }
144
145
146
147
148
149 public final Direction getTheta()
150 {
151 return Scalar3d.cartesianToTheta(getX().si, getY().si, getZ().si);
152 }
153
154
155
156
157
158 public final Direction getPhi()
159 {
160 return Scalar3d.cartesianToPhi(getX().si, getY().si);
161 }
162
163
164
165
166
167 public final Speed getSpeed()
168 {
169 return new Speed(Scalar3d.cartesianToRadius(getX().si, getY().si, getZ().si), SpeedUnit.SI);
170 }
171
172
173 @Override
174 public final String toString()
175 {
176 return String.format(Locale.US, "Speed3d %s (%s, theta %s, phi %s)", this.speed, getSpeed(),
177 new Direction(getTheta().getInUnit(DirectionUnit.EAST_DEGREE), DirectionUnit.EAST_DEGREE),
178 new Direction(getPhi().getInUnit(DirectionUnit.EAST_DEGREE), DirectionUnit.EAST_DEGREE));
179 }
180
181 }