2012-01-03 02:39:48 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2012-01-03 02:39:48 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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
|
2012-01-03 02:39:48 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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.
|
2012-01-03 02:39:48 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2012-01-03 02:39:48 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
2012-01-05 21:38:23 +00:00
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
package com.sk89q.worldedit.regions;
|
|
|
|
|
2014-07-16 02:47:47 +00:00
|
|
|
import com.sk89q.worldedit.BlockVector;
|
|
|
|
import com.sk89q.worldedit.BlockVector2D;
|
|
|
|
import com.sk89q.worldedit.LocalWorld;
|
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldedit.Vector2D;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2014-05-02 00:52:33 +00:00
|
|
|
import com.sk89q.worldedit.math.geom.Polygons;
|
2014-04-04 01:32:51 +00:00
|
|
|
import com.sk89q.worldedit.regions.iterator.FlatRegion3DIterator;
|
|
|
|
import com.sk89q.worldedit.regions.iterator.FlatRegionIterator;
|
2014-04-05 09:59:38 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2014-04-04 01:32:51 +00:00
|
|
|
|
2012-03-13 10:55:47 +00:00
|
|
|
import java.util.Iterator;
|
2013-07-27 09:12:29 +00:00
|
|
|
import java.util.List;
|
2012-01-03 02:39:48 +00:00
|
|
|
|
2014-07-16 02:47:47 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
/**
|
|
|
|
* Represents a cylindrical region.
|
|
|
|
*
|
|
|
|
* @author yetanotherx
|
|
|
|
*/
|
2012-03-13 10:55:47 +00:00
|
|
|
public class CylinderRegion extends AbstractRegion implements FlatRegion {
|
2012-04-22 06:26:44 +00:00
|
|
|
private Vector2D center;
|
2012-01-03 02:39:48 +00:00
|
|
|
private Vector2D radius;
|
|
|
|
private int minY;
|
|
|
|
private int maxY;
|
|
|
|
private boolean hasY = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the region
|
|
|
|
*/
|
|
|
|
public CylinderRegion() {
|
2014-04-05 09:59:38 +00:00
|
|
|
this((World) null);
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
/**
|
|
|
|
* @deprecated cast {@code world} to {@link World}
|
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
@Deprecated
|
|
|
|
public CylinderRegion(LocalWorld world) {
|
|
|
|
this((World) world);
|
|
|
|
}
|
2012-01-03 02:39:48 +00:00
|
|
|
/**
|
|
|
|
* Construct the region.
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param world the world
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
public CylinderRegion(World world) {
|
2012-01-03 02:39:48 +00:00
|
|
|
this(world, new Vector(), new Vector2D(), 0, 0);
|
|
|
|
hasY = false;
|
|
|
|
}
|
|
|
|
|
2014-04-05 09:59:38 +00:00
|
|
|
@Deprecated
|
|
|
|
public CylinderRegion(LocalWorld world, Vector center, Vector2D radius, int minY, int maxY) {
|
|
|
|
this((World) world, center, radius, minY, maxY);
|
|
|
|
}
|
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
/**
|
|
|
|
* Construct the region.
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param world the world
|
|
|
|
* @param center the center position
|
|
|
|
* @param radius the radius along the X and Z axes
|
|
|
|
* @param minY the minimum Y, inclusive
|
|
|
|
* @param maxY the maximum Y, inclusive
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
public CylinderRegion(World world, Vector center, Vector2D radius, int minY, int maxY) {
|
2012-01-03 02:39:48 +00:00
|
|
|
super(world);
|
2012-04-22 06:26:44 +00:00
|
|
|
setCenter(center.toVector2D());
|
2012-01-03 02:39:48 +00:00
|
|
|
setRadius(radius);
|
|
|
|
this.minY = minY;
|
|
|
|
this.maxY = maxY;
|
|
|
|
hasY = true;
|
|
|
|
}
|
|
|
|
|
2014-07-16 02:47:47 +00:00
|
|
|
/**
|
|
|
|
* Construct the region.
|
|
|
|
*
|
|
|
|
* @param center the center position
|
|
|
|
* @param radius the radius along the X and Z axes
|
|
|
|
* @param minY the minimum Y, inclusive
|
|
|
|
* @param maxY the maximum Y, inclusive
|
|
|
|
*/
|
|
|
|
public CylinderRegion(Vector center, Vector2D radius, int minY, int maxY) {
|
|
|
|
super(null);
|
|
|
|
setCenter(center.toVector2D());
|
|
|
|
setRadius(radius);
|
|
|
|
this.minY = minY;
|
|
|
|
this.maxY = maxY;
|
|
|
|
hasY = true;
|
|
|
|
}
|
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
public CylinderRegion(CylinderRegion region) {
|
2012-04-22 06:26:44 +00:00
|
|
|
this(region.world, region.getCenter(), region.getRadius(), region.minY, region.maxY);
|
2012-01-03 02:39:48 +00:00
|
|
|
hasY = region.hasY;
|
|
|
|
}
|
|
|
|
|
2012-08-29 08:00:26 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public Vector getCenter() {
|
2012-04-22 06:26:44 +00:00
|
|
|
return center.toVector((maxY + minY) / 2);
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the main center point of the region
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2012-04-22 06:26:44 +00:00
|
|
|
* @deprecated replaced by {@link #setCenter(Vector2D)}
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
2012-04-22 06:26:44 +00:00
|
|
|
@Deprecated
|
2012-01-03 02:39:48 +00:00
|
|
|
public void setCenter(Vector center) {
|
2012-04-22 06:26:44 +00:00
|
|
|
setCenter(center.toVector2D());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the main center point of the region
|
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param center the center point
|
2012-04-22 06:26:44 +00:00
|
|
|
*/
|
|
|
|
public void setCenter(Vector2D center) {
|
2012-01-03 02:39:48 +00:00
|
|
|
this.center = center;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the radius of the cylinder
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @return the radius along the X and Z axes
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
|
|
|
public Vector2D getRadius() {
|
|
|
|
return radius.subtract(0.5, 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the radius of the cylinder
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param radius the radius along the X and Z axes
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
|
|
|
public void setRadius(Vector2D radius) {
|
|
|
|
this.radius = radius.add(0.5, 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extends the radius to be at least the given radius
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param minRadius the minimum radius
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
|
|
|
public void extendRadius(Vector2D minRadius) {
|
|
|
|
setRadius(Vector2D.getMaximum(minRadius, getRadius()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the minimum Y.
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param y the y
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
|
|
|
public void setMinimumY(int y) {
|
|
|
|
hasY = true;
|
|
|
|
minY = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Se the maximum Y.
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param y the y
|
2012-01-03 02:39:48 +00:00
|
|
|
*/
|
|
|
|
public void setMaximumY(int y) {
|
|
|
|
hasY = true;
|
|
|
|
maxY = y;
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public Vector getMinimumPoint() {
|
2012-04-22 06:26:44 +00:00
|
|
|
return center.subtract(getRadius()).toVector(minY);
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public Vector getMaximumPoint() {
|
2012-04-22 06:26:44 +00:00
|
|
|
return center.add(getRadius()).toVector(maxY);
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public int getMaximumY() {
|
|
|
|
return maxY;
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public int getMinimumY() {
|
|
|
|
return minY;
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public int getArea() {
|
|
|
|
return (int) Math.floor(radius.getX() * radius.getZ() * Math.PI * getHeight());
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public int getWidth() {
|
|
|
|
return (int) (2 * radius.getX());
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public int getHeight() {
|
|
|
|
return maxY - minY + 1;
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public int getLength() {
|
|
|
|
return (int) (2 * radius.getZ());
|
|
|
|
}
|
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
private Vector2D calculateDiff2D(Vector... changes) throws RegionOperationException {
|
2012-03-13 10:00:40 +00:00
|
|
|
Vector2D diff = new Vector2D();
|
|
|
|
for (Vector change : changes) {
|
|
|
|
diff = diff.add(change.toVector2D());
|
|
|
|
}
|
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
if ((diff.getBlockX() & 1) + (diff.getBlockZ() & 1) != 0) {
|
|
|
|
throw new RegionOperationException("Cylinders changes must be even for each horizontal dimensions.");
|
2012-03-13 10:00:40 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
return diff.divide(2).floor();
|
2012-03-13 10:00:40 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
private Vector2D calculateChanges2D(Vector... changes) {
|
|
|
|
Vector2D total = new Vector2D();
|
|
|
|
for (Vector change : changes) {
|
|
|
|
total = total.add(change.toVector2D().positive());
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
return total.divide(2).floor();
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 10:00:40 +00:00
|
|
|
/**
|
|
|
|
* Expand the region.
|
|
|
|
* Expand the region.
|
|
|
|
*
|
|
|
|
* @param changes array/arguments with multiple related changes
|
|
|
|
* @throws RegionOperationException
|
|
|
|
*/
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-03-13 10:00:40 +00:00
|
|
|
public void expand(Vector... changes) throws RegionOperationException {
|
2012-04-22 06:26:44 +00:00
|
|
|
center = center.add(calculateDiff2D(changes));
|
2012-03-14 10:58:26 +00:00
|
|
|
radius = radius.add(calculateChanges2D(changes));
|
2012-03-13 10:00:40 +00:00
|
|
|
for (Vector change : changes) {
|
2012-03-14 10:58:26 +00:00
|
|
|
int changeY = change.getBlockY();
|
|
|
|
if (changeY > 0) {
|
|
|
|
maxY += changeY;
|
|
|
|
} else {
|
|
|
|
minY += changeY;
|
|
|
|
}
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 10:00:40 +00:00
|
|
|
/**
|
|
|
|
* Contract the region.
|
|
|
|
*
|
|
|
|
* @param changes array/arguments with multiple related changes
|
|
|
|
* @throws RegionOperationException
|
|
|
|
*/
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-03-13 10:00:40 +00:00
|
|
|
public void contract(Vector... changes) throws RegionOperationException {
|
2012-04-22 06:26:44 +00:00
|
|
|
center = center.subtract(calculateDiff2D(changes));
|
2012-03-14 10:58:26 +00:00
|
|
|
Vector2D newRadius = radius.subtract(calculateChanges2D(changes));
|
2012-03-13 10:00:40 +00:00
|
|
|
radius = Vector2D.getMaximum(new Vector2D(1.5, 1.5), newRadius);
|
|
|
|
for (Vector change : changes) {
|
2012-04-22 06:26:44 +00:00
|
|
|
int height = maxY - minY;
|
2012-03-14 10:58:26 +00:00
|
|
|
int changeY = change.getBlockY();
|
|
|
|
if (changeY > 0) {
|
2012-04-22 06:26:44 +00:00
|
|
|
minY += Math.min(height, changeY);
|
2012-03-14 10:58:26 +00:00
|
|
|
} else {
|
2012-04-22 06:26:44 +00:00
|
|
|
maxY += Math.max(-height, changeY);
|
2012-03-14 10:58:26 +00:00
|
|
|
}
|
2012-03-13 10:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
@Override
|
|
|
|
public void shift(Vector change) throws RegionOperationException {
|
2012-04-22 06:26:44 +00:00
|
|
|
center = center.add(change.toVector2D());
|
2012-01-03 02:39:48 +00:00
|
|
|
|
|
|
|
int changeY = change.getBlockY();
|
|
|
|
maxY += changeY;
|
|
|
|
minY += changeY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see if a point is inside this region.
|
|
|
|
*/
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-01-03 02:39:48 +00:00
|
|
|
public boolean contains(Vector pt) {
|
|
|
|
final int blockY = pt.getBlockY();
|
|
|
|
if (blockY < minY || blockY > maxY) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-22 06:26:44 +00:00
|
|
|
return pt.toVector2D().subtract(center).divide(radius).lengthSq() <= 1;
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the height of the cylinder to fit the specified Y.
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param y the y value
|
2012-01-03 02:39:48 +00:00
|
|
|
* @return true if the area was expanded
|
|
|
|
*/
|
|
|
|
public boolean setY(int y) {
|
|
|
|
if (!hasY) {
|
|
|
|
minY = y;
|
|
|
|
maxY = y;
|
|
|
|
hasY = true;
|
|
|
|
return true;
|
|
|
|
} else if (y < minY) {
|
|
|
|
minY = y;
|
|
|
|
return true;
|
|
|
|
} else if (y > maxY) {
|
|
|
|
maxY = y;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-16 06:47:43 +00:00
|
|
|
@Override
|
|
|
|
public Iterator<BlockVector> iterator() {
|
|
|
|
return new FlatRegion3DIterator(this);
|
|
|
|
}
|
|
|
|
|
2012-03-13 10:55:47 +00:00
|
|
|
@Override
|
|
|
|
public Iterable<Vector2D> asFlatRegion() {
|
|
|
|
return new Iterable<Vector2D>() {
|
|
|
|
@Override
|
|
|
|
public Iterator<Vector2D> iterator() {
|
|
|
|
return new FlatRegionIterator(CylinderRegion.this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
/**
|
|
|
|
* Returns string representation in the format
|
2012-04-22 06:26:44 +00:00
|
|
|
* "(centerX, centerZ) - (radiusX, radiusZ) - (minY, maxY)"
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2012-01-03 02:39:48 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2012-04-22 06:26:44 +00:00
|
|
|
return center + " - " + radius + "(" + minY + ", " + maxY + ")";
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|
2012-03-20 14:14:41 +00:00
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-03-20 14:14:41 +00:00
|
|
|
public CylinderRegion clone() {
|
|
|
|
return (CylinderRegion) super.clone();
|
|
|
|
}
|
2013-07-27 09:12:29 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<BlockVector2D> polygonize(int maxPoints) {
|
2014-05-02 00:52:33 +00:00
|
|
|
return Polygons.polygonizeCylinder(center, radius, maxPoints);
|
2013-07-27 09:12:29 +00:00
|
|
|
}
|
2014-07-16 02:47:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new instance with the given center and radius in the X and Z
|
|
|
|
* axes with a Y that extends from the bottom of the extent to the top
|
|
|
|
* of the extent.
|
|
|
|
*
|
|
|
|
* @param extent the extent
|
|
|
|
* @param center the center position
|
|
|
|
* @param radius the radius in the X and Z axes
|
|
|
|
* @return a region
|
|
|
|
*/
|
|
|
|
public static CylinderRegion createRadius(Extent extent, Vector center, double radius) {
|
|
|
|
checkNotNull(extent);
|
|
|
|
checkNotNull(center);
|
|
|
|
Vector2D radiusVec = new Vector2D(radius, radius);
|
|
|
|
int minY = extent.getMinimumPoint().getBlockY();
|
|
|
|
int maxY = extent.getMaximumPoint().getBlockY();
|
|
|
|
return new CylinderRegion(center, radiusVec, minY, maxY);
|
|
|
|
}
|
|
|
|
|
2012-01-03 02:39:48 +00:00
|
|
|
}
|