package org.apache.lucene.spatial3d.geom;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
class GeoLongitudeSlice extends GeoBaseBBox {
protected final double leftLon;
protected final double rightLon;
protected final SidedPlane leftPlane;
protected final SidedPlane rightPlane;
protected final SidedPlane backingPlane;
protected final GeoPoint[] planePoints;
protected final GeoPoint centerPoint;
protected final GeoPoint[] edgePoints;
public GeoLongitudeSlice(final PlanetModel planetModel, final double leftLon, double rightLon) {
super(planetModel);
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.leftLon = leftLon;
this.rightLon = rightLon;
final double sinLeftLon = Math.sin(leftLon);
final double cosLeftLon = Math.cos(leftLon);
final double sinRightLon = Math.sin(rightLon);
final double cosRightLon = Math.cos(rightLon);
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, 0.0, sinMiddleLon, 1.0, cosMiddleLon);
this.leftPlane = new SidedPlane(centerPoint, cosLeftLon, sinLeftLon);
this.rightPlane = new SidedPlane(centerPoint, cosRightLon, sinRightLon);
this.backingPlane = new SidedPlane(this.centerPoint, cosMiddleLon, sinMiddleLon, 0.0, 0.0);
this.planePoints = new GeoPoint[]{planetModel.NORTH_POLE, planetModel.SOUTH_POLE};
this.edgePoints = new GeoPoint[]{planetModel.NORTH_POLE};
}
public GeoLongitudeSlice(final PlanetModel planetModel, final InputStream inputStream) throws IOException {
this(planetModel, SerializableObject.readDouble(inputStream), SerializableObject.readDouble(inputStream));
}
@Override
public void write(final OutputStream outputStream) throws IOException {
SerializableObject.writeDouble(outputStream, leftLon);
SerializableObject.writeDouble(outputStream, rightLon);
}
@Override
public GeoBBox expand(final double angle) {
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, Math.PI * 0.5, -Math.PI * 0.5, newLeftLon, newRightLon);
}
@Override
public boolean isWithin(final double x, final double y, final double z) {
return backingPlane.isWithin(x, y, z) &&
leftPlane.isWithin(x, y, z) &&
rightPlane.isWithin(x, y, z);
}
@Override
public double getRadius() {
double extent = rightLon - leftLon;
if (extent < 0.0)
extent += Math.PI * 2.0;
return Math.max(Math.PI * 0.5, extent * 0.5);
}
@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, leftPlane, notablePoints, planePoints, bounds, rightPlane) ||
p.intersects(planetModel, rightPlane, notablePoints, planePoints, bounds, leftPlane);
}
@Override
public boolean intersects(final GeoShape geoShape) {
return geoShape.intersects(leftPlane, planePoints, rightPlane) ||
geoShape.intersects(rightPlane, planePoints, leftPlane);
}
@Override
public void getBounds(Bounds bounds) {
super.getBounds(bounds);
bounds
.addVerticalPlane(planetModel, leftLon, leftPlane, rightPlane)
.addVerticalPlane(planetModel, rightLon, rightPlane, leftPlane)
.addPoint(planetModel.NORTH_POLE)
.addPoint(planetModel.SOUTH_POLE);
}
@Override
protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, rightPlane);
final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, leftPlane);
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(
Math.min(northDistance, southDistance),
Math.min(leftDistance, rightDistance));
}
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoLongitudeSlice))
return false;
GeoLongitudeSlice other = (GeoLongitudeSlice) o;
return super.equals(other) && other.leftLon == leftLon && other.rightLon == rightLon;
}
@Override
public int hashCode() {
int result = super.hashCode();
long temp = Double.doubleToLongBits(leftLon);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(rightLon);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
return "GeoLongitudeSlice: {planetmodel="+planetModel+", leftlon=" + leftLon + "(" + leftLon * 180.0 / Math.PI + "), rightlon=" + rightLon + "(" + rightLon * 180.0 / Math.PI + ")}";
}
}