1 | // Copyright 2006 Google Inc. All Rights Reserved. |
---|---|
2 | |
3 | #include "s2regionintersection.h" |
4 | |
5 | #include "s2cap.h" |
6 | #include "s2cell.h" |
7 | #include "s2latlngrect.h" |
8 | |
9 | S2RegionIntersection::S2RegionIntersection() { } |
10 | |
11 | S2RegionIntersection::S2RegionIntersection(vector<S2Region*>* regions) { |
12 | Init(regions); |
13 | } |
14 | |
15 | S2RegionIntersection::~S2RegionIntersection() { |
16 | for (int i = 0; i < regions_.size(); ++i) { |
17 | delete regions_[i]; |
18 | } |
19 | regions_.clear(); |
20 | } |
21 | |
22 | void S2RegionIntersection::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 | S2RegionIntersection::S2RegionIntersection(S2RegionIntersection 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 S2RegionIntersection::Release(vector<S2Region*>* regions) { |
37 | if (regions != NULL) { |
38 | regions->insert(regions->end(), regions_.begin(), regions_.end()); |
39 | } |
40 | regions_.clear(); |
41 | } |
42 | |
43 | S2RegionIntersection* S2RegionIntersection::Clone() const { |
44 | return new S2RegionIntersection(this); |
45 | } |
46 | |
47 | S2Cap S2RegionIntersection::GetCapBound() const { |
48 | // TODO: This could be optimized to return a tighter bound, but doesn't |
49 | // seem worth it unless profiling shows otherwise. |
50 | return GetRectBound().GetCapBound(); |
51 | } |
52 | |
53 | S2LatLngRect S2RegionIntersection::GetRectBound() const { |
54 | S2LatLngRect result = S2LatLngRect::Full(); |
55 | for (int i = 0; i < num_regions(); ++i) { |
56 | result = result.Intersection(region(i)->GetRectBound()); |
57 | } |
58 | return result; |
59 | } |
60 | |
61 | bool S2RegionIntersection::VirtualContainsPoint(S2Point const& p) const { |
62 | return Contains(p); // The same as Contains(), just virtual. |
63 | } |
64 | |
65 | bool S2RegionIntersection::Contains(S2Cell const& cell) const { |
66 | for (int i = 0; i < num_regions(); ++i) { |
67 | if (!region(i)->Contains(cell)) return false; |
68 | } |
69 | return true; |
70 | } |
71 | |
72 | bool S2RegionIntersection::Contains(S2Point const& p) const { |
73 | for (int i = 0; i < num_regions(); ++i) { |
74 | if (!region(i)->VirtualContainsPoint(p)) return false; |
75 | } |
76 | return true; |
77 | } |
78 | |
79 | bool S2RegionIntersection::MayIntersect(S2Cell const& cell) const { |
80 | for (int i = 0; i < num_regions(); ++i) { |
81 | if (!region(i)->MayIntersect(cell)) return false; |
82 | } |
83 | return true; |
84 | } |
85 |