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