From b07fd594e926f1d00706b26aead5889206060041 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 30 Mar 2014 12:13:45 -0700 Subject: [PATCH] Moved interpolation, filtering packages to math. --- .../java/com/sk89q/worldedit/EditSession.java | 6 +- .../java/com/sk89q/worldedit/HeightMap.java | 2 +- .../worldedit/commands/RegionCommands.java | 4 +- .../convolution}/GaussianKernel.java | 114 ++++----- .../convolution}/HeightMapFilter.java | 240 +++++++++--------- .../convolution}/LinearKernel.java | 88 +++---- .../interpolation/Interpolation.java | 2 +- .../KochanekBartelsInterpolation.java | 2 +- .../interpolation/LinearInterpolation.java | 2 +- .../{ => math}/interpolation/Node.java | 2 +- .../ReparametrisingInterpolation.java | 2 +- .../worldedit/tools/brushes/SmoothBrush.java | 4 +- 12 files changed, 234 insertions(+), 234 deletions(-) rename src/main/java/com/sk89q/worldedit/{filtering => math/convolution}/GaussianKernel.java (94%) rename src/main/java/com/sk89q/worldedit/{filtering => math/convolution}/HeightMapFilter.java (95%) rename src/main/java/com/sk89q/worldedit/{filtering => math/convolution}/LinearKernel.java (93%) rename src/main/java/com/sk89q/worldedit/{ => math}/interpolation/Interpolation.java (97%) rename src/main/java/com/sk89q/worldedit/{ => math}/interpolation/KochanekBartelsInterpolation.java (99%) rename src/main/java/com/sk89q/worldedit/{ => math}/interpolation/LinearInterpolation.java (98%) rename src/main/java/com/sk89q/worldedit/{ => math}/interpolation/Node.java (97%) rename src/main/java/com/sk89q/worldedit/{ => math}/interpolation/ReparametrisingInterpolation.java (98%) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index 4442de0c4..4c7d09e74 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -44,9 +44,9 @@ import com.sk89q.worldedit.function.visitor.DownwardVisitor; import com.sk89q.worldedit.function.visitor.LayerVisitor; import com.sk89q.worldedit.function.visitor.RecursiveVisitor; import com.sk89q.worldedit.function.visitor.RegionVisitor; -import com.sk89q.worldedit.interpolation.Interpolation; -import com.sk89q.worldedit.interpolation.KochanekBartelsInterpolation; -import com.sk89q.worldedit.interpolation.Node; +import com.sk89q.worldedit.math.interpolation.Interpolation; +import com.sk89q.worldedit.math.interpolation.KochanekBartelsInterpolation; +import com.sk89q.worldedit.math.interpolation.Node; import com.sk89q.worldedit.masks.Mask; import com.sk89q.worldedit.math.noise.RandomNoise; import com.sk89q.worldedit.math.transform.AffineTransform; diff --git a/src/main/java/com/sk89q/worldedit/HeightMap.java b/src/main/java/com/sk89q/worldedit/HeightMap.java index d7f786599..7ac34b8a8 100644 --- a/src/main/java/com/sk89q/worldedit/HeightMap.java +++ b/src/main/java/com/sk89q/worldedit/HeightMap.java @@ -21,7 +21,7 @@ package com.sk89q.worldedit; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; -import com.sk89q.worldedit.filtering.HeightMapFilter; +import com.sk89q.worldedit.math.convolution.HeightMapFilter; import com.sk89q.worldedit.regions.Region; /** diff --git a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java index bc72cdb3d..c748a7926 100644 --- a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java @@ -27,8 +27,8 @@ import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.expression.ExpressionException; -import com.sk89q.worldedit.filtering.GaussianKernel; -import com.sk89q.worldedit.filtering.HeightMapFilter; +import com.sk89q.worldedit.math.convolution.GaussianKernel; +import com.sk89q.worldedit.math.convolution.HeightMapFilter; import com.sk89q.worldedit.function.GroundFunction; import com.sk89q.worldedit.function.generator.FloraGenerator; import com.sk89q.worldedit.function.generator.ForestGenerator; diff --git a/src/main/java/com/sk89q/worldedit/filtering/GaussianKernel.java b/src/main/java/com/sk89q/worldedit/math/convolution/GaussianKernel.java similarity index 94% rename from src/main/java/com/sk89q/worldedit/filtering/GaussianKernel.java rename to src/main/java/com/sk89q/worldedit/math/convolution/GaussianKernel.java index 2aa4ac957..44813a426 100644 --- a/src/main/java/com/sk89q/worldedit/filtering/GaussianKernel.java +++ b/src/main/java/com/sk89q/worldedit/math/convolution/GaussianKernel.java @@ -1,57 +1,57 @@ -// $Id$ -/* - * WorldEditLibrary - * Copyright (C) 2010 sk89q and contributors - * - * 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 . -*/ - -package com.sk89q.worldedit.filtering; - -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; - } -} +// $Id$ +/* + * WorldEditLibrary + * Copyright (C) 2010 sk89q and contributors + * + * 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 . +*/ + +package com.sk89q.worldedit.math.convolution; + +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; + } +} diff --git a/src/main/java/com/sk89q/worldedit/filtering/HeightMapFilter.java b/src/main/java/com/sk89q/worldedit/math/convolution/HeightMapFilter.java similarity index 95% rename from src/main/java/com/sk89q/worldedit/filtering/HeightMapFilter.java rename to src/main/java/com/sk89q/worldedit/math/convolution/HeightMapFilter.java index 8078f5f7f..3f802f295 100644 --- a/src/main/java/com/sk89q/worldedit/filtering/HeightMapFilter.java +++ b/src/main/java/com/sk89q/worldedit/math/convolution/HeightMapFilter.java @@ -1,120 +1,120 @@ -// $Id$ -/* - * WorldEditLibrary - * Copyright (C) 2010 sk89q and contributors - * - * 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 . -*/ - -package com.sk89q.worldedit.filtering; - -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; - } -} +// $Id$ +/* + * WorldEditLibrary + * Copyright (C) 2010 sk89q and contributors + * + * 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 . +*/ + +package com.sk89q.worldedit.math.convolution; + +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; + } +} diff --git a/src/main/java/com/sk89q/worldedit/filtering/LinearKernel.java b/src/main/java/com/sk89q/worldedit/math/convolution/LinearKernel.java similarity index 93% rename from src/main/java/com/sk89q/worldedit/filtering/LinearKernel.java rename to src/main/java/com/sk89q/worldedit/math/convolution/LinearKernel.java index ea8aa98ab..1f52474ee 100644 --- a/src/main/java/com/sk89q/worldedit/filtering/LinearKernel.java +++ b/src/main/java/com/sk89q/worldedit/math/convolution/LinearKernel.java @@ -1,44 +1,44 @@ -// $Id$ -/* - * WorldEditLibrary - * Copyright (C) 2010 sk89q and contributors - * - * 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 . -*/ - -package com.sk89q.worldedit.filtering; - -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; - } -} +// $Id$ +/* + * WorldEditLibrary + * Copyright (C) 2010 sk89q and contributors + * + * 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 . +*/ + +package com.sk89q.worldedit.math.convolution; + +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; + } +} diff --git a/src/main/java/com/sk89q/worldedit/interpolation/Interpolation.java b/src/main/java/com/sk89q/worldedit/math/interpolation/Interpolation.java similarity index 97% rename from src/main/java/com/sk89q/worldedit/interpolation/Interpolation.java rename to src/main/java/com/sk89q/worldedit/math/interpolation/Interpolation.java index 62d1412e2..c07d72860 100644 --- a/src/main/java/com/sk89q/worldedit/interpolation/Interpolation.java +++ b/src/main/java/com/sk89q/worldedit/math/interpolation/Interpolation.java @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.sk89q.worldedit.interpolation; +package com.sk89q.worldedit.math.interpolation; import java.util.List; diff --git a/src/main/java/com/sk89q/worldedit/interpolation/KochanekBartelsInterpolation.java b/src/main/java/com/sk89q/worldedit/math/interpolation/KochanekBartelsInterpolation.java similarity index 99% rename from src/main/java/com/sk89q/worldedit/interpolation/KochanekBartelsInterpolation.java rename to src/main/java/com/sk89q/worldedit/math/interpolation/KochanekBartelsInterpolation.java index 72dd305d3..81a338011 100644 --- a/src/main/java/com/sk89q/worldedit/interpolation/KochanekBartelsInterpolation.java +++ b/src/main/java/com/sk89q/worldedit/math/interpolation/KochanekBartelsInterpolation.java @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.sk89q.worldedit.interpolation; +package com.sk89q.worldedit.math.interpolation; import java.util.Collections; import java.util.List; diff --git a/src/main/java/com/sk89q/worldedit/interpolation/LinearInterpolation.java b/src/main/java/com/sk89q/worldedit/math/interpolation/LinearInterpolation.java similarity index 98% rename from src/main/java/com/sk89q/worldedit/interpolation/LinearInterpolation.java rename to src/main/java/com/sk89q/worldedit/math/interpolation/LinearInterpolation.java index 68fb2aaad..0da212a8d 100644 --- a/src/main/java/com/sk89q/worldedit/interpolation/LinearInterpolation.java +++ b/src/main/java/com/sk89q/worldedit/math/interpolation/LinearInterpolation.java @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.sk89q.worldedit.interpolation; +package com.sk89q.worldedit.math.interpolation; import java.util.List; diff --git a/src/main/java/com/sk89q/worldedit/interpolation/Node.java b/src/main/java/com/sk89q/worldedit/math/interpolation/Node.java similarity index 97% rename from src/main/java/com/sk89q/worldedit/interpolation/Node.java rename to src/main/java/com/sk89q/worldedit/math/interpolation/Node.java index 6e1a53e18..d4ccbf5a6 100644 --- a/src/main/java/com/sk89q/worldedit/interpolation/Node.java +++ b/src/main/java/com/sk89q/worldedit/math/interpolation/Node.java @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.sk89q.worldedit.interpolation; +package com.sk89q.worldedit.math.interpolation; import com.sk89q.worldedit.Vector; diff --git a/src/main/java/com/sk89q/worldedit/interpolation/ReparametrisingInterpolation.java b/src/main/java/com/sk89q/worldedit/math/interpolation/ReparametrisingInterpolation.java similarity index 98% rename from src/main/java/com/sk89q/worldedit/interpolation/ReparametrisingInterpolation.java rename to src/main/java/com/sk89q/worldedit/math/interpolation/ReparametrisingInterpolation.java index 047a38df1..6fe515762 100644 --- a/src/main/java/com/sk89q/worldedit/interpolation/ReparametrisingInterpolation.java +++ b/src/main/java/com/sk89q/worldedit/math/interpolation/ReparametrisingInterpolation.java @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.sk89q.worldedit.interpolation; +package com.sk89q.worldedit.math.interpolation; import java.util.List; import java.util.Map.Entry; diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java index cd8bde553..06911372c 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java @@ -24,8 +24,8 @@ import com.sk89q.worldedit.HeightMap; import com.sk89q.worldedit.MaxChangedBlocksException; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldVector; -import com.sk89q.worldedit.filtering.GaussianKernel; -import com.sk89q.worldedit.filtering.HeightMapFilter; +import com.sk89q.worldedit.math.convolution.GaussianKernel; +import com.sk89q.worldedit.math.convolution.HeightMapFilter; import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region;