From e5e5d8901a533d64afbbabb04c019701c19403f8 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 28 Mar 2014 16:13:12 -0700 Subject: [PATCH] Added NoiseFilter and NoiseFilter2D masks. --- .../sk89q/worldedit/masks/NoiseFilter.java | 93 +++++++++++++++++++ .../sk89q/worldedit/masks/NoiseFilter2D.java | 93 +++++++++++++++++++ .../{ => util}/noise/NoiseGenerator.java | 2 +- .../{ => util}/noise/RandomNoise.java | 2 +- 4 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/masks/NoiseFilter.java create mode 100644 src/main/java/com/sk89q/worldedit/masks/NoiseFilter2D.java rename src/main/java/com/sk89q/worldedit/{ => util}/noise/NoiseGenerator.java (97%) rename src/main/java/com/sk89q/worldedit/{ => util}/noise/RandomNoise.java (97%) diff --git a/src/main/java/com/sk89q/worldedit/masks/NoiseFilter.java b/src/main/java/com/sk89q/worldedit/masks/NoiseFilter.java new file mode 100644 index 000000000..4913704bd --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/masks/NoiseFilter.java @@ -0,0 +1,93 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team 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.masks; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.util.noise.NoiseGenerator; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A mask that uses a noise generator and returns true whenever the noise + * generator returns a value above the given density. + */ +public class NoiseFilter extends AbstractMask { + + private NoiseGenerator noiseGenerator; + private double density; + + /** + * Create a new noise filter. + * + * @param noiseGenerator the noise generator + * @param density the density + */ + public NoiseFilter(NoiseGenerator noiseGenerator, double density) { + setNoiseGenerator(noiseGenerator); + setDensity(density); + } + + /** + * Get the noise generator. + * + * @return the noise generator + */ + public NoiseGenerator getNoiseGenerator() { + return noiseGenerator; + } + + /** + * Set the noise generator. + * + * @param noiseGenerator a noise generator + */ + public void setNoiseGenerator(NoiseGenerator noiseGenerator) { + checkNotNull(noiseGenerator); + this.noiseGenerator = noiseGenerator; + } + + /** + * Get the probability of passing as a number between 0 and 1 (inclusive). + * + * @return the density + */ + public double getDensity() { + return density; + } + + /** + * Set the probability of passing as a number between 0 and 1 (inclusive). + * + * @return the density + */ + public void setDensity(double density) { + checkArgument(density >= 0, "density must be >= 0"); + checkArgument(density <= 1, "density must be >= 1"); + this.density = density; + } + + @Override + public boolean matches(EditSession editSession, Vector pos) { + return noiseGenerator.noise(pos) <= density; + } + +} diff --git a/src/main/java/com/sk89q/worldedit/masks/NoiseFilter2D.java b/src/main/java/com/sk89q/worldedit/masks/NoiseFilter2D.java new file mode 100644 index 000000000..4f6290957 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/masks/NoiseFilter2D.java @@ -0,0 +1,93 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team 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.masks; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.Vector2D; +import com.sk89q.worldedit.util.noise.NoiseGenerator; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A mask that uses a noise generator and returns true whenever the noise + * generator returns a value above the given density. + */ +public class NoiseFilter2D extends AbstractMask2D { + + private NoiseGenerator noiseGenerator; + private double density; + + /** + * Create a new noise filter. + * + * @param noiseGenerator the noise generator + * @param density the density + */ + public NoiseFilter2D(NoiseGenerator noiseGenerator, double density) { + setNoiseGenerator(noiseGenerator); + setDensity(density); + } + + /** + * Get the noise generator. + * + * @return the noise generator + */ + public NoiseGenerator getNoiseGenerator() { + return noiseGenerator; + } + + /** + * Set the noise generator. + * + * @param noiseGenerator a noise generator + */ + public void setNoiseGenerator(NoiseGenerator noiseGenerator) { + checkNotNull(noiseGenerator); + this.noiseGenerator = noiseGenerator; + } + + /** + * Get the probability of passing as a number between 0 and 1 (inclusive). + * + * @return the density + */ + public double getDensity() { + return density; + } + + /** + * Set the probability of passing as a number between 0 and 1 (inclusive). + * + * @return the density + */ + public void setDensity(double density) { + checkArgument(density >= 0, "density must be >= 0"); + checkArgument(density <= 1, "density must be >= 1"); + this.density = density; + } + + @Override + public boolean matches(EditSession editSession, Vector2D pos) { + return noiseGenerator.noise(pos) <= density; + } + +} diff --git a/src/main/java/com/sk89q/worldedit/noise/NoiseGenerator.java b/src/main/java/com/sk89q/worldedit/util/noise/NoiseGenerator.java similarity index 97% rename from src/main/java/com/sk89q/worldedit/noise/NoiseGenerator.java rename to src/main/java/com/sk89q/worldedit/util/noise/NoiseGenerator.java index 7e95f5f4a..a16271056 100644 --- a/src/main/java/com/sk89q/worldedit/noise/NoiseGenerator.java +++ b/src/main/java/com/sk89q/worldedit/util/noise/NoiseGenerator.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.noise; +package com.sk89q.worldedit.util.noise; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; diff --git a/src/main/java/com/sk89q/worldedit/noise/RandomNoise.java b/src/main/java/com/sk89q/worldedit/util/noise/RandomNoise.java similarity index 97% rename from src/main/java/com/sk89q/worldedit/noise/RandomNoise.java rename to src/main/java/com/sk89q/worldedit/util/noise/RandomNoise.java index f60480e15..87da1d03d 100644 --- a/src/main/java/com/sk89q/worldedit/noise/RandomNoise.java +++ b/src/main/java/com/sk89q/worldedit/util/noise/RandomNoise.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.noise; +package com.sk89q.worldedit.util.noise; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D;