summaryrefslogtreecommitdiff
path: root/src/third_party/s2/s2regioncoverer.cc
blob: 3c3337e27c74c1e3abc101e039d48e6383745d01 (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2005 Google Inc. All Rights Reserved.

#include "s2regioncoverer.h"

#ifndef OS_WINDOWS
#include <pthread.h>
#endif

#include <algorithm>
using std::min;
using std::max;
using std::swap;
using std::reverse;

#include <functional>
using std::less;

#include <queue>
using std::priority_queue;

#include <vector>
using std::vector;


#include "base/logging.h"
#include "s2.h"
#include "s2cap.h"
#include "s2cellunion.h"
#include "mongo/base/init.h"

// Define storage for header file constants (the values are not needed here).
int const S2RegionCoverer::kDefaultMaxCells = 8;

// We define our own own comparison function on QueueEntries in order to
// make the results deterministic.  Using the default less<QueueEntry>,
// entries of equal priority would be sorted according to the memory address
// of the candidate.

struct S2RegionCoverer::CompareQueueEntries : public less<QueueEntry> {
  bool operator()(QueueEntry const& x, QueueEntry const& y) {
    return x.first < y.first;
  }
};

static S2Cell face_cells[6];

static void Init() {
  for (int face = 0; face < 6; ++face) {
    face_cells[face] = S2Cell::FromFacePosLevel(face, 0, 0);
  }
}

MONGO_INITIALIZER_WITH_PREREQUISITES(S2RegionCovererInit, ("S2CellIdInit"))(mongo::InitializerContext *context) {
    Init();
    return mongo::Status::OK();
}

S2RegionCoverer::S2RegionCoverer() :
  min_level_(0),
  max_level_(S2CellId::kMaxLevel),
  level_mod_(1),
  max_cells_(kDefaultMaxCells),
  region_(NULL),
  result_(new vector<S2CellId>),
  pq_(new CandidateQueue) {
}

S2RegionCoverer::~S2RegionCoverer() {
  // Need to declare explicitly because of scoped pointers.
}

void S2RegionCoverer::set_min_level(int min_level) {
  DCHECK_GE(min_level, 0);
  DCHECK_LE(min_level, S2CellId::kMaxLevel);
  min_level_ = max(0, min(S2CellId::kMaxLevel, min_level));
}

void S2RegionCoverer::set_max_level(int max_level) {
  DCHECK_GE(max_level, 0);
  DCHECK_LE(max_level, S2CellId::kMaxLevel);
  max_level_ = max(0, min(S2CellId::kMaxLevel, max_level));
}

void S2RegionCoverer::set_level_mod(int level_mod) {
  DCHECK_GE(level_mod, 1);
  DCHECK_LE(level_mod, 3);
  level_mod_ = max(1, min(3, level_mod));
}

void S2RegionCoverer::set_max_cells(int max_cells) {
  max_cells_ = max_cells;
}

S2RegionCoverer::Candidate* S2RegionCoverer::NewCandidate(S2Cell const& cell) {
  if (!region_->MayIntersect(cell)) return NULL;

  bool is_terminal = false;
  size_t size = sizeof(Candidate);
  if (cell.level() >= min_level_) {
    if (interior_covering_) {
      if (region_->Contains(cell)) {
        is_terminal = true;
      } else if (cell.level() + level_mod_ > max_level_) {
        return NULL;
      }
    } else {
      if (cell.level() + level_mod_ > max_level_ || region_->Contains(cell)) {
        is_terminal = true;
      }
    }
  }
  if (!is_terminal) {
    size += sizeof(Candidate*) << max_children_shift();
  }
  void* candidateStorage = malloc(size);
  memset(candidateStorage, 0, size);
  Candidate* candidate = new(candidateStorage) Candidate;
  candidate->cell = cell;
  candidate->is_terminal = is_terminal;
  ++candidates_created_counter_;
  return candidate;
}

void S2RegionCoverer::DeleteCandidate(Candidate* candidate,
                                      bool delete_children) {
  if (delete_children) {
    for (int i = 0; i < candidate->num_children; ++i)
      DeleteCandidate(candidate->children[i], true);
  }
  free(candidate);
}

int S2RegionCoverer::ExpandChildren(Candidate* candidate,
                                    S2Cell const& cell, int num_levels) {
  num_levels--;
  S2Cell child_cells[4];
  cell.Subdivide(child_cells);
  int num_terminals = 0;
  for (int i = 0; i < 4; ++i) {
    if (num_levels > 0) {
      if (region_->MayIntersect(child_cells[i])) {
        num_terminals += ExpandChildren(candidate, child_cells[i], num_levels);
      }
      continue;
    }
    Candidate* child = NewCandidate(child_cells[i]);
    if (child) {
      candidate->children[candidate->num_children++] = child;
      if (child->is_terminal) ++num_terminals;
    }
  }
  return num_terminals;
}

void S2RegionCoverer::AddCandidate(Candidate* candidate) {
  if (candidate == NULL) return;

  if (candidate->is_terminal) {
    result_->push_back(candidate->cell.id());
    DeleteCandidate(candidate, true);
    return;
  }
  DCHECK_EQ(0, candidate->num_children);

  // Expand one level at a time until we hit min_level_ to ensure that
  // we don't skip over it.
  int num_levels = (candidate->cell.level() < min_level_) ? 1 : level_mod_;
  int num_terminals = ExpandChildren(candidate, candidate->cell, num_levels);

  if (candidate->num_children == 0) {
    DeleteCandidate(candidate, false);

  } else if (!interior_covering_ &&
             num_terminals == 1 << max_children_shift() &&
             candidate->cell.level() >= min_level_) {
    // Optimization: add the parent cell rather than all of its children.
    // We can't do this for interior coverings, since the children just
    // intersect the region, but may not be contained by it - we need to
    // subdivide them further.
    candidate->is_terminal = true;
    AddCandidate(candidate);

  } else {
    // We negate the priority so that smaller absolute priorities are returned
    // first.  The heuristic is designed to refine the largest cells first,
    // since those are where we have the largest potential gain.  Among cells
    // at the same level, we prefer the cells with the smallest number of
    // intersecting children.  Finally, we prefer cells that have the smallest
    // number of children that cannot be refined any further.
    int priority = -((((candidate->cell.level() << max_children_shift())
                       + candidate->num_children) << max_children_shift())
                     + num_terminals);
    pq_->push(make_pair(priority, candidate));
    VLOG(2) << "Push: " << candidate->cell.id() << " (" << priority << ") ";
  }
}

void S2RegionCoverer::GetInitialCandidates() {
  // Optimization: if at least 4 cells are desired (the normal case),
  // start with a 4-cell covering of the region's bounding cap.  This
  // lets us skip quite a few levels of refinement when the region to
  // be covered is relatively small.
  if (max_cells() >= 4) {
    // Find the maximum level such that the bounding cap contains at most one
    // cell vertex at that level.
    S2Cap cap = region_->GetCapBound();
    int level = min(S2::kMinWidth.GetMaxLevel(2 * cap.angle().radians()),
                    min(max_level(), S2CellId::kMaxLevel - 1));
    if (level_mod() > 1 && level > min_level()) {
      level -= (level - min_level()) % level_mod();
    }
    // We don't bother trying to optimize the level == 0 case, since more than
    // four face cells may be required.
    if (level > 0) {
      // Find the leaf cell containing the cap axis, and determine which
      // subcell of the parent cell contains it.
      vector<S2CellId> base;
      base.reserve(4);
      S2CellId id = S2CellId::FromPoint(cap.axis());
      id.AppendVertexNeighbors(level, &base);
      for (size_t i = 0; i < base.size(); ++i) {
        AddCandidate(NewCandidate(S2Cell(base[i])));
      }
      return;
    }
  }
  // Default: start with all six cube faces.
  for (size_t face = 0; face < 6; ++face) {
    AddCandidate(NewCandidate(face_cells[face]));
  }
}

void S2RegionCoverer::GetCoveringInternal(S2Region const& region) {
  // Strategy: Start with the 6 faces of the cube.  Discard any
  // that do not intersect the shape.  Then repeatedly choose the
  // largest cell that intersects the shape and subdivide it.
  //
  // result_ contains the cells that will be part of the output, while pq_
  // contains cells that we may still subdivide further.  Cells that are
  // entirely contained within the region are immediately added to the output,
  // while cells that do not intersect the region are immediately discarded.
  // Therefore pq_ only contains cells that partially intersect the region.
  // Candidates are prioritized first according to cell size (larger cells
  // first), then by the number of intersecting children they have (fewest
  // children first), and then by the number of fully contained children
  // (fewest children first).

  DCHECK(pq_->empty());
  DCHECK(result_->empty());
  region_ = &region;
  candidates_created_counter_ = 0;

  GetInitialCandidates();
  while (!pq_->empty() &&
         (!interior_covering_ || result_->size() < (size_t)max_cells_)) {
    Candidate* candidate = pq_->top().second;
    pq_->pop();
    VLOG(2) << "Pop: " << candidate->cell.id();
    if (candidate->cell.level() < min_level_ ||
        candidate->num_children == 1 ||
        (int)result_->size() + (int)(interior_covering_ ? 0 : (int)pq_->size()) +
            candidate->num_children <= max_cells_) {
      // Expand this candidate into its children.
      for (int i = 0; i < candidate->num_children; ++i) {
        AddCandidate(candidate->children[i]);
      }
      DeleteCandidate(candidate, false);
    } else if (interior_covering_) {
      DeleteCandidate(candidate, true);
    } else {
      candidate->is_terminal = true;
      AddCandidate(candidate);
    }
  }
  VLOG(2) << "Created " << result_->size() << " cells, " <<
      candidates_created_counter_ << " candidates created, " <<
      pq_->size() << " left";
  while (!pq_->empty()) {
    DeleteCandidate(pq_->top().second, true);
    pq_->pop();
  }
  region_ = NULL;
}

void S2RegionCoverer::GetCovering(S2Region const& region,
                                  vector<S2CellId>* covering) {

  // Rather than just returning the raw list of cell ids generated by
  // GetCoveringInternal(), we construct a cell union and then denormalize it.
  // This has the effect of replacing four child cells with their parent
  // whenever this does not violate the covering parameters specified
  // (min_level, level_mod, etc).  This strategy significantly reduces the
  // number of cells returned in many cases, and it is cheap compared to
  // computing the covering in the first place.

  S2CellUnion tmp;
  GetCellUnion(region, &tmp);
  tmp.Denormalize(min_level(), level_mod(), covering);
}

void S2RegionCoverer::GetInteriorCovering(S2Region const& region,
                                          vector<S2CellId>* interior) {
  S2CellUnion tmp;
  GetInteriorCellUnion(region, &tmp);
  tmp.Denormalize(min_level(), level_mod(), interior);
}

void S2RegionCoverer::GetCellUnion(S2Region const& region,
                                   S2CellUnion* covering) {
  interior_covering_ = false;
  GetCoveringInternal(region);
  covering->InitSwap(result_.get());
}

void S2RegionCoverer::GetInteriorCellUnion(S2Region const& region,
                                           S2CellUnion* interior) {
  interior_covering_ = true;
  GetCoveringInternal(region);
  interior->InitSwap(result_.get());
}

void S2RegionCoverer::FloodFill(
    S2Region const& region, S2CellId const& start, vector<S2CellId>* output) {
  hash_set<S2CellId> all;
  vector<S2CellId> frontier;
  output->clear();
  all.insert(start);
  frontier.push_back(start);
  while (!frontier.empty()) {
    S2CellId id = frontier.back();
    frontier.pop_back();
    if (!region.MayIntersect(S2Cell(id))) continue;
    output->push_back(id);

    S2CellId neighbors[4];
    id.GetEdgeNeighbors(neighbors);
    for (int edge = 0; edge < 4; ++edge) {
      S2CellId nbr = neighbors[edge];
      if (all.insert(nbr).second) {
        frontier.push_back(nbr);
      }
    }
  }
}

void S2RegionCoverer::GetSimpleCovering(
    S2Region const& region, S2Point const& start,
    int level, vector<S2CellId>* output) {
  return FloodFill(region, S2CellId::FromPoint(start).parent(level), output);
}