java代码实现空间切割


Posted in Java/Android onJanuary 18, 2022

问题

给定一个大的立方体和一批小的立方体,对于每个立方体,都知道如图的信息(知道x,y,z坐标和长、宽、高),且任意小立方体都被包含在大立方体内、各立方体之间不重叠。问如何将小立方体从大立方体中切去,留下其他空间?

java代码实现空间切割

代码

package com.dam.algorithm.algorithmPackage.freeSpaceTubePackingAlgorithm.util;

import com.dam.algorithm.common.entity.FreeSpace;
import com.dam.algorithm.common.entity.spiltSpace.Point;
import com.dam.algorithm.common.util.DeepCloneUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 给定一批已用的剩余空间和一个大空间,返回最终的剩余空间
 */
public class GetUsedSpaceUtil {

    private double offSet = 0.01;

    public static void main(String[] args) throws Exception {
        FreeSpace bigFreeSpace = new FreeSpace(0, 0, 0, 100, 100, 100);
        List<FreeSpace> freeSpaceList = new ArrayList<>();
        List<FreeSpace> usedSpaceList = new ArrayList<>();
        usedSpaceList.add(new FreeSpace(0, 50, 50, 30, 30, 30));
        usedSpaceList.add(new FreeSpace(0, 90, 90, 10, 5, 5));

        new GetUsedSpaceUtil().getFreeSpaceUtil(bigFreeSpace, freeSpaceList, usedSpaceList);
        System.out.println(freeSpaceList);
    }

    public void getFreeSpaceUtil(FreeSpace bigFreeSpace, List<FreeSpace> freeSpaceList, List<FreeSpace> usedSpaceList) throws Exception {

        //检验数据是否正确
        boolean flag = this.inspectionData(bigFreeSpace, usedSpaceList);
        if (flag == false) {
            throw new Exception("输入数据有误");
        }

        //处理第一个已用空间
        FreeSpace firstUsedSpace = usedSpaceList.remove(0);
        //获得一批剩余空间,用这些剩余空间来处理后面的usedSpaceList
        List<FreeSpace> freeSpaces = this.eliminateSpace(bigFreeSpace, firstUsedSpace);

        //处理剩下的空间
        while (usedSpaceList.size() > 0) {
            FreeSpace firstUsedSpace1 = usedSpaceList.remove(0);
            List<FreeSpace> removeFreeSpaceList = new ArrayList<>();
            List<FreeSpace> addFreeSpaceList = new ArrayList<>();
            System.out.println("freeSpaces:" + freeSpaces);
            for (FreeSpace freeSpace : freeSpaces) {
                //判断有没有交集
                System.out.println("判断有无交集:");
                System.out.println("空间1" + freeSpace.toString());
                System.out.println("空间2" + firstUsedSpace1.toString());
                int state = this.judgeSpaceRelationship(freeSpace, firstUsedSpace1);
                System.out.println("有无交集state:" + state);
                if (state == 0) {
                    continue;
                } else if (state == 1) {
                    //有交集就要移除
                    removeFreeSpaceList.add(freeSpace);
                    List<FreeSpace>[] spaceArray = this.splitSpace(firstUsedSpace1, freeSpace);
                    //已用空间的分解空间
                    List<FreeSpace> usedList = spaceArray[0];
                    //剩余空间的分解空间
                    List<FreeSpace> freeList = spaceArray[1];
                    addFreeSpaceList.addAll(freeList);
                    //将已用空间的分解空间添加到分解空间列表
                    usedSpaceList.addAll(usedList);
                } else if (state == 2) {
                    removeFreeSpaceList.add(freeSpace);
                    //剩余空间全包含已用空间
                    List<FreeSpace> freeSpaceList1 = this.eliminateSpace(freeSpace, firstUsedSpace1);
                    freeSpaces.addAll(freeSpaceList1);
                    //因为剩余空间包含已用空间,可以直接break
                    break;
                } else if (state == 3) {
                    removeFreeSpaceList.add(freeSpace);
                    //已用空间全包含剩余空间
                    List<FreeSpace> freeSpaceList1 = this.eliminateSpace(firstUsedSpace1, freeSpace);
                    usedSpaceList.addAll(freeSpaceList1);
                }
                System.out.println("来到这里");
            }
            //将分割得到的剩余空间添加回来,以便给下一个已用空间使用
            freeSpaces.addAll(addFreeSpaceList);
            //减去已经使用的剩余空间
            freeSpaces.removeAll(removeFreeSpaceList);
        }

        //存储最终的剩余空间
        freeSpaceList.addAll((List<FreeSpace>) DeepCloneUtil.deepClone(freeSpaces));

        System.out.println("最终剩余空间");
        System.out.println("剩余空间数量" + freeSpaceList.size());
        System.out.println(freeSpaceList);
    }

