aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/helper/LocationTree.java
blob: 3aff3ce33c01eccbf039d2affdc9599be3121dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright 2016 Anton Tananaev (anton@traccar.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.helper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class LocationTree {

    public static class Item {

        private Item left, right;
        private float x, y;
        private String data;

        public Item(float x, float y) {
            this(x, y, null);
        }

        public Item(float x, float y, String data) {
            this.x = x;
            this.y = y;
            this.data = data;
        }

        public String getData() {
            return data;
        }

        private float squaredDistance(Item item) {
            return (x - item.x) * (x - item.x) + (y - item.y) * (y - item.y);
        }

        private float axisSquaredDistance(Item item, int axis) {
            if (axis == 0) {
                return (x - item.x) * (x - item.x);
            } else {
                return (y - item.y) * (y - item.y);
            }
        }

    }

    private Item root;

    private ArrayList<Comparator<Item>> comparators = new ArrayList<>();

    public LocationTree(List<Item> items) {
        comparators.add(new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return Float.compare(o1.x, o2.x);
            }
        });
        comparators.add(new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return Float.compare(o1.y, o2.y);
            }
        });
        root = createTree(items, 0);
    }

    private Item createTree(List<Item> items, int depth) {
        if (items.isEmpty()) {
            return null;
        }
        Collections.sort(items, comparators.get(depth % 2));
        int currentIndex = items.size() / 2;
        Item median = items.get(currentIndex);
        median.left = createTree(new ArrayList<>(items.subList(0, currentIndex)), depth + 1);
        median.right = createTree(new ArrayList<>(items.subList(currentIndex + 1, items.size())), depth + 1);
        return median;
    }

    public Item findNearest(Item search) {
        return findNearest(root, search, 0);
    }

    private Item findNearest(Item current, Item search, int depth) {
        int direction = comparators.get(depth % 2).compare(search, current);

        Item next, other;
        if (direction < 0) {
            next = current.left;
            other = current.right;
        } else {
            next = current.right;
            other = current.left;
        }

        Item best = current;
        if (next != null) {
            best = findNearest(next, search, depth + 1);
        }

        if (current.squaredDistance(search) < best.squaredDistance(search)) {
            best = current;
        }
        if (other != null && current.axisSquaredDistance(search, depth % 2) < best.squaredDistance(search)) {
            Item possibleBest = findNearest(other, search, depth + 1);
            if (possibleBest.squaredDistance(search) < best.squaredDistance(search)) {
                best = possibleBest;
            }
        }

        return best;
    }

}