/src/s2geometry/src/s2/s2wedge_relations.cc
Line | Count | Source |
1 | | // Copyright 2005 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/s2wedge_relations.h" |
19 | | |
20 | | #include "s2/s2point.h" |
21 | | #include "s2/s2predicates.h" |
22 | | |
23 | | namespace S2 { |
24 | | |
25 | | bool WedgeContains( |
26 | | const S2Point& a0, const S2Point& ab1, const S2Point& a2, |
27 | 0 | const S2Point& b0, const S2Point& b2) { |
28 | | // For A to contain B (where each loop interior is defined to be its left |
29 | | // side), the CCW edge order around ab1 must be a2 b2 b0 a0. We split |
30 | | // this test into two parts that test three vertices each. |
31 | 0 | return (s2pred::OrderedCCW(a2, b2, b0, ab1) && |
32 | 0 | s2pred::OrderedCCW(b0, a0, a2, ab1)); |
33 | 0 | } |
34 | | |
35 | | bool WedgeIntersects( |
36 | | const S2Point& a0, const S2Point& ab1, const S2Point& a2, |
37 | 0 | const S2Point& b0, const S2Point& b2) { |
38 | | // For A not to intersect B (where each loop interior is defined to be |
39 | | // its left side), the CCW edge order around ab1 must be a0 b2 b0 a2. |
40 | | // Note that it's important to write these conditions as negatives |
41 | | // (!OrderedCCW(a,b,c,o) rather than Ordered(c,b,a,o)) to get correct |
42 | | // results when two vertices are the same. |
43 | 0 | return !(s2pred::OrderedCCW(a0, b2, b0, ab1) && |
44 | 0 | s2pred::OrderedCCW(b0, a2, a0, ab1)); |
45 | 0 | } |
46 | | |
47 | | WedgeRelation GetWedgeRelation( |
48 | | const S2Point& a0, const S2Point& ab1, const S2Point& a2, |
49 | 0 | const S2Point& b0, const S2Point& b2) { |
50 | | // There are 6 possible edge orderings at a shared vertex (all |
51 | | // of these orderings are circular, i.e. abcd == bcda): |
52 | | // |
53 | | // (1) a2 b2 b0 a0: A contains B |
54 | | // (2) a2 a0 b0 b2: B contains A |
55 | | // (3) a2 a0 b2 b0: A and B are disjoint |
56 | | // (4) a2 b0 a0 b2: A and B intersect in one wedge |
57 | | // (5) a2 b2 a0 b0: A and B intersect in one wedge |
58 | | // (6) a2 b0 b2 a0: A and B intersect in two wedges |
59 | | // |
60 | | // We do not distinguish between 4, 5, and 6. |
61 | | // We pay extra attention when some of the edges overlap. When edges |
62 | | // overlap, several of these orderings can be satisfied, and we take |
63 | | // the most specific. |
64 | 0 | if (a0 == b0 && a2 == b2) return WEDGE_EQUALS; |
65 | | |
66 | 0 | if (s2pred::OrderedCCW(a0, a2, b2, ab1)) { |
67 | | // The cases with this vertex ordering are 1, 5, and 6, |
68 | | // although case 2 is also possible if a2 == b2. |
69 | 0 | if (s2pred::OrderedCCW(b2, b0, a0, ab1)) return WEDGE_PROPERLY_CONTAINS; |
70 | | |
71 | | // We are in case 5 or 6, or case 2 if a2 == b2. |
72 | 0 | return (a2 == b2) ? WEDGE_IS_PROPERLY_CONTAINED : WEDGE_PROPERLY_OVERLAPS; |
73 | 0 | } |
74 | | |
75 | | // We are in case 2, 3, or 4. |
76 | 0 | if (s2pred::OrderedCCW(a0, b0, b2, ab1)) return WEDGE_IS_PROPERLY_CONTAINED; |
77 | 0 | return s2pred::OrderedCCW(a0, b0, a2, ab1) ? |
78 | 0 | WEDGE_IS_DISJOINT : WEDGE_PROPERLY_OVERLAPS; |
79 | 0 | } |
80 | | |
81 | | } // namespace S2 |