Class Key

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.index.bintree;
14  
15  
16  
17 import org.locationtech.jts.index.quadtree.DoubleBits;
18  
19 /**
20  * A Key is a unique identifier for a node in a tree.
21  * It contains a lower-left point and a level number. The level number
22  * is the power of two for the size of the node envelope
23  *
24  * @version 1.7
25  */
26 public class Key {
27  
28   public static int computeLevel(Interval interval)
29   {
30     double dx = interval.getWidth();
31     //int level = BinaryPower.exponent(dx) + 1;
32     int level = DoubleBits.exponent(dx) + 1;
33     return level;
34   }
35  
36  
37   // the fields which make up the key
38   private double pt = 0.0;
39   private int level = 0;
40   // auxiliary data which is derived from the key for use in computation
41   private Interval interval;
42  
43   public Key(Interval interval)
44   {
45     computeKey(interval);
46   }
47  
48   public double getPoint() { return pt; }
49   public int getLevel() { return level; }
50   public Interval getInterval() { return interval; }
51  
52   /**
53    * return a square envelope containing the argument envelope,
54    * whose extent is a power of two and which is based at a power of 2
55    */
56   public void computeKey(Interval itemInterval)
57   {
58     level = computeLevel(itemInterval);
59     interval = new Interval();
60     computeInterval(level, itemInterval);
61     // MD - would be nice to have a non-iterative form of this algorithm
62     while (! interval.contains(itemInterval)) {
63       level += 1;
64       computeInterval(level, itemInterval);
65     }
66   }
67  
68   private void computeInterval(int level, Interval itemInterval)
69   {
70     double size = DoubleBits.powerOf2(level);
71     //double size = pow2.power(level);
72     pt = Math.floor(itemInterval.getMin() / size) * size;
73     interval.init(pt, pt + size);
74   }
75 }
76