Debian Codegraph @

src/uk/me/parabola/util/KdTree.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Gerd Petermann
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
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 * A kd-tree (2D) implementation to solve the nearest neighbor problem.
24 * The tree is not explicitly balanced.
25 *
26 * @author Gerd Petermann
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 // the tree root
42 private KdNode root;
43 // number of saved objects
44 private int size;
45
46 // helpers
47 private T nextPoint ;
48 private double minDist;
49 private double maxDist;
50 private Set<T> set;
51
52 /**
53 * create an empty tree
54 */
55 public KdTree() {
56 root = null;
57 }
58
59 public long size()
60 {
61 return size;
62 }
63
64
65 /**
66 * Add a point to the tree.
67 * @param toAdd
68 */
69 public void add(T toAdd) {
70 size++;
71 root = add(toAdd, root, ROOT_NODE_USES_LONGITUDE);
72 }
73
74 /**
75 * Compares the given axis of both points.
76 * @param longitude <code>true</code>: compare longitude; <code>false</code> compare latitude
77 * @param c1 a point
78 * @param c2 another point
79 * @return <code>true</code> the axis value of c1 is smaller than c2;
80 * <code>false</code> the axis value of c1 is equal or larger than c2
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 * Recursive routine to find the right place for inserting a point into the tree.
92 * @param toAdd the point to add
93 * @param tree the subtree root node where to add (maybe <code>null</code>)
94 * @param useLongitude <code>true</code> the tree node uses longitude for comparison;
95 * <code>false</code> the tree node uses latitude for comparison
96 * @return the subtree root node after insertion
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 * Searches for the point that has smallest distance to the given point.
113 * @param p the given point
114 * @return the point with shortest distance to <var>p</var>
115 */
116 public T findNextPoint(Locatable p) {
117 // reset
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 * Searches for the points that have <var>maxDist</var> distance to the given point.
129 * @param p the given point
130 * @param maxDist the allowed distance
131 * @return the points within distance <var>maxDist</var> to <var>p</var>
132 */
133 public Set<T> findClosePoints(Locatable p, double maxDist) {
134 // reset
135 minDist = Double.MAX_VALUE;
136 this.maxDist = Math.pow(maxDist * 360 / Coord.U, 2); // convert maxDist in meter to distanceInDegreesSquared
137 nextPoint = null;
138 set = new LinkedHashSet<>();
139 findNextPoint(p.getLocation(), root, ROOT_NODE_USES_LONGITUDE);
140 return set;
141 }
142
143 /**
144 * Recursive routine to find the closest point. If set is not null, all
145 * elements within the range given by maxDist are collected.
146 * Closest point is in field nextPoint.
147 *
148 * @param p the location of the given point
149 * @param tree the sub tree
150 * @param useLongitude gives the dimension to search in
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 // do we have to search the other part of the tree?
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 // node is within wanted range
177 set.add(node.point);
178 }
179 if (dist < minDist) {
180 nextPoint = node.point;
181 minDist = dist < maxDist ? maxDist : dist;
182 }
183 }
184 }