package org.apache.lucene.spatial3d.geom;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
class GeoDegenerateLongitudeSlice extends GeoBaseBBox {
protected final double longitude;
protected final SidedPlane boundingPlane;
protected final Plane plane;
protected final GeoPoint interiorPoint;
protected final GeoPoint[] edgePoints;
protected final GeoPoint[] planePoints;
public GeoDegenerateLongitudeSlice(final PlanetModel planetModel, final double longitude) {
super(planetModel);
if (longitude < -Math.PI || longitude > Math.PI)
throw new IllegalArgumentException("Longitude out of range");
this.longitude = longitude;
final double sinLongitude = Math.sin(longitude);
final double cosLongitude = Math.cos(longitude);
this.plane = new Plane(cosLongitude, sinLongitude);
this.interiorPoint = new GeoPoint(planetModel, 0.0, sinLongitude, 1.0, cosLongitude);
this.boundingPlane = new SidedPlane(interiorPoint, -sinLongitude, cosLongitude);
this.edgePoints = new GeoPoint[]{interiorPoint};
this.planePoints = new GeoPoint[]{planetModel.NORTH_POLE, planetModel.SOUTH_POLE};
}
public GeoDegenerateLongitudeSlice(final PlanetModel planetModel, final InputStream inputStream) throws IOException {
this(planetModel, SerializableObject.readDouble(inputStream));
}
@Override
public void write(final OutputStream outputStream) throws IOException {
SerializableObject.writeDouble(outputStream, longitude);
}
@Override
public GeoBBox expand(final double angle) {
double newLeftLon = longitude - angle;
double newRightLon = longitude + angle;
double currentLonSpan = 2.0 * angle;
if (currentLonSpan + 2.0 * angle >= Math.PI * 2.0) {
newLeftLon = -Math.PI;
newRightLon = Math.PI;
}
return GeoBBoxFactory.makeGeoBBox(planetModel, Math.PI * 0.5, -Math.PI * 0.5, newLeftLon, newRightLon);
}
@Override
public boolean isWithin(final double x, final double y, final double z) {
return plane.evaluateIsZero(x, y, z) &&
boundingPlane.isWithin(x, y, z);
}
@Override
public double getRadius() {
return Math.PI * 0.5;
}
@Override
public GeoPoint getCenter() {
return interiorPoint;
}
@Override
public GeoPoint[] getEdgePoints() {
return edgePoints;
}
@Override
public boolean intersects(final Plane p, final GeoPoint[] notablePoints, final Membership... bounds) {
return p.intersects(planetModel, plane, notablePoints, planePoints, bounds, boundingPlane);
}
@Override
public boolean intersects(final GeoShape geoShape) {
return geoShape.intersects(plane, planePoints, boundingPlane);
}
@Override
public void getBounds(Bounds bounds) {
super.getBounds(bounds);
bounds
.addVerticalPlane(planetModel, longitude, plane, boundingPlane)
.addPoint(planetModel.NORTH_POLE).addPoint(planetModel.SOUTH_POLE);
}
@Override
public int getRelationship(final GeoShape path) {
if (intersects(path))
return OVERLAPS;
if (path.isWithin(interiorPoint))
return CONTAINS;
return DISJOINT;
}
@Override
protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
final double distance = distanceStyle.computeDistance(planetModel, plane, x,y,z, boundingPlane);
final double northDistance = distanceStyle.computeDistance(planetModel.NORTH_POLE, x,y,z);
final double southDistance = distanceStyle.computeDistance(planetModel.SOUTH_POLE, x,y,z);
return Math.min(
distance,
Math.min(northDistance, southDistance));
}
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegenerateLongitudeSlice))
return false;
GeoDegenerateLongitudeSlice other = (GeoDegenerateLongitudeSlice) o;
return super.equals(other) && other.longitude == longitude;
}
@Override
public int hashCode() {
int result = super.hashCode();
long temp = Double.doubleToLongBits(longitude);
result = result * 31 + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
return "GeoDegenerateLongitudeSlice: {planetmodel="+planetModel+", longitude=" + longitude + "(" + longitude * 180.0 / Math.PI + ")}";
}
}