1 package org.opentrafficsim.core.math;
2
3 import java.util.Locale;
4
5 import org.djunits.unit.AccelerationUnit;
6 import org.djunits.unit.AngleUnit;
7 import org.djunits.value.StorageType;
8 import org.djunits.value.ValueException;
9 import org.djunits.value.vdouble.scalar.Acceleration;
10 import org.djunits.value.vdouble.scalar.Angle;
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
31
32 public class Acceleration3D
33 {
34
35 private final AccelerationVector acceleration;
36
37
38
39
40
41
42 public Acceleration3D(final AccelerationVector acceleration) throws ValueException
43 {
44 super();
45 if (acceleration.size() != 3)
46 {
47 throw new ValueException("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 ValueException
60 {
61 super();
62 this.acceleration = new AccelerationVector(new Acceleration[]{x, y, z}, StorageType.DENSE);
63 }
64
65
66
67
68
69
70
71
72
73 public Acceleration3D(final double x, final double y, final double z, final AccelerationUnit unit)
74 throws ValueException
75 {
76 super();
77 this.acceleration = new AccelerationVector(new double[]{x, y, z}, unit, StorageType.DENSE);
78 }
79
80
81
82
83
84
85
86
87 public Acceleration3D(final Acceleration acceleration, final Angle.Abs theta, final Angle.Abs phi)
88 throws ValueException
89 {
90 super();
91 double[] xyz = Scalar3D.polarToCartesian(acceleration.getInUnit(), theta.si, phi.si);
92 this.acceleration = new AccelerationVector(xyz, acceleration.getUnit(), StorageType.DENSE);
93 }
94
95
96
97
98
99 public final Acceleration getX()
100 {
101 try
102 {
103 return this.acceleration.get(0);
104 }
105 catch (ValueException exception)
106 {
107
108 throw new RuntimeException("getX() gave an exception; apparently vector " + this.acceleration
109 + " was not constructed right", 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 (ValueException exception)
124 {
125
126 throw new RuntimeException("getY() gave an exception; apparently vector " + this.acceleration
127 + " was not constructed right", exception);
128 }
129 }
130
131
132
133
134
135 public final Acceleration getZ()
136 {
137 try
138 {
139 return this.acceleration.get(2);
140 }
141 catch (ValueException exception)
142 {
143
144 throw new RuntimeException("getZ() gave an exception; apparently vector " + this.acceleration
145 + " was not constructed right", exception);
146 }
147 }
148
149
150
151
152
153 public final Angle.Abs getTheta()
154 {
155 return Scalar3D.cartesianToTheta(getX().si, getY().si, getZ().si);
156 }
157
158
159
160
161
162 public final Angle.Abs 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 public final String toString()
178 {
179 return String.format(Locale.US, "Acceleration3D %s (%s, theta %s, phi %s)", this.acceleration,
180 getAcceleration(), new Angle.Abs(getTheta().getInUnit(AngleUnit.DEGREE), AngleUnit.DEGREE), new Angle.Abs(
181 getPhi().getInUnit(AngleUnit.DEGREE), AngleUnit.DEGREE));
182 }
183
184 }