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