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