WIP filter patterns

This commit is contained in:
Jesse Boyd
2019-05-12 23:32:04 +10:00
parent 459629a2f2
commit c797dcb194
61 changed files with 2075 additions and 793 deletions

View File

@ -29,58 +29,36 @@ import java.util.Comparator;
/**
* An immutable 3-dimensional vector.
*/
public class BlockVector3 {
public abstract class BlockVector3 {
public static final BlockVector3 ZERO = new BlockVector3(0, 0, 0);
public static final BlockVector3 UNIT_X = new BlockVector3(1, 0, 0);
public static final BlockVector3 UNIT_Y = new BlockVector3(0, 1, 0);
public static final BlockVector3 UNIT_Z = new BlockVector3(0, 0, 1);
public static final BlockVector3 ONE = new BlockVector3(1, 1, 1);
public static final BlockVector3 ZERO = new BlockVector3Imp(0, 0, 0);
public static final BlockVector3 UNIT_X = new BlockVector3Imp(1, 0, 0);
public static final BlockVector3 UNIT_Y = new BlockVector3Imp(0, 1, 0);
public static final BlockVector3 UNIT_Z = new BlockVector3Imp(0, 0, 1);
public static final BlockVector3 ONE = new BlockVector3Imp(1, 1, 1);
public static BlockVector3 at(double x, double y, double z) {
return at((int) Math.floor(x), (int) Math.floor(y), (int) Math.floor(z));
}
public static BlockVector3 at(int x, int y, int z) {
return new BlockVector3(x, y, z);
return new BlockVector3Imp(x, y, z);
}
// thread-safe initialization idiom
private static final class YzxOrderComparator {
private static final Comparator<BlockVector3> YZX_ORDER = (a, b) -> {
return ComparisonChain.start()
.compare(a.y, b.y)
.compare(a.z, b.z)
.compare(a.x, b.x)
.result();
};
}
static final Comparator<BlockVector3> YZX_ORDER = (a, b) -> ComparisonChain.start()
.compare(a.getY(), b.getY())
.compare(a.getZ(), b.getZ())
.compare(a.getX(), b.getX())
.result();
/**
* Returns a comparator that sorts vectors first by Y, then Z, then X.
*
*
* <p>
* Useful for sorting by chunk block storage order.
*/
public static Comparator<BlockVector3> sortByCoordsYzx() {
return YzxOrderComparator.YZX_ORDER;
}
protected int x, y, z;
protected BlockVector3(){}
/**
* Construct an instance.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
*/
protected BlockVector3(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
return YZX_ORDER;
}
public MutableBlockVector3 setComponents(double x, double y, double z) {
@ -92,27 +70,27 @@ public class BlockVector3 {
}
public MutableBlockVector3 mutX(double x) {
return new MutableBlockVector3((int) x, y, z);
return new MutableBlockVector3((int) x, getY(), getZ());
}
public MutableBlockVector3 mutY(double y) {
return new MutableBlockVector3(x, (int) y, z);
return new MutableBlockVector3(getX(), (int) y, getZ());
}
public MutableBlockVector3 mutZ(double z) {
return new MutableBlockVector3(x, y, (int) z);
return new MutableBlockVector3(getX(), getY(), (int) z);
}
public MutableBlockVector3 mutX(int x) {
return new MutableBlockVector3(x, y, z);
return new MutableBlockVector3(x, getY(), getZ());
}
public MutableBlockVector3 mutY(int y) {
return new MutableBlockVector3(x, y, z);
return new MutableBlockVector3(getX(), y, getZ());
}
public MutableBlockVector3 mutZ(int z) {
return new MutableBlockVector3(x, y, z);
return new MutableBlockVector3(getX(), getY(), z);
}
/**
@ -120,9 +98,7 @@ public class BlockVector3 {
*
* @return the x coordinate
*/
public int getX() {
return x;
}
public abstract int getX();
/**
* Get the X coordinate.
@ -130,7 +106,7 @@ public class BlockVector3 {
* @return the x coordinate
*/
public int getBlockX() {
return x;
return getX();
}
/**
@ -140,7 +116,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 withX(int x) {
return BlockVector3.at(x, y, z);
return BlockVector3.at(x, getY(), getZ());
}
/**
@ -148,9 +124,7 @@ public class BlockVector3 {
*
* @return the y coordinate
*/
public int getY() {
return y;
}
public abstract int getY();
/**
* Get the Y coordinate.
@ -158,7 +132,7 @@ public class BlockVector3 {
* @return the y coordinate
*/
public int getBlockY() {
return y;
return getY();
}
/**
@ -168,7 +142,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 withY(int y) {
return BlockVector3.at(x, y, z);
return BlockVector3.at(getX(), y, getZ());
}
/**
@ -176,9 +150,7 @@ public class BlockVector3 {
*
* @return the z coordinate
*/
public int getZ() {
return z;
}
public abstract int getZ();
/**
* Get the Z coordinate.
@ -186,7 +158,7 @@ public class BlockVector3 {
* @return the z coordinate
*/
public int getBlockZ() {
return z;
return getZ();
}
/**
@ -196,7 +168,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 withZ(int z) {
return BlockVector3.at(x, y, z);
return BlockVector3.at(getX(), getY(), z);
}
/**
@ -206,7 +178,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 add(BlockVector3 other) {
return add(other.x, other.y, other.z);
return add(other.getX(), other.getY(), other.getZ());
}
/**
@ -218,7 +190,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 add(int x, int y, int z) {
return BlockVector3.at(this.x + x, this.y + y, this.z + z);
return BlockVector3.at(this.getX() + x, this.getY() + y, this.getZ() + z);
}
/**
@ -229,12 +201,12 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 add(BlockVector3... others) {
int newX = x, newY = y, newZ = z;
int newX = getX(), newY = getY(), newZ = getZ();
for (BlockVector3 other : others) {
newX += other.x;
newY += other.y;
newZ += other.z;
newX += other.getX();
newY += other.getY();
newZ += other.getZ();
}
return BlockVector3.at(newX, newY, newZ);
@ -248,7 +220,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 subtract(BlockVector3 other) {
return subtract(other.x, other.y, other.z);
return subtract(other.getX(), other.getY(), other.getZ());
}
/**
@ -261,7 +233,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 subtract(int x, int y, int z) {
return BlockVector3.at(this.x - x, this.y - y, this.z - z);
return BlockVector3.at(this.getX() - x, this.getY() - y, this.getZ() - z);
}
/**
@ -272,12 +244,12 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 subtract(BlockVector3... others) {
int newX = x, newY = y, newZ = z;
int newX = getX(), newY = getY(), newZ = getZ();
for (BlockVector3 other : others) {
newX -= other.x;
newY -= other.y;
newZ -= other.z;
newX -= other.getX();
newY -= other.getY();
newZ -= other.getZ();
}
return BlockVector3.at(newX, newY, newZ);
@ -290,7 +262,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 multiply(BlockVector3 other) {
return multiply(other.x, other.y, other.z);
return multiply(other.getX(), other.getY(), other.getZ());
}
/**
@ -302,7 +274,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 multiply(int x, int y, int z) {
return BlockVector3.at(this.x * x, this.y * y, this.z * z);
return BlockVector3.at(this.getX() * x, this.getY() * y, this.getZ() * z);
}
/**
@ -312,12 +284,12 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 multiply(BlockVector3... others) {
int newX = x, newY = y, newZ = z;
int newX = getX(), newY = getY(), newZ = getZ();
for (BlockVector3 other : others) {
newX *= other.x;
newY *= other.y;
newZ *= other.z;
newX *= other.getX();
newY *= other.getY();
newZ *= other.getZ();
}
return BlockVector3.at(newX, newY, newZ);
@ -340,7 +312,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 divide(BlockVector3 other) {
return divide(other.x, other.y, other.z);
return divide(other.getX(), other.getY(), other.getZ());
}
/**
@ -352,7 +324,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 divide(int x, int y, int z) {
return BlockVector3.at(this.x / x, this.y / y, this.z / z);
return BlockVector3.at(this.getX() / x, this.getY() / y, this.getZ() / z);
}
/**
@ -380,7 +352,7 @@ public class BlockVector3 {
* @return length, squared
*/
public int lengthSq() {
return x * x + y * y + z * z;
return getX() * getX() + getY() * getY() + getZ() * getZ();
}
/**
@ -400,9 +372,9 @@ public class BlockVector3 {
* @return distance
*/
public int distanceSq(BlockVector3 other) {
int dx = other.x - x;
int dy = other.y - y;
int dz = other.z - z;
int dx = other.getX() - getX();
int dy = other.getY() - getY();
int dz = other.getZ() - getZ();
return dx * dx + dy * dy + dz * dz;
}
@ -414,9 +386,9 @@ public class BlockVector3 {
*/
public BlockVector3 normalize() {
double len = length();
double x = this.x / len;
double y = this.y / len;
double z = this.z / len;
double x = this.getX() / len;
double y = this.getY() / len;
double z = this.getZ() / len;
return BlockVector3.at(x, y, z);
}
@ -427,7 +399,7 @@ public class BlockVector3 {
* @return the dot product of this and the other vector
*/
public double dot(BlockVector3 other) {
return x * other.x + y * other.y + z * other.z;
return getX() * other.getX() + getY() * other.getY() + getZ() * other.getZ();
}
/**
@ -437,10 +409,10 @@ public class BlockVector3 {
* @return the cross product of this and the other vector
*/
public BlockVector3 cross(BlockVector3 other) {
return new BlockVector3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
return new BlockVector3Imp(
getY() * other.getZ() - getZ() * other.getY(),
getZ() * other.getX() - getX() * other.getZ(),
getX() * other.getY() - getY() * other.getX()
);
}
@ -452,7 +424,7 @@ public class BlockVector3 {
* @return true if the vector is contained
*/
public boolean containedWithin(BlockVector3 min, BlockVector3 max) {
return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
return getX() >= min.getX() && getX() <= max.getX() && getY() >= min.getY() && getY() <= max.getY() && getZ() >= min.getZ() && getZ() <= max.getZ();
}
/**
@ -464,11 +436,11 @@ public class BlockVector3 {
*/
public BlockVector3 clampY(int min, int max) {
checkArgument(min <= max, "minimum cannot be greater than maximum");
if (y < min) {
return BlockVector3.at(x, min, z);
if (getY() < min) {
return BlockVector3.at(getX(), min, getZ());
}
if (y > max) {
return BlockVector3.at(x, max, z);
if (getY() > max) {
return BlockVector3.at(getX(), max, getZ());
}
return this;
}
@ -512,7 +484,7 @@ public class BlockVector3 {
* @return a new vector
*/
public BlockVector3 abs() {
return BlockVector3.at(Math.abs(x), Math.abs(y), Math.abs(z));
return BlockVector3.at(Math.abs(getX()), Math.abs(getY()), Math.abs(getZ()));
}
/**
@ -528,8 +500,8 @@ public class BlockVector3 {
*/
public BlockVector3 transform2D(double angle, double aboutX, double aboutZ, double translateX, double translateZ) {
angle = Math.toRadians(angle);
double x = this.x - aboutX;
double z = this.z - aboutZ;
double x = this.getX() - aboutX;
double z = this.getZ() - aboutZ;
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double x2 = x * cos - z * sin;
@ -537,7 +509,7 @@ public class BlockVector3 {
return BlockVector3.at(
x2 + aboutX + translateX,
y,
getY(),
z2 + aboutZ + translateZ
);
}
@ -583,10 +555,10 @@ public class BlockVector3 {
* @return minimum
*/
public BlockVector3 getMinimum(BlockVector3 v2) {
return new BlockVector3(
Math.min(x, v2.x),
Math.min(y, v2.y),
Math.min(z, v2.z)
return new BlockVector3Imp(
Math.min(getX(), v2.getX()),
Math.min(getY(), v2.getY()),
Math.min(getZ(), v2.getZ())
);
}
@ -597,10 +569,10 @@ public class BlockVector3 {
* @return maximum
*/
public BlockVector3 getMaximum(BlockVector3 v2) {
return new BlockVector3(
Math.max(x, v2.x),
Math.max(y, v2.y),
Math.max(z, v2.z)
return new BlockVector3Imp(
Math.max(getX(), v2.getX()),
Math.max(getY(), v2.getY()),
Math.max(getZ(), v2.getZ())
);
}
@ -610,31 +582,34 @@ public class BlockVector3 {
* @return a new {@link BlockVector2}
*/
public BlockVector2 toBlockVector2() {
return BlockVector2.at(x, z);
return BlockVector2.at(getX(), getZ());
}
public Vector3 toVector3() {
return Vector3.at(x, y, z);
return Vector3.at(getX(), getY(), getZ());
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (!(obj instanceof BlockVector3)) {
return false;
}
BlockVector3 other = (BlockVector3) obj;
return other.x == this.x && other.y == this.y && other.z == this.z;
return equals((BlockVector3) obj);
}
public final boolean equals(BlockVector3 other) {
return other.getX() == this.getX() && other.getY() == this.getY() && other.getZ() == this.getZ();
}
@Override
public int hashCode() {
return (x ^ (z << 12)) ^ (y << 24);
return (getX() ^ (getZ() << 12)) ^ (getY() << 24);
}
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
return "(" + getX() + ", " + getY() + ", " + getZ() + ")";
}
}

View File

@ -0,0 +1,87 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.math;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
import java.util.Comparator;
/**
* An immutable 3-dimensional vector.
*/
public class BlockVector3Imp extends BlockVector3 {
public static final BlockVector3Imp ZERO = new BlockVector3Imp(0, 0, 0);
public static final BlockVector3Imp UNIT_X = new BlockVector3Imp(1, 0, 0);
public static final BlockVector3Imp UNIT_Y = new BlockVector3Imp(0, 1, 0);
public static final BlockVector3Imp UNIT_Z = new BlockVector3Imp(0, 0, 1);
public static final BlockVector3Imp ONE = new BlockVector3Imp(1, 1, 1);
public static BlockVector3Imp at(double x, double y, double z) {
return at((int) Math.floor(x), (int) Math.floor(y), (int) Math.floor(z));
}
public static BlockVector3Imp at(int x, int y, int z) {
return new BlockVector3Imp(x, y, z);
}
private final int x, y, z;
/**
* Construct an instance.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
*/
protected BlockVector3Imp(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public final int getX() {
return x;
}
@Override
public final int getY() {
return y;
}
@Override
public final int getZ() {
return z;
}
@Override
public int hashCode() {
return (getX() ^ (getZ() << 12)) ^ (getY() << 24);
}
@Override
public String toString() {
return "(" + getX() + ", " + getY() + ", " + getZ() + ")";
}
}

View File

@ -9,6 +9,14 @@ public class MutableBlockVector3 extends BlockVector3 {
}
};
public static MutableBlockVector3 at(double x, double y, double z) {
return at((int) Math.floor(x), (int) Math.floor(y), (int) Math.floor(z));
}
public static MutableBlockVector3 at(int x, int y, int z) {
return new MutableBlockVector3(x, y, z);
}
public static MutableBlockVector3 get(int x, int y, int z) {
return MUTABLE_CACHE.get().setComponents(x, y, z);
}
@ -16,15 +24,19 @@ public class MutableBlockVector3 extends BlockVector3 {
public MutableBlockVector3() {}
public MutableBlockVector3(BlockVector3 other) {
super(other.getX(), other.getY(), other.getZ());
this(other.getX(), other.getY(), other.getZ());
}
public MutableBlockVector3 setComponents(BlockVector3 other) {
return setComponents(other.getBlockX(), other.getBlockY(), other.getBlockZ());
}
private int x,y,z;
public MutableBlockVector3(int x, int y, int z) {
super(x, y, z);
this.x = x;
this.y = y;
this.z = z;
}
@Override
@ -35,6 +47,21 @@ public class MutableBlockVector3 extends BlockVector3 {
return this;
}
@Override
public final int getX() {
return x;
}
@Override
public final int getY() {
return y;
}
@Override
public final int getZ() {
return z;
}
@Override
public MutableBlockVector3 mutX(double x) {
this.x = (int) x;