| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.util; |
| 14 |
|
| 15 |
|
| 16 |
import java.util.LinkedHashSet; |
| 17 |
import java.util.Set; |
| 18 |
|
| 19 |
import uk.me.parabola.imgfmt.app.Coord; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class KdTree <T extends Locatable> { |
| 30 |
private static final boolean ROOT_NODE_USES_LONGITUDE = false; |
| 31 |
|
| 32 |
private class KdNode { |
| 33 |
T point; |
| 34 |
KdNode left; |
| 35 |
KdNode right; |
| 36 |
|
| 37 |
KdNode(T p) { |
| 38 |
point = p; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
private KdNode root; |
| 43 |
|
| 44 |
private int size; |
| 45 |
|
| 46 |
|
| 47 |
private T nextPoint ; |
| 48 |
private double minDist; |
| 49 |
private double maxDist; |
| 50 |
private Set<T> set; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public KdTree() { |
| 56 |
root = null; |
| 57 |
} |
| 58 |
|
| 59 |
public long size() |
| 60 |
{ |
| 61 |
return size; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void add(T toAdd) { |
| 70 |
size++; |
| 71 |
root = add(toAdd, root, ROOT_NODE_USES_LONGITUDE); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private boolean isSmaller(boolean longitude, Coord c1, Coord c2) { |
| 83 |
if (longitude) { |
| 84 |
return c1.getLongitude() < c2.getLongitude(); |
| 85 |
} else { |
| 86 |
return c1.getLatitude() < c2.getLatitude(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private KdNode add(T toAdd, KdNode tree, boolean useLongitude) { |
| 99 |
if (tree == null) { |
| 100 |
tree = new KdNode(toAdd); |
| 101 |
} else { |
| 102 |
if (isSmaller(useLongitude, toAdd.getLocation(), tree.point.getLocation())) { |
| 103 |
tree.left = add(toAdd, tree.left, !useLongitude); |
| 104 |
} else { |
| 105 |
tree.right = add(toAdd, tree.right, !useLongitude); |
| 106 |
} |
| 107 |
} |
| 108 |
return tree; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public T findNextPoint(Locatable p) { |
| 117 |
|
| 118 |
minDist = Double.MAX_VALUE; |
| 119 |
maxDist = -1; |
| 120 |
set = null; |
| 121 |
nextPoint = null; |
| 122 |
|
| 123 |
findNextPoint(p.getLocation(), root, ROOT_NODE_USES_LONGITUDE); |
| 124 |
return nextPoint; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public Set<T> findClosePoints(Locatable p, double maxDist) { |
| 134 |
|
| 135 |
minDist = Double.MAX_VALUE; |
| 136 |
this.maxDist = Math.pow(maxDist * 360 / Coord.U, 2); |
| 137 |
nextPoint = null; |
| 138 |
set = new LinkedHashSet<>(); |
| 139 |
findNextPoint(p.getLocation(), root, ROOT_NODE_USES_LONGITUDE); |
| 140 |
return set; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private void findNextPoint(Coord p, KdNode tree, boolean useLongitude) { |
| 153 |
if (tree == null) |
| 154 |
return; |
| 155 |
|
| 156 |
if (tree.left == null && tree.right == null) { |
| 157 |
processNode(tree, p); |
| 158 |
return; |
| 159 |
} |
| 160 |
boolean smaller = isSmaller(useLongitude, p, tree.point.getLocation()); |
| 161 |
findNextPoint(p, smaller ? tree.left : tree.right, !useLongitude); |
| 162 |
|
| 163 |
processNode(tree, p); |
| 164 |
|
| 165 |
int testLat = useLongitude ? p.getHighPrecLat() : tree.point.getLocation().getHighPrecLat(); |
| 166 |
int testLon = useLongitude ? tree.point.getLocation().getHighPrecLon() : p.getHighPrecLon(); |
| 167 |
Coord test = Coord.makeHighPrecCoord(testLat, testLon); |
| 168 |
if (test.distanceInDegreesSquared(p) < minDist) { |
| 169 |
findNextPoint(p, smaller ? tree.right : tree.left, !useLongitude); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private void processNode(KdNode node, Coord p) { |
| 174 |
double dist = node.point.getLocation().distanceInDegreesSquared(p); |
| 175 |
if (dist <= maxDist && set != null) { |
| 176 |
|
| 177 |
set.add(node.point); |
| 178 |
} |
| 179 |
if (dist < minDist) { |
| 180 |
nextPoint = node.point; |
| 181 |
minDist = dist < maxDist ? maxDist : dist; |
| 182 |
} |
| 183 |
} |
| 184 |
} |