Package com.google.common.geometry
Class S2Point
- java.lang.Object
-
- com.google.common.geometry.S2Point
-
- Direct Known Subclasses:
S2Shape.ReferencePoint
@GwtCompatible(serializable=true) @CheckReturnValue public class S2Point extends java.lang.Object implements S2Region, java.lang.Comparable<S2Point>, java.io.Serializable
An S2Point represents a point on the unit sphere as a 3D vector. Usually points are normalized to be unit length, but some methods do not require this.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
S2Point.Builder
A builder ofS2Point
instances.static class
S2Point.Shape
An S2Shape representing a list of S2Points.
-
Field Summary
Fields Modifier and Type Field Description static S2Point
ORIGIN
Origin of the coordinate system, [0,0,0].(package private) double
x
static S2Point
X_NEG
Opposite direction of the x-axis.static S2Point
X_POS
Direction of the x-axis.(package private) double
y
static S2Point
Y_NEG
Opposite direction of the y-axis.static S2Point
Y_POS
Direction of the y-axis.(package private) double
z
static S2Point
Z_NEG
Opposite direction of the z-axis.static S2Point
Z_POS
Direction of the z-axis.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description S2Point
add(S2Point p)
Returns add(this,p).static S2Point
add(S2Point p1, S2Point p2)
Returns the component-wise addition of 'p1' and 'p2'.(package private) boolean
aequal(S2Point that, double margin)
Compare two vectors, return true if all their components are within a difference of margin.double
angle(S2Point va)
Return the angle between two vectors in radiansint
compareTo(S2Point other)
boolean
contains(S2Cell cell)
If this method returns true, the region completely contains the given cell.boolean
contains(S2Point other)
Returns true if and only if the given point is contained by the region.S2Point
crossProd(S2Point p)
Returns crossProd(this,p).static S2Point
crossProd(S2Point p1, S2Point p2)
Returns the R3 vector cross product of 'p1' and 'p2'.double
crossProdNorm(S2Point va)
Returns the norm of the cross product,S2Point.crossProd(this, va).norm()
.(package private) static S2Point
decode(LittleEndianInput is)
Returns a new S2Point decoded from the given little endian input stream.static S2Point
decode(java.io.InputStream is)
Returns a new S2Point decoded from the given input stream.S2Point
div(double scale)
Returns div(this,scale).static S2Point
div(S2Point p, double m)
Returns the component-wise division of 'p' by 'm'.double
dotProd(S2Point that)
Returns the vector dot product of 'this' with 'that'.(package private) void
encode(LittleEndianOutput os)
Writes this point to the given little endian output stream.void
encode(java.io.OutputStream os)
Writes this point to the given output stream.boolean
equals(java.lang.Object that)
boolean
equalsPoint(S2Point that)
Returns true if this point is equal tothat
.S2Point
fabs()
Returns fabs(this).static S2Point
fabs(S2Point p)
Returns the component-wise absolute point from 'p'.double
get(int axis)
S2Cap
getCapBound()
Return a bounding spherical cap.double
getDistance(S2Point that)
Returns the distance in 3D coordinates from this to that.double
getDistance2(S2Point that)
Returns the square of the distance in 3D coordinates from this to that.S2LatLngRect
getRectBound()
Return a bounding latitude-longitude rectangle.double
getX()
double
getY()
double
getZ()
int
hashCode()
Calcualates hashcode based on stored coordinates.int
largestAbsComponent()
Return the index of the largest component fabs(package private) static int
largestAbsComponent(double x, double y, double z)
Return the index of the largest component fabsboolean
lessThan(S2Point vb)
boolean
mayIntersect(S2Cell cell)
If this method returns false, the region does not intersect the given cell.static S2Point
minus(S2Point p1, S2Point p2)
Returns sub(this,p).S2Point
mul(double scale)
Returns mul(this,scale).static S2Point
mul(S2Point p, double m)
Returns the component-wise multiplication of 'p' with 'm'.S2Point
neg()
Returns neg(this).static S2Point
neg(S2Point p)
Returns the component-wise negation of 'p', i.e.double
norm()
Returns the vector magnitudesqrt(x*x+y*y+z*z)
.double
norm2()
Returns the square of the vector magnitudex*x+y*y+z*z
.S2Point
normalize()
Returns normalize(this).static S2Point
normalize(S2Point p)
Returns a copy of 'p' rescaled to be unit-length.S2Point
ortho()
return a vector orthogonal to this oneS2Point
rotate(S2Point axis, double radians)
Rotates this point around an arbitrary axis.static double
scalarTripleProduct(S2Point a, S2Point b, S2Point c)
Returns the scalar triple product,a.dotProd(b.crossProd(c))
.S2Point
sub(S2Point p)
Returns sub(this,p).static S2Point
sub(S2Point p1, S2Point p2)
Returns the component-wise subtraction of 'p1' and 'p2'.S2Point.Builder
toBuilder()
Returns a new Builder initialized to a copy of this point.java.lang.String
toDegreesString()
java.lang.String
toString()
-
-
-
Field Detail
-
ORIGIN
public static final S2Point ORIGIN
Origin of the coordinate system, [0,0,0].
-
X_POS
public static final S2Point X_POS
Direction of the x-axis.
-
X_NEG
public static final S2Point X_NEG
Opposite direction of the x-axis.
-
Y_POS
public static final S2Point Y_POS
Direction of the y-axis.
-
Y_NEG
public static final S2Point Y_NEG
Opposite direction of the y-axis.
-
Z_POS
public static final S2Point Z_POS
Direction of the z-axis.
-
Z_NEG
public static final S2Point Z_NEG
Opposite direction of the z-axis.
-
x
final double x
-
y
final double y
-
z
final double z
-
-
Method Detail
-
getX
public double getX()
-
getY
public double getY()
-
getZ
public double getZ()
-
add
public static final S2Point add(S2Point p1, S2Point p2)
Returns the component-wise addition of 'p1' and 'p2'.
-
sub
public static final S2Point sub(S2Point p1, S2Point p2)
Returns the component-wise subtraction of 'p1' and 'p2'.
-
mul
public S2Point mul(double scale)
Returns mul(this,scale).
-
mul
public static final S2Point mul(S2Point p, double m)
Returns the component-wise multiplication of 'p' with 'm'.
-
div
public S2Point div(double scale)
Returns div(this,scale).
-
div
public static final S2Point div(S2Point p, double m)
Returns the component-wise division of 'p' by 'm'.
-
dotProd
public final double dotProd(S2Point that)
Returns the vector dot product of 'this' with 'that'.
-
crossProd
public static final S2Point crossProd(S2Point p1, S2Point p2)
Returns the R3 vector cross product of 'p1' and 'p2'.
-
neg
public S2Point neg()
Returns neg(this).
-
neg
public static final S2Point neg(S2Point p)
Returns the component-wise negation of 'p', i.e. its antipodal point.
-
fabs
public S2Point fabs()
Returns fabs(this).
-
fabs
public static final S2Point fabs(S2Point p)
Returns the component-wise absolute point from 'p'.
-
normalize
public S2Point normalize()
Returns normalize(this).
-
normalize
public static final S2Point normalize(S2Point p)
Returns a copy of 'p' rescaled to be unit-length.
-
norm
public double norm()
Returns the vector magnitudesqrt(x*x+y*y+z*z)
.
-
norm2
public final double norm2()
Returns the square of the vector magnitudex*x+y*y+z*z
.
-
scalarTripleProduct
public static final double scalarTripleProduct(S2Point a, S2Point b, S2Point c)
Returns the scalar triple product,a.dotProd(b.crossProd(c))
.This is a faster implementation than calling the dotProd and crossProd methods directly.
-
getDistance
public double getDistance(S2Point that)
Returns the distance in 3D coordinates from this to that.Equivalent to
a.sub(b).norm()
, but significantly faster.If ordering points by angle, this is faster than
norm()
, and much faster thanangle(com.google.common.geometry.S2Point)
, but consider usingS1ChordAngle
.
-
getDistance2
public double getDistance2(S2Point that)
Returns the square of the distance in 3D coordinates from this to that.Equivalent to
getDistance(that)<sup>2</sup>
, but significantly faster.If ordering points by angle, this is much faster than
angle(com.google.common.geometry.S2Point)
, but consider usingS1ChordAngle
.
-
ortho
public final S2Point ortho()
return a vector orthogonal to this one
-
largestAbsComponent
public final int largestAbsComponent()
Return the index of the largest component fabs
-
largestAbsComponent
static final int largestAbsComponent(double x, double y, double z)
Return the index of the largest component fabs
-
get
public final double get(int axis)
-
crossProdNorm
public final double crossProdNorm(S2Point va)
Returns the norm of the cross product,S2Point.crossProd(this, va).norm()
. This is more efficient than calling crossProd() followed by norm().
-
rotate
public S2Point rotate(S2Point axis, double radians)
Rotates this point around an arbitrary axis. The result is normalized.- Parameters:
axis
- point around which rotation should be performed.radians
- radians to rotate the point counterclockwise around the given axis.
-
angle
public final double angle(S2Point va)
Return the angle between two vectors in radians
-
aequal
boolean aequal(S2Point that, double margin)
Compare two vectors, return true if all their components are within a difference of margin.
-
equals
public boolean equals(java.lang.Object that)
- Overrides:
equals
in classjava.lang.Object
-
equalsPoint
public boolean equalsPoint(S2Point that)
Returns true if this point is equal tothat
. Slightly faster thanequals(Object)
.
-
lessThan
public boolean lessThan(S2Point vb)
-
compareTo
public int compareTo(S2Point other)
- Specified by:
compareTo
in interfacejava.lang.Comparable<S2Point>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toDegreesString
public java.lang.String toDegreesString()
-
toBuilder
public S2Point.Builder toBuilder()
Returns a new Builder initialized to a copy of this point.
-
hashCode
public int hashCode()
Calcualates hashcode based on stored coordinates. Since we want +0.0 and -0.0 to be treated the same, we ignore the sign of the coordinates.- Overrides:
hashCode
in classjava.lang.Object
-
contains
public boolean contains(S2Cell cell)
Description copied from interface:S2Region
If this method returns true, the region completely contains the given cell. Otherwise, either the region does not contain the cell or the containment relationship could not be determined.
-
contains
public boolean contains(S2Point other)
Description copied from interface:S2Region
Returns true if and only if the given point is contained by the region.p
is generally required to be unit length, although some subtypes may relax this restriction.
-
getCapBound
public S2Cap getCapBound()
Description copied from interface:S2Region
Return a bounding spherical cap.- Specified by:
getCapBound
in interfaceS2Region
-
getRectBound
public S2LatLngRect getRectBound()
Description copied from interface:S2Region
Return a bounding latitude-longitude rectangle.- Specified by:
getRectBound
in interfaceS2Region
-
mayIntersect
public boolean mayIntersect(S2Cell cell)
Description copied from interface:S2Region
If this method returns false, the region does not intersect the given cell. Otherwise, either region intersects the cell, or the intersection relationship could not be determined.- Specified by:
mayIntersect
in interfaceS2Region
-
encode
public void encode(java.io.OutputStream os) throws java.io.IOException
Writes this point to the given output stream.- Throws:
java.io.IOException
-
encode
void encode(LittleEndianOutput os) throws java.io.IOException
Writes this point to the given little endian output stream.- Throws:
java.io.IOException
-
decode
public static S2Point decode(java.io.InputStream is) throws java.io.IOException
Returns a new S2Point decoded from the given input stream.- Throws:
java.io.IOException
-
decode
static S2Point decode(LittleEndianInput is) throws java.io.IOException
Returns a new S2Point decoded from the given little endian input stream.- Throws:
java.io.IOException
-
-