Search in sources :

Example 1 with VoxelCuboid

use of mekanism.common.lib.math.voxel.VoxelCuboid in project Mekanism by mekanism.

the class MultiblockData method readUpdateTag.

public void readUpdateTag(CompoundNBT tag) {
    NBTUtils.setIntIfPresent(tag, NBTConstants.VOLUME, this::setVolume);
    NBTUtils.setBlockPosIfPresent(tag, NBTConstants.RENDER_LOCATION, value -> renderLocation = value);
    bounds = new VoxelCuboid(NBTUtil.readBlockPos(tag.getCompound(NBTConstants.MIN)), NBTUtil.readBlockPos(tag.getCompound(NBTConstants.MAX)));
    NBTUtils.setUUIDIfPresentElse(tag, NBTConstants.INVENTORY_ID, value -> inventoryID = value, () -> inventoryID = null);
}
Also used : VoxelCuboid(mekanism.common.lib.math.voxel.VoxelCuboid)

Example 2 with VoxelCuboid

use of mekanism.common.lib.math.voxel.VoxelCuboid in project Mekanism by mekanism.

the class MultiblockData method setShape.

public boolean setShape(IShape shape) {
    if (shape instanceof VoxelCuboid) {
        VoxelCuboid cuboid = (VoxelCuboid) shape;
        bounds = cuboid;
        renderLocation = cuboid.getMinPos().relative(Direction.UP);
        setVolume(bounds.length() * bounds.width() * bounds.height());
        return true;
    }
    return false;
}
Also used : VoxelCuboid(mekanism.common.lib.math.voxel.VoxelCuboid)

Example 3 with VoxelCuboid

use of mekanism.common.lib.math.voxel.VoxelCuboid in project Mekanism by mekanism.

the class StructureHelper method fetchCuboid.

/**
 * Fetch a cuboid with all 6 sides present. Quicker than using the below algorithm with all sides.
 *
 * @param structure structure to check
 * @param minBounds minimum size of the cuboid
 * @param maxBounds maximum size of the cuboid
 *
 * @return found cuboid, or null if it doesn't exist
 */
public static VoxelCuboid fetchCuboid(Structure structure, VoxelCuboid minBounds, VoxelCuboid maxBounds) {
    VoxelCuboid prev = null;
    for (Axis axis : Axis.AXES) {
        NavigableMap<Integer, VoxelPlane> majorAxisMap = structure.getMajorAxisMap(axis);
        Map.Entry<Integer, VoxelPlane> firstMajor = majorAxisMap.firstEntry(), lastMajor = majorAxisMap.lastEntry();
        if (firstMajor == null || !firstMajor.getValue().equals(lastMajor.getValue()) || !firstMajor.getValue().isFull()) {
            // or if the plane is missing pieces then fail
            return null;
        }
        VoxelCuboid cuboid = VoxelCuboid.from(firstMajor.getValue(), lastMajor.getValue(), firstMajor.getKey(), lastMajor.getKey());
        // if this is the first axial cuboid check, make sure we have the correct bounds
        if (prev == null && (!cuboid.greaterOrEqual(minBounds) || !maxBounds.greaterOrEqual(cuboid))) {
            return null;
        }
        // if this isn't the first axial cuboid check, make sure the cuboids match
        if (prev != null && !prev.equals(cuboid)) {
            return null;
        }
        // check to make sure that we don't have any framed minor planes sticking out of our cuboid
        NavigableMap<Integer, VoxelPlane> minorAxisMap = structure.getMinorAxisMap(axis);
        if (!minorAxisMap.isEmpty()) {
            if (hasOutOfBoundsNegativeMinor(minorAxisMap, firstMajor.getKey()) || hasOutOfBoundsPositiveMinor(minorAxisMap, lastMajor.getKey())) {
                return null;
            }
        }
        prev = cuboid;
    }
    return prev;
}
Also used : VoxelPlane(mekanism.common.lib.math.voxel.VoxelPlane) VoxelCuboid(mekanism.common.lib.math.voxel.VoxelCuboid) Map(java.util.Map) NavigableMap(java.util.NavigableMap) Axis(mekanism.common.lib.multiblock.Structure.Axis)

Example 4 with VoxelCuboid

