forked from google/s2geometry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_index.cc
44 lines (38 loc) · 1.52 KB
/
point_index.cc
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
// Copyright 2017 Google Inc. All Rights Reserved.
// Author: [email protected] (Eric Veach)
//
// This example shows how to build and query an in-memory index of points
// using S2PointIndex.
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include "s2/base/commandlineflags.h"
#include "s2/s2earth.h"
#include "s2/s1chord_angle.h"
#include "s2/s2closest_point_query.h"
#include "s2/s2point_index.h"
#include "s2/s2testing.h"
S2_DEFINE_int32(num_index_points, 10000, "Number of points to index");
S2_DEFINE_int32(num_queries, 10000, "Number of queries");
S2_DEFINE_double(query_radius_km, 100, "Query radius in kilometers");
int main(int argc, char **argv) {
// Build an index containing random points anywhere on the Earth.
S2PointIndex<int> index;
for (int i = 0; i < FLAGS_num_index_points; ++i) {
index.Add(S2Testing::RandomPoint(), i);
}
// Create a query to search within the given radius of a target point.
S2ClosestPointQuery<int> query(&index);
query.mutable_options()->set_max_distance(
S1Angle::Radians(S2Earth::KmToRadians(FLAGS_query_radius_km)));
// Repeatedly choose a random target point, and count how many index points
// are within the given radius of that point.
int64_t num_found = 0;
for (int i = 0; i < FLAGS_num_queries; ++i) {
S2ClosestPointQuery<int>::PointTarget target(S2Testing::RandomPoint());
num_found += query.FindClosestPoints(&target).size();
}
std::printf("Found %" PRId64 " points in %d queries\n",
num_found, FLAGS_num_queries);
return 0;
}