简体   繁体   English

构造3D KD树

[英]Constructing a 3D KD Tree

So I am trying to construct a 3D KD Tree from a list of randomly generated points. 因此,我试图从随机生成的点的列表中构造3D KD树。 I am trying to accomplish this task recursively as well. 我也试图递归地完成此任务。 But in my recursion I am facing an error when I'm trying to partition my list of points. 但是在递归中,当我尝试对点列表进行分区时遇到错误。 My code is as follows: 我的代码如下:

public class Scratch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ArrayList<Point> points = new ArrayList<Point>();
        Random rand = new Random(System.currentTimeMillis());
        for (int i = 0; i < 100; i++) {
            double x = rand.nextInt(100);
            double y = rand.nextInt(100);
            double z = rand.nextInt(100);
            Point point = new Point(x, y, z);
            points.add(point);
        }
        Node root = kdtree(points, 0);
        System.out.println("Done");
    }

    static class Node {

        Node leftChild;
        Node rightChild;
        Point location;

        Node() {

        }

        Node(Node leftChild, Node rightChild, Point location) {
            this.leftChild = leftChild;
            this.rightChild = rightChild;
            this.location = location;
        }
    }

    public static Node kdtree(ArrayList<Point> points, int depth) {
        int axis = depth % 3;
        switch (axis) {
            case 0:
                Collections.sort(points, Point.X_COMPARATOR);
                break;
            case 1:
                Collections.sort(points, Point.Y_COMPARATOR);
                break;
            case 2:
                Collections.sort(points, Point.Z_COMPARATOR);
                break;
            default:
                break;
        }
        int middle = points.size() / 2;
        Point median = points.get(middle);

        ArrayList<Point> greater = new ArrayList<Point>(points.subList(0, (middle - 1)));
        ArrayList<Point> lesser = new ArrayList<Point>(points.subList((middle + 1), (points.size())));
        Node node = new Node();
        node.location = median;
        if(greater.size() == 0 || lesser.size() == 0) {
            node.leftChild = null;
            node.rightChild = null;
        } else {
            node.leftChild = kdtree(lesser, depth + 1);
            node.rightChild = kdtree(greater, depth + 1);
        }        
        return node;
    }

}

The class point is a simple object which contains an x, y and z coordinate. 类点是一个简单的对象,其中包含x,y和z坐标。 And three comparators used based on the depth of the tree. 并根据树的深度使用了三个比较器。 The error I am getting is as follows: 我收到的错误如下:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(0) > toIndex(-1)
    at java.util.ArrayList.subListRangeCheck(ArrayList.java:1006)
    at java.util.ArrayList.subList(ArrayList.java:996)
    at scratch.Scratch.kdtree(Scratch.java:71)
    at scratch.Scratch.kdtree(Scratch.java:80)
    at scratch.Scratch.kdtree(Scratch.java:79)
    at scratch.Scratch.kdtree(Scratch.java:79)
    at scratch.Scratch.kdtree(Scratch.java:79)
    at scratch.Scratch.kdtree(Scratch.java:79)
    at scratch.Scratch.main(Scratch.java:32)

我将在这里继续猜测,并说如果在点数组上只有一个点(或0),则当您执行middle = points.size / 2等于0(整数除法)时,然后尝试进行从0到-1的子列表,将引发该异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM