| 1 | // Copyright 2005 Google Inc. All Rights Reserved. |
|---|---|
| 2 | |
| 3 | #include "s2regionunion.h" |
| 4 | |
| 5 | #include "s2cap.h" |
| 6 | #include "s2cell.h" |
| 7 | #include "s2latlngrect.h" |
| 8 | |
| 9 | S2RegionUnion::S2RegionUnion() { } |
| 10 | |
| 11 | S2RegionUnion::S2RegionUnion(vector<S2Region*>* regions) { |
| 12 | Init(regions); |
| 13 | } |
| 14 | |
| 15 | S2RegionUnion::~S2RegionUnion() { |
| 16 | for (int i = 0; i < regions_.size(); ++i) { |
| 17 | delete regions_[i]; |
| 18 | } |
| 19 | regions_.clear(); |
| 20 | } |
| 21 | |
| 22 | void S2RegionUnion::Init(vector<S2Region*>* regions) { |
| 23 | DCHECK(regions_.empty()); |
| 24 | // We copy the vector rather than calling swap() to optimize storage. |
| 25 | regions_ = *regions; |
| 26 | regions->clear(); |
| 27 | } |
| 28 | |
| 29 | S2RegionUnion::S2RegionUnion(S2RegionUnion const* src) |
| 30 | : regions_(src->num_regions()) { |
| 31 | for (int i = 0; i < num_regions(); ++i) { |
| 32 | regions_[i] = src->region(i)->Clone(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void S2RegionUnion::Release(vector<S2Region*>* regions) { |
| 37 | if (regions != NULL) { |
| 38 | regions->insert(regions->end(), regions_.begin(), regions_.end()); |
| 39 | } |
| 40 | regions_.clear(); |
| 41 | } |
| 42 | |
| 43 | void S2RegionUnion::Add(S2Region* region) { |
| 44 | regions_.push_back(region); |
| 45 | } |
| 46 | |
| 47 | S2RegionUnion* S2RegionUnion::Clone() const { |
| 48 | return new S2RegionUnion(this); |
| 49 | } |
| 50 | |
| 51 | S2Cap S2RegionUnion::GetCapBound() const { |
| 52 | // TODO: This could be optimized to return a tighter bound, but doesn't |
| 53 | // seem worth it unless profiling shows otherwise. |
| 54 | return GetRectBound().GetCapBound(); |
| 55 | } |
| 56 | |
| 57 | S2LatLngRect S2RegionUnion::GetRectBound() const { |
| 58 | S2LatLngRect result = S2LatLngRect::Empty(); |
| 59 | for (int i = 0; i < num_regions(); ++i) { |
| 60 | result = result.Union(region(i)->GetRectBound()); |
| 61 | } |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | bool S2RegionUnion::VirtualContainsPoint(S2Point const& p) const { |
| 66 | return Contains(p); // The same as Contains(), just virtual. |
| 67 | } |
| 68 | |
| 69 | bool S2RegionUnion::Contains(S2Cell const& cell) const { |
| 70 | // Note that this method is allowed to return false even if the cell |
| 71 | // is contained by the region. |
| 72 | for (int i = 0; i < num_regions(); ++i) { |
| 73 | if (region(i)->Contains(cell)) return true; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | bool S2RegionUnion::Contains(S2Point const& p) const { |
| 79 | for (int i = 0; i < num_regions(); ++i) { |
| 80 | if (region(i)->VirtualContainsPoint(p)) return true; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | bool S2RegionUnion::MayIntersect(S2Cell const& cell) const { |
| 86 | for (int i = 0; i < num_regions(); ++i) { |
| 87 | if (region(i)->MayIntersect(cell)) return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 |