comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/BackJumpCorrector.java @ 642:2dbbb5be30a1

Re-eanbled the calculation of the backjump correction. flys-artifacts/trunk@2026 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 27 May 2011 16:52:37 +0000
parents 663aa18bee7f
children 44175d4720f8
comparison
equal deleted inserted replaced
641:ab9b6cae0d0d 642:2dbbb5be30a1
36 36
37 public double [] getCorrected() { 37 public double [] getCorrected() {
38 return corrected; 38 return corrected;
39 } 39 }
40 40
41 private static final boolean isIncreasing(double [] ws) {
42 int inc = 0;
43 int dec = 0;
44 int sweet = (ws.length-1)/2;
45 for (int i = 1; i < ws.length; ++i) {
46 if (ws[i] > ws[i-1]) {
47 if (++inc > sweet) {
48 return true;
49 }
50 }
51 else if (++dec > sweet) {
52 return false;
53 }
54 }
55 return inc > sweet;
56 }
57
58 public boolean doCorrection(double [] km, double [] ws) { 41 public boolean doCorrection(double [] km, double [] ws) {
59 boolean isIncreasing = isIncreasing(ws); 42
60 43 boolean wsUp = isIncreasing(ws);
61 if (isIncreasing) { 44
62 // mirror along x axis to simulate decreasing values 45 if (wsUp) {
63 ws = (double [])ws.clone(); 46 km = swapClone(km);
64 for (int i = 0; i < ws.length; ++i) { 47 ws = swapClone(ws);
65 ws[i] = -ws[i]; 48 }
66 } 49
50 boolean kmUp = isIncreasing(km);
51
52 if (!kmUp) {
53 km = sumDiffs(km);
54 }
55
56 if (log.isDebugEnabled()) {
57 log.debug("BackJumpCorrector.doCorrection ------- enter");
58 log.debug(" km increasing: " + isIncreasing(km));
59 log.debug(" ws increasing: " + isIncreasing(ws));
60 log.debug("BackJumpCorrector.doCorrection ------- leave");
67 } 61 }
68 62
69 boolean hasBackJumps = doCorrectionClean(km, ws); 63 boolean hasBackJumps = doCorrectionClean(km, ws);
70 64
71 if (hasBackJumps && isIncreasing) { 65 if (hasBackJumps && wsUp) {
72 // mirror back 66 // mirror back
73 for (int i = 0; i < corrected.length; ++i) { 67 swap(corrected);
74 corrected[i] = -corrected[i];
75 }
76 } 68 }
77 69
78 return hasBackJumps; 70 return hasBackJumps;
79 } 71 }
80 72
132 } 124 }
133 break; 125 break;
134 } 126 }
135 127
136 if (back < 0) { 128 if (back < 0) {
137 log.debug("run over left border"); 129 //log.debug("run over left border");
138 // fill with same as ws[i] 130 // fill with same as ws[i]
139 for (int j = 0; j < i; ++j) { 131 for (int j = 0; j < i; ++j) {
140 ws[j] = ws[i]; 132 ws[j] = ws[i];
141 } 133 }
142 continue; 134 continue;
193 185
194 if (interpolator == null) { 186 if (interpolator == null) {
195 interpolator = new SplineInterpolator(); 187 interpolator = new SplineInterpolator();
196 } 188 }
197 189
190 if (log.isDebugEnabled()) {
191 for (int j = 0; j < x.length; ++j) {
192 log.debug(" " + x[j] + " -> " + y[j]);
193 }
194 }
195
198 PolynomialSplineFunction spline = interpolator.interpolate(x, y); 196 PolynomialSplineFunction spline = interpolator.interpolate(x, y);
199 197
200 try { 198 try {
201 if (log.isDebugEnabled()) { 199 if (log.isDebugEnabled()) {
202 log.debug("spline points:"); 200 log.debug("spline points:");
222 } 220 }
223 } // for all km 221 } // for all km
224 222
225 return !backjumps.isEmpty(); 223 return !backjumps.isEmpty();
226 } 224 }
225
226 public static final boolean isIncreasing(double [] array) {
227 int inc = 0;
228 int dec = 0;
229 int sweet = (array.length-1)/2;
230 for (int i = 1; i < array.length; ++i) {
231 if (array[i] > array[i-1]) {
232 if (++inc > sweet) {
233 return true;
234 }
235 }
236 else if (++dec > sweet) {
237 return false;
238 }
239 }
240 return inc > sweet;
241 }
242
243 public static final double [] swap(double [] array) {
244 int lo = 0;
245 int hi = array.length-1;
246 while (hi > lo) {
247 double t = array[lo];
248 array[lo] = array[hi];
249 array[hi] = t;
250 ++lo;
251 --hi;
252 }
253
254 return array;
255 }
256
257 public static final double [] swapClone(double [] array) {
258 double [] out = new double[array.length];
259 int lo = 0;
260
261 int hi = array.length-1;
262 while (hi > lo) {
263 out[lo] = array[hi];
264 out[hi] = array[lo];
265 ++lo;
266 --hi;
267 }
268 return out;
269 }
270
271 public static final double [] sumDiffs(double [] in) {
272 double [] out = new double[in.length];
273
274 if (out.length > 0) {
275 out[0] = 0d;
276 }
277
278 for (int i = 1; i < out.length; ++i) {
279 out[i] = out[i-1] + Math.abs(in[i-1] - in[i]);
280 }
281
282 return out;
283 }
227 } 284 }
228 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 285 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org