refactor Vector2

This commit is contained in:
dfsek
2021-06-30 10:47:51 -07:00
parent bf5e8d903c
commit 445bf3d053
18 changed files with 133 additions and 206 deletions

View File

@@ -4,8 +4,6 @@ import com.dfsek.terra.api.noise.NoiseSampler;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.addons.noise.samplers.noise.simplex.OpenSimplex2Sampler;
import com.dfsek.terra.vector.Vector2Impl;
import com.dfsek.terra.vector.Vector3Impl;
/**
* NoiseSampler implementation for Cellular (Voronoi/Worley) Noise.
@@ -231,7 +229,7 @@ public class CellularSampler extends NoiseFunction {
int xPrimed = (xr - 1) * PRIME_X;
int yPrimedBase = (yr - 1) * PRIME_Y;
Vector2 center = new Vector2Impl(x, y);
Vector2 center = new Vector2(x, y);
switch(distanceFunction) {
default:

View File

@@ -1,28 +1,61 @@
package com.dfsek.terra.api.vector;
import com.dfsek.terra.api.util.MathUtil;
import net.jafama.FastMath;
/**
* oh yeah
*/
public interface Vector2 extends Cloneable {
public class Vector2 implements Cloneable {
private double x;
private double z;
/**
* Create a vector with a given X and Z component
*
* @param x X component
* @param z Z component
*/
public Vector2(double x, double z) {
this.x = x;
this.z = z;
}
/**
* Get X component
*
* @return X component
*/
double getX();
public double getX() {
return x;
}
Vector2 clone();
public Vector2 clone() {
try {
return (Vector2) super.clone();
} catch(CloneNotSupportedException e) {
throw new Error(e);
}
}
Vector2 setX(double x);
public Vector2 setX(double x) {
this.x = x;
return this;
}
/**
* Get Z component
*
* @return Z component
*/
double getZ();
public double getZ() {
return z;
}
Vector2 setZ(double z);
public Vector2 setZ(double z) {
this.z = z;
return this;
}
/**
* Multiply X and Z components by a value.
@@ -30,7 +63,11 @@ public interface Vector2 extends Cloneable {
* @param m Value to multiply
* @return Mutated vector, for chaining.
*/
Vector2 multiply(double m);
public Vector2 multiply(double m) {
x *= m;
z *= m;
return this;
}
/**
* Add this vector to another.
@@ -38,7 +75,11 @@ public interface Vector2 extends Cloneable {
* @param other Vector to add
* @return Mutated vector, for chaining.
*/
Vector2 add(Vector2 other);
public Vector2 add(Vector2 other) {
x += other.getX();
z += other.getZ();
return this;
}
/**
* Subtract a vector from this vector,
@@ -46,14 +87,21 @@ public interface Vector2 extends Cloneable {
* @param other Vector to subtract
* @return Mutated vector, for chaining.
*/
Vector2 subtract(Vector2 other);
public Vector2 subtract(Vector2 other) {
x -= other.getX();
z -= other.getZ();
return this;
}
/**
* Normalize this vector to length 1
*
* @return Mutated vector, for chaining.
*/
Vector2 normalize();
public Vector2 normalize() {
divide(length());
return this;
}
/**
* Divide X and Z components by a value.
@@ -61,21 +109,31 @@ public interface Vector2 extends Cloneable {
* @param d Divisor
* @return Mutated vector, for chaining.
*/
Vector2 divide(double d);
public Vector2 divide(double d) {
x /= d;
z /= d;
return this;
}
/**
* Get the length of this Vector
*
* @return length
*/
double length();
public double length() {
return FastMath.sqrt(lengthSquared());
}
/**
* Get the squared length of this Vector
*
* @return squared length
*/
double lengthSquared();
public double lengthSquared() {
return x * x + z * z;
}
/**
* Get the distance from this vector to another.
@@ -83,7 +141,10 @@ public interface Vector2 extends Cloneable {
* @param other Another vector
* @return Distance between vectors
*/
double distance(Vector2 other);
public double distance(Vector2 other) {
return FastMath.sqrt(distanceSquared(other));
}
/**
* Get the squared distance between 2 vectors.
@@ -91,13 +152,48 @@ public interface Vector2 extends Cloneable {
* @param other Another vector
* @return Squared distance
*/
double distanceSquared(Vector2 other);
public double distanceSquared(Vector2 other) {
double dx = other.getX() - x;
double dz = other.getZ() - z;
return dx * dx + dz * dz;
}
Vector2 add(double x, double z);
int getBlockX();
public Vector3 extrude(double y) {
return new Vector3(this.x, y, this.z);
}
int getBlockZ();
Vector3 extrude(double y);
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Double.hashCode(x);
hash = 31 * hash + Double.hashCode(z);
return hash;
}
public boolean equals(Object obj) {
if(!(obj instanceof Vector2)) return false;
Vector2 other = (Vector2) obj;
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
}
public Vector2 add(double x, double z) {
this.x += x;
this.z += z;
return this;
}
public int getBlockX() {
return FastMath.floorToInt(x);
}
public int getBlockZ() {
return FastMath.floorToInt(z);
}
@Override
public String toString() {
return "(" + x + ", " + z + ")";
}
}

