Debian Codegraph @

src/dr/geo/contouring/SnyderContour.java

raw symbols SCIP
1 /*
2 * SnyderContour.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 /* This class provides 2D contouring functionality. This code is adapted from
35 * ContourPlot.java by David Rand (1997) "Contour Plotting in Java" MacTech, volume 13.
36 * Rand, in turn, ported to Java from Fortan:
37 *
38 * Snyder WV (1978) "Algorithm 531, Contour Plotting [J6]", ACM Trans. Math. Softw., 4, 290-294.
39 *
40 * @author Marc Suchard
41 */
42
43 public class SnyderContour {
44
45 // Below, constant data members:
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 // Below, data members which store the grid steps,
61 // the z values, the interpolation flag, the dimensions
62 // of the contour plot and the increments in the grid:
63 int xSteps, ySteps;
64 float z[][];
65 boolean logInterpolation = false;
66 Dimension d;
67 double deltaX, deltaY;
68
69 // Below, data members, most of which are adapted from
70 // Fortran variables in Snyder's code:
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 // A constructor method.
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 // "DrawKernel" is the guts of drawing and is called
119 // directly or indirectly by "ContourPlotKernel" in order
120 // to draw a segment of a contour or to set the pen
121 // position "prevXY". Its action depends on "iflag":
122 //
123 // iflag == 1 means Continue a contour
124 // iflag == 2 means Start a contour at a boundary
125 // iflag == 3 means Start a contour not at a boundary
126 // iflag == 4 means Finish contour at a boundary
127 // iflag == 5 means Finish closed contour not at boundary
128 // iflag == 6 means Set pen position
129 //
130 // If the constant "SHOW_NUMBERS" is true then when
131 // completing a contour ("iflag" == 4 or 5) the contour
132 // index is drawn adjacent to where the contour ends.
133 //-------------------------------------------------------
134
135 void DrawKernel(List<LinkedList<Point2D>> allPaths) {
136 double //prevU,prevV,
137 u, v;
138
139 if ((iflag == 1) || (iflag == 4) || (iflag == 5)) { // continue drawing ...
140 if (cntrIndex != prevIndex) { // Must change colour
141 //SetColour(g);
142 prevIndex = cntrIndex;
143 }
144 // prevU = ((prevXY[0] - 1.0) * deltaX);
145 // prevV = ((prevXY[1] - 1.0) * deltaY);
146 u = ((xy[0] - 1.0) * deltaX) + offsetX;
147 v = ((xy[1] - 1.0) * deltaY) + offsetY;
148
149 // Interchange horizontal & vertical
150 // g.drawLine(PLOT_MARGIN+prevV,PLOT_MARGIN+prevU,
151 // PLOT_MARGIN+v, PLOT_MARGIN+u);
152 LinkedList<Point2D> path = allPaths.get(allPaths.size() - 1);
153 path.add(new Point2D.Double(u, v));
154 // if ((SHOW_NUMBERS) && ((iflag==4) || (iflag==5))) {
155 // if (u == 0) u = u - WEE_BIT;
156 // else if (u == d.width) u = u + PLOT_MARGIN/2;
157 // else if (v == 0) v = v - PLOT_MARGIN/2;
158 // else if (v == d.height) v = v + WEE_BIT;
159 // g.drawString(Integer.toString(cntrIndex),
160 // PLOT_MARGIN+v, PLOT_MARGIN+u);
161 // }
162 // TODO If end at boundary, close path.
163 }
164 if ((iflag == 2) || (iflag == 3)) { // start new path
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 // "DetectBoundary"
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 // "Routine_label_020" corresponds to a block of code
204 // starting at label 20 in Synder's subroutine "GCONTR".
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 // "Routine_label_050" corresponds to a block of code
227 // starting at label 50 in Synder's subroutine "GCONTR".
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 // "Routine_label_150" corresponds to a block of code
255 // starting at label 150 in Synder's subroutine "GCONTR".
256 //-------------------------------------------------------
257 boolean Routine_label_150() {
258 while (true) {
259 //------------------------------------------------
260 // Lines from z[ij[0]-1][ij[1]-1]
261 // to z[ij[0] ][ij[1]-1]
262 // and z[ij[0]-1][ij[1]]
263 // are not satisfactory. Continue the spiral.
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 // "Routine_label_200" corresponds to a block of code
305 // starting at label 200 in Synder's subroutine "GCONTR".
306 // It has return values 0, 1 or 2.
307 //-------------------------------------------------------
308 short Routine_label_200(//Graphics g
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; // 5. Finish a closed contour
326 iedge = ks + 2;
327 if (iedge > 4) iedge = iedge - 4;
328 intersect[iedge - 1] = intersect[ks - 1];
329 }
330 }
331
332 //-------------------------------------------------------
333 // "CrossedByContour" is true iff the current segment in
334 // the grid is crossed by one of the contour values and
335 // has not already been processed for that value.
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 // "ContinueContour" continues tracing a contour. Edges
358 // are numbered clockwise, the bottom edge being # 1.
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 // The contour crosses all 4 edges of cell being
391 // examined. Choose lines top-to-left & bottom-to-
392 // right if interpolation point on top edge is
393 // less than interpolation point on bottom edge.
394 // Otherwise, choose the other pair. This method
395 // produces the same results if axes are reversed.
396 // The contour may close at any edge, but must not
397 // cross itself inside any cell.
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 // Determine whether the contour will close or run
407 // into a boundary at edge ks of the current cell.
408 //----------------------------------------------------
409 elle = ks - 1;
410 iflag = 1; // 1. Continue a contour
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[]; // Allocate below if data valid
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 // "ContourPlotKernel" is the guts of this class and
441 // corresponds to Synder's subroutine "GCONTR".
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 // DrawKernel(g);
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; // Finish contour at boundary
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 // An acceptable line segment has been found.
496 // Follow contour until it hits a
497 // boundary or closes.
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 }