Refactored and fixed getIntersectingRegions

This commit is contained in:
Eric Puidokas 2011-10-18 13:05:19 -07:00 committed by TomyLobo
parent 4bb967ab4a
commit 27251dda2e
4 changed files with 185 additions and 278 deletions

View File

@ -23,6 +23,7 @@
import java.util.List;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.protection.UnsupportedIntersectionException;
@ -30,16 +31,14 @@ public class GlobalProtectedRegion extends ProtectedRegion {
public GlobalProtectedRegion(String id) {
super(id);
min = new BlockVector(0, 0, 0);
max = new BlockVector(0, 0, 0);
}
@Override
public BlockVector getMinimumPoint() {
return new BlockVector(0, 0, 0);
}
@Override
public BlockVector getMaximumPoint() {
return new BlockVector(0, 0, 0);
public List<BlockVector2D> getPoints() {
List<BlockVector2D> pts = new ArrayList<BlockVector2D>();
pts.add(new BlockVector2D(min.getBlockX(),min.getBlockZ()));
return pts;
}
@Override

View File

@ -33,36 +33,29 @@
*/
public class ProtectedCuboidRegion extends ProtectedRegion {
/**
* Store the first point.
*/
private BlockVector min;
/**
* Store the second point.
*/
private BlockVector max;
/**
* Construct a new instance of this cuboid region.
*
* @param id
* @param min
* @param max
* @param pt1
* @param pt2
*/
public ProtectedCuboidRegion(String id, BlockVector min, BlockVector max) {
public ProtectedCuboidRegion(String id, BlockVector pt1, BlockVector pt2) {
super(id);
this.min = min;
this.max = max;
setMinMaxPoints(pt1, pt2);
}
/**
* Get the lower point of the cuboid.
* Given any two points, sets the minimum and maximum points
*
* @return min point
* @param pt1
* @param pt2
*/
@Override
public BlockVector getMinimumPoint() {
return min;
private void setMinMaxPoints(BlockVector pt1, BlockVector pt2) {
List<Vector> points = new ArrayList<Vector>();
points.add(pt1);
points.add(pt2);
setMinMaxPoints(points);
}
/**
@ -71,17 +64,7 @@ public BlockVector getMinimumPoint() {
* @param pt
*/
public void setMinimumPoint(BlockVector pt) {
min = pt;
}
/**
* Get the upper point of the cuboid.
*
* @return max point
*/
@Override
public BlockVector getMaximumPoint() {
return max;
setMinMaxPoints(pt, max);
}
/**
@ -90,7 +73,22 @@ public BlockVector getMaximumPoint() {
* @param pt
*/
public void setMaximumPoint(BlockVector pt) {
max = pt;
setMinMaxPoints(min, pt);
}
public List<BlockVector2D> getPoints() {
List<BlockVector2D> pts = new ArrayList<BlockVector2D>();
int x1 = min.getBlockX();
int x2 = max.getBlockX();
int z1 = min.getBlockZ();
int z2 = max.getBlockZ();
pts.add(new BlockVector2D(x1, z1));
pts.add(new BlockVector2D(x2, z1));
pts.add(new BlockVector2D(x1, z2));
pts.add(new BlockVector2D(x2, z2));
return pts;
}
/**
@ -134,112 +132,29 @@ public boolean intersectsWith(ProtectedRegion region) throws UnsupportedIntersec
@Override
public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions) throws UnsupportedIntersectionException {
int numRegions = regions.size();
List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>();
int i, i2, i3;
for (i = 0; i < numRegions; i++) {
ProtectedRegion region = regions.get(i);
BlockVector rMinPoint = region.getMinimumPoint();
BlockVector rMaxPoint = region.getMaximumPoint();
for (ProtectedRegion region : regions) {
if (!intersectsBoundingBox(region)) continue;
// Check whether the region is outside the min and max vector
if ((rMinPoint.getBlockX() < min.getBlockX() && rMaxPoint.getBlockX() < min.getBlockX())
|| (rMinPoint.getBlockX() > max.getBlockX() && rMaxPoint.getBlockX() > max.getBlockX())
|| ((rMinPoint.getBlockY() < min.getBlockY() && rMaxPoint.getBlockY() < min.getBlockY())
|| (rMinPoint.getBlockY() > max.getBlockY() && rMaxPoint.getBlockY() > max.getBlockY()))
|| ((rMinPoint.getBlockZ() < min.getBlockZ() && rMaxPoint.getBlockZ() < min.getBlockZ())
|| (rMinPoint.getBlockZ() > max.getBlockZ() && rMaxPoint.getBlockZ() > max.getBlockZ())) )
continue;
// If both regions are Cuboids and their bounding boxes intersect, they intersect
if (region instanceof ProtectedCuboidRegion) {
intersectingRegions.add(regions.get(i));
intersectingRegions.add(region);
continue;
}
// Check whether the regions points are inside the other region
if (region.contains(new Vector(min.getBlockX(), min.getBlockY(), min.getBlockZ()))
|| region.contains(new Vector(min.getBlockX(), min.getBlockY(), max.getBlockZ()))
|| region.contains(new Vector(min.getBlockX(), max.getBlockY(), max.getBlockZ()))
|| region.contains(new Vector(min.getBlockX(), max.getBlockY(), min.getBlockZ()))
|| region.contains(new Vector(max.getBlockX(), max.getBlockY(), max.getBlockZ()))
|| region.contains(new Vector(max.getBlockX(), max.getBlockY(), min.getBlockZ()))
|| region.contains(new Vector(max.getBlockX(), min.getBlockY(), min.getBlockZ()))
|| region.contains(new Vector(max.getBlockX(), min.getBlockY(), max.getBlockZ())) ) {
intersectingRegions.add(regions.get(i));
continue;
}
// Check whether the other regions points are inside the current region
if (region instanceof ProtectedPolygonalRegion) {
for (i2 = 0; i2 < ((ProtectedPolygonalRegion)region).getPoints().size(); i2++) {
BlockVector2D pt2Dr = ((ProtectedPolygonalRegion)region).getPoints().get(i2);
int minYr = ((ProtectedPolygonalRegion)region).minY;
int maxYr = ((ProtectedPolygonalRegion)region).maxY;
Vector ptr = new Vector(pt2Dr.getBlockX(), minYr, pt2Dr.getBlockZ());
Vector ptr2 = new Vector(pt2Dr.getBlockX(), maxYr, pt2Dr.getBlockZ());
if (this.contains(ptr) || this.contains(ptr2)) {
intersectingRegions.add(regions.get(i));
continue;
}
} else if (region instanceof ProtectedPolygonalRegion) {
// If either region contains the points of the other, they intersect
if (containsAny(region.getPoints())) {
intersectingRegions.add(region);
continue;
}
if (region.containsAny(getPoints())) {
intersectingRegions.add(region);
continue;
}
} else {
throw new UnsupportedOperationException("Not supported yet.");
}
// Check whether the current regions edges collide with the regions edges
boolean regionIsIntersecting = false;
List<BlockVector2D> points = new ArrayList<BlockVector2D>();
points.add(new BlockVector2D(min.getBlockX(), min.getBlockZ()));
points.add(new BlockVector2D(min.getBlockX(), max.getBlockZ()));
points.add(new BlockVector2D(max.getBlockX(), max.getBlockZ()));
points.add(new BlockVector2D(max.getBlockX(), min.getBlockZ()));
for (i2 = 0; i2 < points.size(); i2++) {
boolean checkNextPoint = false;
BlockVector2D currPoint = points.get(i2);
BlockVector2D nextPoint;
if (i2 == (points.size() - 1)) {
nextPoint = points.get(0);
} else {
nextPoint = points.get(i2 + 1);
}
int currX = currPoint.getBlockX();
int currZ = currPoint.getBlockZ();
while (!checkNextPoint) {
for(i3 = min.getBlockY(); i3 <= max.getBlockY(); i3++) {
if (region.contains(new Vector(currX, i3, currZ))) {
intersectingRegions.add(regions.get(i));
regionIsIntersecting = true;
break;
}
}
if (currX == nextPoint.getBlockX() || currZ == nextPoint.getBlockZ() || regionIsIntersecting) {
checkNextPoint = true;
}
if (nextPoint.getBlockX() > currPoint.getBlockX()) {
currX++;
} else {
currX--;
}
if (nextPoint.getBlockZ() > currPoint.getBlockZ()) {
currZ++;
} else {
currZ--;
}
}
if (regionIsIntersecting) {
break;
}
}
}
return intersectingRegions;
}
@ -268,6 +183,4 @@ public int volume() {
int volume = xLength * yLength * zLength;
return volume;
}
}
}

View File

@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.List;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.protection.UnsupportedIntersectionException;
@ -31,55 +30,36 @@ public class ProtectedPolygonalRegion extends ProtectedRegion {
protected List<BlockVector2D> points;
protected int minY;
protected int maxY;
private BlockVector min;
private BlockVector max;
public ProtectedPolygonalRegion(String id, List<BlockVector2D> points, int minY, int maxY) {
super(id);
this.points = points;
this.minY = minY;
this.maxY = maxY;
setMinMaxPoints(points, minY, maxY);
this.minY = min.getBlockY();
this.maxY = max.getBlockY();
}
int minX = points.get(0).getBlockX();
int minZ = points.get(0).getBlockZ();
int maxX = points.get(0).getBlockX();
int maxZ = points.get(0).getBlockZ();
for (BlockVector2D v : points) {
int x = v.getBlockX();
int z = v.getBlockZ();
if (x < minX) {
minX = x;
}
if (z < minZ) {
minZ = z;
}
if (x > maxX) {
maxX = x;
}
if (z > maxZ) {
maxZ = z;
}
/**
* Sets the min and max points from all the 2d points and the min/max Y values
*
* @param points2D
* @param minY
* @param maxY
*/
private void setMinMaxPoints(List<BlockVector2D> points2D, int minY, int maxY) {
List<Vector> points = new ArrayList<Vector>();
int y = minY;
for (BlockVector2D point2D : points2D) {
points.add(new Vector(point2D.getBlockX(), y, point2D.getBlockZ()));
y = maxY;
}
min = new BlockVector(minX, minY, minZ);
max = new BlockVector(maxX, maxY, maxZ);
setMinMaxPoints(points);
}
public List<BlockVector2D> getPoints() {
return points;
}
@Override
public BlockVector getMinimumPoint() {
return min;
}
@Override
public BlockVector getMaximumPoint() {
return max;
}
/**
* Checks to see if a point is inside this region.
*/
@ -144,117 +124,25 @@ public boolean contains(Vector pt) {
@Override
public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions) throws UnsupportedIntersectionException {
int numRegions = regions.size();
int numPoints = points.size();
List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>();
int i, i2, i3;
for (i = 0; i < numRegions; i++) {
ProtectedRegion region = regions.get(i);
BlockVector rMinPoint = region.getMinimumPoint();
BlockVector rMaxPoint = region.getMaximumPoint();
for (ProtectedRegion region : regions) {
if (!intersectsBoundingBox(region)) continue;
// Check whether the region is outside the min and max vector
if ((rMinPoint.getBlockX() < min.getBlockX() && rMaxPoint.getBlockX() < min.getBlockX())
|| (rMinPoint.getBlockX() > max.getBlockX() && rMaxPoint.getBlockX() > max.getBlockX())
&& ((rMinPoint.getBlockY() < min.getBlockY() && rMaxPoint.getBlockY() < min.getBlockY())
|| (rMinPoint.getBlockY() > max.getBlockY() && rMaxPoint.getBlockY() > max.getBlockY()))
&& ((rMinPoint.getBlockZ() < min.getBlockZ() && rMaxPoint.getBlockZ() < min.getBlockZ())
|| (rMinPoint.getBlockZ() > max.getBlockZ() && rMaxPoint.getBlockZ() > max.getBlockZ())) ) {
intersectingRegions.add(regions.get(i));
continue;
}
// Check whether the regions points are inside the other region
for (i2 = 0; i < numPoints; i++) {
Vector pt = new Vector(points.get(i2).getBlockX(), minY, points.get(i2).getBlockZ());
Vector pt2 = new Vector(points.get(i2).getBlockX(), maxY, points.get(i2).getBlockZ());
if (region.contains(pt) || region.contains(pt2)) {
intersectingRegions.add(regions.get(i));
if (region instanceof ProtectedPolygonalRegion || region instanceof ProtectedCuboidRegion) {
// If either region contains the points of the other, they intersect
if (containsAny(region.getPoints())) {
intersectingRegions.add(region);
continue;
}
}
// Check whether the other regions points are inside the current region
if (region instanceof ProtectedPolygonalRegion) {
for (i2 = 0; i < ((ProtectedPolygonalRegion)region).getPoints().size(); i++) {
BlockVector2D pt2Dr = ((ProtectedPolygonalRegion)region).getPoints().get(i2);
int minYr = ((ProtectedPolygonalRegion)region).minY;
int maxYr = ((ProtectedPolygonalRegion)region).maxY;
Vector ptr = new Vector(pt2Dr.getBlockX(), minYr, pt2Dr.getBlockZ());
Vector ptr2 = new Vector(pt2Dr.getBlockX(), maxYr, pt2Dr.getBlockZ());
if (this.contains(ptr) || this.contains(ptr2)) {
intersectingRegions.add(regions.get(i));
continue;
}
}
} else if (region instanceof ProtectedCuboidRegion) {
BlockVector ptcMin = region.getMinimumPoint();
BlockVector ptcMax = region.getMaximumPoint();
if (this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ()))
|| this.contains(new Vector(ptcMin.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ()))
|| this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ()))
|| this.contains(new Vector(ptcMin.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ()))
|| this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMax.getBlockZ()))
|| this.contains(new Vector(ptcMax.getBlockX(), ptcMax.getBlockY(), ptcMin.getBlockZ()))
|| this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMin.getBlockZ()))
|| this.contains(new Vector(ptcMax.getBlockX(), ptcMin.getBlockY(), ptcMax.getBlockZ())) ) {
intersectingRegions.add(regions.get(i));
if (region.containsAny(getPoints())) {
intersectingRegions.add(region);
continue;
}
} else {
throw new UnsupportedOperationException("Not supported yet.");
}
// Check whether the current regions edges collide with the regions edges
boolean regionIsIntersecting = false;
for (i2 = 0; i2 < numPoints; i2++) {
boolean checkNextPoint = false;
BlockVector2D currPoint = points.get(i2);
BlockVector2D nextPoint;
if (i2 == (numPoints - 1)) {
nextPoint = points.get(0);
} else {
nextPoint = points.get(i2 + 1);
}
int currX = currPoint.getBlockX();
int currZ = currPoint.getBlockZ();
while (!checkNextPoint) {
for(i3 = this.minY; i3 <= this.maxY; i3++) {
if (region.contains(new Vector(currX, i3, currZ))) {
intersectingRegions.add(regions.get(i));
regionIsIntersecting = true;
break;
}
}
if (currX == nextPoint.getBlockX() || currZ == nextPoint.getBlockZ() || regionIsIntersecting) {
checkNextPoint = true;
}
if (nextPoint.getBlockX() > currPoint.getBlockX()) {
currX++;
} else {
currX--;
}
if (nextPoint.getBlockZ() > currPoint.getBlockZ()) {
currZ++;
} else {
currZ--;
}
}
if (regionIsIntersecting) {
break;
}
}
}
return intersectingRegions;
}

View File

@ -25,6 +25,7 @@
import java.util.regex.Pattern;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.domains.DefaultDomain;
@ -38,6 +39,9 @@
*/
public abstract class ProtectedRegion implements Comparable<ProtectedRegion> {
protected BlockVector min;
protected BlockVector max;
private static final Pattern idPattern = Pattern.compile("^[A-Za-z0-9_,'\\-\\+/]{1,}$");
/**
@ -79,6 +83,37 @@ public ProtectedRegion(String id) {
this.id = id;
}
/**
* Sets the minimum and maximum points of the bounding box for a region
*
* @param points
*/
protected void setMinMaxPoints(List<Vector> points) {
int minX = points.get(0).getBlockX();
int minY = points.get(0).getBlockY();
int minZ = points.get(0).getBlockZ();
int maxX = minX;
int maxY = minY;
int maxZ = minZ;
for (Vector v : points) {
int x = v.getBlockX();
int y = v.getBlockY();
int z = v.getBlockZ();
if (x < minX) minX = x;
if (y < minY) minY = y;
if (z < minZ) minZ = z;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
if (z > maxZ) maxZ = z;
}
min = new BlockVector(minX, minY, minZ);
max = new BlockVector(maxX, maxY, maxZ);
}
/**
* @return the id
*/
@ -91,14 +126,18 @@ public String getId() {
*
* @return min point
*/
public abstract BlockVector getMinimumPoint();
public BlockVector getMinimumPoint() {
return min;
}
/**
* Get the upper point of the cuboid.
*
* @return max point
*/
public abstract BlockVector getMaximumPoint();
public BlockVector getMaximumPoint() {
return max;
}
/**
* @return the priority
@ -314,6 +353,13 @@ public void setFlags(Map<Flag<?>, Object> flags) {
this.flags = flags;
}
/**
* Gets the 2D points for this region
*
* @return
*/
public abstract List<BlockVector2D> getPoints();
/**
* Get the number of blocks in this region
*
@ -329,6 +375,43 @@ public void setFlags(Map<Flag<?>, Object> flags) {
*/
public abstract boolean contains(Vector pt);
/**
* Check to see if a point is inside this region.
*
* @param pt
* @return
*/
public boolean contains(BlockVector2D pt) {
return contains(new Vector(pt.getBlockX(), min.getBlockY(), pt.getBlockZ()));
}
/**
* Check to see if a point is inside this region.
*
* @param x
* @param y
* @param z
* @return
*/
public boolean contains(int x, int y, int z) {
return contains(new Vector(x, y, z));
}
/**
* Check to see if any of the 2D points are inside this region.
*
* @param pts
* @return
*/
public boolean containsAny(List<BlockVector2D> pts) {
for (BlockVector2D pt : pts) {
if (contains(pt)) {
return true;
}
}
return false;
}
/**
* Compares to another region.
*
@ -365,6 +448,30 @@ public abstract List<ProtectedRegion> getIntersectingRegions(
List<ProtectedRegion> regions)
throws UnsupportedIntersectionException;
/**
* Checks if the bounding box of a region intersects with with the bounding
* box of this region
*
* @param region
*/
protected boolean intersectsBoundingBox(ProtectedRegion region) {
BlockVector rMaxPoint = region.getMaximumPoint();
BlockVector min = getMinimumPoint();
if (rMaxPoint.getBlockX() < min.getBlockX()) return false;
if (rMaxPoint.getBlockY() < min.getBlockY()) return false;
if (rMaxPoint.getBlockZ() < min.getBlockZ()) return false;
BlockVector rMinPoint = region.getMinimumPoint();
BlockVector max = getMaximumPoint();
if (rMinPoint.getBlockX() > max.getBlockX()) return false;
if (rMinPoint.getBlockY() > max.getBlockY()) return false;
if (rMinPoint.getBlockZ() > max.getBlockZ()) return false;
return true;
}
/**
* Checks to see if the given ID is accurate.
*