Add transformation compacting.

This commit is contained in:
Chicken Bones committed 2013-08-29 11:10:33 +10:00
1 parent 0941845227
commit 5ef9f6174d
7 files changed
+133 -66

No files matched your search

+38 -4
View File
@@ -28,8 +28,19 @@ public class RayTracer
private int s_side;
private IndexedCuboid6 c_cuboid;
@Deprecated//not threadsafe
public static RayTracer instance = new RayTracer();
private static ThreadLocal<RayTracer> t_inst = new ThreadLocal<RayTracer>();
public static RayTracer instance()
{
RayTracer inst = t_inst.get();
if(inst == null)
t_inst.set(inst = new RayTracer());
return inst;
}
private void traceSide(int side, Vector3 start, Vector3 end, Cuboid6 cuboid)
{
vec.set(start);
@@ -133,7 +144,7 @@ public class RayTracer
MovingObjectPosition mop = rayTraceCuboid(start, end, cuboid);
if(mop != null && s_dist < c_dist)
{
mop = new ExtendedMOP(mop, cuboid.data);
mop = new ExtendedMOP(mop, cuboid.data, s_dist);
c_dist = s_dist;
c_hit = mop;
c_cuboid = cuboid;
@@ -158,13 +169,30 @@ public class RayTracer
return mop;
}
public void rayTraceCuboids(Vector3 start, Vector3 end, List<IndexedCuboid6> cuboids, BlockCoord pos, Block block, List<ExtendedMOP> hitList)
{
for(IndexedCuboid6 cuboid : cuboids)
{
MovingObjectPosition mop = rayTraceCuboid(start, end, cuboid);
if(mop != null)
{
ExtendedMOP emop = new ExtendedMOP(mop, cuboid.data, s_dist);
emop.typeOfHit = EnumMovingObjectType.TILE;
emop.blockX = pos.x;
emop.blockY = pos.y;
emop.blockZ = pos.z;
hitList.add(emop);
}
}
}
public static MovingObjectPosition retraceBlock(World world, EntityPlayer player, int x, int y, int z)
{
Block block = Block.blocksList[world.getBlockId(x, y, z)];
if(block == null)
return null;
Vec3 headVec = Vec3.createVectorHelper(player.posX, (player.posY + 1.62) - player.yOffset, player.posZ);
Vec3 headVec = getCorrectedHeadVec(player);
Vec3 lookVec = player.getLook(1.0F);
double reach = world.isRemote ? getBlockReachDistance_client() : getBlockReachDistance_server((EntityPlayerMP) player);
Vec3 endVec = headVec.addVector(lookVec.xCoord * reach, lookVec.yCoord * reach, lookVec.zCoord * reach);
@@ -189,9 +217,15 @@ public class RayTracer
public static MovingObjectPosition reTrace(World world, EntityPlayer player, double reach)
{
Vec3 headVec = Vec3.createVectorHelper(player.posX, (player.posY + 1.62) - player.yOffset, player.posZ);
Vec3 headVec = getCorrectedHeadVec(player);
Vec3 lookVec = player.getLook(1);
Vec3 endVec = headVec.addVector(lookVec.xCoord * reach, lookVec.yCoord * reach, lookVec.zCoord * reach);
return world.rayTraceBlocks_do_do(headVec, endVec, true, false);
}
public static Vec3 getCorrectedHeadVec(EntityPlayer player)
{
double d = player.worldObj.isRemote ? 0 : (player instanceof EntityPlayerMP && player.isSneaking() ? 1.54 : 1.62);
return Vec3.createVectorHelper(player.posX, player.posY + d, player.posZ);
}
}
+16
View File
@@ -227,6 +227,22 @@ public class Rotation extends Transformation
{
return new Rotation(-angle, axis);
}
@Override
public Transformation merge(Transformation next) {
if(next instanceof Rotation) {
Rotation r = (Rotation)next;
if(r.axis.equalsT(axis)) {
double d = angle+r.angle;
if(d == 0)
return new RedundantTransformation();
return new Rotation(angle, axis);
}
}
return null;
}
@Override
public String toString()
+13
View File
@@ -58,6 +58,19 @@ public class Scale extends Transformation
return new Scale(1/factor.x, 1/factor.y, 1/factor.z);
}
@Override
public Transformation merge(Transformation next) {
if(next instanceof Scale) {
Vector3 vec = factor.copy().multiply(((Scale) next).factor);
if(vec.equalsT(Vector3.one))
return new RedundantTransformation();
return new Scale(vec);
}
return null;
}
@Override
public String toString()
{
+9 -1
View File
@@ -32,7 +32,7 @@ public abstract class Transformation
*/
public Transformation at(Vector3 point)
{
return new TranslatedTransformation(this, point);
return new TransformationList(new Translation(-point.x, -point.y, -point.z), this, point.translation());
}
/**
@@ -44,6 +44,14 @@ public abstract class Transformation
return new TransformationList(this, t);
}
/**
* Returns a simplified transformation that performs this, followed by next. If such a transformation does not exist, returns null
*/
public Transformation merge(Transformation next)
{
return null;
}
@SideOnly(Side.CLIENT)
public abstract void glApply();
+47 -1
View File
@@ -1,6 +1,7 @@
package codechicken.lib.vec;
import java.util.ArrayList;
import java.util.Iterator;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -13,7 +14,12 @@ public class TransformationList extends Transformation
public TransformationList(Transformation... transforms)
{
for(Transformation t : transforms)
with(t);
if(t instanceof TransformationList)
transformations.addAll(((TransformationList)t).transformations);
else
transformations.add(t);
compact();
}
public Matrix4 compile()
@@ -68,24 +74,64 @@ public class TransformationList extends Transformation
@Override
public TransformationList with(Transformation t)
{
if(t instanceof RedundantTransformation)
return this;
mat = null;//matrix invalid
if(t instanceof TransformationList)
transformations.addAll(((TransformationList)t).transformations);
else
transformations.add(t);
compact();
return this;
}
public TransformationList prepend(Transformation t)
{
if(t instanceof RedundantTransformation)
return this;
mat = null;//matrix invalid
if(t instanceof TransformationList)
transformations.addAll(0, ((TransformationList)t).transformations);
else
transformations.add(0, t);
compact();
return this;
}
private void compact() {
if(transformations.isEmpty())
return;
ArrayList<Transformation> newList = new ArrayList<Transformation>(transformations.size());
Iterator<Transformation> iterator = transformations.iterator();
Transformation prev = iterator.next();
while(iterator.hasNext()) {
Transformation t = iterator.next();
Transformation m = prev == null ? null : prev.merge(t);
if(m instanceof RedundantTransformation) {
prev = null;
}
else if(m != null) {
prev = m;
}
else {
newList.add(prev);
prev = t;
}
}
if(prev != null)
newList.add(prev);
if(newList.size() < transformations.size()) {
transformations = newList;
mat = null;
}
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
@@ -1,60 +0,0 @@
package codechicken.lib.vec;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class TranslatedTransformation extends Transformation
{
public Vector3 point;
public Transformation wrapped;
public TranslatedTransformation(Transformation t, Vector3 vec)
{
point = vec;
wrapped = t;
}
@Override
public void apply(Vector3 vec)
{
vec.subtract(point).apply(wrapped).add(point);
}
@Override
public void applyN(Vector3 normal)
{
wrapped.applyN(normal);
}
@Override
public void apply(Matrix4 mat)
{
mat.translate(point).apply(wrapped)
.translate(new Vector3(-point.x, -point.y, -point.z));
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
GL11.glTranslated(point.x, point.y, point.z);
wrapped.glApply();
GL11.glTranslated(-point.x, -point.y, -point.z);
}
@Override
public Transformation inverse()
{
return new TranslatedTransformation(wrapped.inverse(), point);
}
@Override
public String toString()
{
return point.translation()+"\n"+
wrapped.toString()+"\n"+
new Translation(-point.x, -point.y, -point.z);
}
}
+10
View File
@@ -59,6 +59,16 @@ public class Translation extends Transformation
return new Translation(-vec.x, -vec.y, -vec.z);
}
@Override
public Transformation merge(Transformation next) {
if(next instanceof Translation) {
Vector3 vec2 = vec.copy().add(((Translation) next).vec);
return vec2.isZero() ? new RedundantTransformation() : new Translation(vec2);
}
return null;
}
@Override
public String toString()
{