All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.espertech.esper.spatial.quadtree.mxcifrowindex.MXCIFQuadTreeRowIndexQuery Maven / Gradle / Ivy

There is a newer version: 7.1.0
Show newest version
/*
 ***************************************************************************************
 *  Copyright (C) 2006 EsperTech, Inc. All rights reserved.                            *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 ***************************************************************************************
 */
package com.espertech.esper.spatial.quadtree.mxcifrowindex;

import com.espertech.esper.spatial.quadtree.core.BoundingBox;
import com.espertech.esper.spatial.quadtree.mxcif.MXCIFQuadTree;
import com.espertech.esper.spatial.quadtree.mxcif.MXCIFQuadTreeNode;
import com.espertech.esper.spatial.quadtree.mxcif.MXCIFQuadTreeNodeBranch;
import com.espertech.esper.spatial.quadtree.mxcif.MXCIFQuadTreeNodeLeaf;

import java.util.ArrayDeque;
import java.util.Collection;

public class MXCIFQuadTreeRowIndexQuery {
    public static Collection queryRange(MXCIFQuadTree quadTree, double x, double y, double width, double height) {
        return queryNode(quadTree.getRoot(), x, y, width, height, null);
    }

    private static Collection queryNode(MXCIFQuadTreeNode node, double x, double y, double width, double height, Collection result) {
        if (node instanceof MXCIFQuadTreeNodeLeaf) {
            MXCIFQuadTreeNodeLeaf leaf = (MXCIFQuadTreeNodeLeaf) node;
            return visit(leaf, x, y, width, height, result);
        }

        MXCIFQuadTreeNodeBranch branch = (MXCIFQuadTreeNodeBranch) node;
        result = visit(branch, x, y, width, height, result);
        result = queryNode(branch.getNw(), x, y, width, height, result);
        result = queryNode(branch.getNe(), x, y, width, height, result);
        result = queryNode(branch.getSw(), x, y, width, height, result);
        result = queryNode(branch.getSe(), x, y, width, height, result);
        return result;
    }

    private static Collection visit(MXCIFQuadTreeNode node, double x, double y, double width, double height, Collection result) {
        Object data = node.getData();
        if (data == null) {
            return result;
        }
        if (data instanceof XYWHRectangleMultiType) {
            XYWHRectangleMultiType point = (XYWHRectangleMultiType) data;
            return visit(point, x, y, width, height, result);
        }
        Collection collection = (Collection) data;
        for (XYWHRectangleMultiType rectangle : collection) {
            result = visit(rectangle, x, y, width, height, result);
        }
        return result;
    }

    private static Collection visit(XYWHRectangleMultiType rectangle, double x, double y, double width, double height, Collection result) {
        if (!BoundingBox.intersectsBoxIncludingEnd(x, y, x + width, y + height, rectangle.getX(), rectangle.getY(), rectangle.getW(), rectangle.getH())) {
            return result;
        }
        if (result == null) {
            result = new ArrayDeque<>(4);
        }
        rectangle.collectInto(result);
        return result;
    }
}