-
Notifications
You must be signed in to change notification settings - Fork 4
/
Abriss.java
369 lines (300 loc) · 11.2 KB
/
Abriss.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package ch.hgdev.toposuite.calculation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import ch.hgdev.toposuite.App;
import ch.hgdev.toposuite.R;
import ch.hgdev.toposuite.SharedResources;
import ch.hgdev.toposuite.calculation.activities.abriss.AbrissActivity;
import ch.hgdev.toposuite.points.Point;
import ch.hgdev.toposuite.utils.MathUtils;
public class Abriss extends Calculation {
private static final String STATION_NUMBER = "station_number";
private static final String ORIENTATIONS_LIST = "orientations_list";
private Point station;
private final ArrayList<Measure> orientations;
private final ArrayList<Result> results;
private double mean;
/**
* MSE stands for Mean Squared Error.
*/
private double mse;
private double meanErrComp;
public Abriss(boolean hasDAO) {
this(null, hasDAO);
}
public Abriss(Point station, boolean hasDAO) {
super(CalculationType.ABRISS,
App.getContext().getString(R.string.title_activity_abriss),
hasDAO);
this.station = station;
this.orientations = new ArrayList<>();
this.results = new ArrayList<>();
this.mean = 0.0;
this.mse = 0.0;
}
public Abriss(long id, Date lastModification) {
super(id,
CalculationType.ABRISS,
App.getContext().getString(R.string.title_activity_abriss),
lastModification,
true);
this.orientations = new ArrayList<>();
this.results = new ArrayList<>();
}
/**
* Perform the the computation.
*/
@Override
public void compute() throws CalculationException {
if (this.orientations.size() == 0) {
throw new CalculationException("no measures provided");
}
if (this.station == null) {
throw new CalculationException("no station provided");
}
// if some measures have been deactivated, then we have to keep them
// and avoid to clear the previous results list
if (!this.hasDeactivatedMeasure()) {
this.results.clear();
}
this.mean = 0.0;
this.mse = 0.0;
// Small trick to take the deactivated measures into account during some
// of the computations.
int numberOfDeactivatedOrientations = 0;
int index = -1;
double adjZ0 = 0.0;
double z0Sum = 0.0;
for (Measure m : this.orientations) {
index++;
// zenithal angle value is optional, needs to be 100.0 by default
if (MathUtils.isIgnorable(m.getZenAngle())) {
m.setZenAngle(100.0); // default value for zenithal angle
}
// skip deactivated orientations
if (m.isDeactivated()) {
numberOfDeactivatedOrientations++;
continue;
}
Gisement g = new Gisement(this.station, m.getPoint(), false);
double z0 = MathUtils.modulo400(g.getGisement() - m.getHorizDir());
double calcDist = MathUtils.euclideanDistance(this.station, m.getPoint());
// special case for when angle values cross the 400 grad for some
// see issue #625
if (this.results.isEmpty()) {
adjZ0 = z0;
z0Sum = adjZ0;
} else {
// check diff with previous adjusted z0
double diff = z0 - adjZ0;
if (diff < -200) {
adjZ0 = z0 + 400;
} else if (diff > 200) {
adjZ0 = z0 - 400;
} else {
adjZ0 = z0;
}
z0Sum += adjZ0;
}
Result r = new Result(m.getPoint(), g.getHorizDist(),
z0, 0.0, g.getGisement(), calcDist, 0.0, 0.0, 0.0);
if (!this.hasDeactivatedMeasure()) {
this.results.add(r);
} else {
// just used as tmp variable for modifying the pointed value of the
// current result
@SuppressWarnings("unused")
Result oldResult = this.results.get(index);
oldResult = r;
}
}
this.mean = MathUtils.modulo400(z0Sum / (
this.orientations.size() - numberOfDeactivatedOrientations));
index = 0;
for (Measure m : this.orientations) {
// skip deactivated orientations
if (m.isDeactivated()) {
index++;
continue;
}
double orientDir = MathUtils.modulo400(this.mean + m.getHorizDir());
this.results.get(index).setOrientedDirection(orientDir);
// [cc]
double errAngle = (this.results.get(index).getGisement() - orientDir) * 10000;
this.results.get(index).setErrAngle(errAngle);
double calcDist = this.results.get(index).getCalculatedDistance();
// [cm]
double errTrans = (calcDist * (errAngle / 6366.2));
this.results.get(index).setErrTrans(errTrans);
// [cm] => measured distance - calculated distance
double errLong = MathUtils.IGNORE_DOUBLE;
// if no distance is provided, then there cannot be any error with regard to the distance
if (!MathUtils.isIgnorable(m.getDistance())) {
errLong = MathUtils.mToCm(calcDist - (Math.sin(MathUtils.gradToRad(m.getZenAngle())) * m.getDistance()));
}
this.results.get(index).setErrLong(errLong);
this.mse += Math.pow(errAngle, 2);
index++;
}
this.mse = Math.sqrt(this.mse / (index - numberOfDeactivatedOrientations - 1));
this.meanErrComp = this.mse / Math.sqrt(index - numberOfDeactivatedOrientations);
this.postCompute();
}
@Override
protected void postCompute() {
this.setDescription(this.getCalculationName()
+ " - " + App.getContext().getString(R.string.station_label) + ": "
+ this.getStation().toString());
super.postCompute();
}
@Override
public String exportToJSON() throws JSONException {
JSONObject json = new JSONObject();
if (this.station != null) {
json.put(Abriss.STATION_NUMBER, this.station.getNumber());
}
if (this.orientations.size() > 0) {
JSONArray orientationsArray = new JSONArray();
for (Measure m : this.orientations) {
orientationsArray.put(m.toJSONObject());
}
json.put(Abriss.ORIENTATIONS_LIST, orientationsArray);
}
return json.toString();
}
@Override
public void importFromJSON(String jsonInputArgs) throws JSONException {
JSONObject json = new JSONObject(jsonInputArgs);
this.station = SharedResources.getSetOfPoints().find(
json.getString(Abriss.STATION_NUMBER));
JSONArray orientationsArray = json.getJSONArray(Abriss.ORIENTATIONS_LIST);
for (int i = 0; i < orientationsArray.length(); i++) {
JSONObject jo = (JSONObject) orientationsArray.get(i);
Measure m = new Measure(
SharedResources.getSetOfPoints().find(
jo.getString(Measure.ORIENTATION_NUMBER)),
jo.getDouble(Measure.HORIZ_DIR),
jo.getDouble(Measure.ZEN_ANGLE),
jo.getDouble(Measure.DISTANCE),
jo.getDouble(Measure.S));
this.orientations.add(m);
}
}
@Override
public Class<?> getActivityClass() {
return AbrissActivity.class;
}
@Override
public String getCalculationName() {
return App.getContext().getString(R.string.title_activity_abriss);
}
public Point getStation() {
return this.station;
}
public void setStation(Point station) {
this.station = station;
}
public ArrayList<Measure> getMeasures() {
return this.orientations;
}
public ArrayList<Result> getResults() {
return this.results;
}
public double getMean() {
return this.mean;
}
/**
* Getter for the Mean Squared Error.
*
* @return
*/
public double getMSE() {
return this.mse;
}
public double getMeanErrComp() {
return this.meanErrComp;
}
private boolean hasDeactivatedMeasure() {
for (Measure m : this.orientations) {
if (m.isDeactivated()) {
return true;
}
}
return false;
}
public class Result implements Serializable {
private final Point orientation;
private final double distance;
private final double unknownOrientation;
private double orientatedDirection;
private final double gisement;
private final double calculatedDistance;
private double errAngle;
private double errTrans;
private double errLong;
private boolean deactivated;
public Result(Point _orientation, double _distance, double _unknownOrientation,
double _orientationDirection, double _gisement, double _calculatedDistance,
double _errAngle, double _errTrans, double _errLong) {
this.orientation = _orientation;
this.distance = _distance;
this.unknownOrientation = _unknownOrientation;
this.orientatedDirection = _orientationDirection;
this.gisement = _gisement;
this.calculatedDistance = _calculatedDistance;
this.errAngle = _errAngle;
this.errTrans = _errTrans;
this.errLong = _errLong;
this.deactivated = false;
}
public Point getOrientation() {
return this.orientation;
}
public double getDistance() {
return this.distance;
}
public double getUnknownOrientation() {
return this.unknownOrientation;
}
public double getOrientedDirection() {
return this.orientatedDirection;
}
public void setOrientedDirection(double _orientedDirection) {
this.orientatedDirection = _orientedDirection;
}
public double getGisement() {
return this.gisement;
}
public double getCalculatedDistance() {
return this.calculatedDistance;
}
public double getErrAngle() {
return this.errAngle;
}
public void setErrAngle(double _errAngle) {
this.errAngle = _errAngle;
}
public double getErrTrans() {
return this.errTrans;
}
public void setErrTrans(double _errTrans) {
this.errTrans = _errTrans;
}
public double getErrLong() {
return this.errLong;
}
public void setErrLong(double _errLong) {
this.errLong = _errLong;
}
public final boolean isDeactivated() {
return this.deactivated;
}
public final void toggle() {
this.deactivated = !this.deactivated;
}
}
}