Class IntervalRTreeNode

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.index.intervalrtree;
13  
14 import java.util.Comparator;
15  
16 import org.locationtech.jts.geom.Coordinate;
17 import org.locationtech.jts.index.ItemVisitor;
18 import org.locationtech.jts.io.WKTWriter;
19  
20  
21 public abstract class IntervalRTreeNode 
22 {
23     protected double min = Double.POSITIVE_INFINITY;
24     protected double max = Double.NEGATIVE_INFINITY;
25  
26     public double getMin() { return min; }
27     public double getMax() { return max; }
28     
29     public abstract void query(double queryMin, double queryMax, ItemVisitor visitor);
30     
31     protected boolean intersects(double queryMin, double queryMax)
32     {
33         if (min > queryMax 
34                 || max < queryMin)
35             return false;
36         return true;
37     }
38  
39     public String toString()
40     {
41         return WKTWriter.toLineString(new Coordinate(min, 0), new Coordinate(max, 0));
42     }
43   
44   public static class NodeComparator implements Comparator
45   {
46     public int compare(Object o1, Object o2)
47     {
48       IntervalRTreeNode n1 = (IntervalRTreeNode) o1;
49       IntervalRTreeNode n2 = (IntervalRTreeNode) o2;
50       double mid1 = (n1.min + n1.max) / 2;
51       double mid2 = (n2.min + n2.max) / 2;
52       if (mid1 < mid2) return -1;
53       if (mid1 > mid2) return 1;
54       return 0;
55     }
56   }
57  
58 }
59