Added //smooth

This commit is contained in:
Erik Broes
2010-11-21 22:58:05 +01:00
parent 493ec4818d
commit 7acd006b2b
6 changed files with 441 additions and 0 deletions

View File

@ -0,0 +1,57 @@
// $Id$
/*
* WorldEditLibrary
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.filters;
import java.awt.image.Kernel;
/**
* A Gaussian Kernel generator (2D bellcurve)
*
* @author Grum
*/
public class GaussianKernel extends Kernel {
/**
* Constructor of the kernel
*
* @param radius the resulting diameter will be radius * 2 + 1
* @param sigma controls 'flatness'
*/
public GaussianKernel(int radius, double sigma) {
super(radius * 2 + 1, radius * 2 + 1, createKernel(radius, sigma));
}
private static float[] createKernel(int radius, double sigma) {
int diameter = radius * 2 + 1;
float[] data = new float[diameter*diameter];
double sigma22 = 2 * sigma * sigma;
double constant = Math.PI * sigma22;
for (int y = -radius; y <= radius; y++) {
for (int x = -radius; x <= radius; x++) {
data[(y+radius) * diameter + x+radius] = (float) (Math.exp(-(x * x + y * y) / sigma22) / constant);
}
}
return data;
}
}

View File

@ -0,0 +1,118 @@
// $Id$
/*
* WorldEditLibrary
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.filters;
import java.awt.image.Kernel;
/**
* Allows applications of Kernels onto the region's heightmap.
* Only used for smoothing (with a GaussianKernel).
*
* @author Grum
*/
public class HeightMapFilter {
private Kernel kernel;
/**
* Construct the HeightMapFilter object.
*
* @param kernel
*/
public HeightMapFilter(Kernel kernel) {
this.kernel = kernel;
}
/**
* Construct the HeightMapFilter object.
*
* @param kernelWidth
* @param kernelHeight
* @param kernelData
*/
public HeightMapFilter(int kernelWidth, int kernelHeight, float[] kernelData) {
this.kernel = new Kernel(kernelWidth, kernelHeight, kernelData);
}
/**
* @return the kernel
*/
public Kernel getKernel() {
return kernel;
}
/**
* Set Kernel
*
* @param kernel
*/
public void setKernel(Kernel kernel) {
this.kernel = kernel;
}
/**
* Filter with a 2D kernel
*
* @param inData
* @param width
* @param height
* @return the modified heightmap
*/
public int[] filter(int[] inData, int width, int height ) {
int index = 0;
float[] matrix = kernel.getKernelData(null);
int[] outData = new int[inData.length];
int kh = kernel.getHeight();
int kw = kernel.getWidth();
int kox = kernel.getXOrigin();
int koy = kernel.getYOrigin();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float z = 0;
for (int ky = 0; ky < kh; ky++) {
int offsetY = y + ky - koy;
// Clamp coordinates inside data
if (offsetY < 0 || offsetY >= height)
offsetY = y;
offsetY *= width;
int matrixOffset = ky * kw;
for (int kx = 0; kx < kw; kx++) {
float f = matrix[matrixOffset + kx];
if (f == 0) continue;
int offsetX = x + kx - kox;
// Clamp coordinates inside data
if (offsetX < 0 || offsetX >= width)
offsetX = x;
z += f * inData[offsetY + offsetX];
}
}
outData[index++] = (int) (z + 0.5);
}
}
return outData;
}
}

View File

@ -0,0 +1,44 @@
// $Id$
/*
* WorldEditLibrary
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.filters;
import java.awt.image.Kernel;
/**
* A linear Kernel generator (all cells weight the same)
*
* @author Grum
*/
public class LinearKernel extends Kernel {
public LinearKernel(int radius) {
super(radius * 2 + 1, radius * 2 + 1, createKernel(radius));
}
private static float[] createKernel(int radius) {
int diameter = radius * 2 + 1;
float[] data = new float[diameter*diameter];
for (int i = 0; i < data.length; data[i++] = 1.0f/data.length) ;
return data;
}
}