summaryrefslogtreecommitdiff
path: root/src/third_party/s2/s2regionunion.cc
blob: a3e2e5b228bc43f70511eaea346f7382a36a9056 (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
// Copyright 2005 Google Inc. All Rights Reserved.

#include "s2regionunion.h"

#include "s2cap.h"
#include "s2cell.h"
#include "s2latlngrect.h"

S2RegionUnion::S2RegionUnion() { }

S2RegionUnion::S2RegionUnion(vector<S2Region*>* regions) {
  Init(regions);
}

S2RegionUnion::~S2RegionUnion() {
    /*
  for (size_t i = 0; i < regions_.size(); ++i) {
    delete regions_[i];
  }
  regions_.clear();
  */
}

void S2RegionUnion::Init(vector<S2Region*>* regions) {
  DCHECK(regions_.empty());
  // We copy the vector rather than calling swap() to optimize storage.
  regions_ = *regions;
  regions->clear();
}

S2RegionUnion::S2RegionUnion(S2RegionUnion const* src)
  : regions_(src->num_regions()) {
  for (int i = 0; i < num_regions(); ++i) {
    regions_[i] = src->region(i)->Clone();
  }
}

void S2RegionUnion::Release(vector<S2Region*>* regions) {
  if (regions != NULL) {
    regions->insert(regions->end(), regions_.begin(), regions_.end());
  }
  regions_.clear();
}

void S2RegionUnion::Add(S2Region* region) {
  regions_.push_back(region);
}

S2RegionUnion* S2RegionUnion::Clone() const {
  return new S2RegionUnion(this);
}

S2Cap S2RegionUnion::GetCapBound() const {
  // TODO: This could be optimized to return a tighter bound, but doesn't
  // seem worth it unless profiling shows otherwise.
  return GetRectBound().GetCapBound();
}

S2LatLngRect S2RegionUnion::GetRectBound() const {
  S2LatLngRect result = S2LatLngRect::Empty();
  for (int i = 0; i < num_regions(); ++i) {
    result = result.Union(region(i)->GetRectBound());
  }
  return result;
}

bool S2RegionUnion::VirtualContainsPoint(S2Point const& p) const {
  return Contains(p);  // The same as Contains(), just virtual.
}

bool S2RegionUnion::Contains(S2Cell const& cell) const {
  // Note that this method is allowed to return false even if the cell
  // is contained by the region.
  for (int i = 0; i < num_regions(); ++i) {
    if (region(i)->Contains(cell)) return true;
  }
  return false;
}

bool S2RegionUnion::Contains(S2Point const& p) const {
  for (int i = 0; i < num_regions(); ++i) {
    if (region(i)->VirtualContainsPoint(p)) return true;
  }
  return false;
}

bool S2RegionUnion::MayIntersect(S2Cell const& cell) const {
  for (int i = 0; i < num_regions(); ++i) {
    if (region(i)->MayIntersect(cell)) return true;
  }
  return false;
}