|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.common.geo; |
| 20 | + |
| 21 | +import org.apache.lucene.util.BytesRef; |
| 22 | +import org.elasticsearch.common.io.stream.ByteBufferStreamInput; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.ByteBuffer; |
| 26 | + |
| 27 | +import static org.apache.lucene.geo.GeoUtils.lineCrossesLine; |
| 28 | + |
| 29 | +public class EdgeTreeReader { |
| 30 | + final BytesRef bytesRef; |
| 31 | + |
| 32 | + public EdgeTreeReader(BytesRef bytesRef) { |
| 33 | + this.bytesRef = bytesRef; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns true if the rectangle query and the edge tree's shape overlap |
| 38 | + */ |
| 39 | + public boolean containedInOrCrosses(int minX, int minY, int maxX, int maxY) throws IOException { |
| 40 | + return this.containsBottomLeft(minX, minY, maxX, maxY) || this.crosses(minX, minY, maxX, maxY); |
| 41 | + } |
| 42 | + |
| 43 | + boolean containsBottomLeft(int minX, int minY, int maxX, int maxY) throws IOException { |
| 44 | + ByteBufferStreamInput input = new ByteBufferStreamInput(ByteBuffer.wrap(bytesRef.bytes, bytesRef.offset, bytesRef.length)); |
| 45 | + int thisMinX = input.readInt(); |
| 46 | + int thisMinY = input.readInt(); |
| 47 | + int thisMaxX = input.readInt(); |
| 48 | + int thisMaxY = input.readInt(); |
| 49 | + |
| 50 | + if (thisMinY > maxY || thisMaxX < minX || thisMaxY < minY || thisMinX > maxX) { |
| 51 | + return false; // tree and bbox-query are disjoint |
| 52 | + } |
| 53 | + |
| 54 | + if (minX <= thisMinX && minY <= thisMinY && maxX >= thisMaxX && maxY >= thisMaxY) { |
| 55 | + return true; // bbox-query fully contains tree's extent. |
| 56 | + } |
| 57 | + |
| 58 | + return containsBottomLeft(input, readRoot(input, input.position()), minX, minY, maxX, maxY); |
| 59 | + } |
| 60 | + |
| 61 | + public boolean crosses(int minX, int minY, int maxX, int maxY) throws IOException { |
| 62 | + ByteBufferStreamInput input = new ByteBufferStreamInput(ByteBuffer.wrap(bytesRef.bytes, bytesRef.offset, bytesRef.length)); |
| 63 | + int thisMinX = input.readInt(); |
| 64 | + int thisMinY = input.readInt(); |
| 65 | + int thisMaxX = input.readInt(); |
| 66 | + int thisMaxY = input.readInt(); |
| 67 | + |
| 68 | + if (thisMinY > maxY || thisMaxX < minX || thisMaxY < minY || thisMinX > maxX) { |
| 69 | + return false; // tree and bbox-query are disjoint |
| 70 | + } |
| 71 | + |
| 72 | + if (minX <= thisMinX && minY <= thisMinY && maxX >= thisMaxX && maxY >= thisMaxY) { |
| 73 | + return true; // bbox-query fully contains tree's extent. |
| 74 | + } |
| 75 | + |
| 76 | + return crosses(input, readRoot(input, input.position()), minX, minY, maxX, maxY); |
| 77 | + } |
| 78 | + |
| 79 | + public Edge readRoot(ByteBufferStreamInput input, int position) throws IOException { |
| 80 | + return readEdge(input, position); |
| 81 | + } |
| 82 | + |
| 83 | + private static Edge readEdge(ByteBufferStreamInput input, int position) throws IOException { |
| 84 | + input.position(position); |
| 85 | + int minY = input.readInt(); |
| 86 | + int maxY = input.readInt(); |
| 87 | + int x1 = input.readInt(); |
| 88 | + int y1 = input.readInt(); |
| 89 | + int x2 = input.readInt(); |
| 90 | + int y2 = input.readInt(); |
| 91 | + int rightOffset = input.readInt(); |
| 92 | + return new Edge(input.position(), x1, y1, x2, y2, minY, maxY, rightOffset); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + Edge readLeft(ByteBufferStreamInput input, Edge root) throws IOException { |
| 97 | + return readEdge(input, root.streamOffset); |
| 98 | + } |
| 99 | + |
| 100 | + Edge readRight(ByteBufferStreamInput input, Edge root) throws IOException { |
| 101 | + return readEdge(input, root.streamOffset + root.rightOffset); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns true if the bottom-left point of the rectangle query is contained within the |
| 106 | + * tree's edges. |
| 107 | + */ |
| 108 | + private boolean containsBottomLeft(ByteBufferStreamInput input, Edge root, int minX, int minY, int maxX, int maxY) throws IOException { |
| 109 | + boolean res = false; |
| 110 | + if (root.maxY >= minY) { |
| 111 | + // is bbox-query contained within linearRing |
| 112 | + // cast infinite ray to the right from bottom-left of bbox-query to see if it intersects edge |
| 113 | + if (lineCrossesLine(root.x1, root.y1, root.x2, root.y2,minX, minY, Integer.MAX_VALUE, minY)) { |
| 114 | + res = true; |
| 115 | + } |
| 116 | + |
| 117 | + if (root.rightOffset > 0) { /* has left node */ |
| 118 | + res ^= containsBottomLeft(input, readLeft(input, root), minX, minY, maxX, maxY); |
| 119 | + } |
| 120 | + |
| 121 | + if (root.rightOffset > 0 && maxY >= root.minY) { /* no right node if rightOffset == -1 */ |
| 122 | + res ^= containsBottomLeft(input, readRight(input, root), minX, minY, maxX, maxY); |
| 123 | + } |
| 124 | + } |
| 125 | + return res; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns true if the box crosses any edge in this edge subtree |
| 130 | + * */ |
| 131 | + private boolean crosses(ByteBufferStreamInput input, Edge root, int minX, int minY, int maxX, int maxY) throws IOException { |
| 132 | + boolean res = false; |
| 133 | + // we just have to cross one edge to answer the question, so we descend the tree and return when we do. |
| 134 | + if (root.maxY >= minY) { |
| 135 | + |
| 136 | + // does rectangle's edges intersect or reside inside polygon's edge |
| 137 | + if (lineCrossesLine(root.x1, root.y1, root.x2, root.y2, minX, minY, maxX, minY) || |
| 138 | + lineCrossesLine(root.x1, root.y1, root.x2, root.y2, maxX, minY, maxX, maxY) || |
| 139 | + lineCrossesLine(root.x1, root.y1, root.x2, root.y2, maxX, maxY, minX, maxY) || |
| 140 | + lineCrossesLine(root.x1, root.y1, root.x2, root.y2, minX, maxY, minX, minY)) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + if (root.rightOffset > 0) { /* has left node */ |
| 145 | + if (crosses(input, readLeft(input, root), minX, minY, maxX, maxY)) { |
| 146 | + return true; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (root.rightOffset > 0 && maxY >= root.minY) { /* no right node if rightOffset == -1 */ |
| 151 | + if (crosses(input, readRight(input, root), minX, minY, maxX, maxY)) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + private static class Edge { |
| 161 | + int streamOffset; |
| 162 | + int x1; |
| 163 | + int y1; |
| 164 | + int x2; |
| 165 | + int y2; |
| 166 | + int minY; |
| 167 | + int maxY; |
| 168 | + int rightOffset; |
| 169 | + |
| 170 | + /** |
| 171 | + * Object representing an edge node read from bytes |
| 172 | + * |
| 173 | + * @param streamOffset offset in byte-reference where edge terminates |
| 174 | + * @param x1 x-coordinate of first point in segment |
| 175 | + * @param y1 y-coordinate of first point in segment |
| 176 | + * @param x2 x-coordinate of second point in segment |
| 177 | + * @param y2 y-coordinate of second point in segment |
| 178 | + * @param minY minimum y-coordinate in this edge-node's tree |
| 179 | + * @param maxY maximum y-coordinate in this edge-node's tree |
| 180 | + * @param rightOffset the start offset in the byte-reference of the right edge-node |
| 181 | + */ |
| 182 | + Edge(int streamOffset, int x1, int y1, int x2, int y2, int minY, int maxY, int rightOffset) { |
| 183 | + this.streamOffset = streamOffset; |
| 184 | + this.x1 = x1; |
| 185 | + this.y1 = y1; |
| 186 | + this.x2 = x2; |
| 187 | + this.y2 = y2; |
| 188 | + this.minY = minY; |
| 189 | + this.maxY = maxY; |
| 190 | + this.rightOffset = rightOffset; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments