Initial Commit

This commit is contained in:
Chicken Bones committed 2013-07-16 17:58:02 +10:00
1 parent 397135f687
commit 0f32dd8620
82 files changed
+10693

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
package codechicken.lib.vec;
public class AxisCycle
{
public static Transformation[] cycles = new Transformation[]{
new RedundantTransformation(),
new VariableTransformation(new Matrix4(0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d0 = vec.x; double d1 = vec.y; double d2 = vec.z;
vec.x = d2; vec.y = d0; vec.z = d1;
} @Override public Transformation inverse(){
return cycles[2];
}},
new VariableTransformation(new Matrix4(0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d0 = vec.x; double d1 = vec.y; double d2 = vec.z;
vec.x = d1; vec.y = d2; vec.z = d0;
} @Override public Transformation inverse(){
return cycles[1];
}}};
}
+180
View File
@@ -0,0 +1,180 @@
package codechicken.lib.vec;
import net.minecraft.tileentity.TileEntity;
import codechicken.lib.math.MathHelper;
public class BlockCoord implements Comparable<BlockCoord>
{
public int x;
public int y;
public int z;
public BlockCoord(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public BlockCoord(Vector3 v)
{
this(MathHelper.floor_double(v.x), MathHelper.floor_double(v.y), MathHelper.floor_double(v.z));
}
public BlockCoord(TileEntity tile)
{
this(tile.xCoord, tile.yCoord, tile.zCoord);
}
public BlockCoord(int[] ia)
{
this(ia[0], ia[1], ia[2]);
}
public BlockCoord()
{
}
@Override
public boolean equals(Object obj)
{
if(!(obj instanceof BlockCoord))
return false;
BlockCoord o2 = (BlockCoord)obj;
return x == o2.x && y == o2.y && z == o2.z;
}
@Override
public int hashCode()
{
return (x^z)*31 + y;
}
public int compareTo(BlockCoord o)
{
if(x != o.x)return x < o.x ? 1 : -1;
if(y != o.y)return y < o.y ? 1 : -1;
if(z != o.z)return z < o.z ? 1 : -1;
return 0;
}
public Vector3 toVector3Centered()
{
return new Vector3(x+0.5, y+0.5, z+0.5);
}
public BlockCoord multiply(int i)
{
x*=i;
y*=i;
z*=i;
return this;
}
public double mag_()
{
return Math.sqrt(x*x+y*y+z*z);
}
public int mag2()
{
return x*x+y*y+z*z;
}
public boolean isZero()
{
return x == 0 && y == 0 && z == 0;
}
public boolean isAxial()
{
return x == 0 ? (y == 0 || z == 0) : (y == 0 && z == 0);
}
public BlockCoord add(BlockCoord coord2)
{
x+=coord2.x;
y+=coord2.y;
z+=coord2.z;
return this;
}
public BlockCoord add(int i, int j, int k)
{
x+=i;
y+=j;
z+=k;
return this;
}
public BlockCoord sub(BlockCoord coord2)
{
x-=coord2.x;
y-=coord2.y;
z-=coord2.z;
return this;
}
public BlockCoord sub(int i, int j, int k)
{
x-=i;
y-=j;
z-=k;
return this;
}
public BlockCoord offset(int side)
{
return offset(side, 1);
}
public BlockCoord offset(int side, int amount)
{
BlockCoord offset = sideOffsets[side];
x+=offset.x*amount;
y+=offset.y*amount;
z+=offset.z*amount;
return this;
}
public BlockCoord inset(int side)
{
return inset(side, 1);
}
public BlockCoord inset(int side, int amount)
{
return offset(side, -amount);
}
public static final BlockCoord[] sideOffsets = new BlockCoord[]{
new BlockCoord( 0,-1, 0),
new BlockCoord( 0, 1, 0),
new BlockCoord( 0, 0,-1),
new BlockCoord( 0, 0, 1),
new BlockCoord(-1, 0, 0),
new BlockCoord( 1, 0, 0)};
public int[] intArray()
{
return new int[]{x, y, z};
}
public BlockCoord copy()
{
return new BlockCoord(x, y, z);
}
public BlockCoord set(int i, int j, int k)
{
x = i;
y = j;
z = k;
return this;
}
public BlockCoord set(BlockCoord coord)
{
return set(coord.x, coord.y, coord.z);
}
}
+136
View File
@@ -0,0 +1,136 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import net.minecraft.block.Block;
import net.minecraft.util.AxisAlignedBB;
public class Cuboid6
{
public static Cuboid6 full = new Cuboid6(0, 0, 0, 1, 1, 1);
public Vector3 min;
public Vector3 max;
public Cuboid6(Vector3 min, Vector3 max)
{
this.min = min;
this.max = max;
}
public Cuboid6(AxisAlignedBB aabb)
{
min = new Vector3(aabb.minX, aabb.minY, aabb.minZ);
max = new Vector3(aabb.maxX, aabb.maxY, aabb.maxZ);
}
public Cuboid6(Cuboid6 cuboid)
{
min = cuboid.min.copy();
max = cuboid.max.copy();
}
public Cuboid6(double minx, double miny, double minz, double maxx, double maxy, double maxz)
{
min = new Vector3(minx, miny, minz);
max = new Vector3(maxx, maxy, maxz);
}
public AxisAlignedBB toAABB()
{
return AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, max.y, max.z);
}
public Cuboid6 copy()
{
return new Cuboid6(this);
}
public Cuboid6 add(Vector3 vec)
{
min.add(vec);
max.add(vec);
return this;
}
public Cuboid6 sub(Vector3 vec)
{
min.subtract(vec);
max.subtract(vec);
return this;
}
public void setBlockBounds(Block block)
{
block.setBlockBounds((float)min.x, (float)min.y, (float)min.z, (float)max.x, (float)max.y, (float)max.z);
}
public boolean intersects(Cuboid6 b)
{
return max.x-1E-5 > b.min.x &&
b.max.x-1E-5 > min.x &&
max.y-1E-5 > b.min.y &&
b.max.y-1E-5 > min.y &&
max.z-1E-5 > b.min.z &&
b.max.z-1E-5 > min.z;
}
public Cuboid6 offset(Cuboid6 o)
{
min.add(o.min);
max.add(o.max);
return this;
}
public Vector3 center()
{
return min.copy().add(max).multiply(0.5);
}
public static boolean intersects(Cuboid6 a, Cuboid6 b)
{
return a != null && b != null && a.intersects(b);
}
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Cuboid: ("+new BigDecimal(min.x, cont)+", "+new BigDecimal(min.y, cont)+", "+new BigDecimal(min.z, cont)+") -> ("+
new BigDecimal(max.x, cont)+", "+new BigDecimal(max.y, cont)+", "+new BigDecimal(max.z, cont)+")";
}
public Cuboid6 enclose(Vector3 vec)
{
if(min.x > vec.x) min.x = vec.x;
if(min.y > vec.y) min.y = vec.y;
if(min.z > vec.z) min.z = vec.z;
if(max.x < vec.x) max.x = vec.x;
if(max.y < vec.y) max.y = vec.y;
if(max.z < vec.z) max.z = vec.z;
return this;
}
public Cuboid6 enclose(Cuboid6 c)
{
if(min.x > c.min.x) min.x = c.min.x;
if(min.y > c.min.y) min.y = c.min.y;
if(min.z > c.min.z) min.z = c.min.z;
if(max.x < c.max.x) max.x = c.max.x;
if(max.y < c.max.y) max.y = c.max.y;
if(max.z < c.max.z) max.z = c.max.z;
return this;
}
public Cuboid6 transform(Transformation t)
{
t.apply(min);
t.apply(max);
double temp;
if(min.x > max.x) {temp = min.x; min.x = max.x; max.x = temp;}
if(min.y > max.y) {temp = min.y; min.y = max.y; max.y = temp;}
if(min.z > max.z) {temp = min.z; min.z = max.z; max.z = temp;}
return this;
}
}
+113
View File
@@ -0,0 +1,113 @@
package codechicken.lib.vec;
import net.minecraft.util.AxisAlignedBB;
public class CuboidCoord
{
public BlockCoord min;
public BlockCoord max;
public CuboidCoord(BlockCoord min, BlockCoord max)
{
this.min = min;
this.max = max;
}
public CuboidCoord(BlockCoord coord)
{
this(coord, coord.copy());
}
public CuboidCoord(int[] ia)
{
this(ia[0], ia[1], ia[2], ia[3], ia[4], ia[5]);
}
public CuboidCoord(int x1, int y1, int z1, int x2, int y2, int z2)
{
this(new BlockCoord(x1, y1, z1), new BlockCoord(x2, y2, z2));
}
public void expand(int side, int amount)
{
if(side%2 == 0)//negative side
min = min.offset(side, amount);
else
max = max.offset(side, amount);
}
public void shrink(int side, int amount)
{
if(side%2 == 0)//negative side
min = min.inset(side, amount);
else
max = max.inset(side, amount);
}
public int size(int s)
{
switch(s)
{
case 0:
case 1:
return max.y - min.y+1;
case 2:
case 3:
return max.z - min.z+1;
case 4:
case 5:
return max.x - min.x+1;
default:
return 0;
}
}
public int getVolume()
{
return (max.x-min.x+1)*(max.y-min.y+1)*(max.z-min.z+1);
}
public Vector3 getCenterVec()
{
return new Vector3(min.x+(max.x-min.x+1)/2D, min.y+(max.y-min.y+1)/2D, min.z+(max.z-min.z+1)/2D);
}
public BlockCoord getCenter(BlockCoord store)
{
store.set(min.x+(max.x-min.x)/2, min.y+(max.y-min.y)/2, min.z+(max.z-min.z)/2);
return store;
}
public boolean contains(BlockCoord coord)
{
return contains(coord.x, coord.y, coord.z);
}
public boolean contains(int x, int y, int z)
{
return x >= min.x && x <= max.x
&& y >= min.y && y <= max.y
&& z >= min.z && z <= max.z;
}
public int[] intArray()
{
return new int[]{min.x, min.y, min.z, max.x, max.y, max.z};
}
public CuboidCoord copy()
{
return new CuboidCoord(min.copy(), max.copy());
}
public AxisAlignedBB toAABB()
{
return AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x+1, max.y+1, max.z+1);
}
public void set(BlockCoord min, BlockCoord max)
{
this.min = min;
this.max = max;
}
}
@@ -0,0 +1,18 @@
package codechicken.lib.vec;
@SuppressWarnings("serial")
public class IrreversibleTransformationException extends RuntimeException
{
public Transformation t;
public IrreversibleTransformationException(Transformation t)
{
this.t = t;
}
@Override
public String getMessage()
{
return "The following transformation is irreversible:\n"+t;
}
}
+47
View File
@@ -0,0 +1,47 @@
package codechicken.lib.vec;
public class Line3
{
public static final double tol = 0.0001D;
public Vector3 pt1;
public Vector3 pt2;
public Line3(Vector3 pt1, Vector3 pt2)
{
this.pt1 = pt1;
this.pt2 = pt2;
}
public Line3()
{
this(new Vector3(), new Vector3());
}
public static boolean intersection2D(Line3 line1, Line3 line2, Vector3 store)
{
// calculate differences
double xD1 = line1.pt2.x - line1.pt1.x;
double zD1 = line1.pt2.z - line1.pt1.z;
double xD2 = line2.pt2.x - line2.pt1.x;
double zD2 = line2.pt2.z - line2.pt1.z;
double xD3 = line1.pt1.x - line2.pt1.x;
double zD3 = line1.pt1.z - line2.pt1.z;
double div = zD2 * xD1 - xD2 * zD1;
if(div == 0)//lines are parallel
return false;
double ua = (xD2 * zD3 - zD2 * xD3) / div;
store.set(line1.pt1.x + ua * xD1, 0, line1.pt1.z + ua * zD1);
if(store.x >= Math.min(line1.pt1.x, line1.pt2.x)-tol && store.x >= Math.min(line2.pt1.x, line2.pt2.x)-tol
&& store.z >= Math.min(line1.pt1.z, line1.pt2.z)-tol && store.z >= Math.min(line2.pt1.z, line2.pt2.z)-tol
&& store.x <= Math.max(line1.pt1.x, line1.pt2.x)+tol && store.x <= Math.max(line2.pt1.x, line2.pt2.x)+tol
&& store.z <= Math.max(line1.pt1.z, line1.pt2.z)+tol && store.z <= Math.max(line2.pt1.z, line2.pt2.z)+tol)
return true;
return false;
}
}
+353
View File
@@ -0,0 +1,353 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class Matrix4 extends Transformation
{
private static DoubleBuffer glBuf = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
//m<row><column>
public double m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33;
public Matrix4()
{
m00 = m11 = m22 = m33 = 1;
}
public Matrix4(double d00, double d01, double d02, double d03,
double d10, double d11, double d12, double d13,
double d20, double d21, double d22, double d23,
double d30, double d31, double d32, double d33)
{
m00 = d00;
m01 = d01;
m02 = d02;
m03 = d03;
m10 = d10;
m11 = d11;
m12 = d12;
m13 = d13;
m20 = d20;
m21 = d21;
m22 = d22;
m23 = d23;
m30 = d30;
m31 = d31;
m32 = d32;
m33 = d33;
}
public Matrix4(Matrix4 mat)
{
set(mat);
}
public Matrix4 setIdentity()
{
m00 = m11 = m22 = m33 = 1;
m01 = m02 = m03 = m10 = m12 = m13 = m20 = m21 = m23 = m30 = m31 = m32 = 0;
return this;
}
public Matrix4 translate(Vector3 vec)
{
m03 += m00 * vec.x + m01 * vec.y + m02 * vec.z;
m13 += m10 * vec.x + m11 * vec.y + m12 * vec.z;
m23 += m20 * vec.x + m21 * vec.y + m22 * vec.z;
m33 += m30 * vec.x + m31 * vec.y + m32 * vec.z;
return this;
}
public Matrix4 scale(Vector3 vec)
{
m00 *= vec.x;
m10 *= vec.x;
m20 *= vec.x;
m30 *= vec.x;
m01 *= vec.y;
m11 *= vec.y;
m21 *= vec.y;
m31 *= vec.y;
m02 *= vec.z;
m12 *= vec.z;
m22 *= vec.z;
m32 *= vec.z;
return this;
}
public Matrix4 rotate(double angle, Vector3 axis)
{
double c = Math.cos(angle);
double s = Math.sin(angle);
double mc = 1.0f - c;
double xy = axis.x*axis.y;
double yz = axis.y*axis.z;
double xz = axis.x*axis.z;
double xs = axis.x*s;
double ys = axis.y*s;
double zs = axis.z*s;
double f00 = axis.x*axis.x*mc+c;
double f10 = xy*mc+zs;
double f20 = xz*mc-ys;
double f01 = xy*mc-zs;
double f11 = axis.y*axis.y*mc+c;
double f21 = yz*mc+xs;
double f02 = xz*mc+ys;
double f12 = yz*mc-xs;
double f22 = axis.z*axis.z*mc+c;
double t00 = m00 * f00 + m01 * f10 + m02 * f20;
double t10 = m10 * f00 + m11 * f10 + m12 * f20;
double t20 = m20 * f00 + m21 * f10 + m22 * f20;
double t30 = m30 * f00 + m31 * f10 + m32 * f20;
double t01 = m00 * f01 + m01 * f11 + m02 * f21;
double t11 = m10 * f01 + m11 * f11 + m12 * f21;
double t21 = m20 * f01 + m21 * f11 + m22 * f21;
double t31 = m30 * f01 + m31 * f11 + m32 * f21;
m02 = m00 * f02 + m01 * f12 + m02 * f22;
m12 = m10 * f02 + m11 * f12 + m12 * f22;
m22 = m20 * f02 + m21 * f12 + m22 * f22;
m32 = m30 * f02 + m31 * f12 + m32 * f22;
m00 = t00;
m10 = t10;
m20 = t20;
m30 = t30;
m01 = t01;
m11 = t11;
m21 = t21;
m31 = t31;
return this;
}
public Matrix4 rotate(Rotation rotation)
{
rotation.apply(this);
return this;
}
public Matrix4 leftMultiply(Matrix4 mat)
{
double n00 = m00 * mat.m00 + m10 * mat.m01 + m20 * mat.m02 + m30 * mat.m03;
double n01 = m01 * mat.m00 + m11 * mat.m01 + m21 * mat.m02 + m31 * mat.m03;
double n02 = m02 * mat.m00 + m12 * mat.m01 + m22 * mat.m02 + m32 * mat.m03;
double n03 = m03 * mat.m00 + m13 * mat.m01 + m23 * mat.m02 + m33 * mat.m03;
double n10 = m00 * mat.m10 + m10 * mat.m11 + m20 * mat.m12 + m30 * mat.m13;
double n11 = m01 * mat.m10 + m11 * mat.m11 + m21 * mat.m12 + m31 * mat.m13;
double n12 = m02 * mat.m10 + m12 * mat.m11 + m22 * mat.m12 + m32 * mat.m13;
double n13 = m03 * mat.m10 + m13 * mat.m11 + m23 * mat.m12 + m33 * mat.m13;
double n20 = m00 * mat.m20 + m10 * mat.m21 + m20 * mat.m22 + m30 * mat.m23;
double n21 = m01 * mat.m20 + m11 * mat.m21 + m21 * mat.m22 + m31 * mat.m23;
double n22 = m02 * mat.m20 + m12 * mat.m21 + m22 * mat.m22 + m32 * mat.m23;
double n23 = m03 * mat.m20 + m13 * mat.m21 + m23 * mat.m22 + m33 * mat.m23;
double n30 = m00 * mat.m30 + m10 * mat.m31 + m20 * mat.m32 + m30 * mat.m33;
double n31 = m01 * mat.m30 + m11 * mat.m31 + m21 * mat.m32 + m31 * mat.m33;
double n32 = m02 * mat.m30 + m12 * mat.m31 + m22 * mat.m32 + m32 * mat.m33;
double n33 = m03 * mat.m30 + m13 * mat.m31 + m23 * mat.m32 + m33 * mat.m33;
m00 = n00;
m01 = n01;
m02 = n02;
m03 = n03;
m10 = n10;
m11 = n11;
m12 = n12;
m13 = n13;
m20 = n20;
m21 = n21;
m22 = n22;
m23 = n23;
m30 = n30;
m31 = n31;
m32 = n32;
m33 = n33;
return this;
}
public Matrix4 multiply(Matrix4 mat)
{
double n00 = m00 * mat.m00 + m01 * mat.m10 + m02 * mat.m20 + m03 * mat.m30;
double n01 = m00 * mat.m01 + m01 * mat.m11 + m02 * mat.m21 + m03 * mat.m31;
double n02 = m00 * mat.m02 + m01 * mat.m12 + m02 * mat.m22 + m03 * mat.m32;
double n03 = m00 * mat.m03 + m01 * mat.m13 + m02 * mat.m23 + m03 * mat.m33;
double n10 = m10 * mat.m00 + m11 * mat.m10 + m12 * mat.m20 + m13 * mat.m30;
double n11 = m10 * mat.m01 + m11 * mat.m11 + m12 * mat.m21 + m13 * mat.m31;
double n12 = m10 * mat.m02 + m11 * mat.m12 + m12 * mat.m22 + m13 * mat.m32;
double n13 = m10 * mat.m03 + m11 * mat.m13 + m12 * mat.m23 + m13 * mat.m33;
double n20 = m20 * mat.m00 + m21 * mat.m10 + m22 * mat.m20 + m23 * mat.m30;
double n21 = m20 * mat.m01 + m21 * mat.m11 + m22 * mat.m21 + m23 * mat.m31;
double n22 = m20 * mat.m02 + m21 * mat.m12 + m22 * mat.m22 + m23 * mat.m32;
double n23 = m20 * mat.m03 + m21 * mat.m13 + m22 * mat.m23 + m23 * mat.m33;
double n30 = m30 * mat.m00 + m31 * mat.m10 + m32 * mat.m20 + m33 * mat.m30;
double n31 = m30 * mat.m01 + m31 * mat.m11 + m32 * mat.m21 + m33 * mat.m31;
double n32 = m30 * mat.m02 + m31 * mat.m12 + m32 * mat.m22 + m33 * mat.m32;
double n33 = m30 * mat.m03 + m31 * mat.m13 + m32 * mat.m23 + m33 * mat.m33;
m00 = n00;
m01 = n01;
m02 = n02;
m03 = n03;
m10 = n10;
m11 = n11;
m12 = n12;
m13 = n13;
m20 = n20;
m21 = n21;
m22 = n22;
m23 = n23;
m30 = n30;
m31 = n31;
m32 = n32;
m33 = n33;
return this;
}
public Matrix4 transpose()
{
double n00 = m00;
double n10 = m01;
double n20 = m02;
double n30 = m03;
double n01 = m10;
double n11 = m11;
double n21 = m12;
double n31 = m13;
double n02 = m20;
double n12 = m21;
double n22 = m22;
double n32 = m23;
double n03 = m30;
double n13 = m31;
double n23 = m32;
double n33 = m33;
m00 = n00;
m01 = n01;
m02 = n02;
m03 = n03;
m10 = n10;
m11 = n11;
m12 = n12;
m13 = n13;
m20 = n20;
m21 = n21;
m22 = n22;
m23 = n23;
m30 = n30;
m31 = n31;
m32 = n32;
m33 = n33;
return this;
}
public Matrix4 copy()
{
return new Matrix4(this);
}
public Matrix4 set(Matrix4 mat)
{
m00 = mat.m00;
m01 = mat.m01;
m02 = mat.m02;
m03 = mat.m03;
m10 = mat.m10;
m11 = mat.m11;
m12 = mat.m12;
m13 = mat.m13;
m20 = mat.m20;
m21 = mat.m21;
m22 = mat.m22;
m23 = mat.m23;
m30 = mat.m30;
m31 = mat.m31;
m32 = mat.m32;
m33 = mat.m33;
return this;
}
@Override
public void apply(Matrix4 mat)
{
mat.multiply(this);
}
private void mult3x3(Vector3 vec)
{
double x = m00 * vec.x + m01 * vec.y + m02 * vec.z;
double y = m10 * vec.x + m11 * vec.y + m12 * vec.z;
double z = m20 * vec.x + m21 * vec.y + m22 * vec.z;
vec.x = x;
vec.y = y;
vec.z = z;
}
@Override
public void apply(Vector3 vec)
{
mult3x3(vec);
vec.add(m03, m13, m23);
}
@Override
public void applyN(Vector3 vec)
{
mult3x3(vec);
vec.normalize();
}
@Override
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "["+new BigDecimal(m00, cont)+","+new BigDecimal(m01, cont)+","+new BigDecimal(m02, cont)+","+new BigDecimal(m03, cont)+"]\n"+
"["+new BigDecimal(m10, cont)+","+new BigDecimal(m11, cont)+","+new BigDecimal(m12, cont)+","+new BigDecimal(m13, cont)+"]\n"+
"["+new BigDecimal(m20, cont)+","+new BigDecimal(m21, cont)+","+new BigDecimal(m22, cont)+","+new BigDecimal(m23, cont)+"]\n"+
"["+new BigDecimal(m30, cont)+","+new BigDecimal(m31, cont)+","+new BigDecimal(m32, cont)+","+new BigDecimal(m33, cont)+"]";
}
public Matrix4 apply(Transformation t)
{
t.apply(this);
return this;
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
glBuf.put(m00).put(m10).put(m20).put(m30)
.put(m01).put(m11).put(m21).put(m31)
.put(m02).put(m12).put(m22).put(m32)
.put(m03).put(m13).put(m23).put(m33);
glBuf.flip();
GL11.glMultMatrix(glBuf);
}
@Override
public Transformation inverse()
{
throw new IrreversibleTransformationException(this);//Don't waste your cpu with matrix inverses
}
}
+156
View File
@@ -0,0 +1,156 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import codechicken.lib.math.MathHelper;
public class Quat
{
public double x;
public double y;
public double z;
public double s;
public static final double SQRT2 = Math.sqrt(2D);
public Quat()
{
s = 1;
x = 0;
y = 0;
z = 0;
}
public Quat(Quat quat)
{
x = quat.x;
y = quat.y;
z = quat.z;
s = quat.s;
}
public Quat(double d, double d1, double d2, double d3)
{
x = d1;
y = d2;
z = d3;
s = d;
}
public Quat set(Quat quat)
{
x = quat.x;
y = quat.y;
z = quat.z;
s = quat.s;
return this;
}
public Quat set(double d, double d1, double d2, double d3)
{
x = d1;
y = d2;
z = d3;
s = d;
return this;
}
public static Quat aroundAxis(double ax, double ay, double az, double angle)
{
return new Quat().setAroundAxis(ax, ay, az, angle);
}
public static Quat aroundAxis(Vector3 axis, double angle)
{
return aroundAxis(axis.x, axis.y, axis.z, angle);
}
public Quat setAroundAxis(double ax, double ay, double az, double angle)
{
angle *= 0.5;
double d4 = MathHelper.sin(angle);
return set(MathHelper.cos(angle), ax * d4, ay * d4, az * d4);
}
public Quat setAroundAxis(Vector3 axis, double angle)
{
return setAroundAxis(axis.x, axis.y, axis.z, angle);
}
public Quat multiply(Quat quat)
{
double d = s * quat.s - x * quat.x - y * quat.y - z * quat.z;
double d1 = s * quat.x + x * quat.s - y * quat.z + z * quat.y;
double d2 = s * quat.y + x * quat.z + y * quat.s - z * quat.x;
double d3 = s * quat.z - x * quat.y + y * quat.x + z * quat.s;
s = d;
x = d1;
y = d2;
z = d3;
return this;
}
public Quat rightMultiply(Quat quat)
{
double d = s * quat.s - x * quat.x - y * quat.y - z * quat.z;
double d1 = s * quat.x + x * quat.s + y * quat.z - z * quat.y;
double d2 = s * quat.y - x * quat.z + y * quat.s + z * quat.x;
double d3 = s * quat.z + x * quat.y - y * quat.x + z * quat.s;
s = d;
x = d1;
y = d2;
z = d3;
return this;
}
public double mag()
{
return Math.sqrt(x * x + y * y + z * z + s * s);
}
public Quat normalize()
{
double d = mag();
if(d != 0)
{
d = 1 / d;
x *= d;
y *= d;
z *= d;
s *= d;
}
return this;
}
public Quat copy()
{
return new Quat(this);
}
public void rotate(Vector3 vec)
{
double d = -x * vec.x - y * vec.y - z * vec.z;
double d1 = s * vec.x + y * vec.z - z * vec.y;
double d2 = s * vec.y - x * vec.z + z * vec.x;
double d3 = s * vec.z + x * vec.y - y * vec.x;
vec.x = d1 * s - d * x - d2 * z + d3 * y;
vec.y = d2 * s - d * y + d1 * z - d3 * x;
vec.z = d3 * s - d * z - d1 * y + d2 * x;
}
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Quat("+new BigDecimal(s, cont)+", "+new BigDecimal(x, cont)+", "+new BigDecimal(y, cont)+", "+new BigDecimal(z, cont)+")";
}
public Rotation rotation()
{
return new Rotation(this);
}
}
+42
View File
@@ -0,0 +1,42 @@
package codechicken.lib.vec;
public class Rectangle4i
{
public int x;
public int y;
public int w;
public int h;
public Rectangle4i()
{
}
public Rectangle4i(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public Rectangle4i offset(int dx, int dy)
{
x+=dx;
y+=dy;
return this;
}
public Rectangle4i with(int px, int py)
{
if(x > px) x = px;
if(y > py) y = py;
if(x + w <= px) w = px-x+1;
if(y + h <= py) h = py-y+1;
return this;
}
public boolean contains(int px, int py)
{
return x <= px && px < x+w && y <= py && py < y+h;
}
}
@@ -0,0 +1,38 @@
package codechicken.lib.vec;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class RedundantTransformation extends Transformation
{
@Override
public void apply(Vector3 vec){}
@Override
public void apply(Matrix4 mat){}
@Override
public void applyN(Vector3 normal){}
@Override
public Transformation at(Vector3 point)
{
return this;
}
@Override
@SideOnly(Side.CLIENT)
public void glApply(){}
@Override
public Transformation inverse()
{
return this;
}
@Override
public String toString()
{
return "Nothing()";
}
}
+238
View File
@@ -0,0 +1,238 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import net.minecraft.entity.player.EntityPlayer;
import org.lwjgl.opengl.GL11;
import codechicken.lib.math.MathHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class Rotation extends Transformation
{
/**
* Clockwise pi/2 about y looking down
*/
public static Transformation[] quarterRotations = new Transformation[]{
new RedundantTransformation(),
new VariableTransformation(new Matrix4(0, 0,-1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d1 = vec.x; double d2 = vec.z;
vec.x = -d2; vec.z = d1;
} @Override public Transformation inverse(){
return quarterRotations[3];
}},
new VariableTransformation(new Matrix4(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
vec.x = -vec.x; vec.z = -vec.z;
} @Override public Transformation inverse(){
return this;
}},
new VariableTransformation(new Matrix4(0, 0, 1, 0, 0, 1, 0, 0,-1, 0, 0, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d1 = vec.x; double d2 = vec.z;
vec.x = d2; vec.z = -d1;
} @Override public Transformation inverse(){
return quarterRotations[1];
}}
};
public static Transformation[] sideRotations = new Transformation[]{
new RedundantTransformation(),
new VariableTransformation(new Matrix4(1, 0, 0, 0, 0,-1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
vec.y = -vec.y; vec.z = -vec.z;
} @Override public Transformation inverse(){
return this;
}},
new VariableTransformation(new Matrix4(1, 0, 0, 0, 0, 0,-1, 0, 0, 1, 0, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d1 = vec.y; double d2 = vec.z;
vec.y = -d2; vec.z = d1;
} @Override public Transformation inverse(){
return sideRotations[3];
}},
new VariableTransformation(new Matrix4(1, 0, 0, 0, 0, 0, 1, 0, 0,-1, 0, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d1 = vec.y; double d2 = vec.z;
vec.y = d2; vec.z = -d1;
} @Override public Transformation inverse(){
return sideRotations[2];
}},
new VariableTransformation(new Matrix4(0, 1, 0, 0,-1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d0 = vec.x; double d1 = vec.y;
vec.x = d1; vec.y = -d0;
} @Override public Transformation inverse(){
return sideRotations[5];
}},
new VariableTransformation(new Matrix4(0,-1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)){
@Override public void apply(Vector3 vec){
double d0 = vec.x; double d1 = vec.y;
vec.x = -d1; vec.y = d0;
} @Override public Transformation inverse(){
return sideRotations[4];
}}
};
public static Vector3[] axes = new Vector3[]{
new Vector3( 0,-1, 0),
new Vector3( 0, 1, 0),
new Vector3( 0, 0,-1),
new Vector3( 0, 0, 1),
new Vector3(-1, 0, 0),
new Vector3( 1, 0, 0)};
public static int[] sideRotMap = new int[]{
3,4,2,5,
3,5,2,4,
1,5,0,4,
1,4,0,5,
1,2,0,3,
1,3,0,2};
public static int[] rotSideMap = new int[]{
-1,-1, 2, 0, 1, 3,
-1,-1, 2, 0, 3, 1,
2, 0,-1,-1, 3, 1,
2, 0,-1,-1, 1, 3,
2, 0, 1, 3,-1,-1,
2, 0, 3, 1,-1,-1};
/**
* Rotate pi/2 * this offset for [side] about y axis before rotating to the side for the rotation indicies to line up
*/
public static int[] sideRotOffsets = new int[]{0, 2, 2, 0, 1, 3};
public static int rotateSide(int s, int r)
{
return sideRotMap[s<<2|r];
}
/**
* Reverse of rotateSide
*/
public static int rotationTo(int s1, int s2)
{
if((s1&6) == (s2&6))
throw new IllegalArgumentException("Faces "+s1+" and "+s2+" are opposites");
return rotSideMap[s1*6+s2];
}
/**
* @param player The placing player, used for obtaining the look vector
* @param side The side of the block being placed on
* @return The rotation for the face == side^1
*/
public static int getSidedRotation(EntityPlayer player, int side)
{
Vector3 look = new Vector3(player.getLook(1));
double max = 0;
int maxr = 0;
for(int r = 0; r < 4; r++)
{
Vector3 axis = Rotation.axes[rotateSide(side^1, r)];
double d = look.scalarProject(axis);
if(max > d)
{
max = d;
maxr = r;
}
}
return maxr;
}
/**
* @return The rotation quat for side 0 and rotation 0 to side s with rotation r
*/
public static Transformation sideOrientation(int s, int r)
{
return quarterRotations[(r+sideRotOffsets[s])%4].with(sideRotations[s]);
}
public double angle;
public Vector3 axis;
private Quat quat;
public Rotation(double angle, Vector3 axis)
{
this.angle = angle;
this.axis = axis;
}
public Rotation(double angle, double x, double y, double z)
{
this(angle, new Vector3(x, y, z));
}
public Rotation(Quat quat)
{
this.quat = quat;
angle = Math.acos(quat.s)*2;
if(angle == 0)
{
axis = new Vector3(0, 1, 0);
}
else
{
double sa = Math.sin(angle*0.5);
axis = new Vector3(quat.x/sa, quat.y/sa, quat.z/sa);
}
}
@Override
public void apply(Vector3 vec)
{
if(quat == null)
quat = Quat.aroundAxis(axis, angle);
vec.rotate(quat);
}
@Override
public void applyN(Vector3 normal)
{
apply(normal);
}
@Override
public void apply(Matrix4 mat)
{
mat.rotate(angle, axis);
}
public Quat toQuat()
{
if(quat == null)
quat = Quat.aroundAxis(axis, angle);
return quat;
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
GL11.glRotatef((float)(angle*MathHelper.todeg), (float)axis.x, (float)axis.y, (float)axis.z);
}
@Override
public Transformation inverse()
{
return new Rotation(-angle, axis);
}
@Override
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Rotation("+new BigDecimal(angle, cont)+", "+new BigDecimal(axis.x, cont)+", "+
new BigDecimal(axis.y, cont)+", "+new BigDecimal(axis.z, cont)+")";
}
}
+67
View File
@@ -0,0 +1,67 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class Scale extends Transformation
{
private Vector3 factor;
public Scale(Vector3 factor)
{
this.factor = factor;
}
public Scale(double factor)
{
this(new Vector3(factor, factor, factor));
}
public Scale(double x, double y, double z)
{
this(new Vector3(x, y, z));
}
@Override
public void apply(Vector3 vec)
{
vec.multiply(factor);
}
@Override
public void applyN(Vector3 normal)
{
}
@Override
public void apply(Matrix4 mat)
{
mat.scale(factor);
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
GL11.glScaled(factor.x, factor.y, factor.z);
}
@Override
public Transformation inverse()
{
return new Scale(1/factor.x, 1/factor.y, 1/factor.z);
}
@Override
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Scale("+new BigDecimal(factor.x, cont)+", "+new BigDecimal(factor.y, cont)+", "+new BigDecimal(factor.z, cont)+")";
}
}
+27
View File
@@ -0,0 +1,27 @@
package codechicken.lib.vec;
public class SwapYZ extends VariableTransformation
{
public SwapYZ()
{
super(new Matrix4(
1, 0, 0, 0,
0, 0, 1, 0,
0, 1, 0, 0,
0, 0, 0, 1));
}
@Override
public void apply(Vector3 vec)
{
double vz = vec.z;
vec.z = vec.y;
vec.y = vz;
}
@Override
public Transformation inverse()
{
return this;
}
}
+51
View File
@@ -0,0 +1,51 @@
package codechicken.lib.vec;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* Interface for any 3D vector transformation
*/
public abstract class Transformation
{
/**
* Applies this transformation to a position vector
* @param vec The vector to transform
*/
public abstract void apply(Vector3 vec);
/**
* Applies this transformation to a normal (doesn't translate)
* @param normal The normal to transform
*/
public abstract void applyN(Vector3 normal);
/**
* Applies this transformation to a matrix as a multiplication on the right hand side.
* @param mat The matrix to combine this transformation with
*/
public abstract void apply(Matrix4 mat);
/**
* @param point The point in OBJECT space to apply this transformation around
* @return Wraps this transformation in a translation to point and then back from point
*/
public Transformation at(Vector3 point)
{
return new TranslatedTransformation(this, point);
}
/**
* Creates a transformation list composed of this transformation followed by t
* If this is a TransformationList, the transformation will be appended and this returned
*/
public TransformationList with(Transformation t)
{
return new TransformationList(this, t);
}
@SideOnly(Side.CLIENT)
public abstract void glApply();
public abstract Transformation inverse();
}
+101
View File
@@ -0,0 +1,101 @@
package codechicken.lib.vec;
import java.util.ArrayList;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class TransformationList extends Transformation
{
private ArrayList<Transformation> transformations = new ArrayList<Transformation>();
private Matrix4 mat;
public TransformationList(Transformation... transforms)
{
for(Transformation t : transforms)
with(t);
}
public Matrix4 compile()
{
if(mat == null)
{
mat = new Matrix4();
for(int i = transformations.size()-1; i >= 0; i--)
transformations.get(i).apply(mat);
}
return mat;
}
/**
* Returns a global space matrix as opposed to an object space matrix (reverse application order)
* @return
*/
public Matrix4 reverseCompile()
{
Matrix4 mat = new Matrix4();
for(Transformation t : transformations)
t.apply(mat);
return mat;
}
@Override
public void apply(Vector3 vec)
{
if(mat != null)
mat.apply(vec);
else
for(Transformation t : transformations)
t.apply(vec);
}
@Override
public void applyN(Vector3 normal)
{
if(mat != null)
mat.applyN(normal);
else
for(Transformation t : transformations)
t.applyN(normal);
}
@Override
public void apply(Matrix4 mat)
{
mat.multiply(compile());
}
@Override
public TransformationList with(Transformation t)
{
mat = null;//matrix invalid
transformations.add(t);
return this;
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
for(int i = transformations.size()-1; i >= 0; i--)
transformations.get(i).glApply();
}
@Override
public Transformation inverse()
{
TransformationList rev = new TransformationList();
for(int i = transformations.size()-1; i >= 0; i--)
rev.with(transformations.get(i).inverse());
return rev;
}
@Override
public String toString()
{
String s = "";
for(Transformation t : transformations)
s+="\n"+t.toString();
return s.trim();
}
}
@@ -0,0 +1,60 @@
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);
}
}
+68
View File
@@ -0,0 +1,68 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class Translation extends Transformation
{
private Vector3 vec;
public Translation(Vector3 vec)
{
this.vec = vec;
}
public Translation(double x, double y, double z)
{
this(new Vector3(x, y, z));
}
@Override
public void apply(Vector3 vec)
{
vec.add(this.vec);
}
@Override
public void applyN(Vector3 normal)
{
}
@Override
public void apply(Matrix4 mat)
{
mat.translate(vec);
}
@Override
public Transformation at(Vector3 point)
{
return this;
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
GL11.glTranslated(vec.x, vec.y, vec.z);
}
@Override
public Transformation inverse()
{
return new Translation(-vec.x, -vec.y, -vec.z);
}
@Override
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Translation("+new BigDecimal(vec.x, cont)+", "+new BigDecimal(vec.y, cont)+", "+new BigDecimal(vec.z, cont)+")";
}
}
@@ -0,0 +1,33 @@
package codechicken.lib.vec;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public abstract class VariableTransformation extends Transformation
{
public Matrix4 mat;
public VariableTransformation(Matrix4 mat)
{
this.mat = mat;
}
@Override
public void applyN(Vector3 normal)
{
apply(normal);
}
@Override
public void apply(Matrix4 mat)
{
mat.multiply(this.mat);
}
@Override
@SideOnly(Side.CLIENT)
public void glApply()
{
mat.glApply();
}
}
+423
View File
@@ -0,0 +1,423 @@
package codechicken.lib.vec;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import codechicken.lib.math.MathHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
public class Vector3
{
public static Vector3 zero = new Vector3();
public static Vector3 one = new Vector3(1, 1, 1);
public static Vector3 center = new Vector3(0.5, 0.5, 0.5);
public double x;
public double y;
public double z;
public Vector3()
{
}
public Vector3(double d, double d1, double d2)
{
x = d;
y = d1;
z = d2;
}
public Vector3(Vector3 vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
}
public Vector3(Vec3 vec)
{
x = vec.xCoord;
y = vec.yCoord;
z = vec.zCoord;
}
public Vector3 copy()
{
return new Vector3(this);
}
public static Vector3 fromEntity(Entity e)
{
return new Vector3(e.posX, e.posY, e.posZ);
}
public static Vector3 fromEntityCenter(Entity e)
{
return new Vector3(e.posX, e.posY - e.yOffset + e.height/2, e.posZ);
}
public static Vector3 fromTileEntity(TileEntity e)
{
return new Vector3(e.xCoord, e.yCoord, e.zCoord);
}
public static Vector3 fromTileEntityCenter(TileEntity e)
{
return new Vector3(e.xCoord + 0.5, e.yCoord + 0.5, e.zCoord + 0.5);
}
@Deprecated
public static Vector3 fromVec3D(Vec3 vec)
{
return new Vector3(vec);
}
public Vector3 set(double d, double d1, double d2)
{
x = d;
y = d1;
z = d2;
return this;
}
public Vector3 set(Vector3 vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
return this;
}
public double dotProduct(Vector3 vec)
{
double d = vec.x * x + vec.y * y + vec.z * z;
if(d > 1 && d < 1.00001)
d = 1;
else if(d < -1 && d > -1.00001)
d = -1;
return d;
}
public double dotProduct(double d, double d1, double d2)
{
return d * x + d1 * y + d2 * z;
}
public Vector3 crossProduct(Vector3 vec)
{
double d = y * vec.z - z * vec.y;
double d1 = z * vec.x - x * vec.z;
double d2 = x * vec.y - y * vec.x;
x = d;
y = d1;
z = d2;
return this;
}
public Vector3 add(double d, double d1, double d2)
{
x += d;
y += d1;
z += d2;
return this;
}
public Vector3 add(Vector3 vec)
{
x += vec.x;
y += vec.y;
z += vec.z;
return this;
}
public Vector3 add(double d)
{
return add(d, d, d);
}
public Vector3 sub(Vector3 vec)
{
return subtract(vec);
}
public Vector3 subtract(Vector3 vec)
{
x -= vec.x;
y -= vec.y;
z -= vec.z;
return this;
}
public Vector3 negate(Vector3 vec)
{
x = -x;
y = -y;
z = -z;
return this;
}
public Vector3 multiply(double d)
{
x *= d;
y *= d;
z *= d;
return this;
}
public Vector3 multiply(Vector3 f)
{
x *= f.x;
y *= f.y;
z *= f.z;
return this;
}
public Vector3 multiply(double fx, double fy, double fz)
{
x *= fx;
y *= fy;
z *= fz;
return this;
}
public double mag()
{
return Math.sqrt(x * x + y * y + z * z);
}
public double magSquared()
{
return x * x + y * y + z * z;
}
public Vector3 normalize()
{
double d = mag();
if(d != 0)
{
multiply(1 / d);
}
return this;
}
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Vector3("+new BigDecimal(x, cont)+", "+new BigDecimal(y, cont)+", "+new BigDecimal(z, cont)+")";
}
public Vector3 perpendicular()
{
if(z == 0)
return zCrossProduct();
return xCrossProduct();
}
public Vector3 xCrossProduct()
{
double d = z;
double d1 = -y;
x = 0;
y = d;
z = d1;
return this;
}
public Vector3 zCrossProduct()
{
double d = y;
double d1 = -x;
x = d;
y = d1;
z = 0;
return this;
}
public Vector3 yCrossProduct()
{
double d = -z;
double d1 = x;
x = d;
y = 0;
z = d1;
return this;
}
public Vector3 rotate(double angle, Vector3 axis){
Quat.aroundAxis(axis.copy().normalize(), angle).rotate(this);
return this;
}
public Vector3 rotate(Quat rotator)
{
rotator.rotate(this);
return this;
}
public Vec3 toVec3D()
{
return Vec3.createVectorHelper(x, y, z);
}
public double angle(Vector3 vec)
{
return Math.acos(copy().normalize().dotProduct(vec.copy().normalize()));
}
public boolean isZero()
{
return x == 0 && y == 0 && z == 0;
}
public boolean isAxial()
{
return x == 0 ? (y == 0 || z == 0) : (y == 0 && z == 0);
}
@SideOnly(Side.CLIENT)
public Vector3f vector3f()
{
return new Vector3f((float)x, (float)y, (float)z);
}
@SideOnly(Side.CLIENT)
public Vector4f vector4f()
{
return new Vector4f((float)x, (float)y, (float)z, 1);
}
public Vector3 YZintercept(Vector3 end, double px)
{
double dx = end.x - x;
double dy = end.y - y;
double dz = end.z - z;
if (dx == 0)
return null;
double d = (px - x) / dx;
if(MathHelper.between(-1E-5, d, 1E-5))
return this;
if(!MathHelper.between(0, d, 1))
return null;
x = px;
y += d*dy;
z += d*dz;
return this;
}
public Vector3 XZintercept(Vector3 end, double py)
{
double dx = end.x - x;
double dy = end.y - y;
double dz = end.z - z;
if (dy == 0)
return null;
double d = (py - y) / dy;
if(MathHelper.between(-1E-5, d, 1E-5))
return this;
if(!MathHelper.between(0, d, 1))
return null;
x += d*dx;
y = py;
z += d*dz;
return this;
}
public Vector3 XYintercept(Vector3 end, double pz)
{
double dx = end.x - x;
double dy = end.y - y;
double dz = end.z - z;
if (dz == 0)
return null;
double d = (pz - z) / dz;
if(MathHelper.between(-1E-5, d, 1E-5))
return this;
if(!MathHelper.between(0, d, 1))
return null;
x += d*dx;
y += d*dy;
z = pz;
return this;
}
public Vector3 negate()
{
x = -x;
y = -y;
z = -z;
return this;
}
public Translation translation()
{
return new Translation(this);
}
public double scalarProject(Vector3 b)
{
double l = b.mag();
return l == 0 ? 0 : dotProduct(b)/l;
}
public Vector3 project(Vector3 b)
{
double l = b.magSquared();
if(l == 0)
{
set(0, 0, 0);
return this;
}
double m = dotProduct(b)/l;
set(b).multiply(m);
return this;
}
@Override
public boolean equals(Object o)
{
if(!(o instanceof Vector3))
return false;
Vector3 v = (Vector3)o;
return x == v.x && y == v.y && z == v.z;
}
/**
* Equals method with tolerance
* @return true if this is equal to v within +-1E-5
*/
public boolean equalsT(Vector3 v)
{
return MathHelper.between(x-1E-5, v.x, x+1E-5) &&
MathHelper.between(y-1E-5, v.y, y+1E-5) &&
MathHelper.between(z-1E-5, v.z, z+1E-5);
}
public Vector3 apply(Transformation t)
{
t.apply(this);
return this;
}
}