亲宝软件园·资讯

展开

java空间切割

老丹丘 人气:0

问题

给定一个大的立方体和一批小的立方体,对于每个立方体,都知道如图的信息(知道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));
    }
}

结果

在这里插入图片描述

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

在这里插入图片描述

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

总结

加载全部内容

相关教程
猜你喜欢
用户评论