Re: interpolating polygons
Posted by Tony Shepherd on
URL: http://imagej.273.s1.nabble.com/interpolating-polygons-tp3701679p3701681.html
Thanks Wayne, that works a treat.
I've adapted it to create an object containing connected pixel co-ordinates
if anybody wants to use it (below). Note that the constructor needs an roi
object, which could be an ellipse, polygon or whatever.
T
public class Tboundary {
int XY[][];
int n;
// A Tboundary is a list of all connected pixels in the ellipse boundary
// note that the first and last co-ordinate pairs are the same (first
pixel revisited)
public Tboundary(Roi roi)
{
Polygon temproi = roi.getPolygon();
PolygonRoi p = new PolygonRoi(temproi,2);
int n = p.getNCoordinates();
int[] x = p.getXCoordinates();
int[] y = p.getYCoordinates();
Rectangle r = roi.getBoundingRect();
int xbase = r.x;
int ybase = r.y;
boolean areaPath = roi.getType()<=Roi.TRACED_ROI;
double length = 0.0;
double segmentLength;
int xdelta, ydelta, iLength;
double[] segmentLengths = new double[n];
int[] dx = new int[n];
int[] dy = new int[n];
for (int i=0; i<(n-1); i++) {
xdelta = x[i+1] - x[i];
ydelta = y[i+1] - y[i];
segmentLength = Math.sqrt(xdelta*xdelta+ydelta*ydelta);
length += segmentLength;
segmentLengths[i] = segmentLength;
dx[i] = xdelta;
dy[i] = ydelta;
}
if (areaPath) {
xdelta = x[0] - x[n-1];
ydelta = y[0] - y[n-1];
segmentLength = Math.sqrt(xdelta*xdelta+ydelta*ydelta);
length += segmentLength;
segmentLengths[n-1] = segmentLength;
dx[n-1] = xdelta;
dy[n-1] = ydelta;
}
int size = (int)(1.1*length);
int arrayLength = size;
int[][] xypath = new int[2][size];
int pathLength = 0;
double leftOver = 1.0;
double distance = 0.0;
int index = -1;
for (int i=0; i<n; i++) {
double len = segmentLengths[i];
if (len==0.0)
continue;
double xinc = dx[i]/len;
double yinc = dy[i]/len;
double start = 1.0-leftOver;
double rx = xbase+x[i]+start*xinc;
double ry = ybase+y[i]+start*yinc;
double len2 = len - start;
int n2 = (int)len2;
for (int j=0; j<=n2; j++) {
index++;
if (index<xypath[0].length) {
xypath[0][index] = (int)Math.round(rx);
xypath[1][index] = (int)Math.round(ry);
pathLength = index+1;
}
rx += xinc;
ry += yinc;
}
distance += len;
leftOver = len2 - n2;
}
this.XY = xypath;
this.n = pathLength;
}