View File

@@ -12,7 +12,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -35,7 +34,7 @@ public abstract class AbstractBlockFunction implements Function<Void> {
}
void setBlock(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap, TerraImplementationArguments arguments, BlockState rot) {
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -11,7 +11,6 @@ import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -35,7 +34,7 @@ public class BiomeFunction implements Function<String> {
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -9,7 +9,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -30,7 +29,7 @@ public class CheckBlockFunction implements Function<String> {
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -16,7 +16,6 @@ import com.dfsek.terra.api.world.biome.UserDefinedBiome;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.generator.SamplerCache;
import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -42,7 +41,7 @@ public class CheckFunction implements Function<String> {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -14,7 +14,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import java.util.Map;
@@ -38,7 +37,7 @@ public class EntityFunction implements Function<Void> {
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -9,7 +9,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -28,7 +27,7 @@ public class GetMarkFunction implements Function<String> {
@Override
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
String mark = arguments.getBuffer().getMark(new Vector3(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())));

View File

@@ -14,7 +14,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -41,7 +40,7 @@ public class LootFunction implements Function<Void> {
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -14,7 +14,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -37,7 +36,7 @@ public class PullFunction implements Function<Void> {
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
BlockState rot = data.clone();

View File

@@ -9,7 +9,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -30,7 +29,7 @@ public class SetMarkFunction implements Function<Void> {
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -11,7 +11,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.Map;
@@ -34,7 +33,7 @@ public class StateFunction implements Function<Void> {
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
arguments.getBuffer().addItem(new BufferedStateManipulator(main, data.apply(implementationArguments, variableMap)), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(), FastMath.roundToInt(xz.getZ())));

View File

@@ -14,7 +14,6 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.vector.Vector2Impl;
import net.jafama.FastMath;
import java.util.List;
@@ -51,7 +50,7 @@ public class StructureFunction implements Function<Boolean> {
if(arguments.getRecursions() > main.getTerraConfig().getMaxRecursion())
throw new RuntimeException("Structure recursion too deep: " + arguments.getRecursions());
Vector2 xz = new Vector2Impl(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = new Vector2(x.apply(implementationArguments, variableMap).doubleValue(), z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());

View File

@@ -2,10 +2,10 @@ package com.dfsek.terra.api.world.biome.pipeline;
import com.dfsek.terra.api.util.GlueList;
import com.dfsek.terra.api.util.seeded.StageSeeded;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.world.biome.generation.pipeline.BiomeHolder;
import com.dfsek.terra.api.world.biome.generation.pipeline.BiomeSource;
import com.dfsek.terra.api.world.biome.generation.pipeline.Stage;
import com.dfsek.terra.vector.Vector2Impl;
import java.util.List;
import java.util.stream.Collectors;
@@ -31,7 +31,7 @@ public class BiomePipelineImpl {
* @return BiomeHolder containing biomes.
*/
public BiomeHolder getBiomes(int x, int z) {
BiomeHolder holder = new BiomeHolderImpl(init, new Vector2Impl(x * (init - 1), z * (init - 1)));
BiomeHolder holder = new BiomeHolderImpl(init, new Vector2(x * (init - 1), z * (init - 1)));
holder.fill(source);
for(Stage stage : stages) holder = stage.apply(holder);
return holder;

View File

@@ -7,7 +7,6 @@ import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.biome.generation.pipeline.BiomeHolder;
import com.dfsek.terra.api.world.biome.pipeline.BiomePipelineImpl;
import com.dfsek.terra.vector.Vector2Impl;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
@@ -52,6 +51,6 @@ public class StandardBiomeProvider implements BiomeProvider {
int fdX = FastMath.floorDiv(x, pipeline.getSize());
int fdZ = FastMath.floorDiv(z, pipeline.getSize());
return holderCache.getUnchecked(new Vector2Impl(fdX, fdZ)).getBiome(x - fdX * pipeline.getSize(), z - fdZ * pipeline.getSize());
return holderCache.getUnchecked(new Vector2(fdX, fdZ)).getBiome(x - fdX * pipeline.getSize(), z - fdZ * pipeline.getSize());
}
}

View File

@@ -1,154 +0,0 @@
package com.dfsek.terra.vector;
import com.dfsek.terra.api.util.MathUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.vector.Vector3;
import net.jafama.FastMath;
/**
* oh yeah
*/
public class Vector2Impl implements Vector2 {
private double x;
private double z;
/**
* Create a vector with a given X and Z component
*
* @param x X component
* @param z Z component
*/
public Vector2Impl(double x, double z) {
this.x = x;
this.z = z;
}
@Override
public double getX() {
return x;
}
@Override
public Vector2 setX(double x) {
this.x = x;
return this;
}
@Override
public double getZ() {
return z;
}
@Override
public Vector2 setZ(double z) {
this.z = z;
return this;
}
@Override
public Vector2 multiply(double m) {
x *= m;
z *= m;
return this;
}
@Override
public Vector2 add(Vector2 other) {
x += other.getX();
z += other.getZ();
return this;
}
@Override
public Vector2 subtract(Vector2 other) {
x -= other.getX();
z -= other.getZ();
return this;
}
@Override
public Vector2 normalize() {
divide(length());
return this;
}
@Override
public Vector2 divide(double d) {
x /= d;
z /= d;
return this;
}
@Override
public double length() {
return FastMath.sqrt(lengthSquared());
}
@Override
public double lengthSquared() {
return x * x + z * z;
}
@Override
public double distance(Vector2 other) {
return FastMath.sqrt(distanceSquared(other));
}
@Override
public double distanceSquared(Vector2 other) {
double dx = other.getX() - x;
double dz = other.getZ() - z;
return dx * dx + dz * dz;
}
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Double.hashCode(x);
hash = 31 * hash + Double.hashCode(z);
return hash;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Vector2Impl)) return false;
Vector2Impl other = (Vector2Impl) obj;
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
}
@Override
public Vector2 clone() {
try {
return (Vector2) super.clone();
} catch(CloneNotSupportedException e) {
throw new Error(e);
}
}
@Override
public Vector2 add(double x, double z) {
this.x += x;
this.z += z;
return this;
}
@Override
public int getBlockX() {
return FastMath.floorToInt(x);
}
@Override
public int getBlockZ() {
return FastMath.floorToInt(z);
}
@Override
public Vector3 extrude(double y) {
return new Vector3(this.x, y, this.z);
}
@Override
public String toString() {
return "(" + x + ", " + z + ")";
}
}

View File

@@ -10,7 +10,6 @@ import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.UserDefinedBiome;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.generator.TerraBlockPopulator;
import com.dfsek.terra.vector.Vector2Impl;
import com.dfsek.terra.world.population.items.flora.FloraLayer;
import org.jetbrains.annotations.NotNull;
@@ -42,7 +41,7 @@ public class FloraPopulator implements TerraBlockPopulator {
for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) {
UserDefinedBiome biome = (UserDefinedBiome) provider.getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z);
Vector2 l = new Vector2Impl(x, z);
Vector2 l = new Vector2(x, z);
layers.put(l, biome.getConfig().getFlora());
}
}

View File

@@ -3,13 +3,13 @@ package com.dfsek.terra.world.population;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.util.PopulationUtil;
import com.dfsek.terra.api.vector.Vector2;
import com.dfsek.terra.api.world.Chunk;
import com.dfsek.terra.api.world.TerraWorld;
import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.UserDefinedBiome;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.generator.TerraBlockPopulator;
import com.dfsek.terra.vector.Vector2Impl;
import com.dfsek.terra.world.population.items.tree.TreeLayer;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
@@ -43,7 +43,7 @@ public class TreePopulator implements TerraBlockPopulator {
UserDefinedBiome biome = (UserDefinedBiome) provider.getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z);
for(TreeLayer layer : biome.getConfig().getTrees()) {
if(layer.getDensity() >= random.nextDouble() * 100) {
layer.place(chunk, new Vector2Impl(offset(random, x), offset(random, z)));
layer.place(chunk, new Vector2(offset(random, x), offset(random, z)));
}
}
}