Search in sources :

Example 1 with KdTree

use of uk.me.parabola.util.KdTree in project mkgmap by openstreetmap.

the class KdTreeTest method TestFindNextPoint.

@Test
public void TestFindNextPoint() {
    KdTree<MapPoint> t = new KdTree<>();
    int[][] test = { { 70, 20 }, { 50, 40 }, { 90, 60 }, { 20, 30 }, { 40, 70 }, { 80, 10 }, { -10, 20 }, { -30, -40 } };
    Coord[] testCoords = new Coord[test.length];
    for (int i = 0; i < test.length; i++) {
        MapPoint p = new MapPoint();
        testCoords[i] = new Coord(test[i][0], test[i][1]);
        p.setLocation(testCoords[i]);
        t.add(p);
    }
    // compare naive search result with kd--tree result
    MapPoint toFind = new MapPoint();
    for (int x = -100; x < 100; x++) {
        for (int y = -100; y < 100; y++) {
            Coord co = new Coord(x, y);
            double minDist = Double.MAX_VALUE;
            for (int i = 0; i < testCoords.length; i++) {
                Double dist = testCoords[i].distanceInDegreesSquared(co);
                if (dist < minDist) {
                    minDist = dist;
                }
            }
            toFind.setLocation(co);
            MapPoint next = (MapPoint) t.findNextPoint(toFind);
            double dist = next.getLocation().distanceInDegreesSquared(co);
            double delta = Math.abs(dist - minDist);
            // if this test fails because
            assertFalse("delta should be 0.0: " + delta, delta != 0.0);
        }
    }
}
Also used : Coord(uk.me.parabola.imgfmt.app.Coord) KdTree(uk.me.parabola.util.KdTree) MapPoint(uk.me.parabola.mkgmap.general.MapPoint) MapPoint(uk.me.parabola.mkgmap.general.MapPoint) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 Coord (uk.me.parabola.imgfmt.app.Coord)1 MapPoint (uk.me.parabola.mkgmap.general.MapPoint)1 KdTree (uk.me.parabola.util.KdTree)1