package org.apache.lucene.spatial3d.geom;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
class GeoSouthRectangle extends GeoBaseBBox {
protected final double topLat;
protected final double leftLon;
protected final double rightLon;
protected final double cosMiddleLat;
protected final GeoPoint ULHC;
protected final GeoPoint URHC;
protected final SidedPlane topPlane;
protected final SidedPlane leftPlane;
protected final SidedPlane rightPlane;
protected final SidedPlane backingPlane;
protected final GeoPoint[] topPlanePoints;
protected final GeoPoint[] leftPlanePoints;
protected final GeoPoint[] rightPlanePoints;
protected final GeoPoint centerPoint;
protected final GeoPoint[] edgePoints;
public GeoSouthRectangle(final PlanetModel planetModel, final double topLat, final double leftLon, double rightLon) {
super(planetModel);
if (topLat > Math.PI * 0.5 || topLat < -Math.PI * 0.5)
throw new IllegalArgumentException("Top latitude out of range");
if (leftLon < -Math.PI || leftLon > Math.PI)
throw new IllegalArgumentException("Left longitude out of range");
if (rightLon < -Math.PI || rightLon > Math.PI)
throw new IllegalArgumentException("Right longitude out of range");
double extent = rightLon - leftLon;
if (extent < 0.0) {
extent += 2.0 * Math.PI;
}
if (extent > Math.PI)
throw new IllegalArgumentException("Width of rectangle too great");
this.topLat = topLat;
this.leftLon = leftLon;
this.rightLon = rightLon;
final double sinTopLat = Math.sin(topLat);
final double cosTopLat = Math.cos(topLat);
final double sinLeftLon = Math.sin(leftLon);
final double cosLeftLon = Math.cos(leftLon);
final double sinRightLon = Math.sin(rightLon);
final double cosRightLon = Math.cos(rightLon);
this.ULHC = new GeoPoint(planetModel, sinTopLat, sinLeftLon, cosTopLat, cosLeftLon, topLat, leftLon);
this.URHC = new GeoPoint(planetModel, sinTopLat, sinRightLon, cosTopLat, cosRightLon, topLat, rightLon);
final double middleLat = (topLat - Math.PI * 0.5) * 0.5;
final double sinMiddleLat = Math.sin(middleLat);
this.cosMiddleLat = Math.cos(middleLat);
while (leftLon > rightLon) {
rightLon += Math.PI * 2.0;
}
final double middleLon = (leftLon + rightLon) * 0.5;
final double sinMiddleLon = Math.sin(middleLon);
final double cosMiddleLon = Math.cos(middleLon);
this.centerPoint = new GeoPoint(planetModel, sinMiddleLat, sinMiddleLon, cosMiddleLat, cosMiddleLon);
this.topPlane = new SidedPlane(centerPoint, planetModel, sinTopLat);
this.leftPlane = new SidedPlane(centerPoint, cosLeftLon, sinLeftLon);
this.rightPlane = new SidedPlane(centerPoint, cosRightLon, sinRightLon);
assert(topPlane.isWithin(centerPoint));
assert(leftPlane.isWithin(centerPoint));
assert(rightPlane.isWithin(centerPoint));
this.backingPlane = new SidedPlane(this.centerPoint, cosMiddleLon, sinMiddleLon, 0.0, 0.0);
this.topPlanePoints = new GeoPoint[]{ULHC, URHC};
this.leftPlanePoints = new GeoPoint[]{ULHC, planetModel.SOUTH_POLE};
this.rightPlanePoints = new GeoPoint[]{URHC, planetModel.SOUTH_POLE};
this.edgePoints = new GeoPoint[]{planetModel.SOUTH_POLE};
}
public GeoSouthRectangle(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, leftLon);
SerializableObject.writeDouble(outputStream, rightLon);
}
@Override
public GeoBBox expand(final double angle) {
final double newTopLat = topLat + angle;
final double newBottomLat = -Math.PI * 0.5;
double currentLonSpan = rightLon - leftLon;
if (currentLonSpan < 0.0)
currentLonSpan += Math.PI * 2.0;
double newLeftLon = leftLon - angle;
double newRightLon = rightLon + 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 backingPlane.isWithin(x, y, z) &&
topPlane.isWithin(x, y, z) &&
leftPlane.isWithin(x, y, z) &&
rightPlane.isWithin(x, y, z);
}
@Override
public double getRadius() {
final double centerAngle = (rightLon - (rightLon + leftLon) * 0.5) * cosMiddleLat;
final double topAngle = centerPoint.arcDistance(URHC);
return Math.max(centerAngle, topAngle);
}
@Override
public GeoPoint[] getEdgePoints() {
return edgePoints;
}
@Override
public GeoPoint getCenter() {
return centerPoint;
}
@Override
public boolean intersects(final Plane p, final GeoPoint[] notablePoints, final Membership... bounds) {
return p.intersects(planetModel, topPlane, notablePoints, topPlanePoints, bounds, leftPlane, rightPlane) ||
p.intersects(planetModel, leftPlane, notablePoints, leftPlanePoints, bounds, rightPlane, topPlane) ||
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, leftPlane, topPlane);
}
@Override
public boolean intersects(final GeoShape geoShape) {
return geoShape.intersects(topPlane, topPlanePoints, leftPlane, rightPlane) ||
geoShape.intersects(leftPlane, leftPlanePoints, rightPlane, topPlane) ||
geoShape.intersects(rightPlane, rightPlanePoints, leftPlane, topPlane);
}
@Override
public void getBounds(Bounds bounds) {
super.getBounds(bounds);
bounds
.addHorizontalPlane(planetModel, topLat, topPlane, leftPlane, rightPlane)
.addVerticalPlane(planetModel, leftLon, leftPlane, topPlane, rightPlane)
.addVerticalPlane(planetModel, rightLon, rightPlane, topPlane, leftPlane)
.addPoint(URHC).addPoint(ULHC).addPoint(planetModel.SOUTH_POLE);
}
@Override
protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
final double topDistance = distanceStyle.computeDistance(planetModel, topPlane, x,y,z, leftPlane, rightPlane);
final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, rightPlane, topPlane);
final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, leftPlane, topPlane);
final double ULHCDistance = distanceStyle.computeDistance(ULHC, x,y,z);
final double URHCDistance = distanceStyle.computeDistance(URHC, x,y,z);
return Math.min(
Math.min(
topDistance,
Math.min(leftDistance, rightDistance)),
Math.min(ULHCDistance, URHCDistance));
}
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoSouthRectangle))
return false;
GeoSouthRectangle other = (GeoSouthRectangle) o;
return super.equals(other) && other.ULHC.equals(ULHC) && other.URHC.equals(URHC);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + ULHC.hashCode();
result = 31 * result + URHC.hashCode();
return result;
}
@Override
public String toString() {
return "GeoSouthRectangle: {planetmodel="+planetModel+", toplat=" + topLat + "(" + topLat * 180.0 / Math.PI + "), leftlon=" + leftLon + "(" + leftLon * 180.0 / Math.PI + "), rightlon=" + rightLon + "(" + rightLon * 180.0 / Math.PI + ")}";
}
}