8 changed files with 252 additions and 28 deletions
-
49src/main/java/t/j/dual/CachedPointWorld.java
-
50src/main/java/t/j/dual/GridCache.java
-
19src/main/java/t/j/dual/PointWorld.java
-
21src/main/java/t/j/dual/RandomPointWorld.java
-
39src/main/java/t/j/dual/RecelledPointWorld.java
-
45src/main/java/t/j/dual/ScaledPointWorld.java
-
12src/main/java/t/j/dual/Utilities.java
-
45src/main/java/t/j/dual/WorldProviderPeace.java
@ -0,0 +1,49 @@ |
|||
package t.j.dual; |
|||
|
|||
import java.lang.ref.SoftReference; |
|||
import java.util.ArrayList; |
|||
import java.util.WeakHashMap; |
|||
|
|||
import net.minecraft.util.math.*; |
|||
|
|||
import t.j.dual.PointWorld; |
|||
import t.j.dual.GridCache; |
|||
|
|||
public class CachedPointWorld implements PointWorld { |
|||
PointWorld pworld; |
|||
GridCache<ArrayList<Vec3d>> cache; |
|||
float mean; |
|||
Vec3d cellsize; |
|||
|
|||
public CachedPointWorld(PointWorld pworld) { |
|||
this.pworld = pworld; |
|||
this.cellsize = pworld.getCellSize(); |
|||
this.cache = new GridCache<ArrayList<Vec3d>>((cell) -> pworld.pointsInCell(cell)); |
|||
} |
|||
|
|||
public float getMeanPointsPerCell() { |
|||
return mean; |
|||
} |
|||
|
|||
// TODO: maybe cache? |
|||
public Vec3d getCellOrigin(Vec3i cell) { |
|||
return pworld.getCellOrigin(cell); |
|||
} |
|||
|
|||
public Vec3d getCellSize() { |
|||
return cellsize; |
|||
} |
|||
|
|||
public ArrayList<Vec3d> cache(Vec3i cell) { |
|||
return cache.cache(cell); |
|||
} |
|||
|
|||
public void expunge(Vec3i cell) { |
|||
cache.expunge(cell); |
|||
} |
|||
|
|||
public ArrayList<Vec3d> pointsInCell(Vec3i cell) { |
|||
return cache.get(cell); |
|||
} |
|||
} |
|||
|
@ -0,0 +1,50 @@ |
|||
package t.j.dual; |
|||
|
|||
import java.lang.ref.SoftReference; |
|||
import java.util.WeakHashMap; |
|||
import java.util.function.Function; |
|||
|
|||
import net.minecraft.util.math.*; |
|||
|
|||
import t.j.dual.Utilities; |
|||
|
|||
public class GridCache<T> { |
|||
Function<Vec3i, T> source; |
|||
WeakHashMap<Vec3i, SoftReference<CacheEntry<T>>> cacheMap; |
|||
|
|||
class CacheEntry<T> { |
|||
public Vec3i keyRef; |
|||
public T value; |
|||
|
|||
public CacheEntry(Vec3i keyRef, T value) { |
|||
this.keyRef = keyRef; |
|||
this.value = value; |
|||
} |
|||
} |
|||
|
|||
public GridCache(Function<Vec3i, T> source) { |
|||
this.source = source; |
|||
this.cacheMap = new WeakHashMap<Vec3i, SoftReference<CacheEntry<T>>>(); |
|||
} |
|||
|
|||
public T cache(Vec3i cell) { |
|||
T toCache = source.apply(cell); |
|||
cacheMap.put(cell, new SoftReference<CacheEntry<T>>(new CacheEntry<T>(cell, toCache))); |
|||
return toCache; |
|||
} |
|||
|
|||
public void expunge(Vec3i cell) { |
|||
cacheMap.remove(cell); |
|||
} |
|||
|
|||
public T get(Vec3i cell) { |
|||
SoftReference<CacheEntry<T>> softRef = cacheMap.get(cell); |
|||
if (softRef != null) { |
|||
CacheEntry<T> entry = softRef.get(); |
|||
if (entry != null) { |
|||
return entry.value; |
|||
} |
|||
} |
|||
return cache(cell); |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
package t.j.dual; |
|||
|
|||
import net.minecraft.util.math.*; |
|||
import java.util.Random; |
|||
import java.util.ArrayList; |
|||
|
|||
import t.j.dual.PointWorld; |
|||
|
|||
class RecelledPointWorld implements PointWorld { |
|||
PointWorld pworld; |
|||
Vec3d cellsize; |
|||
float mean; |
|||
|
|||
public RecelledPointWorld(PointWorld pworld, Vec3d cellsize) { |
|||
this.pworld = pworld; |
|||
this.cellsize = cellsize; |
|||
this.mean = pworld.getMeanPointsPerCell() * (float) (cellsize.x * cellsize.y * cellsize.z); |
|||
} |
|||
|
|||
public float getMeanPointsPerCell() { |
|||
return mean; |
|||
} |
|||
|
|||
public Vec3d getCellOrigin(Vec3i cell) { |
|||
return new Vec3d(cell.getX() * cellsize.x, cell.getY() * cellsize.y, cell.getZ() * cellsize.z); |
|||
} |
|||
|
|||
public Vec3d getCellSize() { |
|||
return cellsize; |
|||
} |
|||
|
|||
private Vec3d getCellRemote(Vec3i cell) { |
|||
return new Vec3d((cell.getX() + 1) * cellsize.x, (cell.getY() + 1) * cellsize.y, (cell.getZ() + 1) * cellsize.z); |
|||
} |
|||
|
|||
public ArrayList<Vec3d> pointsInCell(Vec3i cell) { |
|||
return pworld.pointsInAABB(getCellOrigin(cell), getCellRemote(cell)); |
|||
} |
|||
} |
@ -0,0 +1,45 @@ |
|||
package t.j.dual; |
|||
|
|||
import net.minecraft.util.math.*; |
|||
import java.util.Random; |
|||
import java.util.ArrayList; |
|||
|
|||
import t.j.dual.PointWorld; |
|||
import t.j.dual.Utilities; |
|||
|
|||
class ScaledPointWorld implements PointWorld { |
|||
PointWorld pworld; |
|||
Vec3d scale; |
|||
Vec3d iscale; |
|||
Vec3d cellsize; |
|||
float mean; |
|||
|
|||
public ScaledPointWorld(PointWorld pworld, Vec3d scale) { |
|||
this.pworld = pworld; |
|||
this.scale = scale; |
|||
this.iscale = new Vec3d(1.0 / scale.x, 1.0 / scale.y, 1.0 / scale.z); |
|||
this.cellsize = Utilities.ewMult(pworld.getCellSize(), scale); |
|||
this.mean = pworld.getMeanPointsPerCell(); |
|||
} |
|||
|
|||
public float getMeanPointsPerCell() { |
|||
return mean; |
|||
} |
|||
|
|||
public Vec3d getCellOrigin(Vec3i cell) { |
|||
return Utilities.ewMult(pworld.getCellOrigin(cell), scale); |
|||
} |
|||
|
|||
public Vec3d getCellSize() { |
|||
return cellsize; |
|||
} |
|||
|
|||
public ArrayList<Vec3d> pointsInCell(Vec3i cell) { |
|||
ArrayList<Vec3d> unscaled = pworld.pointsInCell(cell); |
|||
ArrayList<Vec3d> scaled = new ArrayList<Vec3d>(unscaled.size()); |
|||
for (Vec3d point : unscaled) { |
|||
scaled.add(Utilities.ewMult(point, scale)); |
|||
} |
|||
return scaled; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue