Plex-FAWE/worldedit-core/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java

228 lines
6.1 KiB
Java
Raw Normal View History

/*
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
*
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
* (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.
*
2014-04-04 22:03:18 +00:00
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2014-04-04 22:03:18 +00:00
*/
package com.sk89q.worldedit.regions;
2018-12-23 16:19:33 +00:00
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.storage.ChunkStore;
import java.util.HashSet;
2018-06-16 05:29:48 +00:00
import java.util.Set;
/**
* Represents an ellipsoid region.
*/
public class EllipsoidRegion extends AbstractRegion {
/**
* Stores the center.
*/
2019-01-09 07:13:44 +00:00
private BlockVector3 center;
/**
* Stores the radii plus 0.5 on each axis.
*/
2019-01-09 07:13:44 +00:00
private Vector3 radius;
/**
* Construct a new instance of this ellipsoid region.
*
* @param pos1 the first position
* @param pos2 the second position
*/
2018-12-23 16:19:33 +00:00
public EllipsoidRegion(BlockVector3 pos1, Vector3 pos2) {
this(null, pos1, pos2);
}
/**
* Construct a new instance of this ellipsoid region.
*
* @param world the world
* @param center the center
* @param radius the radius
*/
2018-12-23 16:19:33 +00:00
public EllipsoidRegion(World world, BlockVector3 center, Vector3 radius) {
super(world);
2019-01-09 07:13:44 +00:00
this.center = center;
setRadius(radius);
}
public EllipsoidRegion(EllipsoidRegion ellipsoidRegion) {
this(ellipsoidRegion.world, ellipsoidRegion.center, ellipsoidRegion.getRadius());
}
@Override
2018-12-23 16:19:33 +00:00
public BlockVector3 getMinimumPoint() {
return center.toVector3().subtract(getRadius()).toBlockPoint();
}
@Override
2018-12-23 16:19:33 +00:00
public BlockVector3 getMaximumPoint() {
return center.toVector3().add(getRadius()).toBlockPoint();
}
@Override
public int getArea() {
return (int) Math.floor((4.0 / 3.0) * Math.PI * radius.getX() * radius.getY() * radius.getZ());
}
@Override
public int getWidth() {
return (int) (2 * radius.getX());
}
@Override
public int getHeight() {
return (int) (2 * radius.getY());
}
@Override
public int getLength() {
return (int) (2 * radius.getZ());
}
2018-12-23 16:19:33 +00:00
private BlockVector3 calculateDiff(BlockVector3... changes) throws RegionOperationException {
BlockVector3 diff = BlockVector3.ZERO.add(changes);
if ((diff.getBlockX() & 1) + (diff.getBlockY() & 1) + (diff.getBlockZ() & 1) != 0) {
throw new RegionOperationException(
"Ellipsoid changes must be even for each dimensions.");
}
return diff.divide(2).floor();
}
private Vector3 calculateChanges(BlockVector3... changes) {
Vector3 total = Vector3.ZERO;
2018-12-23 16:19:33 +00:00
for (BlockVector3 change : changes) {
total = total.add(change.abs().toVector3());
}
return total.divide(2).floor();
}
@Override
2018-12-23 16:19:33 +00:00
public void expand(BlockVector3... changes) throws RegionOperationException {
2019-01-09 07:13:44 +00:00
center = center.add(calculateDiff(changes));
radius = radius.add(calculateChanges(changes));
}
@Override
2018-12-23 16:19:33 +00:00
public void contract(BlockVector3... changes) throws RegionOperationException {
2019-01-09 07:13:44 +00:00
center = center.subtract(calculateDiff(changes));
Vector3 newRadius = radius.subtract(calculateChanges(changes));
radius = Vector3.at(1.5, 1.5, 1.5).getMaximum(newRadius);
}
@Override
2018-12-23 16:19:33 +00:00
public void shift(BlockVector3 change) throws RegionOperationException {
2019-01-09 07:13:44 +00:00
center = center.add(change);
}
/**
* Get the center.
*
* @return center
*/
2012-08-29 08:00:26 +00:00
@Override
2018-12-23 16:19:33 +00:00
public Vector3 getCenter() {
return center.toVector3();
}
/**
* Set the center.
*
* @param center the center
*/
2018-12-23 16:19:33 +00:00
public void setCenter(BlockVector3 center) {
2019-01-09 07:13:44 +00:00
this.center = center;
}
/**
* Get the radii.
*
* @return radii
*/
2018-12-23 16:19:33 +00:00
public Vector3 getRadius() {
2019-01-09 07:13:44 +00:00
return radius.subtract(0.5, 0.5, 0.5);
}
/**
* Set the radii.
*
* @param radius the radius
*/
2019-01-09 07:13:44 +00:00
public void setRadius(Vector3 radius) {
this.radius = radius.add(0.5, 0.5, 0.5);
}
@Override
2018-12-23 16:19:33 +00:00
public Set<BlockVector2> getChunks() {
final Set<BlockVector2> chunks = new HashSet<>();
2018-12-23 16:19:33 +00:00
final BlockVector3 min = getMinimumPoint();
final BlockVector3 max = getMaximumPoint();
final int centerY = center.getBlockY();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (!contains(BlockVector3.at(x, centerY, z))) {
continue;
}
chunks.add(BlockVector2.at(
2018-12-23 16:19:33 +00:00
x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS
));
}
}
return chunks;
}
@Override
public boolean contains(BlockVector3 position) {
return position.subtract(center).toVector3().divide(radius).lengthSq() <= 1;
}
/**
* Returns string representation in the format
* "(centerX, centerY, centerZ) - (radiusX, radiusY, radiusZ)".
*
* @return string
*/
@Override
public String toString() {
return center + " - " + getRadius();
}
2018-12-23 16:19:33 +00:00
public void extendRadius(Vector3 minRadius) {
2019-01-09 07:13:44 +00:00
setRadius(minRadius.getMaximum(getRadius()));
}
2012-03-20 14:14:41 +00:00
@Override
2012-03-20 14:14:41 +00:00
public EllipsoidRegion clone() {
return (EllipsoidRegion) super.clone();
}
}