2011-12-27 22:24:57 +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
|
2011-12-27 22:24:57 +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
|
2011-12-27 22:24:57 +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.
|
2011-12-27 22:24:57 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2011-12-27 22:24:57 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-04 22:03:18 +00:00
|
|
|
*/
|
2011-12-27 22:24:57 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.regions;
|
|
|
|
|
2012-03-30 04:06:43 +00:00
|
|
|
import com.sk89q.worldedit.BlockVector;
|
|
|
|
import com.sk89q.worldedit.BlockVector2D;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.MutableBlockVector;
|
2011-12-27 22:24:57 +00:00
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldedit.Vector2D;
|
2014-04-05 09:59:38 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.storage.ChunkStore;
|
2011-12-27 22:24:57 +00:00
|
|
|
import java.util.HashSet;
|
2018-06-16 05:29:48 +00:00
|
|
|
import java.util.Set;
|
2011-12-27 22:24:57 +00:00
|
|
|
|
|
|
|
/**
|
2014-07-26 07:29:12 +00:00
|
|
|
* Represents an ellipsoid region.
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
2012-01-03 14:57:29 +00:00
|
|
|
public class EllipsoidRegion extends AbstractRegion {
|
2014-07-26 07:29:12 +00:00
|
|
|
|
2011-12-27 22:24:57 +00:00
|
|
|
/**
|
|
|
|
* Stores the center.
|
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
private MutableBlockVector center;
|
2014-07-26 07:29:12 +00:00
|
|
|
|
2011-12-27 22:24:57 +00:00
|
|
|
/**
|
2014-04-05 09:59:38 +00:00
|
|
|
* Stores the radii plus 0.5 on each axis.
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
2018-08-12 14:03:07 +00:00
|
|
|
private MutableBlockVector radius;
|
|
|
|
private MutableBlockVector radiusSqr;
|
|
|
|
private int radiusLengthSqr;
|
|
|
|
private boolean sphere;
|
2014-07-26 07:29:12 +00:00
|
|
|
|
2011-12-27 22:24:57 +00:00
|
|
|
/**
|
|
|
|
* Construct a new instance of this ellipsoid region.
|
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param pos1 the first position
|
|
|
|
* @param pos2 the second position
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
|
|
|
public EllipsoidRegion(Vector pos1, Vector pos2) {
|
|
|
|
this(null, pos1, pos2);
|
|
|
|
}
|
2012-03-30 04:06:43 +00:00
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2011-12-27 22:24:57 +00:00
|
|
|
/**
|
|
|
|
* Construct a new instance of this ellipsoid region.
|
2012-03-30 04:06:43 +00:00
|
|
|
*
|
2018-08-12 14:03:07 +00:00
|
|
|
* @param world the world
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param center the center
|
|
|
|
* @param radius the radius
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
2014-04-05 09:59:38 +00:00
|
|
|
public EllipsoidRegion(World world, Vector center, Vector radius) {
|
2012-01-03 14:57:29 +00:00
|
|
|
super(world);
|
2018-08-12 14:03:07 +00:00
|
|
|
this.center = new MutableBlockVector(center);
|
2012-01-03 16:26:22 +00:00
|
|
|
setRadius(radius);
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public EllipsoidRegion(EllipsoidRegion ellipsoidRegion) {
|
2012-01-03 16:26:22 +00:00
|
|
|
this(ellipsoidRegion.world, ellipsoidRegion.center, ellipsoidRegion.getRadius());
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public Vector getMinimumPoint() {
|
2018-08-12 14:03:07 +00:00
|
|
|
return center.subtract(getRadius()).clampY(0, 255);
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public Vector getMaximumPoint() {
|
2018-08-12 14:03:07 +00:00
|
|
|
return center.add(getRadius()).clampY(0, 255);
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public int getArea() {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (radius == null) return 0;
|
2011-12-27 22:24:57 +00:00
|
|
|
return (int) Math.floor((4.0 / 3.0) * Math.PI * radius.getX() * radius.getY() * radius.getZ());
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public int getWidth() {
|
|
|
|
return (int) (2 * radius.getX());
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public int getHeight() {
|
2018-08-12 14:03:07 +00:00
|
|
|
return Math.max((int) (2 * radius.getY()), 256);
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public int getLength() {
|
|
|
|
return (int) (2 * radius.getZ());
|
|
|
|
}
|
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
private Vector calculateDiff(Vector... changes) throws RegionOperationException {
|
|
|
|
Vector diff = new Vector().add(changes);
|
2012-03-13 10:00:40 +00:00
|
|
|
|
2012-03-14 10:58:26 +00:00
|
|
|
if ((diff.getBlockX() & 1) + (diff.getBlockY() & 1) + (diff.getBlockZ() & 1) != 0) {
|
2012-03-13 10:00:40 +00:00
|
|
|
throw new RegionOperationException(
|
2012-03-14 10:58:26 +00:00
|
|
|
"Ellipsoid changes must be even for each 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 Vector calculateChanges(Vector... changes) {
|
|
|
|
Vector total = new Vector();
|
|
|
|
for (Vector change : changes) {
|
|
|
|
total = total.add(change.positive());
|
|
|
|
}
|
|
|
|
|
|
|
|
return total.divide(2).floor();
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-03-13 10:00:40 +00:00
|
|
|
public void expand(Vector... changes) throws RegionOperationException {
|
2018-08-12 14:03:07 +00:00
|
|
|
center = new MutableBlockVector(center.add(calculateDiff(changes)));
|
|
|
|
setRadius(radius.add(calculateChanges(changes)));
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2012-03-13 10:00:40 +00:00
|
|
|
public void contract(Vector... changes) throws RegionOperationException {
|
2018-08-12 14:03:07 +00:00
|
|
|
center = new MutableBlockVector(center.subtract(calculateDiff(changes)));
|
2012-03-14 10:58:26 +00:00
|
|
|
Vector newRadius = radius.subtract(calculateChanges(changes));
|
2018-08-12 14:03:07 +00:00
|
|
|
setRadius(Vector.getMaximum(new Vector(1.5, 1.5, 1.5), newRadius));
|
2012-03-13 10:00:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 15:13:50 +00:00
|
|
|
@Override
|
|
|
|
public void shift(Vector change) throws RegionOperationException {
|
2018-08-12 14:03:07 +00:00
|
|
|
center = new MutableBlockVector(center.add(change));
|
2012-01-03 15:13:50 +00:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:24:57 +00:00
|
|
|
/**
|
|
|
|
* Get the center.
|
|
|
|
*
|
|
|
|
* @return center
|
|
|
|
*/
|
2012-08-29 08:00:26 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public Vector getCenter() {
|
|
|
|
return center;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the center.
|
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param center the center
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
|
|
|
public void setCenter(Vector center) {
|
2018-08-12 14:03:07 +00:00
|
|
|
this.center = new MutableBlockVector(center);
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-26 07:29:12 +00:00
|
|
|
* Get the radii.
|
2011-12-27 22:24:57 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @return radii
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
|
|
|
public Vector getRadius() {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (radius == null) return null;
|
2011-12-27 22:24:57 +00:00
|
|
|
return radius.subtract(0.5, 0.5, 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-26 07:29:12 +00:00
|
|
|
* Set the radii.
|
2011-12-27 22:24:57 +00:00
|
|
|
*
|
2014-07-26 07:29:12 +00:00
|
|
|
* @param radius the radius
|
2011-12-27 22:24:57 +00:00
|
|
|
*/
|
|
|
|
public void setRadius(Vector radius) {
|
2018-08-12 14:03:07 +00:00
|
|
|
this.radius = new MutableBlockVector(radius.add(0.5, 0.5, 0.5));
|
|
|
|
radiusSqr = new MutableBlockVector(radius.multiply(radius));
|
|
|
|
radiusLengthSqr = radiusSqr.getBlockX();
|
|
|
|
if (radius.getBlockY() == radius.getBlockX() && radius.getBlockX() == radius.getBlockZ()) {
|
|
|
|
this.sphere = true;
|
|
|
|
} else {
|
|
|
|
this.sphere = false;
|
|
|
|
}
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2011-12-27 22:24:57 +00:00
|
|
|
public Set<Vector2D> getChunks() {
|
2018-08-12 14:03:07 +00:00
|
|
|
final Set<Vector2D> chunks = new HashSet<Vector2D>();
|
2011-12-27 22:24:57 +00:00
|
|
|
|
2013-07-31 08:35:47 +00:00
|
|
|
final Vector min = getMinimumPoint();
|
|
|
|
final Vector max = getMaximumPoint();
|
|
|
|
final int centerY = getCenter().getBlockY();
|
2011-12-27 22:24:57 +00:00
|
|
|
|
|
|
|
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
|
2013-07-31 08:35:47 +00:00
|
|
|
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
|
|
|
|
if (!contains(new BlockVector(x, centerY, z))) {
|
|
|
|
continue;
|
2012-03-30 04:06:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 08:35:47 +00:00
|
|
|
chunks.add(new BlockVector2D(
|
2018-08-12 14:03:07 +00:00
|
|
|
x >> ChunkStore.CHUNK_SHIFTS,
|
|
|
|
z >> ChunkStore.CHUNK_SHIFTS
|
2013-07-31 08:35:47 +00:00
|
|
|
));
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:29:12 +00:00
|
|
|
@Override
|
2014-07-29 18:04:04 +00:00
|
|
|
public boolean contains(Vector position) {
|
2018-08-12 14:03:07 +00:00
|
|
|
int cx = position.getBlockX() - center.getBlockX();
|
|
|
|
int cx2 = cx * cx;
|
|
|
|
if (cx2 > radiusSqr.getBlockX()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int cz = position.getBlockZ() - center.getBlockZ();
|
|
|
|
int cz2 = cz * cz;
|
|
|
|
if (cz2 > radiusSqr.getBlockZ()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int cy = position.getBlockY() - center.getBlockY();
|
|
|
|
int cy2 = cy * cy;
|
|
|
|
if (radiusSqr.getBlockY() < 255 && cy2 > radiusSqr.getBlockY()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sphere) {
|
|
|
|
return cx2 + cy2 + cz2 <= radiusLengthSqr;
|
|
|
|
}
|
|
|
|
double cxd = (double) cx / radius.getBlockX();
|
|
|
|
double cyd = (double) cy / radius.getBlockY();
|
|
|
|
double czd = (double) cz / radius.getBlockZ();
|
|
|
|
return cxd * cxd + cyd * cyd + czd * czd <= 1;
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns string representation in the format
|
|
|
|
* "(centerX, centerY, centerZ) - (radiusX, radiusY, radiusZ)".
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return center + " - " + getRadius();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void extendRadius(Vector minRadius) {
|
|
|
|
setRadius(Vector.getMaximum(minRadius, getRadius()));
|
|
|
|
}
|
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 EllipsoidRegion clone() {
|
|
|
|
return (EllipsoidRegion) super.clone();
|
|
|
|
}
|
2011-12-27 22:24:57 +00:00
|
|
|
}
|