001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.util;
020
021import com.plotsquared.core.location.Location;
022import com.sk89q.worldedit.math.BlockVector2;
023import com.sk89q.worldedit.math.BlockVector3;
024import com.sk89q.worldedit.regions.CuboidRegion;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027import java.awt.geom.Rectangle2D;
028import java.util.Collection;
029import java.util.Iterator;
030
031public class RegionUtil {
032
033    public static @NonNull Location[] getCorners(
034            final @NonNull String world,
035            final @NonNull CuboidRegion region
036    ) {
037        final BlockVector3 min = region.getMinimumPoint();
038        final BlockVector3 max = region.getMaximumPoint();
039        return new Location[]{Location.at(world, min), Location.at(world, max)};
040    }
041
042    public static @NonNull Location[] getCorners(String world, Collection<CuboidRegion> regions) {
043        CuboidRegion aabb = getAxisAlignedBoundingBox(regions);
044        return getCorners(world, aabb);
045    }
046
047    /**
048     * Create a minimum {@link CuboidRegion} containing all given regions.
049     *
050     * @param regions The regions the bounding box should contain.
051     * @return a CuboidRegion that contains all given regions.
052     */
053    public static @NonNull CuboidRegion getAxisAlignedBoundingBox(Iterable<CuboidRegion> regions) {
054        Iterator<CuboidRegion> iterator = regions.iterator();
055        if (!iterator.hasNext()) {
056            throw new IllegalArgumentException("No regions given");
057        }
058        CuboidRegion next = iterator.next();
059        BlockVector3 min = next.getMinimumPoint();
060        BlockVector3 max = next.getMaximumPoint();
061
062        while (iterator.hasNext()) {
063            next = iterator.next();
064            // as max >= min, this is enough to check
065            min = min.getMinimum(next.getMinimumPoint());
066            max = max.getMaximum(next.getMaximumPoint());
067        }
068        return new CuboidRegion(min, max);
069    }
070
071    @Deprecated(forRemoval = true, since = "6.6.0")
072    public static CuboidRegion createRegion(int pos1x, int pos2x, int pos1z, int pos2z) {
073        return createRegion(pos1x, pos2x, 0, 255, pos1z, pos2z);
074    }
075
076    public static CuboidRegion createRegion(
077            int pos1x, int pos2x, int pos1y, int pos2y, int pos1z,
078            int pos2z
079    ) {
080        BlockVector3 pos1 = BlockVector3.at(pos1x, pos1y, pos1z);
081        BlockVector3 pos2 = BlockVector3.at(pos2x, pos2y, pos2z);
082        return new CuboidRegion(pos1, pos2);
083    }
084
085    public static boolean contains(CuboidRegion region, int x, int z) {
086        BlockVector3 min = region.getMinimumPoint();
087        BlockVector3 max = region.getMaximumPoint();
088        return x >= min.getX() && x <= max.getX() && z >= min.getZ() && z <= max.getZ();
089    }
090
091    public static boolean contains(CuboidRegion region, int x, int y, int z) {
092        BlockVector3 min = region.getMinimumPoint();
093        BlockVector3 max = region.getMaximumPoint();
094        return x >= min.getX() && x <= max.getX() && z >= min.getZ() && z <= max.getZ() && y >= min
095                .getY() && y <= max.getY();
096    }
097
098    /**
099     * @deprecated Unused internally. Scheduled for removal in next major release.
100     */
101    @Deprecated(forRemoval = true, since = "6.11.1")
102    public static @NonNull Rectangle2D toRectangle(final @NonNull CuboidRegion region) {
103        final BlockVector2 min = region.getMinimumPoint().toBlockVector2();
104        final BlockVector2 max = region.getMaximumPoint().toBlockVector2();
105        return new Rectangle2D.Double(min.getX(), min.getZ(), max.getX(), max.getZ());
106    }
107
108    // Because WorldEdit (not FastAsyncWorldEdit) lack this for CuboidRegion
109    public static boolean intersects(CuboidRegion region, CuboidRegion other) {
110        BlockVector3 regionMin = region.getMinimumPoint();
111        BlockVector3 regionMax = region.getMaximumPoint();
112
113        BlockVector3 otherMin = other.getMinimumPoint();
114        BlockVector3 otherMax = other.getMaximumPoint();
115
116        return otherMin.getX() <= regionMax.getX() && otherMax.getX() >= regionMin.getX()
117                && otherMin.getZ() <= regionMax.getZ() && otherMax.getZ() >= regionMin.getZ();
118    }
119
120}