use of mekanism.common.lib.math.voxel.VoxelCuboid in project Mekanism by mekanism.

the class StructureHelper method fetchCuboid.

/**
 * Fetch a cuboid with a defined amount of sides. At least two sides should be provided; otherwise it's impossible to discern the overall dimensions about the
 * cuboid.
 *
 * @param structure structure to check
 * @param minBounds minimum size of the cuboid
 * @param maxBounds maximum size of the cuboid
 * @param sides     sides to check
 * @param tolerance how many missing blocks are tolerated in the completed structure (will double count edges & triple count corners)
 *
 * @return found cuboid, or null if it doesn't exist
 */
public static VoxelCuboid fetchCuboid(Structure structure, VoxelCuboid minBounds, VoxelCuboid maxBounds, Set<CuboidSide> sides, int tolerance) {
    // make sure we have enough sides to create cuboidal dimensions
    if (sides.size() < 2) {
        return null;
    }
    int missing = 0;
    CuboidBuilder builder = new CuboidBuilder();
    for (CuboidSide side : sides) {
        Axis axis = side.getAxis(), horizontal = axis.horizontal(), vertical = axis.vertical();
        NavigableMap<Integer, VoxelPlane> majorAxisMap = structure.getMajorAxisMap(axis);
        Map.Entry<Integer, VoxelPlane> majorEntry = side.getFace().isPositive() ? majorAxisMap.lastEntry() : majorAxisMap.firstEntry();
        // fail fast if the plane doesn't exist
        if (majorEntry == null) {
            return null;
        }
        VoxelPlane plane = majorEntry.getValue();
        // handle missing blocks based on tolerance value
        missing += plane.getMissing();
        if (missing > tolerance) {
            return null;
        }
        int majorKey = majorEntry.getKey();
        // set bounds from dimension of plane's axis
        builder.set(side, majorKey);
        // update cuboidal dimensions from each corner of the plane
        if (!builder.trySet(CuboidSide.get(Face.NEGATIVE, horizontal), plane.getMinCol()) || !builder.trySet(CuboidSide.get(Face.POSITIVE, horizontal), plane.getMaxCol()) || !builder.trySet(CuboidSide.get(Face.NEGATIVE, vertical), plane.getMinRow()) || !builder.trySet(CuboidSide.get(Face.POSITIVE, vertical), plane.getMaxRow())) {
            return null;
        }
        // check to make sure that we don't have any framed minor planes sticking out of our plane
        NavigableMap<Integer, VoxelPlane> minorAxisMap = structure.getMinorAxisMap(axis);
        if (!minorAxisMap.isEmpty()) {
            if (side.getFace().isPositive()) {
                if (hasOutOfBoundsPositiveMinor(minorAxisMap, majorKey)) {
                    return null;
                }
            } else if (hasOutOfBoundsNegativeMinor(minorAxisMap, majorKey)) {
                return null;
            }
        }
    }
    VoxelCuboid ret = builder.build();
    // make sure the cuboid has the correct bounds
    if (!ret.greaterOrEqual(minBounds) || !maxBounds.greaterOrEqual(ret)) {
        return null;
    }
    return ret;
}
Also used : CuboidBuilder(mekanism.common.lib.math.voxel.VoxelCuboid.CuboidBuilder) VoxelPlane(mekanism.common.lib.math.voxel.VoxelPlane) CuboidSide(mekanism.common.lib.math.voxel.VoxelCuboid.CuboidSide) VoxelCuboid(mekanism.common.lib.math.voxel.VoxelCuboid) Map(java.util.Map) NavigableMap(java.util.NavigableMap) Axis(mekanism.common.lib.multiblock.Structure.Axis)

Aggregations

VoxelCuboid (mekanism.common.lib.math.voxel.VoxelCuboid)4 Map (java.util.Map)2 NavigableMap (java.util.NavigableMap)2 VoxelPlane (mekanism.common.lib.math.voxel.VoxelPlane)2 Axis (mekanism.common.lib.multiblock.Structure.Axis)2 CuboidBuilder (mekanism.common.lib.math.voxel.VoxelCuboid.CuboidBuilder)1 CuboidSide (mekanism.common.lib.math.voxel.VoxelCuboid.CuboidSide)1