|
| 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.geo.geometry.LinearRing; |
| 23 | +import org.elasticsearch.geo.geometry.Polygon; |
| 24 | +import org.elasticsearch.test.ESTestCase; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +public class GeometryTreeTests extends ESTestCase { |
| 30 | + |
| 31 | + public void testRectangleShape() throws IOException { |
| 32 | + for (int i = 0; i < 1000; i++) { |
| 33 | + int minX = randomIntBetween(-180, 170); |
| 34 | + int maxX = randomIntBetween(minX + 10, 180); |
| 35 | + int minY = randomIntBetween(-90, 80); |
| 36 | + int maxY = randomIntBetween(minY + 10, 90); |
| 37 | + double[] x = new double[]{minX, maxX, maxX, minX, minX}; |
| 38 | + double[] y = new double[]{minY, minY, maxY, maxY, minY}; |
| 39 | + GeometryTreeWriter writer = new GeometryTreeWriter(new Polygon(new LinearRing(y, x), Collections.emptyList())); |
| 40 | + BytesRef bytes = writer.toBytesRef(); |
| 41 | + GeometryTreeReader reader = new GeometryTreeReader(bytes); |
| 42 | + |
| 43 | + // box-query touches bottom-left corner |
| 44 | + assertTrue(reader.containedInOrCrosses(minX - randomIntBetween(1, 180), minY - randomIntBetween(1, 180), minX, minY)); |
| 45 | + // box-query touches bottom-right corner |
| 46 | + assertTrue(reader.containedInOrCrosses(maxX, minY - randomIntBetween(1, 180), maxX + randomIntBetween(1, 180), minY)); |
| 47 | + // box-query touches top-right corner |
| 48 | + assertTrue(reader.containedInOrCrosses(maxX, maxY, maxX + randomIntBetween(1, 180), maxY + randomIntBetween(1, 180))); |
| 49 | + // box-query touches top-left corner |
| 50 | + assertTrue(reader.containedInOrCrosses(minX - randomIntBetween(1, 180), maxY, minX, maxY + randomIntBetween(1, 180))); |
| 51 | + // box-query fully-enclosed inside rectangle |
| 52 | + assertTrue(reader.containedInOrCrosses((3 * minX + maxX) / 4, (3 * minY + maxY) / 4, (3 * maxX + minX) / 4, |
| 53 | + (3 * maxY + minY) / 4)); |
| 54 | + // box-query fully-contains poly |
| 55 | + assertTrue(reader.containedInOrCrosses(minX - randomIntBetween(1, 180), minY - randomIntBetween(1, 180), |
| 56 | + maxX + randomIntBetween(1, 180), maxY + randomIntBetween(1, 180))); |
| 57 | + // box-query half-in-half-out-right |
| 58 | + assertTrue(reader.containedInOrCrosses((3 * minX + maxX) / 4, (3 * minY + maxY) / 4, maxX + randomIntBetween(1, 1000), |
| 59 | + (3 * maxY + minY) / 4)); |
| 60 | + // box-query half-in-half-out-left |
| 61 | + assertTrue(reader.containedInOrCrosses(minX - randomIntBetween(1, 1000), (3 * minY + maxY) / 4, (3 * maxX + minX) / 4, |
| 62 | + (3 * maxY + minY) / 4)); |
| 63 | + // box-query half-in-half-out-top |
| 64 | + assertTrue(reader.containedInOrCrosses((3 * minX + maxX) / 4, (3 * minY + maxY) / 4, maxX + randomIntBetween(1, 1000), |
| 65 | + maxY + randomIntBetween(1, 1000))); |
| 66 | + // box-query half-in-half-out-bottom |
| 67 | + assertTrue(reader.containedInOrCrosses((3 * minX + maxX) / 4, minY - randomIntBetween(1, 1000), |
| 68 | + maxX + randomIntBetween(1, 1000), (3 * maxY + minY) / 4)); |
| 69 | + |
| 70 | + // box-query outside to the right |
| 71 | + assertFalse(reader.containedInOrCrosses(maxX + randomIntBetween(1, 1000), minY, maxX + randomIntBetween(1001, 2000), maxY)); |
| 72 | + // box-query outside to the left |
| 73 | + assertFalse(reader.containedInOrCrosses(maxX - randomIntBetween(1001, 2000), minY, minX - randomIntBetween(1, 1000), maxY)); |
| 74 | + // box-query outside to the top |
| 75 | + assertFalse(reader.containedInOrCrosses(minX, maxY + randomIntBetween(1, 1000), maxX, maxY + randomIntBetween(1001, 2000))); |
| 76 | + // box-query outside to the bottom |
| 77 | + assertFalse(reader.containedInOrCrosses(minX, minY - randomIntBetween(1001, 2000), maxX, minY - randomIntBetween(1, 1000))); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public void testPacMan() throws Exception { |
| 82 | + // pacman |
| 83 | + double[] px = {0, 10, 10, 0, -8, -10, -8, 0, 10, 10, 0}; |
| 84 | + double[] py = {0, 5, 9, 10, 9, 0, -9, -10, -9, -5, 0}; |
| 85 | + |
| 86 | + // candidate containedInOrCrosses cell |
| 87 | + int xMin = 2;//-5; |
| 88 | + int xMax = 11;//0.000001; |
| 89 | + int yMin = -1;//0; |
| 90 | + int yMax = 1;//5; |
| 91 | + |
| 92 | + // test cell crossing poly |
| 93 | + GeometryTreeWriter writer = new GeometryTreeWriter(new Polygon(new LinearRing(py, px), Collections.emptyList())); |
| 94 | + GeometryTreeReader reader = new GeometryTreeReader(writer.toBytesRef()); |
| 95 | + assertTrue(reader.containedInOrCrosses(xMin, yMin, xMax, yMax)); |
| 96 | + } |
| 97 | +} |
0 commit comments