gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

wedge_relations.go (3834B)


      1 // Copyright 2017 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 package s2
     16 
     17 // WedgeRel enumerates the possible relation between two wedges A and B.
     18 type WedgeRel int
     19 
     20 // Define the different possible relationships between two wedges.
     21 //
     22 // Given an edge chain (x0, x1, x2), the wedge at x1 is the region to the
     23 // left of the edges. More precisely, it is the set of all rays from x1x0
     24 // (inclusive) to x1x2 (exclusive) in the *clockwise* direction.
     25 const (
     26 	WedgeEquals              WedgeRel = iota // A and B are equal.
     27 	WedgeProperlyContains                    // A is a strict superset of B.
     28 	WedgeIsProperlyContained                 // A is a strict subset of B.
     29 	WedgeProperlyOverlaps                    // A-B, B-A, and A intersect B are non-empty.
     30 	WedgeIsDisjoint                          // A and B are disjoint.
     31 )
     32 
     33 // WedgeRelation reports the relation between two non-empty wedges
     34 // A=(a0, ab1, a2) and B=(b0, ab1, b2).
     35 func WedgeRelation(a0, ab1, a2, b0, b2 Point) WedgeRel {
     36 	// There are 6 possible edge orderings at a shared vertex (all
     37 	// of these orderings are circular, i.e. abcd == bcda):
     38 	//
     39 	//  (1) a2 b2 b0 a0: A contains B
     40 	//  (2) a2 a0 b0 b2: B contains A
     41 	//  (3) a2 a0 b2 b0: A and B are disjoint
     42 	//  (4) a2 b0 a0 b2: A and B intersect in one wedge
     43 	//  (5) a2 b2 a0 b0: A and B intersect in one wedge
     44 	//  (6) a2 b0 b2 a0: A and B intersect in two wedges
     45 	//
     46 	// We do not distinguish between 4, 5, and 6.
     47 	// We pay extra attention when some of the edges overlap.  When edges
     48 	// overlap, several of these orderings can be satisfied, and we take
     49 	// the most specific.
     50 	if a0 == b0 && a2 == b2 {
     51 		return WedgeEquals
     52 	}
     53 
     54 	// Cases 1, 2, 5, and 6
     55 	if OrderedCCW(a0, a2, b2, ab1) {
     56 		// The cases with this vertex ordering are 1, 5, and 6,
     57 		if OrderedCCW(b2, b0, a0, ab1) {
     58 			return WedgeProperlyContains
     59 		}
     60 
     61 		// We are in case 5 or 6, or case 2 if a2 == b2.
     62 		if a2 == b2 {
     63 			return WedgeIsProperlyContained
     64 		}
     65 		return WedgeProperlyOverlaps
     66 
     67 	}
     68 	// We are in case 2, 3, or 4.
     69 	if OrderedCCW(a0, b0, b2, ab1) {
     70 		return WedgeIsProperlyContained
     71 	}
     72 
     73 	if OrderedCCW(a0, b0, a2, ab1) {
     74 		return WedgeIsDisjoint
     75 	}
     76 	return WedgeProperlyOverlaps
     77 }
     78 
     79 // WedgeContains reports whether non-empty wedge A=(a0, ab1, a2) contains B=(b0, ab1, b2).
     80 // Equivalent to WedgeRelation == WedgeProperlyContains || WedgeEquals.
     81 func WedgeContains(a0, ab1, a2, b0, b2 Point) bool {
     82 	// For A to contain B (where each loop interior is defined to be its left
     83 	// side), the CCW edge order around ab1 must be a2 b2 b0 a0.  We split
     84 	// this test into two parts that test three vertices each.
     85 	return OrderedCCW(a2, b2, b0, ab1) && OrderedCCW(b0, a0, a2, ab1)
     86 }
     87 
     88 // WedgeIntersects reports whether non-empty wedge A=(a0, ab1, a2) intersects B=(b0, ab1, b2).
     89 // Equivalent but faster than WedgeRelation != WedgeIsDisjoint
     90 func WedgeIntersects(a0, ab1, a2, b0, b2 Point) bool {
     91 	// For A not to intersect B (where each loop interior is defined to be
     92 	// its left side), the CCW edge order around ab1 must be a0 b2 b0 a2.
     93 	// Note that it's important to write these conditions as negatives
     94 	// (!OrderedCCW(a,b,c,o) rather than Ordered(c,b,a,o)) to get correct
     95 	// results when two vertices are the same.
     96 	return !(OrderedCCW(a0, b2, b0, ab1) && OrderedCCW(b0, a2, a0, ab1))
     97 }