    /**
     * 检验数据是否正确
     * 1、所有已用空间不能互相重合
     * 2、所有已用空间都在最大空间内
     *
     * @param bigFreeSpace
     * @param usedSpaceList
     * @return false:数据不合理
     */
    private boolean inspectionData(FreeSpace bigFreeSpace, List<FreeSpace> usedSpaceList) {

        for (int i = 0; i < usedSpaceList.size(); i++) {
            //判断是否所有usedSpace都被包含在bigFreeSpace中
            if (this.judgeSpaceRelationship(bigFreeSpace, usedSpaceList.get(i)) != 2) {
                System.out.println("不是所有usedSpace都被包含在bigFreeSpace中");
                return false;
            }

            //判断usedSpace之间是否相互重合
            for (int j = 0; j < usedSpaceList.size(); j++) {
                System.out.println("相交类型:"+this.judgeSpaceRelationship(usedSpaceList.get(i), usedSpaceList.get(j)));
                if (i != j && (this.judgeSpaceRelationship(usedSpaceList.get(i), usedSpaceList.get(j)) != 0)) {
                    System.out.println("usedSpace之间相互重合");
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 剔除空间
     * 给定一个大空间和一个小空间,将小空间从大空间中剔除,最多返回六个剩余空间
     *
     * @param bigSpace
     * @param smallSpace
     */
    public List<FreeSpace> eliminateSpace(FreeSpace bigSpace, FreeSpace smallSpace) {
        List<FreeSpace> freeSpaceList = new ArrayList<>();

        //获取第一个空间
        if (Math.abs(bigSpace.getX() - smallSpace.getX()) <= offSet) {
            //这个空间已经很小了,抛弃
        } else {
            freeSpaceList.add(new FreeSpace(bigSpace.getX(),
                    bigSpace.getY(),
                    bigSpace.getZ(),
                    smallSpace.getX() - bigSpace.getX(),
                    bigSpace.getWidth(),
                    bigSpace.getHeight()));
        }

        //获取第二个空间
        if (Math.abs(bigSpace.getX() + bigSpace.getLength() - smallSpace.getX() - smallSpace.getLength()) <= offSet) {
            //这个空间已经很小了,抛弃
        } else {
            freeSpaceList.add(new FreeSpace(smallSpace.getX() + smallSpace.getLength(),
                    bigSpace.getY(),
                    bigSpace.getZ(),
                    bigSpace.getX() + bigSpace.getLength() - smallSpace.getX() - smallSpace.getLength(),
                    bigSpace.getWidth(),
                    bigSpace.getHeight()));
        }

        //获取第三个空间
        if (Math.abs(bigSpace.getZ() + bigSpace.getHeight() - smallSpace.getZ() - smallSpace.getHeight()) <= offSet) {
            //这个空间已经很小了,抛弃
        } else {
            freeSpaceList.add(new FreeSpace(smallSpace.getX(),
                    bigSpace.getY(),
                    smallSpace.getZ() + smallSpace.getHeight(),
                    smallSpace.getLength(),
                    bigSpace.getWidth(),
                    bigSpace.getZ() + bigSpace.getHeight() - smallSpace.getZ() - smallSpace.getHeight()));
        }


        //获取第四个空间
        if (Math.abs(bigSpace.getZ() - smallSpace.getZ()) <= offSet) {
            //这个空间已经很小了,抛弃
        } else {
            freeSpaceList.add(new FreeSpace(smallSpace.getX(),
                    bigSpace.getY(),
                    bigSpace.getZ(),
                    smallSpace.getLength(),
                    bigSpace.getWidth(),
                    smallSpace.getZ() - bigSpace.getZ()));
        }

        //获取第五个空间
        if (Math.abs(bigSpace.getY() + bigSpace.getWidth() - smallSpace.getY() - smallSpace.getWidth()) <= offSet) {
            //这个空间已经很小了,抛弃
        } else {
            freeSpaceList.add(new FreeSpace(smallSpace.getX(),
                    smallSpace.getY() + smallSpace.getWidth(),
                    smallSpace.getZ(),
                    smallSpace.getLength(),
                    bigSpace.getY() + bigSpace.getWidth() - smallSpace.getY() - smallSpace.getWidth(),
                    smallSpace.getHeight()));
        }

        //获取第六个空间
        if (Math.abs(smallSpace.getY() - bigSpace.getY()) <= offSet) {
            //这个空间已经很小了,抛弃
        } else {
            freeSpaceList.add(new FreeSpace(smallSpace.getX(),
                    bigSpace.getY(),
                    smallSpace.getZ(),
                    smallSpace.getLength(),
                    smallSpace.getY() - bigSpace.getY(),
                    smallSpace.getHeight()));
        }

        return freeSpaceList;
    }

    /**
     * 判断两个空间是否有交集,有返回1,没有返回0,
     * 若freeSpace1全包含freeSpace2,返回2,
     * 若freeSpace2全包含freeSpace1,返回3
     *
     * @param freeSpace1
     * @param freeSpace2
     * @return
     */
    public int judgeSpaceRelationship(FreeSpace freeSpace1, FreeSpace freeSpace2) {

        //判断freeSpace1是否全包含freeSpace2
        if (freeSpace1.getX() <= freeSpace2.getX() + offSet &&
                freeSpace1.getY() <= freeSpace2.getY() + offSet &&
                freeSpace1.getZ() <= freeSpace2.getZ() + offSet &&
                freeSpace1.getX() + freeSpace1.getLength() + offSet >= freeSpace2.getX() + freeSpace2.getLength() &&
                freeSpace1.getY() + freeSpace1.getWidth() + offSet >= freeSpace2.getY() + freeSpace2.getWidth() &&
                freeSpace1.getZ() + freeSpace1.getHeight() + offSet >= freeSpace2.getZ() + freeSpace2.getHeight()
        ) {
            return 2;
        }
        //判断freeSpace2是否全包含freeSpace1
        if (freeSpace2.getX() <= freeSpace1.getX() - offSet &&
                freeSpace2.getY() <= freeSpace1.getY() - offSet &&
                freeSpace2.getZ() <= freeSpace1.getZ() - offSet &&
                freeSpace2.getX() + freeSpace2.getLength() - offSet >= freeSpace1.getX() + freeSpace1.getLength() &&
                freeSpace2.getY() + freeSpace2.getWidth() - offSet >= freeSpace1.getY() + freeSpace1.getWidth() &&
                freeSpace2.getZ() + freeSpace2.getHeight() - offSet >= freeSpace1.getZ() + freeSpace1.getHeight()
        ) {
            return 3;
        }

        //获取两个空间的中心坐标
        double x1 = freeSpace1.getX() + (freeSpace1.getLength() * 1.0) / 2;
        double y1 = freeSpace1.getY() + (freeSpace1.getWidth() * 1.0) / 2;
        double z1 = freeSpace1.getZ() + (freeSpace1.getHeight() * 1.0) / 2;
        double x2 = freeSpace2.getX() + (freeSpace2.getLength() * 1.0) / 2;
        double y2 = freeSpace2.getY() + (freeSpace2.getWidth() * 1.0) / 2;
        double z2 = freeSpace2.getZ() + (freeSpace2.getHeight() * 1.0) / 2;

        //判断是否相交
        if (Math.abs(x1 - x2) <= (freeSpace1.getLength() + freeSpace2.getLength()) * 1.0 / 2 - offSet
                && Math.abs(y1 - y2) <= (freeSpace1.getWidth() + freeSpace2.getWidth()) * 1.0 / 2 - offSet
                && Math.abs(z1 - z2) <= (freeSpace1.getHeight() + freeSpace2.getHeight()) * 1.0 / 2 - offSet) {
            return 1;
        }

        return 0;
    }

    /**
     * 给定一个剩余空间,返回剩余空间的八个角坐标
     *
     * @param space
     * @return
     */
    public List<Point> getPointListWithSpace(FreeSpace space) {
        List<Point> pointList = new ArrayList<>();

        //点1
        pointList.add(new Point(space.getX(), space.getY(), space.getZ(), 1));
        //点2
        pointList.add(new Point(space.getX() + space.getLength(), space.getY(), space.getZ(), 2));
        //点3
        pointList.add(new Point(space.getX() + space.getLength(), space.getY() + space.getWidth(), space.getZ(), 3));
        //点4
        pointList.add(new Point(space.getX(), space.getY() + space.getWidth(), space.getZ(), 4));
        //点5
        pointList.add(new Point(space.getX(), space.getY(), space.getZ() + space.getHeight(), 5));
        //点6
        pointList.add(new Point(space.getX() + space.getLength(), space.getY(), space.getZ() + space.getHeight(), 6));
        //点7
        pointList.add(new Point(space.getX() + space.getLength(), space.getY() + space.getWidth(), space.getZ() + space.getHeight(), 7));
        //点8
        pointList.add(new Point(space.getX(), space.getY() + space.getWidth(), space.getZ() + space.getHeight(), 8));

        return pointList;
    }

    /**
     * 给定一个点和一个空间,判断点是否被包含在空间中
     *
     * @param space
     * @param point
     * @return
     */
    public boolean isContainedInSpace(FreeSpace space, Point point) {
        if (space.getX() - offSet <= point.getX() && point.getX() - offSet <= space.getX() + space.getLength() &&
                space.getY() - offSet <= point.getY() && point.getY() - offSet <= space.getY() + space.getWidth() &&
                space.getZ() - offSet <= point.getZ() && point.getZ() - offSet <= space.getZ() + space.getHeight()
        ) {
            return true;
        }
        return false;
    }


    /**
     * 集合1:freeSpace1包含freeSpace2的角集合
     *
     * @param freeSpace1
     * @param freeSpace2
     * @return
     */
    public List<Point> calculatePointNum(FreeSpace freeSpace1, FreeSpace freeSpace2) {
        List<Point> pointList = new ArrayList<>();

        List<Point> eightPointList = getPointListWithSpace(freeSpace2);
//        System.out.println("八个点集合:");
//        System.out.println(eightPointList.toString());

//        System.out.println("空间:" + freeSpace1.toString());

        for (Point point : eightPointList) {
            if (this.isContainedInSpace(freeSpace1, point) == true) {
                pointList.add(point);
            }
        }

//        System.out.println("找到的点数量" + pointList.size());

        return pointList;
    }

    /**
     * 给定两个空间,获取两个空间的共有空间
     *
     * @param freeSpace1
     * @param freeSpace2
     * @return
     */
    public FreeSpace getSameSpace(FreeSpace freeSpace1, FreeSpace freeSpace2) {
        //freeSpace1包含freeSpace2的角个数
        List<Point> pointList1 = this.calculatePointNum(freeSpace1, freeSpace2);
        int size1 = pointList1.size();
        //freeSpace2包含freeSpace1的角个数
        List<Point> pointList2 = this.calculatePointNum(freeSpace2, freeSpace1);
        int size2 = pointList2.size();

        System.out.println("size1" + size1 + ">>" + "size2" + size2);

        //freeSpace1大于freeSpace2(不是体积意义上的大于)
        if (size1 >= size2) {
            //只有一个角被包含
            if (size1 == 1) {
                return this.getSameSpaceWithOnePoint(freeSpace1, pointList1.get(0));
            } else if (size1 == 2) {
                return this.getSameSpaceWithTwoPoint(freeSpace1, pointList1.get(0), pointList1.get(1));
            } else if (size1 == 4) {
                return this.getSameSpaceWithFourPoint(freeSpace1, pointList1.get(0), pointList1.get(1), pointList1.get(2), pointList1.get(3));
            }
        } else {
            //只有一个角被包含
            if (size2 == 1) {
                return this.getSameSpaceWithOnePoint(freeSpace2, pointList2.get(0));
            } else if (size2 == 2) {
                return this.getSameSpaceWithTwoPoint(freeSpace2, pointList2.get(0), pointList2.get(1));
            } else if (size2 == 4) {
                System.out.println("寻找共同空间");
                return this.getSameSpaceWithFourPoint(freeSpace2, pointList2.get(0), pointList2.get(1), pointList2.get(2), pointList2.get(3));
            }
        }
        return null;
    }

    /**
     * 一个空间只有一个点被另一个空间包含时,找共同空间
     *
     * @return
     */
    public FreeSpace getSameSpaceWithOnePoint(FreeSpace bigSpace, Point point) {
        List<Point> pointListWithBigSpace = this.getPointListWithSpace(bigSpace);

        //1>七
        if (point.getIndex() == 1) {
            return new FreeSpace(point.getX(),
                    point.getY(),
                    point.getZ(),
                    pointListWithBigSpace.get(6).getX() - point.getX(),
                    pointListWithBigSpace.get(6).getY() - point.getY(),
                    pointListWithBigSpace.get(6).getZ() - point.getZ());
        }

        //2>八
        if (point.getIndex() == 2) {
            return new FreeSpace(pointListWithBigSpace.get(7).getX(),
                    point.getY(),
                    point.getZ(),
                    point.getX() - pointListWithBigSpace.get(7).getX(),
                    pointListWithBigSpace.get(7).getY() - point.getY(),
                    pointListWithBigSpace.get(7).getZ() - point.getZ());
        }

        //3>五
        if (point.getIndex() == 3) {
            return new FreeSpace(pointListWithBigSpace.get(4).getX(),
                    pointListWithBigSpace.get(4).getY(),
                    point.getZ(),
                    point.getX() - pointListWithBigSpace.get(4).getX(),
                    point.getY() - pointListWithBigSpace.get(4).getY(),
                    pointListWithBigSpace.get(4).getZ() - point.getZ());
        }

        //4>六
        if (point.getIndex() == 4) {
            return new FreeSpace(point.getX(),
                    pointListWithBigSpace.get(5).getY(),
                    point.getZ(),
                    pointListWithBigSpace.get(5).getX() - point.getX(),
                    point.getY() - pointListWithBigSpace.get(5).getY(),
                    pointListWithBigSpace.get(5).getZ() - point.getZ());
        }

        //5>三
        if (point.getIndex() == 5) {
            return new FreeSpace(point.getX(),
                    point.getY(),
                    pointListWithBigSpace.get(2).getZ(),
                    pointListWithBigSpace.get(2).getX() - point.getX(),
                    pointListWithBigSpace.get(2).getY() - point.getY(),
                    point.getZ() - pointListWithBigSpace.get(2).getZ());
        }

        //6>四
        if (point.getIndex() == 6) {
            return new FreeSpace(pointListWithBigSpace.get(3).getX(),
                    point.getY(),
                    point.getZ(),
                    point.getX() - pointListWithBigSpace.get(3).getX(),
                    pointListWithBigSpace.get(3).getY() - point.getY(),
                    point.getZ() - pointListWithBigSpace.get(3).getZ());
        }

        //7>一
        if (point.getIndex() == 7) {
            return new FreeSpace(pointListWithBigSpace.get(0).getX(),
                    pointListWithBigSpace.get(0).getY(),
                    pointListWithBigSpace.get(0).getZ(),
                    point.getX() - pointListWithBigSpace.get(0).getX(),
                    point.getY() - pointListWithBigSpace.get(0).getY(),
                    point.getZ() - pointListWithBigSpace.get(0).getZ());
        }

        //8>二
        if (point.getIndex() == 8) {
            return new FreeSpace(pointListWithBigSpace.get(1).getX(),
                    pointListWithBigSpace.get(1).getY(),
                    point.getZ(),
                    point.getX() - pointListWithBigSpace.get(1).getX(),
                    point.getY() - pointListWithBigSpace.get(1).getY(),
                    pointListWithBigSpace.get(1).getZ() - point.getZ());
        }

        return null;
    }

    /**
     * 一个空间有两个点被另一个空间包含时,找共同空间
     *
     * @return
     */
    public FreeSpace getSameSpaceWithTwoPoint(FreeSpace bigSpace, Point point1, Point point2) {
        List<Point> pointListWithBigSpace = this.getPointListWithSpace(bigSpace);

        //1、2
        if (point1.getIndex() == 1 && point2.getIndex() == 2) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    point1.getZ(),
                    point2.getX() - point1.getX(),
                    pointListWithBigSpace.get(7).getY() - point1.getY(),
                    pointListWithBigSpace.get(7).getZ() - point1.getZ());
        }

        //3、4
        if (point1.getIndex() == 3 && point2.getIndex() == 4) {
            return new FreeSpace(point2.getX(),
                    pointListWithBigSpace.get(4).getY(),
                    point2.getZ(),
                    point1.getX() - point2.getX(),
                    point2.getY() - pointListWithBigSpace.get(4).getY(),
                    pointListWithBigSpace.get(4).getZ() - point2.getZ());
        }

        //5、6
        if (point1.getIndex() == 5 && point2.getIndex() == 6) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    pointListWithBigSpace.get(3).getZ(),
                    point2.getX() - point1.getX(),
                    pointListWithBigSpace.get(3).getY() - point1.getY(),
                    point1.getZ() - pointListWithBigSpace.get(3).getZ());
        }

        //7、8
        if (point1.getIndex() == 7 && point2.getIndex() == 8) {
            return new FreeSpace(point2.getX(),
                    pointListWithBigSpace.get(0).getY(),
                    pointListWithBigSpace.get(0).getZ(),
                    point1.getX() - point2.getX(),
                    point2.getY() - pointListWithBigSpace.get(0).getY(),
                    point2.getZ() - pointListWithBigSpace.get(0).getZ());
        }

        //1、4
        if (point1.getIndex() == 1 && point2.getIndex() == 4) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    point1.getZ(),
                    pointListWithBigSpace.get(5).getX() - point1.getX(),
                    point2.getY() - point1.getY(),
                    pointListWithBigSpace.get(5).getZ() - point1.getZ());
        }

        //5、8
        if (point1.getIndex() == 5 && point2.getIndex() == 8) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    pointListWithBigSpace.get(1).getZ(),
                    pointListWithBigSpace.get(1).getX() - point1.getX(),
                    point2.getY() - point1.getY(),
                    point1.getZ() - pointListWithBigSpace.get(1).getZ());
        }

        //2、3
        if (point1.getIndex() == 2 && point2.getIndex() == 3) {
            return new FreeSpace(pointListWithBigSpace.get(1).getX(),
                    point1.getY(),
                    point1.getZ(),
                    point1.getX() - pointListWithBigSpace.get(4).getX(),
                    point2.getY() - point1.getY(),
                    pointListWithBigSpace.get(4).getZ() - point1.getZ());
        }

        //6、7
        if (point1.getIndex() == 6 && point2.getIndex() == 7) {
            return new FreeSpace(point1.getX(),
                    pointListWithBigSpace.get(0).getY(),
                    pointListWithBigSpace.get(0).getZ(),
                    point2.getX() - point1.getX(),
                    point2.getY() - pointListWithBigSpace.get(0).getY(),
                    point1.getZ() - pointListWithBigSpace.get(0).getZ());
        }

        //1、5
        if (point1.getIndex() == 1 && point2.getIndex() == 5) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    point1.getZ(),
                    pointListWithBigSpace.get(2).getX() - point1.getX(),
                    pointListWithBigSpace.get(2).getY() - point1.getY(),
                    point2.getZ() - point1.getZ());
        }

        //2、6
        if (point1.getIndex() == 2 && point2.getIndex() == 6) {
            return new FreeSpace(pointListWithBigSpace.get(3).getX(),
                    point1.getY(),
                    point1.getZ(),
                    point1.getX() - pointListWithBigSpace.get(3).getX(),
                    pointListWithBigSpace.get(3).getY() - point1.getY(),
                    point2.getZ() - point1.getZ());
        }

        //4、8
        if (point1.getIndex() == 4 && point2.getIndex() == 8) {
            return new FreeSpace(point1.getX(),
                    pointListWithBigSpace.get(1).getY(),
                    point1.getZ(),
                    pointListWithBigSpace.get(1).getX() - point1.getX(),
                    point1.getY() - pointListWithBigSpace.get(1).getY(),
                    point2.getZ() - point1.getZ());
        }

        //3、7
        if (point1.getIndex() == 3 && point2.getIndex() == 7) {
            return new FreeSpace(pointListWithBigSpace.get(0).getX(),
                    pointListWithBigSpace.get(0).getY(),
                    point1.getZ(),
                    point1.getX() - pointListWithBigSpace.get(0).getX(),
                    point2.getY() - pointListWithBigSpace.get(0).getY(),
                    point2.getZ() - point1.getZ());
        }

        return null;
    }

    /**
     * 一个空间有四个点被另一个空间包含时,找共同空间
     *
     * @return
     */
    public FreeSpace getSameSpaceWithFourPoint(FreeSpace bigSpace, Point point1, Point point2, Point point3, Point point4) {
        List<Point> pointListWithBigSpace = this.getPointListWithSpace(bigSpace);

        //1、2、3、4
        if (point1.getIndex() == 1 && point2.getIndex() == 2 && point3.getIndex() == 3 && point4.getIndex() == 4) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    point1.getZ(),
                    point2.getX() - point1.getX(),
                    point4.getY() - point1.getY(),
                    pointListWithBigSpace.get(4).getZ() - point1.getZ());
        }

        //5、6、7、8
        if (point1.getIndex() == 5 && point2.getIndex() == 6 && point3.getIndex() == 7 && point4.getIndex() == 8) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    pointListWithBigSpace.get(3).getZ(),
                    point2.getX() - point1.getX(),
                    point4.getY() - point1.getY(),
                    point1.getZ() - pointListWithBigSpace.get(3).getZ());
        }

        //1、4、5、8
        if (point1.getIndex() == 1 && point2.getIndex() == 4 && point3.getIndex() == 5 && point4.getIndex() == 8) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    point1.getZ(),
                    pointListWithBigSpace.get(1).getX() - point1.getX(),
                    point2.getY() - point1.getY(),
                    point3.getZ() - point1.getZ());
        }

        //2、3、6、7
        if (point1.getIndex() == 2 && point2.getIndex() == 3 && point3.getIndex() == 6 && point4.getIndex() == 7) {
            return new FreeSpace(pointListWithBigSpace.get(0).getX(),
                    point1.getY(),
                    point1.getZ(),
                    point1.getX() - pointListWithBigSpace.get(0).getX(),
                    point2.getY() - point1.getY(),
                    point3.getZ() - point1.getZ());
        }

        //3、4、7、8
        if (point1.getIndex() == 3 && point2.getIndex() == 4 && point3.getIndex() == 7 && point4.getIndex() == 8) {
            return new FreeSpace(point2.getX(),
                    pointListWithBigSpace.get(0).getY(),
                    point2.getZ(),
                    point1.getX() - point2.getX(),
                    point2.getY() - pointListWithBigSpace.get(0).getY(),
                    point4.getZ() - point2.getZ());
        }

        //1、2、5、6
        if (point1.getIndex() == 1 && point2.getIndex() == 2 && point3.getIndex() == 5 && point4.getIndex() == 6) {
            return new FreeSpace(point1.getX(),
                    point1.getY(),
                    point1.getZ(),
                    point2.getX() - point1.getX(),
                    pointListWithBigSpace.get(3).getY() - point1.getY(),
                    point3.getZ() - point1.getZ());
        }


        return null;
    }

    /**
     * freeSpace1:已用空间
     * freeSpace2:剩余空间
     *
     * @return 数组
     * * 数组元素1:已用空间的分解
     * * 数组元素2:返回剩余空间
     */
    public List<FreeSpace>[] splitSpace(FreeSpace freeSpace1, FreeSpace freeSpace2) {
        List<FreeSpace>[] listArry = new List[2];
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println("freeSpace1" + freeSpace1 + ">>" + "freeSpace2" + freeSpace2);
        FreeSpace sameSpace = this.getSameSpace(freeSpace1, freeSpace2);
        System.out.println("sameSpace" + sameSpace);
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        listArry[0] = this.eliminateSpace(freeSpace1, sameSpace);
        listArry[1] = this.eliminateSpace(freeSpace2, sameSpace);
        return listArry;
    }
}

