Search in sources :

Example 1 with IdObjectQuadTree

use of com.revolsys.geometry.index.quadtree.IdObjectQuadTree in project com.revolsys.open by revolsys.

the class IntArrayScaleTriangulatedIrregularNetwork method newIdSpatialIndex.

private QuadTree<Integer> newIdSpatialIndex() {
    final QuadTree<Integer> triangleSpatialIndex = new IdObjectQuadTree<Integer>(this.geometryFactory) {

        private static final long serialVersionUID = 1L;

        @Override
        protected boolean intersectsBounds(final Object id, final double x, final double y) {
            final Integer triangleIndex = (Integer) id;
            final int[] triangleXCoordinates = IntArrayScaleTriangulatedIrregularNetwork.this.triangleXCoordinates;
            final int[] triangleYCoordinates = IntArrayScaleTriangulatedIrregularNetwork.this.triangleYCoordinates;
            final int triangleVertexIndex = triangleIndex * 3;
            int minXInt = Integer.MAX_VALUE;
            int maxXInt = Integer.MIN_VALUE;
            int minYInt = Integer.MAX_VALUE;
            int maxYInt = Integer.MIN_VALUE;
            for (int vertexIndex = 0; vertexIndex < 3; vertexIndex++) {
                final int vertexX = triangleXCoordinates[triangleVertexIndex + vertexIndex];
                if (vertexX != Integer.MIN_VALUE) {
                    if (vertexX < minXInt) {
                        minXInt = vertexX;
                    }
                    if (vertexX > maxXInt) {
                        maxXInt = vertexX;
                    }
                }
                final int vertexY = triangleYCoordinates[triangleVertexIndex + vertexIndex];
                if (vertexY != Integer.MIN_VALUE) {
                    if (vertexY < minYInt) {
                        minYInt = vertexY;
                    }
                    if (vertexY > maxYInt) {
                        maxYInt = vertexY;
                    }
                }
            }
            if (x < minXInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleX) {
                return false;
            } else if (x > maxXInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleX) {
                return false;
            } else if (y < minYInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleY) {
                return false;
            } else if (y > maxYInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleY) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        protected boolean intersectsBounds(final Object id, double x1, double y1, double x2, double y2) {
            final Integer triangleIndex = (Integer) id;
            final int[] triangleXCoordinates = IntArrayScaleTriangulatedIrregularNetwork.this.triangleXCoordinates;
            final int[] triangleYCoordinates = IntArrayScaleTriangulatedIrregularNetwork.this.triangleYCoordinates;
            final int triangleVertexIndex = triangleIndex * 3;
            int minXInt = Integer.MAX_VALUE;
            int maxXInt = Integer.MIN_VALUE;
            int minYInt = Integer.MAX_VALUE;
            int maxYInt = Integer.MIN_VALUE;
            for (int vertexIndex = 0; vertexIndex < 3; vertexIndex++) {
                final int vertexX = triangleXCoordinates[triangleVertexIndex + vertexIndex];
                if (vertexX != Integer.MIN_VALUE) {
                    if (vertexX < minXInt) {
                        minXInt = vertexX;
                    }
                    if (vertexX > maxXInt) {
                        maxXInt = vertexX;
                    }
                }
                final int vertexY = triangleYCoordinates[triangleVertexIndex + vertexIndex];
                if (vertexY != Integer.MIN_VALUE) {
                    if (vertexY < minYInt) {
                        minYInt = vertexY;
                    }
                    if (vertexY > maxYInt) {
                        maxYInt = vertexY;
                    }
                }
            }
            if (x1 > x2) {
                final double t = x1;
                x1 = x2;
                x2 = t;
            }
            if (y1 > y2) {
                final double t = y1;
                y1 = y2;
                y2 = t;
            }
            if (x2 < minXInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleX) {
                return false;
            } else if (x1 > maxXInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleX) {
                return false;
            } else if (y2 < minYInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleY) {
                return false;
            } else if (y1 > maxYInt / IntArrayScaleTriangulatedIrregularNetwork.this.scaleY) {
                return false;
            } else {
                return true;
            }
        }
    };
    triangleSpatialIndex.setUseEquals(true);
    int triangleVertexIndex = 0;
    final double scaleX = this.scaleX;
    final double scaleY = this.scaleY;
    final int[] triangleXCoordinates = this.triangleXCoordinates;
    final int[] triangleYCoordinates = this.triangleYCoordinates;
    final int triangleCount = this.triangleCount;
    for (int triangleIndex = 0; triangleIndex < triangleCount; triangleIndex++) {
        int minXInt = Integer.MAX_VALUE;
        int maxXInt = Integer.MIN_VALUE;
        int minYInt = Integer.MAX_VALUE;
        int maxYInt = Integer.MIN_VALUE;
        for (int vertexIndex = 0; vertexIndex < 3; vertexIndex++) {
            final int vertexX = triangleXCoordinates[triangleVertexIndex + vertexIndex];
            if (vertexX != Integer.MIN_VALUE) {
                if (vertexX < minXInt) {
                    minXInt = vertexX;
                }
                if (vertexX > maxXInt) {
                    maxXInt = vertexX;
                }
            }
            final int vertexY = triangleYCoordinates[triangleVertexIndex + vertexIndex];
            if (vertexY != Integer.MIN_VALUE) {
                if (vertexY < minYInt) {
                    minYInt = vertexY;
                }
                if (vertexY > maxYInt) {
                    maxYInt = vertexY;
                }
            }
        }
        final double minX = minXInt / scaleX;
        final double minY = minYInt / scaleY;
        final double maxX = maxXInt / scaleX;
        final double maxY = maxYInt / scaleY;
        triangleSpatialIndex.insertItem(minX, minY, maxX, maxY, triangleIndex);
        triangleVertexIndex += 3;
    }
    return triangleSpatialIndex;
}
Also used : Point(com.revolsys.geometry.model.Point) IdObjectQuadTree(com.revolsys.geometry.index.quadtree.IdObjectQuadTree)

Aggregations

IdObjectQuadTree (com.revolsys.geometry.index.quadtree.IdObjectQuadTree)1 Point (com.revolsys.geometry.model.Point)1