diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 2a84f875c..7efb2b240 100644 --- a/src/bukkit/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -23,11 +23,13 @@ import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.wepif.PermissionsResolverManager; import com.sk89q.worldedit.*; import com.sk89q.worldedit.bukkit.selections.CuboidSelection; +import com.sk89q.worldedit.bukkit.selections.CylinderSelection; import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection; import com.sk89q.worldedit.bukkit.selections.Selection; import com.sk89q.worldedit.extension.platform.PlatformRejectionException; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.regions.CylinderRegion; import com.sk89q.worldedit.regions.Polygonal2DRegion; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.RegionSelector; @@ -389,6 +391,8 @@ public class WorldEditPlugin extends JavaPlugin { return new CuboidSelection(world, selector, (CuboidRegion) region); } else if (region instanceof Polygonal2DRegion) { return new Polygonal2DSelection(world, selector, (Polygonal2DRegion) region); + } else if (region instanceof CylinderRegion) { + return new CylinderSelection(world, selector, (CylinderRegion) region); } else { return null; } diff --git a/src/main/java/com/sk89q/worldedit/bukkit/selections/CylinderSelection.java b/src/main/java/com/sk89q/worldedit/bukkit/selections/CylinderSelection.java new file mode 100644 index 000000000..c7dee7c79 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/bukkit/selections/CylinderSelection.java @@ -0,0 +1,80 @@ +/* + * 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 Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.bukkit.selections; + +import com.sk89q.worldedit.regions.selector.CylinderRegionSelector; +import org.bukkit.World; + +import com.sk89q.worldedit.BlockVector2D; +import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.bukkit.BukkitUtil; +import com.sk89q.worldedit.regions.CylinderRegion; +import com.sk89q.worldedit.regions.RegionSelector; + +/** + * A selection representing a {@link CylinderRegion} + */ +public class CylinderSelection extends RegionSelection { + + private CylinderRegion cylRegion; + + public CylinderSelection(World world, RegionSelector selector, CylinderRegion region) { + super(world, selector, region); + this.cylRegion = region; + } + + public CylinderSelection(World world, BlockVector2D center, BlockVector2D radius, int minY, int maxY) { + super(world); + LocalWorld lWorld = BukkitUtil.getLocalWorld(world); + + // Validate input + minY = Math.min(Math.max(0, minY), world.getMaxHeight()); + maxY = Math.min(Math.max(0, maxY), world.getMaxHeight()); + + // Create and set up new selector + CylinderRegionSelector sel = new CylinderRegionSelector(lWorld, center, radius, minY, maxY); + + // set up selection + cylRegion = sel.getIncompleteRegion(); + + // set up RegionSelection + setRegionSelector(sel); + setRegion(cylRegion); + } + + /** + * Returns the center vector of the cylinder + * + * @return the center + */ + public BlockVector2D getCenter() { + return cylRegion.getCenter().toVector2D().toBlockVector2D(); + } + + /** + * Returns the radius vector of the cylinder + * + * @return the radius + */ + public BlockVector2D getRadius() { + return cylRegion.getRadius().toBlockVector2D(); + } + +} diff --git a/src/main/java/com/sk89q/worldedit/math/geom/Polygons.java b/src/main/java/com/sk89q/worldedit/math/geom/Polygons.java new file mode 100644 index 000000000..c6f011243 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/math/geom/Polygons.java @@ -0,0 +1,64 @@ +/* + * 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 Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.math.geom; + +import com.sk89q.worldedit.BlockVector2D; +import com.sk89q.worldedit.Vector2D; + +import java.util.ArrayList; +import java.util.List; + +/** + * Helper method for anything related to polygons. + */ +public final class Polygons { + + private Polygons() { + + } + + /** + * Calculates the polygon shape of a cylinder which can then be used for e.g. intersection detection. + * + * @param center the center point of the cylinder + * @param radius the radius of the cylinder + * @param maxPoints max points to be used for the calculation + * @return a list of {@link BlockVector2D} which resemble the shape as a polygon + */ + public static List polygonizeCylinder(Vector2D center, Vector2D radius, int maxPoints) { + int nPoints = (int) Math.ceil(Math.PI*radius.length()); + + // These strange semantics for maxPoints are copied from the selectSecondary method. + if (maxPoints >= 0 && nPoints >= maxPoints) { + nPoints = maxPoints - 1; + } + + final List points = new ArrayList(nPoints); + for (int i = 0; i < nPoints; ++i) { + double angle = i * (2.0 * Math.PI) / nPoints; + final Vector2D pos = new Vector2D(Math.cos(angle), Math.sin(angle)); + final BlockVector2D blockVector2D = pos.multiply(radius).add(center).toBlockVector2D(); + points.add(blockVector2D); + } + + return points; + } + +} diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index 73d1e0f44..e6eea8cff 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -20,11 +20,11 @@ package com.sk89q.worldedit.regions; import com.sk89q.worldedit.*; +import com.sk89q.worldedit.math.geom.Polygons; import com.sk89q.worldedit.regions.iterator.FlatRegion3DIterator; import com.sk89q.worldedit.regions.iterator.FlatRegionIterator; import com.sk89q.worldedit.world.World; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -375,22 +375,6 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { @Override public List polygonize(int maxPoints) { - final Vector2D radius = getRadius(); - int nPoints = (int) Math.ceil(Math.PI*radius.length()); - - // These strange semantics for maxPoints are copied from the selectSecondary method. - if (maxPoints >= 0 && nPoints >= maxPoints) { - nPoints = maxPoints - 1; - } - - final List points = new ArrayList(nPoints); - for (int i = 0; i < nPoints; ++i) { - double angle = i * (2.0 * Math.PI) / nPoints; - final Vector2D pos = new Vector2D(Math.cos(angle), Math.sin(angle)); - final BlockVector2D blockVector2D = pos.multiply(radius).add(center).toBlockVector2D(); - points.add(blockVector2D); - } - - return points; + return Polygons.polygonizeCylinder(center, radius, maxPoints); } }