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

+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()
{