func hasCrossingRelation()

in s2/loop.go [1666:1715]


func hasCrossingRelation(a, b *Loop, relation loopRelation) bool {
	// We look for CellID ranges where the indexes of A and B overlap, and
	// then test those edges for crossings.
	ai := newRangeIterator(a.index)
	bi := newRangeIterator(b.index)

	ab := newLoopCrosser(a, b, relation, false) // Tests edges of A against B
	ba := newLoopCrosser(b, a, relation, true)  // Tests edges of B against A

	for !ai.done() || !bi.done() {
		if ai.rangeMax < bi.rangeMin {
			// The A and B cells don't overlap, and A precedes B.
			ai.seekTo(bi)
		} else if bi.rangeMax < ai.rangeMin {
			// The A and B cells don't overlap, and B precedes A.
			bi.seekTo(ai)
		} else {
			// One cell contains the other. Determine which cell is larger.
			abRelation := int64(ai.it.CellID().lsb() - bi.it.CellID().lsb())
			if abRelation > 0 {
				// A's index cell is larger.
				if ab.hasCrossingRelation(ai, bi) {
					return true
				}
			} else if abRelation < 0 {
				// B's index cell is larger.
				if ba.hasCrossingRelation(bi, ai) {
					return true
				}
			} else {
				// The A and B cells are the same. Since the two cells
				// have the same center point P, check whether P satisfies
				// the crossing targets.
				aClipped := ai.it.IndexCell().shapes[0]
				bClipped := bi.it.IndexCell().shapes[0]
				if containsCenterMatches(aClipped, ab.aCrossingTarget) &&
					containsCenterMatches(bClipped, ab.bCrossingTarget) {
					return true
				}
				// Otherwise test all the edge crossings directly.
				if aClipped.numEdges() > 0 && bClipped.numEdges() > 0 && ab.cellCrossesCell(aClipped, bClipped) {
					return true
				}
				ai.next()
				bi.next()
			}
		}
	}
	return false
}