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