java代碼實現空間切割

問題

給定一個大的立方體和一批小的立方體,對於每個立方體,都知道如圖的信息(知道x,y,z坐標和長、寬、高),且任意小立方體都被包含在大立方體內、各立方體之間不重疊。問如何將小立方體從大立方體中切去,留下其他空間?

在這裡插入圖片描述

代碼

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空間切割內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: