DistancePath.java

Go to the documentation of this file.
00001 package theba.core.math;
00002 
00003 import java.awt.Point;
00004 import java.util.ArrayList;
00005 
00012 public class DistancePath {
00013 
00014         int w, h;
00015 
00016         short[] oldgeo, oldborder;
00017 
00018         short[] geodesic, borderdist, border;
00019 
00027         public short[] getPath(short[] geodesic, short[] border, short[] dist,
00028                         int w, int h) {
00029                 this.geodesic = geodesic;
00030                 this.border = border;
00031 
00032                 short[] out = new short[h * w];
00033                 ArrayList<Point> points = new ArrayList<Point> ();
00034 
00035                 for (int y = 0; y < h; y++) {
00036                         for (int x = 0; x < w; x++) {
00037                                 if (border[x + y * w] != 0)
00038                                         points.add(new Point(x, y));
00039                         }
00040                 }
00041 
00042                 Point po, p;
00043 
00044                 int counter = 0; // Keep track of path length
00045 
00046                 while (true) {
00047 
00048                         counter++;
00049 
00050                         if (counter > 120) {
00051                                 // If maximum path length is reached, then abort.
00052                                 // The path has most probably gotten lost.
00053                                 System.err.println("Path too long to connect lumens");
00054                                 for (int i = 0; i < border.length; i++)
00055                                         border[i] = 0;
00056                                 return border;
00057                         }
00058 
00059                         int min = Integer.MAX_VALUE;
00060 
00061                         po = null;
00062                         for (int t = 0; t < points.size(); t++) {
00063 
00064                                 p = points.get(t);
00065 
00066                                 int val = geodesic[p.y * w + p.x];
00067 
00068                                 val -= 2 * dist[p.y * w + p.x];
00069 
00070                                 if (val < min && out[p.y * w + p.x] == 0) {
00071                                         po = p;
00072                                         min = val;
00073                                 }
00074                         }
00075 
00076                         if (po == null)
00077                                 return new short[geodesic.length]; // path has closed itself
00078 
00079                         if (geodesic[po.y * w + po.x] < 2)
00080                                 break; // destination reached
00081 
00082                         out[po.y * w + po.x] = (byte) 255;
00083 
00084                         points.clear();
00085                         for (int a = -1; a <= 1; a++) {
00086                                 for (int b = -1; b <= 1; b++) {
00087                                         if (a == 0 && b == 0)
00088                                                 continue;
00089                                         if (po.x + a < 0 || po.x + a >= w || po.y + b < 0 || po.y + b >= h)
00090                                                 continue;
00091                                         points.add(new Point(po.x + a, po.y + b));
00092                                 }
00093                         }
00094 
00095                 }
00096 
00097                 return out;
00098         }
00099 }

Generated on Fri Nov 13 08:57:07 2009 for Theba by  doxygen 1.6.1