效果演示

测试数据

package com.dam.controller;


import com.dam.algorithm.algorithmPackage.freeSpaceTubePackingAlgorithm.util.GetUsedSpaceUtil;
import com.dam.algorithm.common.entity.FreeSpace;
import com.dam.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/splitSpace")
@Api(tags = "获取装载方案")
public class SplitSpaceController {

    @GetMapping("/usedSpace")
    @ApiOperation("获取装载方案集合")
    private Result usedSpace() throws Exception {

        //添加已用空间
        List<FreeSpace> usedSpaceList = new ArrayList<>();

        //需要修改大空间
        usedSpaceList.add(new FreeSpace(0, 0, 0, 100, 100, 100));

        this.addUsedSpace(usedSpaceList);

        return Result.ok().data("usedSpaceList", usedSpaceList);
    }


    @GetMapping("/splitSpace")
    @ApiOperation("获取装载方案集合")
    private Result splitSpace() throws Exception {

        System.out.println("开始计算");

        long start = System.currentTimeMillis();
        
        FreeSpace bigFreeSpace = new FreeSpace(0, 0, 0, 100, 100, 100);

        List<FreeSpace> freeSpaceList = new ArrayList<>();
        freeSpaceList.add(bigFreeSpace);

        //添加已用空间
        List<FreeSpace> usedSpaceList = new ArrayList<>();
        this.addUsedSpace(usedSpaceList);

        //计算
        new GetUsedSpaceUtil().getFreeSpaceUtil(bigFreeSpace, freeSpaceList, usedSpaceList);

        long end = System.currentTimeMillis();

        System.out.println("计算时间:" + (end - start) + "ms");

        return Result.ok().data("freeSpaceList", freeSpaceList);
    }

