Package com.google.common.geometry
Class S2ShapeMeasures
- java.lang.Object
-
- com.google.common.geometry.S2ShapeMeasures
-
@GwtCompatible final class S2ShapeMeasures extends java.lang.Object
Defines various angle and area measures forS2Shape
objects. Unlike the built-inS2Polygon
andS2Polyline
methods, these methods allow the underlying data to be represented arbitrarily.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interface
S2ShapeMeasures.BiConsumer<T,U>
A consumer which accepts two arguments.private static interface
S2ShapeMeasures.IntFunction<T>
A function which accepts an int.(package private) static class
S2ShapeMeasures.LoopOrder
Represents a cyclic ordering of the loop vertices, starting at the index "first" and proceeding in direction "dir" (either +1 or -1).private static class
S2ShapeMeasures.LoopOrderComparator
private static class
S2ShapeMeasures.MutableDouble
Wraps a mutable primitive double.
-
Constructor Summary
Constructors Modifier Constructor Description private
S2ShapeMeasures()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
area(S2Shape shape)
For shapes of dimension 2, returns the area of the shape on the unit sphere.(package private) static S2ShapeMeasures.LoopOrder
canonicalLoopOrder(java.util.List<S2Point> loop)
Returns an index "first" and a direction "dir" such that the vertex sequence (first, first + dir, ..., first + (n - 1) * dir) does not change when the loop vertex order is rotated or reversed.static S2Point
centroid(S2Shape shape)
Returns the centroid of shape multiplied by the measure of shape.private static void
forEachChainEdge(S2Shape shape, int chainId, S2ShapeMeasures.BiConsumer<S2Point,S2Point> edgeConsumer)
Passes each edge (a, b) in the chain of shape at index chainId to edgeConsumer.static S1Angle
length(S2Shape shape)
Returns the sum of all polyline lengths on the unit sphere for shapes of dimension 1, orS1Angle.ZERO
otherwise.(package private) static double
loopArea(S2Shape shape, int chainId)
Returns the area of the loop interior, i.e.(package private) static double
loopArea(java.util.List<S2Point> loop)
Same asloopArea(S2Shape, int)
, but takes a loop as a list of vertices.(package private) static S2Point
loopCentroid(S2Shape shape, int chainId)
Returns the true centroid of the loop multiplied by the area of the loop.(package private) static S1Angle
loopPerimeter(S2Shape shape, int chainId)
Returns the perimeter of the loop, orS1Angle.ZERO
if the loop has 0 or 1 vertices.static S1Angle
perimeter(S2Shape shape)
Returns the sum of all loop perimeters on the unit sphere for shapes of dimension 2, orS1Angle.ZERO
otherwise.(package private) static S2Point
polylineCentroid(S2Shape shape, int chainId)
Returns the true centroid of the polyline multiplied by the length of the polyline.(package private) static S1Angle
polylineLength(S2Shape shape, int chainId)
Returns the length of the polyline, orS1Angle.ZERO
if the polyline has fewer than two vertices.(package private) static java.util.List<S2Point>
pruneDegeneracies(java.util.List<S2Point> input)
Returns a new loop obtained by removing all degeneracies from "input".private static double
signedLoopArea(S2Shape shape, int chainId)
Returns the area of the loop interior, i.e.private static double
signedLoopArea(java.util.List<S2Point> loop)
Same assignedLoopArea(S2Shape, int)
, but takes a loop as a list of vertices.(package private) static double
turningAngle(S2Shape shape, int chainId)
(package private) static double
turningAngle(java.util.List<S2Point> loop)
Returns the geodesic curvature of the loop, defined as the sum of the turn angles at each vertex (seeS2.turnAngle(S2Point, S2Point, S2Point)
).private static java.util.List<S2Point>
vertices(S2Shape shape, int chainId)
-
-
-
Method Detail
-
length
public static S1Angle length(S2Shape shape)
Returns the sum of all polyline lengths on the unit sphere for shapes of dimension 1, orS1Angle.ZERO
otherwise. Seeperimeter(S2Shape)
for shapes of dimension 2.See
S2ShapeIndexMeasures.length(S2ShapeIndex)
for more info.
-
polylineLength
static S1Angle polylineLength(S2Shape shape, int chainId)
Returns the length of the polyline, orS1Angle.ZERO
if the polyline has fewer than two vertices.
-
perimeter
public static S1Angle perimeter(S2Shape shape)
Returns the sum of all loop perimeters on the unit sphere for shapes of dimension 2, orS1Angle.ZERO
otherwise. Seelength(S2Shape)
for shapes of dimension 1.
-
loopPerimeter
static S1Angle loopPerimeter(S2Shape shape, int chainId)
Returns the perimeter of the loop, orS1Angle.ZERO
if the loop has 0 or 1 vertices.
-
area
public static double area(S2Shape shape)
For shapes of dimension 2, returns the area of the shape on the unit sphere. The result is between 0 and 4*Pi steradians. Otherwise returns zero. This method has good relative accuracy for both very large and very small regions.
-
loopArea
static double loopArea(S2Shape shape, int chainId)
Returns the area of the loop interior, i.e. the region on the left side of the loop. The result is between 0 and 4*Pi steradians. The implementation ensures that nearly-degenerate clockwise loops have areas close to zero, while nearly-degenerate counter-clockwise loops have areas close to 4*Pi.
-
loopArea
static double loopArea(java.util.List<S2Point> loop)
Same asloopArea(S2Shape, int)
, but takes a loop as a list of vertices.
-
signedLoopArea
private static double signedLoopArea(S2Shape shape, int chainId)
Returns the area of the loop interior, i.e. the region on the left side of the loop. The result is between 0 and 4*Pi steradians. The implementation ensures that nearly-degenerate clockwise loops have areas close to zero, while nearly-degenerate counter-clockwise loops have areas close to 4*Pi.
-
signedLoopArea
private static double signedLoopArea(java.util.List<S2Point> loop)
Same assignedLoopArea(S2Shape, int)
, but takes a loop as a list of vertices.
-
turningAngle
static double turningAngle(S2Shape shape, int chainId)
-
turningAngle
static double turningAngle(java.util.List<S2Point> loop)
Returns the geodesic curvature of the loop, defined as the sum of the turn angles at each vertex (seeS2.turnAngle(S2Point, S2Point, S2Point)
). The result is positive if the loop is counter-clockwise, negative if the loop is clockwise, and zero if the loop is a great circle. The geodesic curvature is equal to 2*Pi minus the area of the loop.The following cases are handled specially:
- Degenerate loops (consisting of an isolated vertex or composed entirely of sibling edge pairs) have a curvature of 2*Pi exactly.
- The full loop (containing all points, and represented as a loop with no vertices) has a curvature of -2*Pi exactly.
- All other loops have a non-zero curvature in the range (-2*Pi, 2*Pi). For any such loop, reversing the order of the vertices is guaranteed to negate the curvature. This property can be used to define a unique normalized orientation for every loop.
-
canonicalLoopOrder
static S2ShapeMeasures.LoopOrder canonicalLoopOrder(java.util.List<S2Point> loop)
Returns an index "first" and a direction "dir" such that the vertex sequence (first, first + dir, ..., first + (n - 1) * dir) does not change when the loop vertex order is rotated or reversed. This allows the loop vertices to be traversed in a canonical order.
-
pruneDegeneracies
static java.util.List<S2Point> pruneDegeneracies(java.util.List<S2Point> input)
Returns a new loop obtained by removing all degeneracies from "input". In particular, the result will not contain any adjacent duplicate vertices or sibling edge pairs, i.e. vertex sequences of the form (A, A) or (A, B, A).
-
centroid
public static S2Point centroid(S2Shape shape)
Returns the centroid of shape multiplied by the measure of shape.See
S2ShapeIndexMeasures.centroid(S2ShapeIndex)
for more info.
-
polylineCentroid
static S2Point polylineCentroid(S2Shape shape, int chainId)
Returns the true centroid of the polyline multiplied by the length of the polyline.Scaling by the polyline length makes it easy to compute the centroid of several polylines (by simply adding up their centroids).
CAVEAT: Returns
S2Point.ORIGIN
for degenerate polylines (e.g., AA). [Note that this answer is correct; the result of this function is a line integral over the polyline, whose value is always zero if the polyline is degenerate].
-
loopCentroid
static S2Point loopCentroid(S2Shape shape, int chainId)
Returns the true centroid of the loop multiplied by the area of the loop.See
S2ShapeIndexMeasures.centroid(S2ShapeIndex)
for more info.
-
forEachChainEdge
private static void forEachChainEdge(S2Shape shape, int chainId, S2ShapeMeasures.BiConsumer<S2Point,S2Point> edgeConsumer)
Passes each edge (a, b) in the chain of shape at index chainId to edgeConsumer.
-
-