| 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 |
package dr.geo.contouring; |
| 27 |
|
| 28 |
import java.awt.*; |
| 29 |
import java.awt.geom.Point2D; |
| 30 |
import java.util.LinkedList; |
| 31 |
import java.util.List; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class SnyderContour { |
| 44 |
|
| 45 |
|
| 46 |
final static boolean SHOW_NUMBERS = true; |
| 47 |
final static int BLANK = 32, |
| 48 |
OPEN_SUITE = (int) '{', |
| 49 |
CLOSE_SUITE = (int) '}', |
| 50 |
BETWEEN_ARGS = (int) ',', |
| 51 |
N_CONTOURS = 1, |
| 52 |
PLOT_MARGIN = 20, |
| 53 |
WEE_BIT = 3, |
| 54 |
NUMBER_LENGTH = 3; |
| 55 |
final static double Z_MAX_MAX = 1.0E+10, |
| 56 |
Z_MIN_MIN = -Z_MAX_MAX; |
| 57 |
final static String EOL = |
| 58 |
System.getProperty("line.separator"); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
int xSteps, ySteps; |
| 64 |
float z[][]; |
| 65 |
boolean logInterpolation = false; |
| 66 |
Dimension d; |
| 67 |
double deltaX, deltaY; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
int ncv = N_CONTOURS; |
| 72 |
int l1[] = new int[4]; |
| 73 |
int l2[] = new int[4]; |
| 74 |
int ij[] = new int[2]; |
| 75 |
int i1[] = new int[2]; |
| 76 |
int i2[] = new int[2]; |
| 77 |
int i3[] = new int[6]; |
| 78 |
int ibkey, icur, jcur, ii, jj, elle, ix, iedge, iflag, ni, ks; |
| 79 |
int cntrIndex, prevIndex; |
| 80 |
int idir, nxidir, k; |
| 81 |
double z1, z2, cval, zMax, zMin; |
| 82 |
double intersect[] = new double[4]; |
| 83 |
double xy[] = new double[2]; |
| 84 |
double prevXY[] = new double[2]; |
| 85 |
float cv[] = new float[ncv]; |
| 86 |
boolean jump; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public SnyderContour(int x, int y) { |
| 92 |
super(); |
| 93 |
xSteps = x; |
| 94 |
ySteps = y; |
| 95 |
} |
| 96 |
|
| 97 |
public void setDeltas(double xDelta, double yDelta) { |
| 98 |
this.deltaX = xDelta; |
| 99 |
this.deltaY = yDelta; |
| 100 |
} |
| 101 |
|
| 102 |
double offsetX, offsetY; |
| 103 |
|
| 104 |
public void setOffsets(double xOffset, double yOffset) { |
| 105 |
this.offsetX = xOffset; |
| 106 |
this.offsetY = yOffset; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
int sign(int a, int b) { |
| 112 |
a = Math.abs(a); |
| 113 |
if (b < 0) return -a; |
| 114 |
else return a; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
void DrawKernel(List<LinkedList<Point2D>> allPaths) { |
| 136 |
double |
| 137 |
u, v; |
| 138 |
|
| 139 |
if ((iflag == 1) || (iflag == 4) || (iflag == 5)) { |
| 140 |
if (cntrIndex != prevIndex) { |
| 141 |
|
| 142 |
prevIndex = cntrIndex; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
u = ((xy[0] - 1.0) * deltaX) + offsetX; |
| 147 |
v = ((xy[1] - 1.0) * deltaY) + offsetY; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
LinkedList<Point2D> path = allPaths.get(allPaths.size() - 1); |
| 153 |
path.add(new Point2D.Double(u, v)); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
} |
| 164 |
if ((iflag == 2) || (iflag == 3)) { |
| 165 |
u = ((xy[0] - 1.0) * deltaX) + offsetX; |
| 166 |
v = ((xy[1] - 1.0) * deltaY) + offsetY; |
| 167 |
LinkedList<Point2D> path = new LinkedList<Point2D>(); |
| 168 |
path.add(new Point2D.Double(u, v)); |
| 169 |
allPaths.add(path); |
| 170 |
} |
| 171 |
prevXY[0] = xy[0]; |
| 172 |
prevXY[1] = xy[1]; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void DetectBoundary() { |
| 179 |
ix = 1; |
| 180 |
if (ij[1 - elle] != 1) { |
| 181 |
ii = ij[0] - i1[1 - elle]; |
| 182 |
jj = ij[1] - i1[elle]; |
| 183 |
if (z[ii - 1][jj - 1] <= Z_MAX_MAX) { |
| 184 |
ii = ij[0] + i2[elle]; |
| 185 |
jj = ij[1] + i2[1 - elle]; |
| 186 |
if (z[ii - 1][jj - 1] < Z_MAX_MAX) ix = 0; |
| 187 |
} |
| 188 |
if (ij[1 - elle] >= l1[1 - elle]) { |
| 189 |
ix = ix + 2; |
| 190 |
return; |
| 191 |
} |
| 192 |
} |
| 193 |
ii = ij[0] + i1[1 - elle]; |
| 194 |
jj = ij[1] + i1[elle]; |
| 195 |
if (z[ii - 1][jj - 1] > Z_MAX_MAX) { |
| 196 |
ix = ix + 2; |
| 197 |
return; |
| 198 |
} |
| 199 |
if (z[ij[0]][ij[1]] >= Z_MAX_MAX) ix = ix + 2; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
boolean Routine_label_020() { |
| 207 |
l2[0] = ij[0]; |
| 208 |
l2[1] = ij[1]; |
| 209 |
l2[2] = -ij[0]; |
| 210 |
l2[3] = -ij[1]; |
| 211 |
idir = 0; |
| 212 |
nxidir = 1; |
| 213 |
k = 1; |
| 214 |
ij[0] = Math.abs(ij[0]); |
| 215 |
ij[1] = Math.abs(ij[1]); |
| 216 |
if (z[ij[0] - 1][ij[1] - 1] > Z_MAX_MAX) { |
| 217 |
elle = idir % 2; |
| 218 |
ij[elle] = sign(ij[elle], l1[k - 1]); |
| 219 |
return true; |
| 220 |
} |
| 221 |
elle = 0; |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
boolean Routine_label_050() { |
| 230 |
while (true) { |
| 231 |
if (ij[elle] >= l1[elle]) { |
| 232 |
if (++elle <= 1) continue; |
| 233 |
elle = idir % 2; |
| 234 |
ij[elle] = sign(ij[elle], l1[k - 1]); |
| 235 |
if (Routine_label_150()) return true; |
| 236 |
continue; |
| 237 |
} |
| 238 |
ii = ij[0] + i1[elle]; |
| 239 |
jj = ij[1] + i1[1 - elle]; |
| 240 |
if (z[ii - 1][jj - 1] > Z_MAX_MAX) { |
| 241 |
if (++elle <= 1) continue; |
| 242 |
elle = idir % 2; |
| 243 |
ij[elle] = sign(ij[elle], l1[k - 1]); |
| 244 |
if (Routine_label_150()) return true; |
| 245 |
continue; |
| 246 |
} |
| 247 |
break; |
| 248 |
} |
| 249 |
jump = false; |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
boolean Routine_label_150() { |
| 258 |
while (true) { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if (ij[elle] < l1[k - 1]) { |
| 266 |
ij[elle]++; |
| 267 |
if (ij[elle] > l2[k - 1]) { |
| 268 |
l2[k - 1] = ij[elle]; |
| 269 |
idir = nxidir; |
| 270 |
nxidir = idir + 1; |
| 271 |
k = nxidir; |
| 272 |
if (nxidir > 3) nxidir = 0; |
| 273 |
} |
| 274 |
ij[0] = Math.abs(ij[0]); |
| 275 |
ij[1] = Math.abs(ij[1]); |
| 276 |
if (z[ij[0] - 1][ij[1] - 1] > Z_MAX_MAX) { |
| 277 |
elle = idir % 2; |
| 278 |
ij[elle] = sign(ij[elle], l1[k - 1]); |
| 279 |
continue; |
| 280 |
} |
| 281 |
elle = 0; |
| 282 |
return false; |
| 283 |
} |
| 284 |
if (idir != nxidir) { |
| 285 |
nxidir++; |
| 286 |
ij[elle] = l1[k - 1]; |
| 287 |
k = nxidir; |
| 288 |
elle = 1 - elle; |
| 289 |
ij[elle] = l2[k - 1]; |
| 290 |
if (nxidir > 3) nxidir = 0; |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
if (ibkey != 0) return true; |
| 295 |
ibkey = 1; |
| 296 |
ij[0] = icur; |
| 297 |
ij[1] = jcur; |
| 298 |
if (Routine_label_020()) continue; |
| 299 |
return false; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
short Routine_label_200( |
| 309 |
List<LinkedList<Point2D>> allPaths |
| 310 |
, boolean workSpace[]) { |
| 311 |
while (true) { |
| 312 |
xy[elle] = 1.0 * ij[elle] + intersect[iedge - 1]; |
| 313 |
xy[1 - elle] = 1.0 * ij[1 - elle]; |
| 314 |
workSpace[2 * (xSteps * (ySteps * cntrIndex + ij[1] - 1) |
| 315 |
+ ij[0] - 1) + elle] = true; |
| 316 |
DrawKernel(allPaths); |
| 317 |
if (iflag >= 4) { |
| 318 |
icur = ij[0]; |
| 319 |
jcur = ij[1]; |
| 320 |
return 1; |
| 321 |
} |
| 322 |
ContinueContour(); |
| 323 |
if (!workSpace[2 * (xSteps * (ySteps * cntrIndex |
| 324 |
+ ij[1] - 1) + ij[0] - 1) + elle]) return 2; |
| 325 |
iflag = 5; |
| 326 |
iedge = ks + 2; |
| 327 |
if (iedge > 4) iedge = iedge - 4; |
| 328 |
intersect[iedge - 1] = intersect[ks - 1]; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
boolean CrossedByContour(boolean workSpace[]) { |
| 338 |
ii = ij[0] + i1[elle]; |
| 339 |
jj = ij[1] + i1[1 - elle]; |
| 340 |
z1 = z[ij[0] - 1][ij[1] - 1]; |
| 341 |
z2 = z[ii - 1][jj - 1]; |
| 342 |
for (cntrIndex = 0; cntrIndex < ncv; cntrIndex++) { |
| 343 |
int i = 2 * (xSteps * (ySteps * cntrIndex + ij[1] - 1) + ij[0] - 1) + elle; |
| 344 |
|
| 345 |
if (!workSpace[i]) { |
| 346 |
float x = cv[cntrIndex]; |
| 347 |
if ((x > Math.min(z1, z2)) && (x <= Math.max(z1, z2))) { |
| 348 |
workSpace[i] = true; |
| 349 |
return true; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
void ContinueContour() { |
| 361 |
short local_k; |
| 362 |
|
| 363 |
ni = 1; |
| 364 |
if (iedge >= 3) { |
| 365 |
ij[0] = ij[0] - i3[iedge - 1]; |
| 366 |
ij[1] = ij[1] - i3[iedge + 1]; |
| 367 |
} |
| 368 |
for (local_k = 1; local_k < 5; local_k++) |
| 369 |
if (local_k != iedge) { |
| 370 |
ii = ij[0] + i3[local_k - 1]; |
| 371 |
jj = ij[1] + i3[local_k]; |
| 372 |
z1 = z[ii - 1][jj - 1]; |
| 373 |
ii = ij[0] + i3[local_k]; |
| 374 |
jj = ij[1] + i3[local_k + 1]; |
| 375 |
z2 = z[ii - 1][jj - 1]; |
| 376 |
if ((cval > Math.min(z1, z2) && (cval <= Math.max(z1, z2)))) { |
| 377 |
if ((local_k == 1) || (local_k == 4)) { |
| 378 |
double zz = z2; |
| 379 |
|
| 380 |
z2 = z1; |
| 381 |
z1 = zz; |
| 382 |
} |
| 383 |
intersect[local_k - 1] = (cval - z1) / (z2 - z1); |
| 384 |
ni++; |
| 385 |
ks = local_k; |
| 386 |
} |
| 387 |
} |
| 388 |
if (ni != 2) { |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
ks = 5 - iedge; |
| 400 |
if (intersect[2] >= intersect[0]) { |
| 401 |
ks = 3 - iedge; |
| 402 |
if (ks <= 0) ks = ks + 4; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
elle = ks - 1; |
| 410 |
iflag = 1; |
| 411 |
jump = true; |
| 412 |
if (ks >= 3) { |
| 413 |
ij[0] = ij[0] + i3[ks - 1]; |
| 414 |
ij[1] = ij[1] + i3[ks + 1]; |
| 415 |
elle = ks - 3; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
void ContourKernel(double[][] data, List<LinkedList<Point2D>> allPaths, double level) { |
| 421 |
|
| 422 |
ncv = 1; |
| 423 |
cv[0] = (float) level; |
| 424 |
|
| 425 |
int workLength = 2 * xSteps * ySteps * ncv; |
| 426 |
boolean workSpace[]; |
| 427 |
|
| 428 |
z = new float[data.length][data[0].length]; |
| 429 |
for (int i = 0; i < data.length; i++) { |
| 430 |
for (int j = 0; j < data[i].length; j++) |
| 431 |
z[i][j] = (float) data[i][j]; |
| 432 |
} |
| 433 |
|
| 434 |
workSpace = new boolean[workLength]; |
| 435 |
ContourPlotKernel(allPaths, workSpace); |
| 436 |
|
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
void ContourPlotKernel(List<LinkedList<Point2D>> allPaths, |
| 444 |
boolean workSpace[]) { |
| 445 |
short val_label_200; |
| 446 |
|
| 447 |
l1[0] = xSteps; |
| 448 |
l1[1] = ySteps; |
| 449 |
l1[2] = -1; |
| 450 |
l1[3] = -1; |
| 451 |
i1[0] = 1; |
| 452 |
i1[1] = 0; |
| 453 |
i2[0] = 1; |
| 454 |
i2[1] = -1; |
| 455 |
i3[0] = 1; |
| 456 |
i3[1] = 0; |
| 457 |
i3[2] = 0; |
| 458 |
i3[3] = 1; |
| 459 |
i3[4] = 1; |
| 460 |
i3[5] = 0; |
| 461 |
prevXY[0] = 0.0; |
| 462 |
prevXY[1] = 0.0; |
| 463 |
xy[0] = 1.0; |
| 464 |
xy[1] = 1.0; |
| 465 |
cntrIndex = 0; |
| 466 |
prevIndex = -1; |
| 467 |
iflag = 6; |
| 468 |
|
| 469 |
icur = Math.max(1, Math.min((int) Math.floor(xy[0]), xSteps)); |
| 470 |
jcur = Math.max(1, Math.min((int) Math.floor(xy[1]), ySteps)); |
| 471 |
ibkey = 0; |
| 472 |
ij[0] = icur; |
| 473 |
ij[1] = jcur; |
| 474 |
if (Routine_label_020() && |
| 475 |
Routine_label_150()) return; |
| 476 |
if (Routine_label_050()) return; |
| 477 |
while (true) { |
| 478 |
DetectBoundary(); |
| 479 |
if (jump) { |
| 480 |
if (ix != 0) iflag = 4; |
| 481 |
iedge = ks + 2; |
| 482 |
if (iedge > 4) iedge = iedge - 4; |
| 483 |
intersect[iedge - 1] = intersect[ks - 1]; |
| 484 |
val_label_200 = Routine_label_200(allPaths, workSpace); |
| 485 |
if (val_label_200 == 1) { |
| 486 |
if (Routine_label_020() && Routine_label_150()) return; |
| 487 |
if (Routine_label_050()) return; |
| 488 |
continue; |
| 489 |
} |
| 490 |
if (val_label_200 == 2) continue; |
| 491 |
return; |
| 492 |
} |
| 493 |
if ((ix != 3) && (ix + ibkey != 0) && CrossedByContour(workSpace)) { |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
iedge = elle + 1; |
| 500 |
cval = cv[cntrIndex]; |
| 501 |
if (ix != 1) iedge = iedge + 2; |
| 502 |
iflag = 2 + ibkey; |
| 503 |
intersect[iedge - 1] = (cval - z1) / (z2 - z1); |
| 504 |
val_label_200 = Routine_label_200(allPaths, workSpace); |
| 505 |
if (val_label_200 == 1) { |
| 506 |
if (Routine_label_020() && Routine_label_150()) return; |
| 507 |
if (Routine_label_050()) return; |
| 508 |
continue; |
| 509 |
} |
| 510 |
if (val_label_200 == 2) continue; |
| 511 |
return; |
| 512 |
} |
| 513 |
if (++elle > 1) { |
| 514 |
elle = idir % 2; |
| 515 |
ij[elle] = sign(ij[elle], l1[k - 1]); |
| 516 |
if (Routine_label_150()) return; |
| 517 |
} |
| 518 |
if (Routine_label_050()) return; |
| 519 |
} |
| 520 |
} |
| 521 |
} |