Class S2


  • public final class S2
    extends Object
    • Method Detail

      • posToOrientation

        public static int posToOrientation​(int position)
        Returns an XOR bit mask indicating how the orientation of a child subcell is related to the orientation of its parent cell. The returned value can be XOR'd with the parent cell's orientation to give the orientation of the child cell.
        Parameters:
        position - the position of the subcell in the Hilbert traversal, in the range [0,3].
        Returns:
        a bit mask containing some combination of SWAP_MASK and INVERT_MASK.
        Throws:
        IllegalArgumentException - if position is out of bounds.
      • posToIJ

        public static int posToIJ​(int orientation,
                                  int position)
        Return the IJ-index of the subcell at the given position in the Hilbert curve traversal with the given orientation. This is the inverse of ijToPos(int, int).
        Parameters:
        orientation - the subcell orientation, in the range [0,3].
        position - the position of the subcell in the Hilbert traversal, in the range [0,3].
        Returns:
        the IJ-index where 0->(0,0), 1->(0,1), 2->(1,0), 3->(1,1).
        Throws:
        IllegalArgumentException - if either parameter is out of bounds.
      • ijToPos

        public static final int ijToPos​(int orientation,
                                        int ijIndex)
        Returns the order in which a specified subcell is visited by the Hilbert curve. This is the inverse of posToIJ(int, int).
        Parameters:
        orientation - the subcell orientation, in the range [0,3].
        ijIndex - the subcell index where 0->(0,0), 1->(0,1), 2->(1,0), 3->(1,1).
        Returns:
        the position of the subcell in the Hilbert traversal, in the range [0,3].
        Throws:
        IllegalArgumentException - if either parameter is out of bounds.
      • origin

        public static S2Point origin()
        Return a unique "origin" on the sphere for operations that need a fixed reference point. It should *not* be a point that is commonly used in edge tests in order to avoid triggering code to handle degenerate cases. (This rules out the north and south poles.)
      • isUnitLength

        public static boolean isUnitLength​(S2Point p)
        Return true if the given point is approximately unit length (this is mainly useful for assertions).
      • simpleCrossing

        public static boolean simpleCrossing​(S2Point a,
                                             S2Point b,
                                             S2Point c,
                                             S2Point d)
        Return true if edge AB crosses CD at a point that is interior to both edges. Properties: (1) SimpleCrossing(b,a,c,d) == SimpleCrossing(a,b,c,d) (2) SimpleCrossing(c,d,a,b) == SimpleCrossing(a,b,c,d)
      • robustCrossProd

        public static S2Point robustCrossProd​(S2Point a,
                                              S2Point b)
        Return a vector "c" that is orthogonal to the given unit-length vectors "a" and "b". This function is similar to a.CrossProd(b) except that it does a better job of ensuring orthogonality when "a" is nearly parallel to "b", and it returns a non-zero result even when a == b or a == -b. It satisfies the following properties (RCP == RobustCrossProd): (1) RCP(a,b) != 0 for all a, b (2) RCP(b,a) == -RCP(a,b) unless a == b or a == -b (3) RCP(-a,b) == -RCP(a,b) unless a == b or a == -b (4) RCP(a,-b) == -RCP(a,b) unless a == b or a == -b
      • ortho

        public static S2Point ortho​(S2Point a)
        Return a unit-length vector that is orthogonal to "a". Satisfies Ortho(-a) = -Ortho(a) for all a.
      • girardArea

        public static double girardArea​(S2Point a,
                                        S2Point b,
                                        S2Point c)
        Return the area of the triangle computed using Girard's formula. This is slightly faster than the Area() method above is not accurate for very small triangles.
      • signedArea

        public static double signedArea​(S2Point a,
                                        S2Point b,
                                        S2Point c)
        Like Area(), but returns a positive value for counterclockwise triangles and a negative value otherwise.
      • planarCentroid

        public static S2Point planarCentroid​(S2Point a,
                                             S2Point b,
                                             S2Point c)
        Return the centroid of the planar triangle ABC. This can be normalized to unit length to obtain the "surface centroid" of the corresponding spherical triangle, i.e. the intersection of the three medians. However, note that for large spherical triangles the surface centroid may be nowhere near the intuitive "center" (see example above).
      • trueCentroid

        public static S2Point trueCentroid​(S2Point a,
                                           S2Point b,
                                           S2Point c)
        Returns the true centroid of the spherical triangle ABC multiplied by the signed area of spherical triangle ABC. The reasons for multiplying by the signed area are (1) this is the quantity that needs to be summed to compute the centroid of a union or difference of triangles, and (2) it's actually easier to calculate this way.
      • simpleCCW

        public static boolean simpleCCW​(S2Point a,
                                        S2Point b,
                                        S2Point c)
        Return true if the points A, B, C are strictly counterclockwise. Return false if the points are clockwise or colinear (i.e. if they are all contained on some great circle). Due to numerical errors, situations may arise that are mathematically impossible, e.g. ABC may be considered strictly CCW while BCA is not. However, the implementation guarantees the following: If SimpleCCW(a,b,c), then !SimpleCCW(c,b,a) for all a,b,c. In other words, ABC and CBA are guaranteed not to be both CCW
      • robustCCW

        public static int robustCCW​(S2Point a,
                                    S2Point b,
                                    S2Point c)
        WARNING! This requires arbitrary precision arithmetic to be truly robust. This means that for nearly colinear AB and AC, this function may return the wrong answer.

        Like SimpleCCW(), but returns +1 if the points are counterclockwise and -1 if the points are clockwise. It satisfies the following conditions: (1) RobustCCW(a,b,c) == 0 if and only if a == b, b == c, or c == a (2) RobustCCW(b,c,a) == RobustCCW(a,b,c) for all a,b,c (3) RobustCCW(c,b,a) ==-RobustCCW(a,b,c) for all a,b,c In other words: (1) The result is zero if and only if two points are the same. (2) Rotating the order of the arguments does not affect the result. (3) Exchanging any two arguments inverts the result. This function is essentially like taking the sign of the determinant of a,b,c, except that it has additional logic to make sure that the above properties hold even when the three points are coplanar, and to deal with the limitations of floating-point arithmetic. Note: a, b and c are expected to be of unit length. Otherwise, the results are undefined.

      • robustCCW

        public static int robustCCW​(S2Point a,
                                    S2Point b,
                                    S2Point c,
                                    S2Point aCrossB)
        A more efficient version of RobustCCW that allows the precomputed cross-product of A and B to be specified. Note: a, b and c are expected to be of unit length. Otherwise, the results are undefined
      • orderedCCW

        public static boolean orderedCCW​(S2Point a,
                                         S2Point b,
                                         S2Point c,
                                         S2Point o)
        Return true if the edges OA, OB, and OC are encountered in that order while sweeping CCW around the point O. You can think of this as testing whether A <= B <= C with respect to a continuous CCW ordering around O. Properties:
        1. If orderedCCW(a,b,c,o) && orderedCCW(b,a,c,o), then a == b
        2. If orderedCCW(a,b,c,o) && orderedCCW(a,c,b,o), then b == c
        3. If orderedCCW(a,b,c,o) && orderedCCW(c,b,a,o), then a == b == c
        4. If a == b or b == c, then orderedCCW(a,b,c,o) is true
        5. Otherwise if a == c, then orderedCCW(a,b,c,o) is false
      • angle

        public static double angle​(S2Point a,
                                   S2Point b,
                                   S2Point c)
        Return the angle at the vertex B in the triangle ABC. The return value is always in the range [0, Pi]. The points do not need to be normalized. Ensures that Angle(a,b,c) == Angle(c,b,a) for all a,b,c. The angle is undefined if A or C is diametrically opposite from B, and becomes numerically unstable as the length of edge AB or BC approaches 180 degrees.
      • turnAngle

        public static double turnAngle​(S2Point a,
                                       S2Point b,
                                       S2Point c)
        Return the exterior angle at the vertex B in the triangle ABC. The return value is positive if ABC is counterclockwise and negative otherwise. If you imagine an ant walking from A to B to C, this is the angle that the ant turns at vertex B (positive = left, negative = right). Ensures that TurnAngle(a,b,c) == -TurnAngle(c,b,a) for all a,b,c.
        Parameters:
        a -
        b -
        c -
        Returns:
        the exterior angle at the vertex B in the triangle ABC
      • approxEquals

        public static boolean approxEquals​(S2Point a,
                                           S2Point b,
                                           double maxError)
        Return true if two points are within the given distance of each other (mainly useful for testing).
      • approxEquals

        public static boolean approxEquals​(S2Point a,
                                           S2Point b)
      • approxEquals

        public static boolean approxEquals​(double a,
                                           double b,
                                           double maxError)
      • approxEquals

        public static boolean approxEquals​(double a,
                                           double b)