package org.apache.lucene.spatial3d.geom;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class GeoDegenerateVerticalLine extends GeoBaseBBox {
protected final double topLat;
protected final double bottomLat;
protected final double longitude;
protected final GeoPoint UHC;
protected final GeoPoint LHC;
protected final SidedPlane topPlane;
protected final SidedPlane bottomPlane;
protected final SidedPlane boundingPlane;
protected final Plane plane;
protected final GeoPoint[] planePoints;
protected final GeoPoint centerPoint;
protected final GeoPoint[] edgePoints;
public GeoDegenerateVerticalLine(final PlanetModel planetModel, final double topLat, final double bottomLat, final double longitude) {
super(planetModel);
if (topLat > Math.PI * 0.5 || topLat < -Math.PI * 0.5)
throw new IllegalArgumentException("Top latitude out of range");
if (bottomLat > Math.PI * 0.5 || bottomLat < -Math.PI * 0.5)
throw new IllegalArgumentException("Bottom latitude out of range");
if (topLat < bottomLat)
throw new IllegalArgumentException("Top latitude less than bottom latitude");
if (longitude < -Math.PI || longitude > Math.PI)
throw new IllegalArgumentException("Longitude out of range");
this.topLat = topLat;
this.bottomLat = bottomLat;
this.longitude = longitude;
final double sinTopLat = Math.sin(topLat);
final double cosTopLat = Math.cos(topLat);
final double sinBottomLat = Math.sin(bottomLat);
final double cosBottomLat = Math.cos(bottomLat);
final double sinLongitude = Math.sin(longitude);
final double cosLongitude = Math.cos(longitude);
this.UHC = new GeoPoint(planetModel, sinTopLat, sinLongitude, cosTopLat, cosLongitude, topLat, longitude);
this.LHC = new GeoPoint(planetModel, sinBottomLat, sinLongitude, cosBottomLat, cosLongitude, bottomLat, longitude);
this.plane = new Plane(cosLongitude, sinLongitude);
final double middleLat = (topLat + bottomLat) * 0.5;
final double sinMiddleLat = Math.sin(middleLat);
final double cosMiddleLat = Math.cos(middleLat);
this.centerPoint = new GeoPoint(planetModel, sinMiddleLat, sinLongitude, cosMiddleLat, cosLongitude);
this.topPlane = new SidedPlane(centerPoint, planetModel, sinTopLat);
this.bottomPlane = new SidedPlane(centerPoint, planetModel, sinBottomLat);
this.boundingPlane = new SidedPlane(centerPoint, -sinLongitude, cosLongitude);
this.planePoints = new GeoPoint[]{UHC, LHC};
this.edgePoints = new GeoPoint[]{centerPoint};
}
public GeoDegenerateVerticalLine(final PlanetModel planetModel, final InputStream inputStream) throws IOException {
this(planetModel, SerializableObject.readDouble(inputStream), SerializableObject.readDouble(inputStream), SerializableObject.readDouble(inputStream));
}
@Override
public void write(final OutputStream outputStream) throws IOException {
SerializableObject.writeDouble(outputStream, topLat);
SerializableObject.writeDouble(outputStream, bottomLat);
SerializableObject.writeDouble(outputStream, longitude);
}
@Override
public GeoBBox expand(final double angle) {
final double newTopLat = topLat + angle;
final double newBottomLat = bottomLat - 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, newTopLat, newBottomLat, 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) &&
topPlane.isWithin(x, y, z) &&
bottomPlane.isWithin(x, y, z);
}
@Override
public double getRadius() {
final double topAngle = centerPoint.arcDistance(UHC);
final double bottomAngle = centerPoint.arcDistance(LHC);
return Math.max(topAngle, bottomAngle);
}
@Override
public GeoPoint getCenter() {
return centerPoint;
}
@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, topPlane, bottomPlane);
}
@Override
public boolean intersects(final GeoShape geoShape) {
return geoShape.intersects(plane, planePoints, boundingPlane, topPlane, bottomPlane);
}
@Override
public void getBounds(Bounds bounds) {
super.getBounds(bounds);
bounds.addVerticalPlane(planetModel, longitude, plane, boundingPlane, topPlane, bottomPlane)
.addPoint(UHC).addPoint(LHC);
}
@Override
public int getRelationship(final GeoShape path) {
if (intersects(path)) {
return OVERLAPS;
}
if (path.isWithin(centerPoint)) {
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, topPlane, bottomPlane, boundingPlane);
final double UHCDistance = distanceStyle.computeDistance(UHC, x,y,z);
final double LHCDistance = distanceStyle.computeDistance(LHC, x,y,z);
return Math.min(
distance,
Math.min(UHCDistance, LHCDistance));
}
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegenerateVerticalLine))
return false;
GeoDegenerateVerticalLine other = (GeoDegenerateVerticalLine) o;
return super.equals(other) && other.UHC.equals(UHC) && other.LHC.equals(LHC);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + UHC.hashCode();
result = 31 * result + LHC.hashCode();
return result;
}
@Override
public String toString() {
return "GeoDegenerateVerticalLine: {longitude=" + longitude + "(" + longitude * 180.0 / Math.PI + "), toplat=" + topLat + "(" + topLat * 180.0 / Math.PI + "), bottomlat=" + bottomLat + "(" + bottomLat * 180.0 / Math.PI + ")}";
}
}