/src/s2geometry/src/s2/s2closest_edge_query.cc
Line | Count | Source |
1 | | // Copyright 2013 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS-IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | |
16 | | // Author: ericv@google.com (Eric Veach) |
17 | | |
18 | | #include "s2/s2closest_edge_query.h" |
19 | | |
20 | | #include "s2/s1angle.h" |
21 | | #include "s2/s1chord_angle.h" |
22 | | #include "s2/s2closest_edge_query_base.h" |
23 | | #include "s2/s2edge_distances.h" |
24 | | |
25 | | void S2ClosestEdgeQuery::Options::set_conservative_max_distance( |
26 | 0 | S1ChordAngle max_distance) { |
27 | 0 | set_max_distance(Distance(max_distance.PlusError( |
28 | 0 | S2::GetUpdateMinDistanceMaxError(max_distance)).Successor())); |
29 | 0 | } |
30 | | |
31 | | void S2ClosestEdgeQuery::Options::set_conservative_max_distance( |
32 | 0 | S1Angle max_distance) { |
33 | 0 | set_conservative_max_distance(S1ChordAngle(max_distance)); |
34 | 0 | } |
35 | | |
36 | 0 | int S2ClosestEdgeQuery::PointTarget::max_brute_force_index_size() const { |
37 | | // Using BM_FindClosest (which finds the single closest edge), the |
38 | | // break-even points are approximately 80, 100, and 250 edges for point |
39 | | // cloud, fractal, and regular loop geometry respectively. |
40 | 0 | return 120; |
41 | 0 | } |
42 | | |
43 | 0 | int S2ClosestEdgeQuery::EdgeTarget::max_brute_force_index_size() const { |
44 | | // Using BM_FindClosestToEdge (which finds the single closest edge), the |
45 | | // break-even points are approximately 40, 50, and 100 edges for point |
46 | | // cloud, fractal, and regular loop geometry respectively. |
47 | 0 | return 60; |
48 | 0 | } |
49 | | |
50 | 0 | int S2ClosestEdgeQuery::CellTarget::max_brute_force_index_size() const { |
51 | | // Using BM_FindClosestToCell (which finds the single closest edge), the |
52 | | // break-even points are approximately 20, 25, and 40 edges for point cloud, |
53 | | // fractal, and regular loop geometry respectively. |
54 | 0 | return 30; |
55 | 0 | } |
56 | | |
57 | 0 | int S2ClosestEdgeQuery::ShapeIndexTarget::max_brute_force_index_size() const { |
58 | | // For BM_FindClosestToSameSizeAbuttingIndex (which uses two nearby indexes |
59 | | // with similar edge counts), the break-even points are approximately 20, |
60 | | // 30, and 40 edges for point cloud, fractal, and regular loop geometry |
61 | | // respectively. |
62 | 0 | return 25; |
63 | 0 | } |
64 | | |
65 | 0 | S2ClosestEdgeQuery::S2ClosestEdgeQuery() { |
66 | | // Prevent inline constructor bloat by defining here. |
67 | 0 | } |
68 | | |
69 | 0 | S2ClosestEdgeQuery::~S2ClosestEdgeQuery() { |
70 | | // Prevent inline destructor bloat by defining here. |
71 | 0 | } |
72 | | |
73 | | bool S2ClosestEdgeQuery::IsDistanceLess(Target* target, S1ChordAngle limit, |
74 | 0 | ShapeFilter filter) { |
75 | 0 | static_assert(sizeof(Options) <= 32, "Consider not copying Options here"); |
76 | 0 | Options tmp_options = options_; |
77 | 0 | tmp_options.set_max_results(1); |
78 | 0 | tmp_options.set_max_distance(limit); |
79 | 0 | tmp_options.set_max_error(S1ChordAngle::Straight()); |
80 | 0 | return !base_.FindClosestEdge(target, tmp_options, filter).is_empty(); |
81 | 0 | } |
82 | | |
83 | | bool S2ClosestEdgeQuery::IsDistanceLessOrEqual(Target* target, |
84 | | S1ChordAngle limit, |
85 | 0 | ShapeFilter filter) { |
86 | 0 | static_assert(sizeof(Options) <= 32, "Consider not copying Options here"); |
87 | 0 | Options tmp_options = options_; |
88 | 0 | tmp_options.set_max_results(1); |
89 | 0 | tmp_options.set_inclusive_max_distance(limit); |
90 | 0 | tmp_options.set_max_error(S1ChordAngle::Straight()); |
91 | 0 | return !base_.FindClosestEdge(target, tmp_options, filter).is_empty(); |
92 | 0 | } |
93 | | |
94 | | bool S2ClosestEdgeQuery::IsConservativeDistanceLessOrEqual(Target* target, |
95 | | S1ChordAngle limit, |
96 | 0 | ShapeFilter filter) { |
97 | 0 | static_assert(sizeof(Options) <= 32, "Consider not copying Options here"); |
98 | 0 | Options tmp_options = options_; |
99 | 0 | tmp_options.set_max_results(1); |
100 | 0 | tmp_options.set_conservative_max_distance(limit); |
101 | 0 | tmp_options.set_max_error(S1ChordAngle::Straight()); |
102 | 0 | return !base_.FindClosestEdge(target, tmp_options, filter).is_empty(); |
103 | 0 | } |
104 | | |
105 | | void S2ClosestEdgeQuery::VisitClosestShapes(Target* target, Options options, |
106 | 0 | ResultVisitor visitor, const ShapeFilter filter) { |
107 | | // Often we'll see the same shape's edges consecutively, caching the last |
108 | | // accepted shape id will prevent us from having to hit the hash set most of |
109 | | // the time. |
110 | 0 | int last_shape = -1; |
111 | 0 | absl::flat_hash_set<int> seen_shapes; |
112 | 0 | VisitClosestEdges(target, options, |
113 | 0 | [&](const Result& result) { |
114 | 0 | const int shape_id = result.shape_id(); |
115 | 0 | if (shape_id != last_shape && seen_shapes.insert(shape_id).second) { |
116 | 0 | last_shape = shape_id; |
117 | 0 | return visitor(result); |
118 | 0 | } |
119 | 0 | return true; |
120 | 0 | }, |
121 | 0 | filter); |
122 | 0 | } |