-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pablo Herrera <[email protected]>
- Loading branch information
1 parent
3dd6af7
commit 2402a58
Showing
10 changed files
with
231 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package tc.oc.pgm.regions; | ||
|
||
import org.bukkit.util.Vector; | ||
import tc.oc.pgm.api.region.Region; | ||
import tc.oc.pgm.util.math.TransformMatrix; | ||
|
||
public class ResizedRegion extends TransformedRegion { | ||
private final Vector min, max; | ||
private final boolean relative; | ||
private TransformMatrix matrix; | ||
private TransformMatrix inverse; | ||
|
||
public ResizedRegion(Region region, Vector min, Vector max, boolean relative) { | ||
super(region); | ||
this.min = min; | ||
this.max = max; | ||
this.relative = relative; | ||
} | ||
|
||
@Override | ||
protected Vector transform(Vector point) { | ||
if (matrix == null) ensureInitialized(); | ||
return matrix.transform(point); | ||
} | ||
|
||
@Override | ||
protected Vector untransform(Vector point) { | ||
if (inverse == null) ensureInitialized(); | ||
return inverse.transform(point); | ||
} | ||
|
||
@Override | ||
public Bounds getBounds() { | ||
ensureInitialized(); | ||
return super.getBounds(); | ||
} | ||
|
||
private void ensureInitialized() { | ||
if (matrix != null) return; | ||
|
||
var oldBounds = region.getBounds(); | ||
var oldSize = oldBounds.getSize(); | ||
|
||
if (relative) { | ||
min.multiply(oldSize); | ||
max.multiply(oldSize); | ||
} | ||
|
||
this.bounds = | ||
new Bounds(oldBounds.getMin().subtract(min), oldBounds.getMax().add(max)); | ||
var newSize = bounds.getSize(); | ||
|
||
this.matrix = TransformMatrix.concat( | ||
TransformMatrix.untranslate(oldBounds.getMin()), | ||
TransformMatrix.scale(newSize.clone().divide(oldSize)), | ||
TransformMatrix.translate(bounds.getMin())); | ||
|
||
this.inverse = TransformMatrix.concat( | ||
TransformMatrix.untranslate(bounds.getMin()), | ||
TransformMatrix.scale(oldSize.clone().divide(newSize)), | ||
TransformMatrix.translate(oldBounds.getMin())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
core/src/main/java/tc/oc/pgm/util/xml/parsers/NumberBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tc.oc.pgm.util.xml.parsers; | ||
|
||
import org.jdom2.Element; | ||
import org.jetbrains.annotations.Nullable; | ||
import tc.oc.pgm.util.xml.InvalidXMLException; | ||
import tc.oc.pgm.util.xml.Node; | ||
import tc.oc.pgm.util.xml.XMLUtils; | ||
|
||
public class NumberBuilder<T extends Number> extends Builder<T, NumberBuilder<T>> { | ||
|
||
private final Class<T> type; | ||
private boolean infinity; | ||
|
||
public NumberBuilder(Class<T> type, @Nullable Element el, String... prop) { | ||
super(el, prop); | ||
this.type = type; | ||
} | ||
|
||
/** Allow infinity like oo or -oo */ | ||
public NumberBuilder<T> inf() { | ||
this.infinity = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected T parse(Node node) throws InvalidXMLException { | ||
return XMLUtils.parseNumber(node, type, infinity); | ||
} | ||
|
||
@Override | ||
protected NumberBuilder<T> getThis() { | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
util/src/main/java/tc/oc/pgm/util/math/TransformMatrix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package tc.oc.pgm.util.math; | ||
|
||
import org.bukkit.util.Vector; | ||
|
||
public class TransformMatrix { | ||
private final double[] matrix; | ||
|
||
private TransformMatrix(double[] matrix) { | ||
this.matrix = matrix; | ||
} | ||
|
||
public static TransformMatrix translate(Vector vector) { | ||
return new TransformMatrix(new double[] { | ||
1, 0, 0, vector.getX(), | ||
0, 1, 0, vector.getY(), | ||
0, 0, 1, vector.getZ(), | ||
0, 0, 0, 1 | ||
}); | ||
} | ||
|
||
public static TransformMatrix untranslate(Vector vector) { | ||
return new TransformMatrix(new double[] { | ||
1, 0, 0, -vector.getX(), | ||
0, 1, 0, -vector.getY(), | ||
0, 0, 1, -vector.getZ(), | ||
0, 0, 0, 1 | ||
}); | ||
} | ||
|
||
public static TransformMatrix scale(Vector vector) { | ||
double x = vector.getX(); | ||
double y = vector.getY(); | ||
double z = vector.getZ(); | ||
return new TransformMatrix(new double[] {x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1}); | ||
} | ||
|
||
public static TransformMatrix concat(TransformMatrix... a) { | ||
if (a.length == 1) return a[0]; | ||
|
||
TransformMatrix result = a[a.length - 1]; | ||
for (int i = a.length - 2; i >= 0; i--) { | ||
result = result.multiply(a[i]); | ||
} | ||
return result; | ||
} | ||
|
||
public TransformMatrix multiply(TransformMatrix other) { | ||
double[] a = this.matrix; | ||
double[] b = other.matrix; | ||
|
||
return new TransformMatrix(new double[] { | ||
a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12], | ||
a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13], | ||
a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14], | ||
a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15], | ||
a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12], | ||
a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13], | ||
a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14], | ||
a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15], | ||
a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12], | ||
a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13], | ||
a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14], | ||
a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15], | ||
a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12], | ||
a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13], | ||
a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14], | ||
a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15], | ||
}); | ||
} | ||
|
||
public Vector transform(Vector point) { | ||
double oldX = point.getX(); | ||
double oldY = point.getY(); | ||
double oldZ = point.getZ(); | ||
|
||
double x = matrix[0] * oldX + matrix[1] * oldY + matrix[2] * oldZ + matrix[3]; | ||
double y = matrix[4] * oldX + matrix[5] * oldY + matrix[6] * oldZ + matrix[7]; | ||
double z = matrix[8] * oldX + matrix[9] * oldY + matrix[10] * oldZ + matrix[11]; | ||
return new Vector(x, y, z); | ||
} | ||
} |