    private void addUsedSpace(List<FreeSpace> usedSpaceList) {
        usedSpaceList.add(new FreeSpace(0, 50, 50, 30, 30, 30));
        usedSpaceList.add(new FreeSpace(0, 80, 80, 10, 20, 15));
        usedSpaceList.add(new FreeSpace(36, 35, 50, 50, 30, 30));
        usedSpaceList.add(new FreeSpace(0, 50, 0, 70, 50, 30));
        usedSpaceList.add(new FreeSpace(0, 0, 0, 90, 20, 20));
    }
}

结果

java代码实现空间切割

大立方体和待切除的小立方体

java代码实现空间切割

切除小立方体之后剩余的空间

总结

到此这篇关于java代码实现空间切割的文章就介绍到这了,更多相关java空间切割内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
Java实现斗地主之洗牌发牌
Jun 14 Java/Android
Java SSM配置文件案例详解
Aug 30 Java/Android
Java使用Unsafe类的示例详解
Sep 25 Java/Android
解决persistence.xml配置文件修改存放路径的问题
Feb 24 Java/Android
Java 异步任务计算FutureTask
Apr 28 Java/Android
Spring Cloud OAuth2实现自定义token返回格式
Jun 25 Java/Android
Java+swing实现抖音上的表白程序详解
Jun 25 Java/Android
利用Java连接Hadoop进行编程
Jun 28 Java/Android
java中如何截取字符串最后一位
Jul 07 Java/Android
SpringBoot详解执行过程
Jul 15 Java/Android
向Spring IOC 容器动态注册bean实现方式
Jul 15 Java/Android
springboot新建项目pom.xml文件第一行报错的解决
Jan 18 #Java/Android
关于@OnetoMany关系映射的排序问题,使用注解@OrderBy
Dec 06 #Java/Android
SpringBoot整合阿里云视频点播的过程详解
Dec 06 #Java/Android
SpringDataJPA在Entity中常用的注解介绍
Dec 06 #Java/Android
Java中try catch处理异常示例
Dec 06 #Java/Android
SpringDataJPA实体类关系映射配置方式
Dec 06 #Java/Android
Java异常处理try catch的基本用法
Dec 06 #Java/Android
You might like
PHP执行linux命令常用函数汇总
2016/02/02 PHP
Laravel框架模板继承操作示例
2018/06/11 PHP
PHP中mysqli_get_server_version()的实例用法
2020/02/03 PHP
JavaScript延迟加载
2021/03/09 Javascript
jQuery-Easyui 1.2 实现多层菜单效果的代码
2012/01/13 Javascript
js onkeypress与onkeydown 事件区别详细说明
2012/12/13 Javascript
常用的JavaScript验证正则表达式汇总
2013/11/26 Javascript
jQuery实现鼠标滚轮动态改变样式或效果
2015/01/05 Javascript
JSON字符串转JSON对象
2015/07/31 Javascript
jQuery使用$.ajax进行即时验证的方法
2015/12/08 Javascript
angularjs中ng-attr的用法详解
2016/12/31 Javascript
Vue中img的src属性绑定与static文件夹实例
2017/05/18 Javascript
关于javascript获取内联样式与嵌入式样式的实例
2017/06/01 Javascript
Bootstrap Table从零开始
2017/06/30 Javascript
原生js获取left值和top值的三种方法
2017/08/02 Javascript
weex里Vuex state使用storage持久化详解
2017/09/09 Javascript
Chrome调试折腾记之JS断点调试技巧
2017/09/11 Javascript
Javascript 严格模式use strict详解
2017/09/16 Javascript
详解react-native WebView 返回处理(非回调方法可解决)
2018/02/27 Javascript
微信小程序picker组件关于objectArray数据类型的绑定方法
2019/03/13 Javascript
Python生成随机数的方法
2014/01/14 Python
一篇文章了解Python中常见的序列化操作
2019/06/20 Python
Django单元测试工具test client使用详解
2019/08/02 Python
在python image 中安装中文字体的实现方法
2019/08/22 Python
用Python爬取QQ音乐评论并制成词云图的实例
2019/08/24 Python
Django Path转换器自定义及正则代码实例
2020/05/29 Python
python中tab键是什么意思
2020/06/18 Python
python实现画图工具
2020/08/27 Python
python tqdm实现进度条的示例代码
2020/11/10 Python
python中pop()函数的语法与实例
2020/12/01 Python
Eagle Eyes Optics鹰眼光学:高性能太阳镜
2018/12/07 全球购物
系统管理员的职责包括那些?管理的对象是什么?
2016/09/20 面试题
外贸公司实习自我鉴定
2013/09/24 职场文书
幼师自我鉴定
2014/02/01 职场文书
门市房租房协议书
2014/12/04 职场文书
我们的节日重阳节活动总结
2015/03/24 职场文书