in s2/edge_crosser.go [163:227]
func (e *EdgeCrosser) crossingSign(d Point, bda Direction) Crossing {
// Compute the actual result, and then save the current vertex D as the next
// vertex C, and save the orientation of the next triangle ACB (which is
// opposite to the current triangle BDA).
defer func() {
e.c = d
e.acb = -bda
}()
// At this point, a very common situation is that A,B,C,D are four points on
// a line such that AB does not overlap CD. (For example, this happens when
// a line or curve is sampled finely, or when geometry is constructed by
// computing the union of S2CellIds.) Most of the time, we can determine
// that AB and CD do not intersect using the two outward-facing
// tangents at A and B (parallel to AB) and testing whether AB and CD are on
// opposite sides of the plane perpendicular to one of these tangents. This
// is moderately expensive but still much cheaper than expensiveSign.
// The error in RobustCrossProd is insignificant. The maximum error in
// the call to CrossProd (i.e., the maximum norm of the error vector) is
// (0.5 + 1/sqrt(3)) * dblEpsilon. The maximum error in each call to
// DotProd below is dblEpsilon. (There is also a small relative error
// term that is insignificant because we are comparing the result against a
// constant that is very close to zero.)
maxError := (1.5 + 1/math.Sqrt(3)) * dblEpsilon
if (e.c.Dot(e.aTangent.Vector) > maxError && d.Dot(e.aTangent.Vector) > maxError) || (e.c.Dot(e.bTangent.Vector) > maxError && d.Dot(e.bTangent.Vector) > maxError) {
return DoNotCross
}
// Otherwise, eliminate the cases where two vertices from different edges are
// equal. (These cases could be handled in the code below, but we would rather
// avoid calling ExpensiveSign if possible.)
if e.a == e.c || e.a == d || e.b == e.c || e.b == d {
return MaybeCross
}
// Eliminate the cases where an input edge is degenerate. (Note that in
// most cases, if CD is degenerate then this method is not even called
// because acb and bda have different signs.)
if e.a == e.b || e.c == d {
return DoNotCross
}
// Otherwise it's time to break out the big guns.
if e.acb == Indeterminate {
e.acb = -expensiveSign(e.a, e.b, e.c)
}
if bda == Indeterminate {
bda = expensiveSign(e.a, e.b, d)
}
if bda != e.acb {
return DoNotCross
}
cbd := -RobustSign(e.c, d, e.b)
if cbd != e.acb {
return DoNotCross
}
dac := RobustSign(e.c, d, e.a)
if dac != e.acb {
return DoNotCross
}
return Cross
}