Debian Codegraph @

src/dr/geo/contouring/ContourGenerator.java

raw symbols SCIP
1 /*
2 * ContourGenerator.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 dr.geo.contouring.ContourAttrib;
29
30 import java.util.*;
31
32
33 /**
34 * <p> An object used to generate a list of contour lines
35 * or paths from a set of gridded three dimensional data.
36 * </p>
37 *
38 * <p> Based on contour_plot.c from NeXTcontour1.4 by Thomas H. Pulliam,
39 * [email protected], MS 202A-1 NASA Ames Research Center,
40 * Moffett Field, CA 94035.
41 * I don't know how the original Fortran code looked like or where it came from,
42 * other than that NeXTcontour1.4 is based on Pieter Bunings' PLOT3D package
43 * for Computational Fluid Dynamics.
44 * </p>
45 *
46 * <p> Ported from C to Java by Joseph A. Huwaldt, November 16, 2000. </p>
47 *
48 * <p> Modified by: Joseph A. Huwaldt </p>
49 *
50 * @author Joseph A. Huwaldt Date: November 11, 2000
51 * @version November 23, 2000
52 *
53 * @author Marc Suchard
54 *
55 **/
56 public class ContourGenerator {
57
58 // Debug flag.
59 private static final boolean DEBUG = false;
60
61 // Error messages.
62 private static final String kCancelMsg = "Method ContourGenerator.getContours() canceled by user.";
63 private static final String kInconsistantArrMsg = "Inconsistant array sizes.";
64 private static final String kArrSizeMsg = "Data arrays must have more than one row or column.";
65 private static final String kNegLogDataMsg = "Function data must be > 0 for logarithmic intervals.";
66
67 // Path buffer size.
68 private static final int kBufSize = 1000;
69
70 // The minimum number of points allowed in a contour path.
71 private static final int kMinNumPoints = 3;
72
73 // A list of contour paths.
74 private List pathList = new ArrayList();
75
76 // A flag to indicate that the contours have been computed or not.
77 private boolean cCalculated = false;
78
79 // Data arrays used for generating the contours.
80 private double[][] xArray, yArray, funcArray;
81
82 // Data arrays used when generating contours for 1D X & Y arrays.
83 private double[] xArr1D, yArr1D;
84
85 // Array of contour attributes, one for each contour level.
86 private ContourAttrib[] cAttr;
87
88 // The fraction of the task that is completed.
89 private float fracComplete = 0;
90
91 /**
92 * Used to indicate that the user wishes to cancel the calculation
93 * of contours.
94 **/
95 private boolean isCanceled = false;
96
97
98 // Variables in the original FORTRAN program.
99 private double[] pathbufxt, pathbufyt;
100 private int[] pathbufia;
101 private int lnstrt; // lnstrt=1 indicates starting a new line.
102 private int ignext;
103 private int icont; // Current contour level index.
104 private double cont; // The current contour level.
105 private int iss, iee, jss, jee; // i & j start and end index values.
106 private int ima; // ima tells which boundary region we are on.
107 private int iae; // Index to last element in the IA list.
108 private int ibeg, jbeg;
109 private int gi, gj; // Indexes into data arrays.
110 private double fij; // Data value at i,j in data array.
111 private int idir; // Indicates current direction.
112 private int np=0; // Number of points in current contour line.
113 private double wx=0, wy=0; // Starting point of a contour line.
114
115
116 /**
117 * Construct a ContourGenerator object using the specified data arrays
118 * and the specified attribute array. This constructor allows you
119 * to use data on an uneven X, Y grid.
120 *
121 * @param xArr 2D array containing the grid x coordinate data.
122 * @param yArr 2D array containing the grid y coordinate data.
123 * @param fArr 2D array containing the grid function (z) data.
124 * @param cAttr Array containing attributes of the contour levels.
125 **/
126 public ContourGenerator(double[][] xArr, double[][] yArr, double[][] fArr, ContourAttrib[] cAttr) {
127
128 // Make sure input data is reasonable.
129 if (yArr.length != xArr.length || yArr.length != fArr.length)
130 throw new IllegalArgumentException(kInconsistantArrMsg);
131 if (yArr[0].length != xArr[0].length || yArr[0].length != fArr[0].length)
132 throw new IllegalArgumentException(kInconsistantArrMsg);
133 if (xArr.length <= 1 || xArr[0].length <= 1)
134 throw new IllegalArgumentException(kArrSizeMsg);
135
136 this.cAttr = cAttr;
137 xArray = xArr;
138 yArray = yArr;
139 funcArray = fArr;
140
141 }
142
143 /**
144 * Construct a ContourGenerator object using the specified data arrays
145 * and the specified attribute array. This constructor allows you
146 * to use data on an evenly spaced grid where "X" values are invarient
147 * with "Y" and "Y" values are invarient with "X". This often occures
148 * where the data is on an evenly spaced cartesian grid.
149 *
150 * @param xArr 1D array containing the grid x coordinate data.
151 * @param yArr 1D array containing the grid y coordinate data.
152 * @param fArr 2D array containing the grid function (z) data.
153 * @param cAttr Array containing attributes of the contour levels.
154 **/
155 public ContourGenerator(double[] xArr, double[] yArr, double[][] fArr, ContourAttrib[] cAttr) {
156
157 // Make sure input data is reasonable.
158 if (yArr.length != fArr.length || xArr.length != fArr[0].length)
159 throw new IllegalArgumentException(kInconsistantArrMsg);
160 if (xArr.length <= 1)
161 throw new IllegalArgumentException(kArrSizeMsg);
162
163 this.cAttr = cAttr;
164 xArr1D = xArr;
165 yArr1D = yArr;
166 funcArray = fArr;
167 }
168
169 /**
170 * Construct a ContourGenerator object using the specified data arrays.
171 * Contour attributes, including the interval, are generated
172 * automatically. This constructor allows you to use data on an
173 * uneven X, Y grid.
174 *
175 * @param xArr 2D array containing the grid x coordinate data.
176 * @param yArr 2D array containing the grid y coordinate data.
177 * @param fArr 2D array containing the grid function (z) data.
178 * @param nc The number of contour levels to generate.
179 * @param logInterval Uses a logarithmic contour interval if true, and
180 * uses a linear interval if false.
181 **/
182 public ContourGenerator(double[][] xArr, double[][] yArr, double[][] fArr,
183 int nc, boolean logInterval) {
184
185 // Make sure input data is reasonable.
186 if (yArr.length != xArr.length || yArr.length != fArr.length)
187 throw new IllegalArgumentException(kInconsistantArrMsg);
188 if (yArr[0].length != xArr[0].length || yArr[0].length != fArr[0].length)
189 throw new IllegalArgumentException(kInconsistantArrMsg);
190 if (xArr.length <= 1 || xArr[0].length <= 1)
191 throw new IllegalArgumentException(kArrSizeMsg);
192
193 xArray = xArr;
194 yArray = yArr;
195 funcArray = fArr;
196
197 if (logInterval)
198 findLogIntervals(nc);
199 else
200 findLinearIntervals(nc);
201 }
202
203 /**
204 * Construct a ContourGenerator object using the specified data arrays.
205 * Contour attributes, including the interval, are generated
206 * automatically. This constructor allows you
207 * to use data on an evenly spaced grid where "X" values are invarient
208 * with "Y" and "Y" values are invarient with "X". This often occures
209 * where the data is on an evenly spaced cartesian grid.
210 *
211 * @param xArr 1D array containing the grid x coordinate data.
212 * @param yArr 1D array containing the grid y coordinate data.
213 * @param fArr 2D array containing the grid function (z) data.
214 * @param nc The number of contour levels to generate.
215 * @param logInterval Uses a logarithmic contour interval if true, and
216 * uses a linear interval if false.
217 **/
218 public ContourGenerator(double[] xArr, double[] yArr, double[][] fArr,
219 int nc, boolean logInterval) {
220
221 // Make sure input data is reasonable.
222 if (yArr.length != fArr.length || xArr.length != fArr[0].length)
223 throw new IllegalArgumentException(kInconsistantArrMsg);
224 if (xArr.length <= 1)
225 throw new IllegalArgumentException(kArrSizeMsg);
226
227 xArr1D = xArr;
228 yArr1D = yArr;
229 funcArray = fArr;
230
231 if (logInterval)
232 findLogIntervals(nc);
233 else
234 findLinearIntervals(nc);
235 }
236
237
238 /**
239 * Generate the contour paths and return them as an array
240 * of ContourPath objects. If there is a lot of data, this method
241 * method may take a long time, so be patient. Progress can be
242 * checked from another thread by calling "getProgress()".
243 *
244 * @return An array of contour path objects.
245 * @throws InterruptedException if the user cancels this process
246 * (by calling "cancel()" from another thread).
247 **/
248 public ContourPath[] getContours() throws InterruptedException {
249
250 if (!cCalculated) {
251 isCanceled = false;
252 pathList.clear();
253
254 // Go off an compute the contour paths.
255 computeContours();
256
257 // Now turn loose all our data arrays to be garbage collected.
258 cAttr = null;
259 xArray = yArray = funcArray = null;
260 xArr1D = yArr1D = null;
261
262 // Set our "done" flags.
263 cCalculated = true;
264 fracComplete = 1;
265 }
266
267 // Turn our pathList into an array and return the array.
268 int size = pathList.size();
269 ContourPath[] arr = new ContourPath[size];
270 for (int i=0; i < size; ++i)
271 arr[i] = (ContourPath)pathList.get(i);
272
273 return arr;
274 }
275
276 /**
277 * Returns true if the contour generation process is done. False if it is not.
278 **/
279 public boolean done() {
280 return cCalculated;
281 }
282
283 /**
284 * Call this method to cancel the generation of contours.
285 **/
286 public void cancel() {
287 isCanceled = true;
288 }
289
290 /**
291 * Returns the progress of the currently executing contour generation
292 * process: 0.0 (just starting) to 1.0 (done).
293 **/
294 public float getProgress() {
295 return fracComplete;
296 }
297
298
299 /**
300 * Find contour intervals that are linearly spaced through the data.
301 **/
302 private void findLinearIntervals(int nc) {
303
304 // Find min and max Z values.
305 double zMin = Double.MAX_VALUE;
306 double zMax = -zMin;
307 int ni = funcArray.length;
308 for (int i=0; i < ni; ++i) {
309 int nj = funcArray[i].length;
310 for (int j=0; j < nj; ++j) {
311 double zVal = funcArray[i][j];
312 zMin = Math.min(zMin, zVal);
313 zMax = Math.max(zMax, zVal);
314 }
315 }
316
317 // Allocate memory for contour attribute array.
318 cAttr = new ContourAttrib[nc];
319
320 // Determine contour levels.
321 double delta = (zMax-zMin)/(nc+1);
322 for (int i=0; i < nc; i++) {
323 cAttr[i] = new ContourAttrib( zMin + (i+1)*delta );
324 if (DEBUG)
325 System.out.println("level[" + i + "] = " + (zMin + (i+1)*delta));
326 }
327
328 }
329
330 /**
331 * Find contour intervals that are logarithmically spaced through the data.
332 **/
333 private void findLogIntervals(int nc) {
334
335 // Find min and max Z values.
336 double zMin = Double.MAX_VALUE;
337 double zMax = -zMin;
338 int ni = funcArray.length;
339 for (int i=0; i < ni; ++i) {
340 int nj = funcArray[i].length;
341 for (int j=0; j < nj; ++j) {
342 double zVal = funcArray[i][j];
343 zMin = Math.min(zMin, zVal);
344 zMax = Math.max(zMax, zVal);
345 }
346 }
347
348 if (zMin < 0)
349 throw new IllegalArgumentException(kNegLogDataMsg);
350
351 // Allocate memory for contour attribute array.
352 cAttr = new ContourAttrib[nc];
353
354 // Determine contour levels.
355 double temp = Math.log(zMin);
356 double delta = (Math.log(zMax) - temp)/(nc+1);
357 for (int i=0; i < nc; i++)
358 cAttr[i] = new ContourAttrib( Math.exp(temp + (i+1)*delta) );
359
360 }
361
362
363 /**
364 * Computes contour lines for gridded data and stores information about
365 * those contours. The result of this routine is a list of contour lines
366 * or paths.
367 **/
368 private void computeContours() throws InterruptedException {
369
370 int ncont = cAttr.length; // Number of contour levels.
371
372 // Find the number of data points in "I" and "J" directions.
373 int nx=0, ny=0;
374 if (xArray != null) {
375 ny = xArray.length;
376 nx = xArray[0].length;
377 } else {
378 nx = xArr1D.length;
379 ny = yArr1D.length;
380 }
381
382 // Allocate temporary storage space for path buffers.
383 pathbufxt = new double[kBufSize];
384 pathbufyt = new double[kBufSize];
385 pathbufia = new int[kBufSize*3];
386
387 // lnstrt=1 (line start) means we're starting a new line.
388 lnstrt = 1;
389 ignext = 0;
390
391 // Loop through each contour level.
392 for (icont = 0; icont < ncont; ++icont) {
393
394 // Check to see if the user has canceled.
395 if (isCanceled)
396 throw new InterruptedException(kCancelMsg);
397
398 // Begin working on this contour level.
399 cont = cAttr[icont].getLevel();
400 iss = 1;
401 iee = nx;
402 jss = 1;
403 jee = ny;
404
405 boolean subDivFlg = false;
406 /*L110*/ do {
407 // Find where function increases through the contour level.
408 FlagContourPassings();
409
410 boolean L10flg = false;
411 /*L210*/ do {
412
413 if (!L10flg) {
414 /* Search along the boundaries for contour line starts.
415 * IMA tells which boundary of the region we're on.
416 */
417 ima = 1;
418 ibeg = iss - 1;
419 jbeg = jss;
420 }
421
422 /*L6*/ imaLoop:
423 do {
424
425 if (!L10flg) {
426 boolean imb = false;
427 boolean doneFlg = false;
428 do {
429
430 switch(ima) {
431 case 1:
432 ++ibeg;
433 if (ibeg == iee)
434 ima = 2;
435 break;
436
437 case 2:
438 ++jbeg;
439 if (jbeg == jee)
440 ima = 3;
441 break;
442
443 case 3:
444 --ibeg;
445 if (ibeg == iss)
446 ima = 4;
447 break;
448
449 case 4:
450 --jbeg;
451 if (jbeg == jss)
452 ima = 5;
453 break;
454
455 case 5:
456 continue imaLoop;
457 }
458
459 if (funcArray[jbeg -1][ibeg -1] <= cont) {
460 imb = true;
461 doneFlg = false;
462
463 } else if (imb == true)
464 doneFlg = true;
465
466 } while (!doneFlg);
467
468 // Got a start point.
469 gi = ibeg; // x index of starting point.
470 gj = jbeg; // y index of starting point.
471 fij = funcArray[jbeg -1][ibeg -1]; // z value of starting point.
472
473 // Round the corner if necessary.
474 /* Look different directions to see which way the contour line
475 * went:
476 * 4
477 * 1-|-3
478 * 2
479 */
480 switch (ima) {
481 case 1:
482 Routine_L21();
483 break;
484
485 case 2:
486 if (gj != jss) {
487 if (!Routine_L31())
488 Routine_L21();
489 } else
490 Routine_L21();
491 break;
492
493 case 3:
494 if (gi != iee) {
495 if (!Routine_L41())
496 Routine_L21();
497 } else {
498 if (!Routine_L31())
499 Routine_L21();
500 }
501 break;
502
503 case 4:
504 if (gj != jee) {
505 if (!Routine_L51())
506 Routine_L21();
507 } else {
508 if (!Routine_L41())
509 Routine_L21();
510 }
511 break;
512
513 case 5:
514 if (!Routine_L51())
515 Routine_L21();
516 break;
517 }
518
519 } // end if(!L10flg)
520
521
522 // This is the end of a contour line. After this, we'll start a
523 // new line.
524 L10flg = false;
525 /*L90*/ lnstrt = 1; // Contour line start flag.
526 ignext = 0;
527 accumContour(np, icont, pathbufxt, pathbufyt, cAttr[icont]);
528
529 // If we're not done looking along the boundaries,
530 // go look there some more.
531 } while (ima != 5);
532
533
534 // Otherwise, get the next start out of IA.
535 /*L91*/ if (iae != 0) {
536 int ntmp3 = iae;
537 for (int iia = 1; iia <= ntmp3; ++iia) {
538 if (pathbufia[iia -1] != 0) {
539 // This is how we start in the middle of the region, using IA.
540 gi = pathbufia[iia - 1]/1000;
541 gj = pathbufia[iia - 1] - gi*1000;
542 fij = funcArray[gj -1][gi -1];
543 pathbufia[iia - 1] = 0;
544
545 Routine_L21();
546
547 L10flg = true;
548 break;
549 }
550 }
551 }
552
553 } while ( L10flg );
554
555 /* And if there are no more of these, we're done with this region.
556 * If we've subdivided, update the region pointers and go back for more.
557 */
558 subDivFlg = false;
559 if (iee == nx) {
560 if (jee != ny) {
561 jss = jee;
562 jee = ny;
563 subDivFlg = true;
564 }
565 } else {
566 iss = iee;
567 iee = nx;
568 subDivFlg = true;
569 }
570
571 } while (subDivFlg);
572
573
574 // Update progress information.
575 fracComplete = (float)(icont+1)/(float)(ncont);
576
577 // Loop back for the next contour level.
578 } // Next icont
579
580
581 // Turn loose temporary arrays used to generate contours.
582 pathbufxt = null;
583 pathbufyt = null;
584 pathbufia = null;
585
586 }
587
588
589 /**
590 * Flag points in IA where the the function increases through the contour
591 * level, not including the boundaries. This is so we have a list of at least
592 * one point on each contour line that doesn't intersect a boundary.
593 **/
594 private void FlagContourPassings() {
595
596 iae = 0;
597 int ntmp2 = jee - 1;
598 for (int j=jss + 1; j <= ntmp2; ++j) {
599 boolean imb = false;
600 int iaend = iae;
601 int ntmp3 = iee;
602 for (int i=iss; i <= ntmp3; ++i) {
603 if (funcArray[j -1][i -1] <= cont)
604 imb = true;
605 else if (imb == true) {
606 ++iae;
607 pathbufia[iae - 1] = i*1000 + j;
608 imb = false;
609
610 /* Check if the IA array is full. If so, the subdividing
611 * algorithm goes like this: if we've marked at least one
612 * J row, drop back to the last completed J and call that
613 * the region. If we haven't even finished one J row, our
614 * region just extends to this I location.
615 */
616 if (iae == kBufSize*3) {
617 if (j > jss + 1) {
618 iae = iaend;
619 jee = j;
620 } else {
621 // Compute minimum.
622 jee = Math.min(j+1, jee);
623 iee = i;
624 }
625
626 // Break out of i & j loops.
627 return;
628 }
629 }
630 } // Next i
631 } // Next j
632
633 }
634
635 /**
636 * This function represents the block of code in the original
637 * FORTRAN program that comes after line 21.
638 **/
639 private void Routine_L21() {
640 while (true) {
641 --gi;
642 if (gi < iss)
643 return; // Goto L90.
644
645 idir = 1;
646 if (funcArray[gj -1][gi -1] <= cont) {
647 // Wipe this point out of IA if it's in the list.
648 /*L52*/ if (iae != 0) {
649 int ij = gi*1000 + gj + 1000;
650 int ntmp3 = iae;
651 for (int iia = 1; iia <= ntmp3; ++iia) {
652 if (pathbufia[iia - 1] == ij) {
653 pathbufia[iia - 1] = 0;
654 break;
655 }
656 }
657 }
658 doInterpolation();
659 return; // Goto L90.
660 }
661
662 fij = funcArray[gj -1][gi -1];
663
664 if (Routine_L31()) return; // Goto L90
665 }
666 }
667
668 /**
669 * This function represents the block of code in the original
670 * FORTRAN program that comes after line 31.
671 **/
672 private boolean Routine_L31() {
673 --gj;
674 if (gj < jss)
675 return true;
676
677 idir = 2;
678 if (funcArray[gj -1][gi -1] <= cont) {
679 doInterpolation();
680 return true;
681 }
682
683 fij = funcArray[gj -1][gi -1];
684
685 return (Routine_L41());
686 }
687
688 /**
689 * This function represents the block of code in the original
690 * FORTRAN program that comes after line 41.
691 **/
692 private boolean Routine_L41() {
693 ++gi;
694 if (gi > iee)
695 return true;
696
697 idir = 3;
698 if (funcArray[gj -1][gi -1] <= cont) {
699 doInterpolation();
700 return true;
701 }
702
703 fij = funcArray[gj -1][gi -1];
704
705 return (Routine_L51());
706 }
707
708 /**
709 * This function represents the block of code in the original
710 * FORTRAN program that comes after line 51.
711 **/
712 private boolean Routine_L51() {
713 ++gj;
714 idir = 4;
715 if (gj > jee)
716 return true;
717
718 if (funcArray[gj -1][gi -1] <= cont) {
719 doInterpolation();
720 return true;
721 }
722
723 fij = funcArray[gj -1][gi -1];
724
725 return false;
726 }
727
728 /**
729 * Do interpolation for X, Y coordinates.
730 *
731 * This function represents the block of code in the original
732 * FORTRAN program that comes after line 60.
733 **/
734 private void doInterpolation() {
735
736 // Do interpolation for X,Y coordinates.
737 double func = funcArray[gj -1][gi -1];
738 double xyf = (cont - func)/(fij - func);
739
740 /* This tests for a contour point coinciding with a grid point. In this case
741 * the contour routine comes up with the same physical coordinate twice. If
742 * If we don't trap it, it can (in some cases significantly) increase the
743 * number of points in a contour line. Also, if this happens on the first
744 * point in a line, the second point could be misinterpreted as the end of a
745 * (circling) contour line.
746 */
747 if (xyf == 0)
748 ++ignext;
749
750 double wxx=0, wyy=0;
751 double xVal=0, yVal=0;
752 if (xArray != null) {
753 // We have 2D arrays for the X & Y grid points.
754 xVal = xArray[gj -1][gi -1];
755 yVal = yArray[gj -1][gi -1];
756 switch (idir) {
757 case 1: // East
758 wxx = xVal + xyf*(xArray[gj -1][gi + 1 -1] - xVal);
759 wyy = yVal + xyf*(yArray[gj -1][gi + 1 -1] - yVal);
760 break;
761
762 case 2: // North
763 wxx = xVal + xyf*(xArray[gj + 1 -1][gi -1] - xVal);
764 wyy = yVal + xyf*(yArray[gj + 1 -1][gi -1] - yVal);
765 break;
766
767 case 3: // West
768 wxx = xVal + xyf*(xArray[gj -1][gi - 1 -1] - xVal);
769 wyy = yVal + xyf*(yArray[gj -1][gi - 1 -1] - yVal);
770 break;
771
772 case 4: // South
773 wxx = xVal + xyf*(xArray[gj - 1 -1][gi -1] - xVal);
774 wyy = yVal + xyf*(yArray[gj - 1 -1][gi -1] - yVal);
775 break;
776 }
777
778 } else {
779 // We have 1D arrays for the X & Y grid points.
780 xVal = xArr1D[gi -1];
781 yVal = yArr1D[gj -1];
782 switch (idir) {
783 case 1: // East
784 wxx = xVal + xyf*(xArr1D[gi + 1 -1] - xVal);
785 wyy = yVal;
786 break;
787
788 case 2: // North
789 wxx = xVal;
790 wyy = yVal + xyf*(yArr1D[gj + 1 -1] - yVal);
791 break;
792
793 case 3: // West
794 wxx = xVal + xyf*(xArr1D[gi - 1 -1] - xVal);
795 wyy = yVal;
796 break;
797
798 case 4: // South
799 wxx = xVal;
800 wyy = yVal + xyf*(yArr1D[gj - 1 -1] - yVal);
801 break;
802 }
803 }
804
805 if (DEBUG) {
806 System.out.println("i, j = " + gi + "," + gj);
807 System.out.println("cont = " + (float)cont + ", fij = " + (float)fij +
808 ", func = " + (float)func + ", xyf = " + (float)xyf);
809 System.out.println("xVal = " + (float)xVal + ", yVal = " + (float)yVal);
810 System.out.println("wxx = " + (float)wxx + ", wyy = " + (float)wyy);
811 }
812
813 // Figure out what to do with this point.
814 if (lnstrt == 1) {
815 // This is the 1st point in the contour line.
816
817 np = 1;
818 pathbufxt[np -1] = wxx;
819 pathbufyt[np -1] = wyy;
820
821 // Save starting point as wx, wy.
822 wx = wxx;
823 wy = wyy;
824
825 // Clear the first point flag, we've got one now.
826 lnstrt = 0;
827
828 } else {
829
830 boolean skipFlg = false;
831
832 // Second point and after comes here.
833 // Add a point to this line. Check for duplicate point first.
834 if (ignext == 2) {
835 if (wxx == pathbufxt[np -1] && wyy == pathbufyt[np -1]) {
836 ignext = 0;
837 skipFlg = true;
838
839 } else
840 ignext = 1;
841 }
842
843 if (!skipFlg) {
844
845 // Increment # of points in contour.
846 ++np;
847 pathbufxt[np -1] = wxx;
848 pathbufyt[np -1] = wyy;
849
850 // See if the temporary array xt, yt are full.
851 if (np == kBufSize) {
852 accumContour(np, icont, pathbufxt, pathbufyt, cAttr[icont]);
853
854 // Last point becomes 1st point to continue.
855 pathbufxt[0] = pathbufxt[np -1];
856 pathbufyt[0] = pathbufyt[np -1];
857 np =1;
858 }
859
860 // Check to see if we're back to the intial point.
861 if (wxx == wx && wyy == wy)
862 return;
863 }
864
865 }
866
867 // Search for the next point on this line.
868 /*L67*/ switch(idir) {
869 case 1:
870 ++gi;
871 if (!Routine_L51())
872 Routine_L21();
873 break;
874
875 case 2:
876 ++gj;
877 Routine_L21();
878 break;
879
880 case 3:
881 --gi;
882 if (!Routine_L31())
883 Routine_L21();
884 break;
885
886 case 4:
887 --gj;
888 if (!Routine_L41())
889 Routine_L21();
890 break;
891 }
892
893 return;
894 }
895
896 /**
897 * Accumulate contour paths, as they are generated, into
898 * an overall list of contours.
899 *
900 * @param np The number of points in the contour path buffers.
901 * @param icont The index to the current contour level.
902 * @param x,y Buffers containing x & y coordinates of contour points.
903 * @param cAttr The attributes for this particular contour level.
904 **/
905 private void accumContour(int np, int icont, double[] x, double[] y, ContourAttrib cAttr) {
906
907 // To few points for a contour line.
908 if (np < kMinNumPoints) return;
909
910 // Copy over coordinate points from buffers to their own arrays.
911 double[] xArr = new double[np];
912 double[] yArr = new double[np];
913 System.arraycopy(x, 0, xArr, 0, np);
914 System.arraycopy(y, 0, yArr, 0, np);
915
916 // Create a new contour path and add it to the list.
917 ContourPath path = new ContourPath(cAttr, icont, xArr, yArr);
918 pathList.add(path);
919
920 }
921
922 }
923