TechReborn/src/main/java/techreborn/lib/vecmath/Vecs3dCube.java

187 lines
3.1 KiB
Java
Raw Normal View History

2015-04-20 22:28:54 +02:00
package techreborn.lib.vecmath;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
2015-07-02 20:51:24 +02:00
import java.util.List;
2015-04-20 22:28:54 +02:00
public class Vecs3dCube {
2015-04-24 15:20:09 +02:00
private Vecs3d min, max;
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube(double minX, double minY, double minZ, double maxX,
double maxY, double maxZ)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
this(minX, minY, minZ, maxX, maxY, maxZ, (World) null);
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube(double minX, double minY, double minZ, double maxX,
double maxY, double maxZ, World world)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
this(new Vecs3d(minX, minY, minZ, world), new Vecs3d(maxX, maxY, maxZ,
world));
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube(Vecs3d a, Vecs3d b)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
World w = a.getWorld();
if (w == null)
w = b.getWorld();
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
min = a;
max = b;
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
fix();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube(AxisAlignedBB aabb)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
this(aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ);
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3d getMin()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return min;
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3d getMax()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return max;
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3d getCenter()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return new Vecs3d((getMinX() + getMaxX()) / 2D,
(getMinY() + getMaxY()) / 2D, (getMinZ() + getMaxZ()) / 2D,
getMin().getWorld());
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public double getMinX()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return min.getX();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public double getMinY()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return min.getY();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public double getMinZ()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return min.getZ();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public double getMaxX()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return max.getX();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public double getMaxY()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return max.getY();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public double getMaxZ()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return max.getZ();
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public AxisAlignedBB toAABB()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return AxisAlignedBB.getBoundingBox(getMinX(), getMinY(), getMinZ(),
getMaxX(), getMaxY(), getMaxZ());
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
@Override
public Vecs3dCube clone()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return new Vecs3dCube(min.clone(), max.clone());
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube expand(double size)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
min.sub(size, size, size);
max.add(size, size, size);
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return this;
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube fix()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
Vecs3d a = min.clone();
Vecs3d b = max.clone();
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
double minX = Math.min(a.getX(), b.getX());
double minY = Math.min(a.getY(), b.getY());
double minZ = Math.min(a.getZ(), b.getZ());
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
double maxX = Math.max(a.getX(), b.getX());
double maxY = Math.max(a.getY(), b.getY());
double maxZ = Math.max(a.getZ(), b.getZ());
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
min = new Vecs3d(minX, minY, minZ, a.w);
max = new Vecs3d(maxX, maxY, maxZ, b.w);
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return this;
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public Vecs3dCube add(double x, double y, double z)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
min.add(x, y, z);
max.add(x, y, z);
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return this;
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
public static final Vecs3dCube merge(List<Vecs3dCube> cubes)
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
double minx = Double.MAX_VALUE;
double miny = Double.MAX_VALUE;
double minz = Double.MAX_VALUE;
double maxx = Double.MIN_VALUE;
double maxy = Double.MIN_VALUE;
double maxz = Double.MIN_VALUE;
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
for (Vecs3dCube c : cubes)
{
minx = Math.min(minx, c.getMinX());
miny = Math.min(miny, c.getMinY());
minz = Math.min(minz, c.getMinZ());
maxx = Math.max(maxx, c.getMaxX());
maxy = Math.max(maxy, c.getMaxY());
maxz = Math.max(maxz, c.getMaxZ());
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
if (cubes.size() == 0)
return new Vecs3dCube(0, 0, 0, 0, 0, 0);
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return new Vecs3dCube(minx, miny, minz, maxx, maxy, maxz);
}
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
@Override
public int hashCode()
{
2015-04-20 22:28:54 +02:00
2015-04-24 15:20:09 +02:00
return min.hashCode() << 8 + max.hashCode();
}
2015-04-20 22:28:54 +02:00
}