aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/geo_location.cpp
blob: 437cc2498811a9487bbc22077254b59484ff20c0 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "geo_location.h"

using vespalib::geo::ZCurve;

namespace search::common {

namespace {

uint64_t abs_diff(int32_t a, int32_t b) {
    return (a > b)
        ? (int64_t(a) - int64_t(b))
        : (int64_t(b) - int64_t(a));
}

ZCurve::BoundingBox to_z(GeoLocation::Box box) {
    return ZCurve::BoundingBox(box.x.low, box.x.high,
                               box.y.low, box.y.high);
}

GeoLocation::Box
adjust_bounding_box(GeoLocation::Box orig, GeoLocation::Point point, uint32_t radius, GeoLocation::Aspect x_aspect)
{
    if (radius == GeoLocation::radius_inf) {
        // only happens if GeoLocation is explicitly constructed with "infinite" radius
        return orig;
    }
    uint32_t maxdx = radius;
    if (x_aspect.active()) {
        // x_aspect is a 32-bit fixed-point number in range [0,1]
        // so this implements maxdx = ceil(radius/x_aspect)
        uint64_t maxdx2 = ((static_cast<uint64_t>(radius) << 32) + 0xffffffffu) / x_aspect.multiplier;
        if (maxdx2 >= 0xffffffffu) {
            maxdx = 0xffffffffu;
        } else {
            maxdx = static_cast<uint32_t>(maxdx2);
        }
    }
    // implied limits from radius and point:
    int64_t implied_max_x = int64_t(point.x) + int64_t(maxdx);
    int64_t implied_min_x = int64_t(point.x) - int64_t(maxdx);

    int64_t implied_max_y = int64_t(point.y) + int64_t(radius);
    int64_t implied_min_y = int64_t(point.y) - int64_t(radius);

    int32_t max_x = orig.x.high;
    int32_t min_x = orig.x.low;

    int32_t max_y = orig.y.high;
    int32_t min_y = orig.y.low;

    if (implied_max_x < max_x) max_x = implied_max_x;
    if (implied_min_x > min_x) min_x = implied_min_x;

    if (implied_max_y < max_y) max_y = implied_max_y;
    if (implied_min_y > min_y) min_y = implied_min_y;

    return GeoLocation::Box{GeoLocation::Range{min_x, max_x},
                            GeoLocation::Range{min_y, max_y}};
}

} // namespace <unnamed>

GeoLocation::GeoLocation()
  : has_point(false),
    point{0, 0},
    radius(radius_inf),
    x_aspect(),
    bounding_box(no_box),
    _sq_radius(sq_radius_inf),
    _z_bounding_box(to_z(no_box))
{}

GeoLocation::GeoLocation(Point p)
  : has_point(true),
    point(p),
    radius(radius_inf),
    x_aspect(),
    bounding_box(no_box),
    _sq_radius(sq_radius_inf),
    _z_bounding_box(to_z(no_box))
{}

GeoLocation::GeoLocation(Point p, Aspect xa)
  : has_point(true),
    point(p),
    radius(radius_inf),
    x_aspect(xa),
    bounding_box(no_box),
    _sq_radius(sq_radius_inf),
    _z_bounding_box(to_z(no_box))
{}

GeoLocation::GeoLocation(Point p, uint32_t r)
  : has_point(true),
    point(p),
    radius(r),
    x_aspect(),
    bounding_box(adjust_bounding_box(no_box, p, r, Aspect())),
    _sq_radius(uint64_t(r) * uint64_t(r)),
    _z_bounding_box(to_z(bounding_box))
{}

GeoLocation::GeoLocation(Point p, uint32_t r, Aspect xa)
  : has_point(true),
    point(p),
    radius(r),
    x_aspect(xa),
    bounding_box(adjust_bounding_box(no_box, p, r, xa)),
    _sq_radius(uint64_t(r) * uint64_t(r)),
    _z_bounding_box(to_z(bounding_box))
{}

GeoLocation::GeoLocation(Box b)
  : has_point(false),
    point{0, 0},
    radius(radius_inf),
    x_aspect(),
    bounding_box(b),
    _sq_radius(sq_radius_inf),
    _z_bounding_box(to_z(bounding_box))
{}

GeoLocation::GeoLocation(Box b, Point p)
  : has_point(true),
    point(p),
    radius(radius_inf),
    x_aspect(),
    bounding_box(b),
    _sq_radius(sq_radius_inf),
    _z_bounding_box(to_z(bounding_box))
{}

GeoLocation::GeoLocation(Box b, Point p, Aspect xa)
  : has_point(true),
    point(p),
    radius(radius_inf),
    x_aspect(xa),
    bounding_box(b),
    _sq_radius(sq_radius_inf),
    _z_bounding_box(to_z(bounding_box))
{}

GeoLocation::GeoLocation(Box b, Point p, uint32_t r)
  : has_point(true),
    point(p),
    radius(r),
    x_aspect(),
    bounding_box(adjust_bounding_box(b, p, r, Aspect())),
    _sq_radius(uint64_t(r) * uint64_t(r)),
    _z_bounding_box(to_z(bounding_box))
{}

GeoLocation::GeoLocation(Box b, Point p, uint32_t r, Aspect xa)
  : has_point(true),
    point(p),
    radius(r),
    x_aspect(xa),
    bounding_box(adjust_bounding_box(b, p, r, xa)),
    _sq_radius(uint64_t(r) * uint64_t(r)),
    _z_bounding_box(to_z(bounding_box))
{}

uint64_t GeoLocation::sq_distance_to(Point p) const {
    if (has_point) {
        uint64_t dx = abs_diff(p.x, point.x);
        if (x_aspect.active()) {
            // x_aspect is a 32-bit fixed-point number in range [0,1]
            // this implements dx = (dx * x_aspect)
            dx = (dx * x_aspect.multiplier) >> 32;
        }
        uint64_t dy = abs_diff(p.y, point.y);
        return dx*dx + dy*dy;
    }
    return 0;
}

bool GeoLocation::inside_limit(Point p) const {
    if (p.x < bounding_box.x.low) return false;
    if (p.x > bounding_box.x.high) return false;

    if (p.y < bounding_box.y.low) return false;
    if (p.y > bounding_box.y.high) return false;

    uint64_t sq_dist = sq_distance_to(p);
    return sq_dist <= _sq_radius;
}

} // namespace search::common