*/
public static void distance3d(Stack voxels) {
int width = voxels.getWidth();
int height = voxels.getHeight();
int depth = voxels.getDepth();
ThebaGUI control = ThebaGUI.getInstance();
// set borders to 0
for (int z = 0; z < depth; z++) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int index = x + y * width;
if (z == 0 || x == 0 || y == 0 || z == depth - 1
|| y == height - 1 || x == width - 1)
voxels.getSlice(z)[index] = (short) 0;
else
voxels.getSlice(z)[index] = (short) (0xff - (voxels
.getSlice(z)[index]));
}
}
}
// int a = 3, b = 4, c = 3, d = 4, e = 5;
int a = 3, b = 4, c = 5, d = 3, e = 7;
int[] wf = new int[]{e, d, e, d, c, d, e, d, e, b, a, b, a, 255, 255,
255, 255, 255,};
int[] wb = new int[]{255, 255, 255, 255, 255, a, b, a, b, e, d, e, d,
c, d, e, d, e,};
int[] slask = new int[2 * 3 * 3];
// Note that we ignore pixels on the edge of the image to avoid problems
for (int z = 1; z < depth - 1; z++) {
control.setProgress(z / 2);
for (int x = 1; x < width - 1; x++) {
for (int y = 1; y < height - 1; y++) {
int index = x + y * width;
for (int k = -1; k < 1; k++) {
for (int j = -1; j < 2; j++) {
for (int i = -1; i < 2; i++) {
int slaskindex = (i + 1) + (j + 1) * 3
+ (k + 1) * 3 * 3;
slask[slaskindex] = (voxels.getSlice(z + k)[index
+ (i) + j * width])
+ wf[slaskindex];
}
}
}
int minval = slask[0]; // init to max
for (int i = 1; i < slask.length; i++) {
if ((slask[i]) < minval)
minval = (slask[i]);
}
if ((voxels.getSlice(z)[index]) > minval) {
voxels.getSlice(z)[index] = (short) minval;
}
}
}
}
// Second backward iteration:
for (int z = depth - 2; z > 0; z--) {
control.setProgress((depth - z / 2));
for (int x = width - 2; x > 0; x--) {
for (int y = height - 2; y > 0; y--) {
int index = x + y * width;
for (int k = 0; k < 2; k++) {
for (int j = -1; j < 2; j++) {
for (int i = -1; i < 2; i++) {
int slaskindex = (i + 1) + (j + 1) * 3 + (k)
* 3 * 3;
slask[slaskindex] = (voxels.getSlice(z + k)[index
+ (i) + j * width])
+ wb[slaskindex];
}
}
}
int minval = slask[0]; // init to max
for (int i = 1; i < slask.length; i++) {
if ((slask[i]) < minval)
minval = (slask[i]);
}
if ((voxels.getSlice(z)[index]) > minval)
voxels.getSlice(z)[index] = (short) minval;
}
}
}
try {
System.out.println("Writing to file dmap.raw");
FileOutputStream fs = new FileOutputStream("dmap.raw");
short[] buffer = new short[width]; // read and write one scanline at once
for (int z = 0; z < depth; z++)
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
buffer[x] = voxels.getSlice(z)[x + y * width];
}
byte[] out = DataConversion.shortToBytes(buffer);
fs.write(out);
}
} catch (IOException e1) {
e1.printStackTrace();
}
control.setProgressComplete();
}